Lately I've been playing with writing my own programming language, following the excellent Crafting Interpreters book but I've hit something of a snag.
I'd like to extend the parser to accept variable declarations in the form of: var x,y,z and to allow for unpacking like [x,y,z]=[1,2,3] (noting that jlox doesn't, as yet, support arrays). Finally I'd like to allow for an in statement so you can do: for(x in a) or to test:
var a = {'Larry': 1, 'Curly': 2}
var x = 'Moe'
var b = x in a
print b
Output should be false. That is: the in statement is context-sensitive because it behaves as both an assignment/iterator statement as well as a boolean test. This context-sensitivity is especially problematic because while parsing you'd have to employ some sort of lookback to see if you're in a loop declaration or if you're "just an expression."
Anyway - given jlox - I was wondering if there's something trivial I'm missing to implement the above, or if it's going to be a rather hairy task?
x in aare not ambiguous for any top-down parser, but would be a reduce/reduce conflict for LR.for(EXP) BODYis already valid Lox. In this case you might want to keepinjust for the check, and use. e.g.for(x of seq)for iteration (see JavaScript).ininfor(x in a)has to be part of the syntax for the loop, just like the tokensxandahas to be separate tokens. Otherwise, if it was just afor(expr), you would have a lot of difficulties later actually iteratingaand making the namexavailable in the loop body.