naringas / craftinginterpreters-plox

Lox from Crafting Interpreters but in Python
0 stars 0 forks source link

nested ternary doesn't work #2

Closed ysangkok closed 1 year ago

ysangkok commented 1 year ago

applying the StrEnum fix i proposed in #1 , nested ternaries are not working:

 % rlwrap python plox/plox.py
1?2?3:4:5
TOKENS
[ '1' `1.0`]
[ '?' `None`]
[ '2' `2.0`]
[ '?' `None`]
[ '3' `3.0`]
[ ':' `None`]
[ '4' `4.0`]
[ ':' `None`]
[ '5' `5.0`]
[ '' `None`]

STATEMENTS
Error at line 1 at end. expected a ';' after expressionStmt. where is my  ; !?
none stmt

INTERPRETATION
skip none stmt `None`

1?2:3?4:5
TOKENS
[ '1' `1.0`]
[ '?' `None`]
[ '2' `2.0`]
[ ':' `None`]
[ '3' `3.0`]
[ '?' `None`]
[ '4' `4.0`]
[ ':' `None`]
[ '5' `5.0`]
[ '' `None`]

STATEMENTS
Error at line 1 at end. expected a ';' after expressionStmt. where is my  ; !?
none stmt

INTERPRETATION
skip none stmt `None`
naringas commented 1 year ago

you need to end all statements with ';'

this is not javascript ahahaha. 😅

plox> 1?2?3:4:5     ;
TOKENS
[NUMBER '1' `1.0`]
[QUESTION '?' `None`]
[NUMBER '2' `2.0`]
[QUESTION '?' `None`]
[NUMBER '3' `3.0`]
[COLON ':' `None`]
[NUMBER '4' `4.0`]
[COLON ':' `None`]
[NUMBER '5' `5.0`]
[SEMICOLON ';' `None`]
[EOF '' `None`]

STATEMENTS
StmtExpr(if [Literal(value=1.0)]; then [Ternary(comparison=Literal(value=2.0), left=Literal(value=3.0), right=Literal(value=4.0))]; else [Literal(value=5.0)])

INTERPRETATION
#  3.0

what I'm more interested on, is why the nested ternary 2?3:4 not getting printed 'nicely'...

ysangkok commented 1 year ago

Ah, cool, thanks!