Keeping track of returned and break-ed values between code blocks

Your syntax is quite confusing; I didn't even notice the for loop in there! (I think = is more visible here rather than is.)

While my own tax also allows statements as expressions, loops only return a void, so I don't have experience of return proper values. But, what is the value of a loop anyway, one without an early exit?

Let's assume that would be the value of the last expression in the loop body, and that the expected type the loop returns is T. That means that set i += 1 in your example is expected to yield such a result.

In the case of break within the loop, its expression should also be of type T.

That is pretty much it in terms of performing type analysis and annotating the AST with the types of each node and each terminal.

Making sure that the value from an embedded break makes it way to wherever it needs to be for the loop result, is the headache of the code-generator. So if generating code for a stack machine, you just push that result then perform a jump to the common loop exit point.

But you must also allow for a normal loop exit. And also, the situation where the loop performs zero iterations; what will be the return value then?

/r/ProgrammingLanguages Thread