terralang / terra

Terra is a low-level system programming language that is embedded in and meta-programmed by the Lua programming language.
terralang.org
Other
2.72k stars 201 forks source link

Multiple-Expression in escapes #431

Closed XeroOl closed 4 years ago

XeroOl commented 4 years ago

Contrary to what the API says, it's not possible to escape multiple expressions in one escape. This is important because it means that there's no way to make vararg escapes.

According to the terra api, it should be possible to have an escape with multiple values. The API provides this as an example, where "[luaexpr] is a multiple-expression escape since it occurs as the last expression in a list of expressions."

terra foo()
    bar(3,4,[luaexpr])
end

Obviously, this example is missing some context (ie a definition of bar) before it can compile. Here's what I came up with:

local luaexpr1 = 5
local luaexpr2 = 6

terra bar(a: int8, b: int8, c:int8, d:int8)

end

terra foo()
    bar(3,4,[luaexpr1, luaexpr2])
end

I've split the lua expression into two to form a multiple-expression escape. I expected this to compile, with the escape essentially desugaring to bar(3, 4, 5, 6), but instead, there is a compiler error:

multiple_expression.t:10: ']' expected near ','

Since the API reference claims that this behavior is possible, either the API reference or the implementation is wrong.

PS: If you have a lua expression that expands to multiple values, ie bar(3, 4, [luaexpr()]), it truncates the function's result down to one value.

probable-basilisk commented 4 years ago

You need to create them as a list of expressions:

terra bar(a: int8, b: int8, c:int8, d:int8): int8
  return d
end

local function escape_two()
  local rets = terralib.newlist()
  rets:insert(`12)
  rets:insert(`13)
  return rets
end

terra foo(): int8
  return bar(3,4,[escape_two()])
end
aiverson commented 4 years ago

Or you can just do an inline table like bar(3, 4, [{luaexpr1, luaexpr2}]

XeroOl commented 4 years ago

Thanks for the help! I was just misunderstanding the docs