PDA

Click to See Complete Forum and Search --> : This block of code doesnt make sense!!


TadaTensai
Jun 3rd, 2001, 02:42 PM
for( i=1,d=c; i && *c; c++ )
i+=*c=='[', i-=*c==']';
if(!i) {
c[-1]=0;
while(a[p])
{
interpret(d);
}
c[-1]=']';
break;
}


This is C code for an interpreter for the Brainf*** language, and [ is the beginning of the looping structure. I am making a superset to the language, called TripleHack, but I NEED to understand this looping structure. I can't figure out what this does! The code is in the middle of the interpret function. I took out all the unnecessary stuff.

Thank you


It is said that the only language all programmers understand is profanity.

Zaei
Jun 4th, 2001, 03:39 PM
In psudo code:

i = 1 at start of loop
d = address of c
while i <> 0 and data at address of c <> 0
if data at address of c == '[' then add 1 to i, and subtract 1 from i if the data is ']'
end while
if i = 0 then
the byte before the address of c = 0
blah blah blah
the byte before the address of c = ']'
break
end if


Ill assume that c is a char*, with a line of code. What this code does is evaluate the code that is inside a series of brackets. It uses some pretty freaky stuff (ie, direct memory modification).

Z.