logoalt Hacker News

wavemodetoday at 2:41 AM1 replyview on HN

I don't think if-expressions have to affect existing semantics. Basically, in the parser you would have two different kinds of AST nodes, one for when the `if` keyword is encountered in statement position and another for when it's encountered in expression position.

Right now, `if` in expression position is just a syntax error ("unexpected symbol")


Replies

Joker_vDtoday at 4:48 AM

Well, I believe there could be some complications with parsing related to the fact that Lua grammar doesn't really requires semicolons between the statements.

But other than that, yeah, detecting "if" in the expression position is pretty unambiguous. No idea why most languages went with "cond-expr ? then-expr : else-expr" bracketed syntax instead.

show 1 reply