PlutoLang / Pluto

A superset of Lua 5.4 with a focus on general-purpose programming.
https://pluto-lang.org/docs/Introduction
MIT License
339 stars 20 forks source link

Fix instances of E_NO_COLON getting lost or ignored #784

Closed Sainan closed 2 months ago

Sainan commented 2 months ago

To allow a ? b in c : e to be parsed as expected

XmiliaH commented 2 months ago

How about a ? b <=> c : d?

XmiliaH commented 2 months ago

And I would also like

--- a/src/lparser.cpp
+++ b/src/lparser.cpp
@@ -3690,7 +3690,7 @@ static void expr (LexState *ls, expdesc *v, TypeHint *prop, int flags) {
     luaK_patchtohere(fs, condition);
     checknext(ls, ':');
     ls->pushContext(PARCTX_TERNARY_C);
-    expr(ls, v, prop);
+    expr(ls, v, prop, flags & E_NO_COLON);
     ls->popContext(PARCTX_TERNARY_C);
     luaK_exp2reg(fs, v, reg);
     luaK_patchtohere(fs, escape);

for the ternary nested case. But this is my preference.

XmiliaH commented 2 months ago

There is b ? c |> d : e : f() which is parsed as b ? (c |> d : e) : f() but this might make sense.

Sainan commented 2 months ago

I'm personally not a huge fan of nested ternaries, but it's probably better to parse those with E_NO_COLON.

XmiliaH commented 2 months ago

I am not even a fan of ternaries in lua due to the : ambiguity (is it the end of the ternary or a method call?).

Sainan commented 2 months ago

Well, we still have the if-expression type of ternary, but guess which one is more popular...

So, yeah, parsing it is really cursed, but people do prefer it.