Re: C to Eu translator
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Jan 05, 2004
- 406 views
On Sun, 04 Jan 2004 15:46:31 +0100, Juergen Luethje <j.lue at gmx.de> wrote: Whilst agreeing with other comments in this thread, and thinking it would be better to leave it in C, but (learn how to) make it a dll, there was one point you made I thought deserved special notice: >How about only translating things that are safe and clear to do, >e.g. replacing all symbols such as "//", "==", "++", "&&", "||", "~", I don't believe even these would be safe and clear to translate: == might want to be '=' in some cases, 'equal()' in others, and not possible in others (eg a pointer value). You can ++a or a++ in C, so it would have to be a separate statement in Euphoria, before, after, or even sometimes in the middle of an expression... && and || may also sometimes short-circuit differently in C to Euphoria(?). When I translate C to Euphoria (which is not particularly often), I start by commenting out the entire source, then using this as a guide code one function (or line!) at a time, testing thoroughly as I go. The only example (not specially relevant, either) to hand is a little mandlebrot demo, first in C: main(){ int x, y, k; char *b = " .:,;!/>)|&IH%*#"; float r, i, z, Z, t, c, C; for (y=30; puts(""), C = y*0.1 - 1.5, y--;){ for (x=0; c = x*0.04 - 2, z=0, Z=0, x++ < 75;){ for (r=c, i=C, k=0; t = z*z - Z*Z + r, Z = 2*z*Z + i, z=t, k<112; k++) if (z*z + Z*Z > 10) break; printf ("%c", b[k%16]); } } } ...and below in Euphoria: constant b=" .:,;!/>)|&IH%*#" atom r, i, c, C, z, Z, t integer k for y=30 to 1 by -1 do C=y*0.1-1.5 puts(1,'\n') for x=0 to 74 do c=x*0.04-2 z=0 Z=0 r=c i=C k=0 while k<112 do t=z*z-Z*Z+r Z=2*z*Z+i z=t if z*z+Z*Z>10 then exit end if k+=1 end while puts(1,b[remainder(k,16)+1]) end for end for After some careful study you will see it requires a fairly deep semantic understanding of the C code in order to rip it apart into single Euphoria statements, in the right order, were you to try this automagically. Notice that the inner loop is a while, because k is used outside it, x and y are no longer declared, the fact that zero indexing is the norm in C adds to the fun, etc... HTH, Pete