home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!UB.com!pacbell.com!sgiblab!spool.mu.edu!agate!doc.ic.ac.uk!warwick!uknet!comlab.ox.ac.uk!imc
- From: imc@comlab.ox.ac.uk (Ian Collier)
- Newsgroups: alt.hackers
- Subject: Re: obHack [polyglots]
- Message-ID: <2990.imc@uk.ac.ox.prg>
- Date: 28 Jan 93 15:58:21 GMT
- References: <1jks8bINNsj4@matt.ksu.ksu.edu> <C182ow.BAv@deshaw.com> <1js1teINNhq4@matt.ksu.ksu.edu>
- Organization: Oxford University Computing Laboratory
- Lines: 67
- Approved: or else it wouldn't get posted.
- X-Local-Date: Thursday, 28th January 1993 at 3:58pm GMT
- Originator: imc@msc7.comlab
-
- In article <1js1teINNhq4@matt.ksu.ksu.edu>, holland@matt.ksu.ksu.edu (Rich Holland) wrote:
- >I hacked together a quick fortran/c example to demonstrate it, but POLYGLOT
- >*really* does a good job of confusing people. :-)
-
- >char a; /*
- >C$NOEXT
- > PROGRAM hello
- > WRITE (*,*) 'This is a fortran/c program!'
- > END
- >C*/ main () /*
- >C*/ { /*
- >C*/ printf("This is a fortran/c program!\n"); /*
- >C*/ }
-
- At least it's possible with this program to understand exactly how it
- works... :-) One problem with POLYGLOT is that I don't know by heart the
- commenting conventions of *all* the languages mentioned (perhaps I'm not a
- real programmer then, even if I can program a Z80 in hex. But I digress...)
- Another is that there is so much junk at the start of each line that it's
- hard to keep track of everything.
-
- ObHack: Someone asked for macros? :-)
-
- Pretty standard, but written in a terse manner...
-
- #define set(x,b) (x[(b)/8]|=1<<(7-(b)%8))
- #define clr(x,b) (x[(b)/8]&=~(1<<(7-(b)%8)))
- #define let(x,b,v) (v?set(x,(b)):clr(x,(b)))
- #define tst(x,b) ((x[(b)/8]&(1<<(7-(b)%8)))!=0)
-
- From an earlier (and easier to understand :-) ) version of my 1992 IOCCC
- entry:
-
- #define q sscanf
- #define rf exit(1)
- #define sx(xs,f) q(++s,xs,&f)!=1&&c>++l&&q(v[l],xs,&f)!=1&&--l
- #define qh(j,i,f,g) t=0,*++s&&(t=q(s,j,&f,&g)),(!t&&c>++l&&\
- !(t=q(v[l],j,&f,&g)))?--l:(t<2&&c>++l&&!(t=q(v[l],i,&g))&&--l
- #define i2 (qh("%d,%d","%d",xs,ys),(xs&&ys)||(fputs(m2,stderr),rf)))
- #define d1 (sx("%lg",f))
- #define i1(f) (sx("%d",f),f||(fputs(m2,stderr),rf))
- #define d2(f,g) (qh("%lg,%lg","%lg",f,g)))
-
- result: if v is the argument array (i.e. argv), c is the argument count
- (i.e. argc), l is the current argument number, and s points to the character
- after an argument flag in v[l] (e.g. 4 in -m4,5); and t is a spare int
- variable, then
-
- i2 gets two integers into xs and ys respectively, printing error message
- m2 if either is zero
- i1(x) gets an integer into x, printing error message m2 if it is zero
- d1 gets a double into f
- d2(f,g) gets two doubles into f and g respectively.
-
- Afterwards, l is the number of the last used argument string.
-
- The complexity of the macros makes sure that the arguments get picked up if
- they are written in any of the following forms:
-
- -x1,2 -x1 2 -x 1 2 -x 1,2 -xylophone 1,2 -xylophone 1 2
- -y1 -y 1 -yes 1
-
- It is OK to omit any of the numeric arguments; they just get left at their
- default (i.e. original) values.
-
- Ian Collier
- Ian.Collier@prg.ox.ac.uk | imc@ecs.ox.ac.uk
-