Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

Precedence of pipeline operator with list syntax w/o explicit line continuation

Open refi64 opened this issue 2 years ago • 3 comments

Right now, if I compile this code:

f
  * a
    |> b

the resulting Lua is:

return b(f({ -- 2
  a -- 2
})) -- 2

This is the opposite of what I'd expect: based on the indentation, I would've thought it would be parsed as f({ a|> b }), not f({ a }) |> b. If I use an explicit slash:

f
  * a \
    |> b

then it compiles like I'd expect:

return f({ -- 2
  b(a) -- 2
}) -- 3

I'm not sure if this is intentional? But it was certainly rather confusing :sweat_smile:

refi64 avatar Jun 20 '23 01:06 refi64

It is a syntax precedence problem. To support syntax like:

readFile "example.txt"
  |> extract language, {}
  |> parse language
  |> emit
  |> render
  |> print

The multiline pipes are parsed as statements instead of parts of an expression for the moment. So that the newline characters are breaking the expression in the end of function call argument. If we make pipe syntax a higher precedence, the example codes above will be parsed as:

readFile("example.txt" |> extract(language, {} |> parse(language |> emit |> render |> print))

So we have to carefully write every bracket for the right multiline pipes parsing that way.

readFile("example.txt")
  |> extract(language, {})
  |> parse(language)
  |> emit
  |> render
  |> print

pigpigyyy avatar Jun 20 '23 02:06 pigpigyyy

That makes sense, but I feel like the indentation is what makes this weirder? Like I'd certainly expect that behavior from, say:

f
  * a
  |> b

which is closer to the example you gave in terms of indentation. The weird part in the original example to me is that it's reaching across multiple levels of indentation.

refi64 avatar Jun 20 '23 04:06 refi64

In the current Yuescript compiler the orignal code:

f
  * a
    |> b

is just seen as:

[expression][newline character]
[advanced indentation][pipe expression]

And there is no rule to tell different [advanced indentation] levels should have different meanings.

pigpigyyy avatar Jun 20 '23 07:06 pigpigyyy