Yuescript
Yuescript copied to clipboard
Luacode macros fail to compile
Luacode macros fail to compile with the response lua macro can only be placed where block macro is allowed.
Example from website fails to compile:
macro lua = (code)-> {
:code
type: "lua"
}
-- the raw string leading and ending symbols are auto trimed
$lua[==[
-- raw Lua codes insertion
if cond then
print("output")
end
]==]
From my usage, it seems export makes the macro available to use as such, but the same issue occurs when importing the export luacode macro from another file.
This fails:
macro PRINT = (var) -> {
code: "print(#{var})"
type: "lua"
}
$PRINT "Hello, world!"
This works:
export macro PRINT = (var) -> {
code: "print(#{var})"
type: "lua"
}
$PRINT "Hello, world!"
This fails:
-- main.yue
export macro PRINT = (var) -> {
code: "print(#{var})"
type: "lua"
}
$PRINT "Hello, world!"
-- other.yue
import "main" as $
$PRINT "Hello, world!"
The problem is
macro PRINT = (var) -> {
code: "print(#{var})"
type: "lua"
}
$PRINT "Hello, world!"
With the default compiler option implicit_return_root = true. It is treated like
macro PRINT = (var) -> {
code: "print(#{var})"
type: "lua"
}
return $PRINT "Hello, world!"
And the Lua macro is expected to generate code block instead of an expression and the generated code block may not be available to be placed after a return statement.
On the website, the compiler is using option implicit_return_root = false for better demonstration without adding the return keyword at the last line that made this confusing behavior.
You can add an extra return line at the end of your codes for the moment when using the Yuescript compiler. And I'm going to think of a way to deal with the confusing problem.
macro PRINT = (var) -> {
code: "print(#{var})"
type: "lua"
}
$PRINT "Hello, world!"
return
-- main.yue
export macro PRINT = (var) -> {
code: "print(#{var})"
type: "lua"
}
$PRINT "Hello, world!"
-- other.yue
import "main" as $
$PRINT "Hello, world!"
return
Forgot to close this issue, thank you for the clarification!