mattbierner / atum

Javascript Interpreter in Functional-Style Javascript
MIT License
19 stars 2 forks source link

Labels #2

Open mattbierner opened 11 years ago

mattbierner commented 11 years ago

One odd aspect of Javascript is that to my knowledge labels are only used on loops. Breaks / continues may identify the specific loop targeted, but the targeting behaves as if all loops are while loops. For loops continued for example do not reevaluate the init expression, just the test and the update (which can be considered part of the body but which is evaluated on continue completions as well as normal ones).

Because labels do not support generalized goto flow control, a more targeted approach to supporting them may be applicable.

LABEL: for (INIT; TEST: UPDATE) BODY

is translated to:

INIT
LABEL
TEST
BODY
// If body is normal or continue
UPDATE
goto label
// else

If labels are imagined to be attached to blocks of code:

INIT
LABEL {
    TEST
    BODY
    // If body is normal or continue
    UPDATE
    goto label
    // else
}

Break LABEL goes to the label but does not evaluate the body, it is a normal completion of the value stored in the break completion.

Continue LABEL goes to the label and evaluates the body, resulting in either the result of body or a normal completion of the value stored in the continue completion.