Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
refi64 opened this issue Jun 20, 2023 · 3 comments

Comments

@refi64
Copy link

refi64 commented Jun 20, 2023

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 😅

@pigpigyyy
Copy link
Owner

pigpigyyy commented Jun 20, 2023

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

@refi64
Copy link
Author

refi64 commented Jun 20, 2023

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.

@pigpigyyy
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants