▲ | fuzztester 3 hours ago | |
Coincidentally just saw this video a few days ago. Interesting approach, to use RPN for expressions, including for the condition in the while statement. I could understand most of the logic of the interpreter, except for the part about how the while statement is implemented. I did not quite understand the parts of the code with
and
fragments.That is, around the region of lines 11 to 14 or so, at about 18:0 in the video. How does the code there, work? | ||
▲ | pansa2 2 hours ago | parent | next [-] | |
`case while` evaluates the condition. If the condition's true it moves forward to the next line (i.e. into the body of the loop), but if it's false it moves forward several lines, until the line after the `end` statement. `case end` simply moves back to the `while` statement and lets the `case while` code run again. Essentially:
becomes:
| ||
▲ | Drakim 2 hours ago | parent | prev [-] | |
By random chance I'm working on making my own programming language and I very recently used RPN for my expressions. It's a very neat and clean way to implement order precedence and then afterwards you can translate the entire flat expression into a tree-like structure to fit into your AST and be sure that transformation doesn't mess anything up. I'm surprised you think the condition in the while statement would be any different though, an expression is an expression, it doesn't matter if you are assigning a variable, evaluating an if condition, or doing a while loop, you still need an expression for each of them, and the expressions should behave the same way in each of them. As for the case "while" and case "end", I think the case "end", he actually does explain them pretty well as he is writing them. It can be a little confusing because he puts ... as a placeholder and implements the code a little later in the video. |