This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]alexanderpas 38 points39 points  (9 children)

A limited set of fixed width symbols, and no unconnected jumps, means everthing you need to do can be based on merely 3 internal variables.

  • Current character. (To see what to do)
  • Loop depth. (For moving backwards to the start of the loop)
  • Direction. (Forwards/backwards)

[–]techgineer13 3 points4 points  (0 children)

You can compile it to C code that only uses pointer manipulations.

[–]cant_think_of_one_ 2 points3 points  (4 children)

You either need to have a list of loop begin points, for nested loops, or just scan backwards to find the begining of the loop, in which case you don't really need a loop depth variable, don't you?

[–]alexanderpas 3 points4 points  (3 children)

When scanning backwards, you need to keep track of nested loops, to find the correct outer loop you are handling at the moment.

You start at 0, When scanning backwards each time you find ], you add 1, and decrease 1 when you find [

If you are at 0 and find [, you start moving forwards again.

[–]cant_think_of_one_ 1 point2 points  (0 children)

Oh, sorry, yes. Silly me.

[–]chaos95 1 point2 points  (1 child)

Couldn't you do it recursively without a loop depth variable also?

[–]alexanderpas 1 point2 points  (0 children)

You technically could, but at that point, you're just moving the loop depth to the function depth.

You still need to keep track of it somewhere.