Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

Accepted values in backcall/pipeline operator.

Open daelvn opened this issue 4 years ago • 4 comments

Hello. While trying to move one of my projects over from Moonscript to MoonPlus, I tried to do the following:

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

But I was met with the following error:

Fail to compile: fir/generic/emit/markdown.mp.
193: backcall operator must be followed by chain value
  |> (_) -> extract _, language, {}
    ^

In this case, some questions or proposals come to mind:

  1. What counts as a "chain value"?
  2. Could a function be supported in a future update, as in my snippet?
  3. Could there be a simpler way to do what I'm trying to do? Something along the lines of |> extract $, language, {} or |> extract @, language, {}.
  4. Although this isn't a serious complain, the Fail to compile error does not roll off the tongue very well, maybe something like Could not compile or Failed to compile?

daelvn avatar Jan 05 '21 00:01 daelvn

  1. A chain value for backcall syntax could be one of the followings:

Callable:

@, @field, (a + b), func, t.func, t\func, t.func?, $macro

Invoke:

f!, f(1,2,3), $macro!, $macro(1,2,3)

DotChainItem:

with t
  1 |> .func

ColonChainItem:

with t
  1 |> \func
  1. The accepted form for your example could be
readFile("example.txt")
  |> ((_)-> extract _, language, {})
  |> ((_)-> parse _, language)
  |> emit
  |> render
  |> print

-- or

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

The back call operator has a higher operator precedence. Without brackets the codes would be compiled as:

-- the example without proper brackets
readFile "example.txt"
  |> extract language, {}
  |> parse language
  |> emit
  |> render
  |> print
-- is treated like
readFile ("example.txt" |> extract language, ({} |> parse (language |> emit |> render |> print)))
  1. It is really a problem. Got it fixed. 6a48f108666126893dcf7ff54b12172795858d3a

pigpigyyy avatar Jan 05 '21 01:01 pigpigyyy

Thank you for the help!

daelvn avatar Jan 05 '21 01:01 daelvn

Just found that I missed a question. There is a syntax for your need:

readFile("example.txt")
  |> extract(_, language, {}) -- _ is a placeholder here instead of a variable indicating where to insert the argument
  |> parse(_, language)
  |> emit
  |> render
  |> print

2 |> func(1, _, 3) -- will be compiled to func(1, 2, 3)

pigpigyyy avatar Jan 05 '21 02:01 pigpigyyy

Changed the backcall operator syntax.

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

will be compiled to the expected output with latest commit, extra parentheses are no longer needed.

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

pigpigyyy avatar Feb 10 '21 15:02 pigpigyyy