Yuescript
Yuescript copied to clipboard
Allow lua 5.4 import
Lua 5.4 uses import
instead of require
. Is there a way we can use import "module"
instead of require("module")
in generated code?
I can not find the import feature you mentioned in official doc http://www.lua.org/manual/5.4/. Can you provide a link or something to get me informed?
Fair point, perhaps I was mistaken. Playdate's embedded Lua only uses the import
keyword, not require
. Right now I've been trying to do this with macros, but since Playdate doesn't have require
, I cannot use from "class" import A
. Would it be possible to add a toggle or some kind of config?
Also, is import "a/b"
supposed to result in a syntax error?
"import" is treated as a keyword for the moment, but I can try to change that. And supporting 'import("module")' as a function call in Yuescript.
Would a macro suffice here?
export macro raw = (code) ->
{ :code, type: "lua" }
-- usage
$raw [[ import "x" ]]
This is what I do since xmake
uses an import
function:
$raw[[
import("core.base.option")
]]
@chrsm Macros would work, but what about import from and import as?
I'd say that import
should be turned into a soft keyword that's only reserved when used as a statement, but not as an expression, so that
import "foo"
still compiles to
local foo = require("foo")
but
foo = import "foo"
becomes
local foo = import("foo")
If someone wanted to make the import
statement work in an environment that renames require
to import
, they could simple put something like
global require = import
at the beginning of their script.
Digging around the parser rules a little while, I'm thinking of adding special aliasing syntax like:
`import "abc" -- the backtick will turn keyword import to variable name
Or we have to keep using macro function like this to get similar result:
macro import = (moduleName) -> "_G['import'](#{moduleName})"
$import "abc.zyx"
mod = $import "module"
nil
compiles to:
_G['import']("abc.zyx")
local mod = _G['import']("module")
return nil
Making import
a soft keyword and making import "abc"
and import("abc")
with different meanings will lead to inconsistency in syntax.
And an alternative writing could be just:
abc = _G.import "abc"
_G.import "module"
@SlashScreen
Fair point, perhaps I was mistaken. Playdate's embedded Lua only uses the
import
keyword, notrequire
. Right now I've been trying to do this with macros, but since Playdate doesn't haverequire
, I cannot usefrom "class" import A
. Would it be possible to add a toggle or some kind of config? Also, isimport "a/b"
supposed to result in a syntax error?
If you want to use the import()
function with from "class" import A
syntax in current Yuescript. We can do it with:
macro Playdate = (moduleName) -> "_G.import(#{moduleName})"
from $Playdate("module") import a, b, c
get
local a, b, c
do
local _obj_0 = _G.import("module")
a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
end
import "a/b"
is supposed to result in a syntax error. You have to do import "a.b"
as a Lua common practice instead. Or use require "a/b"
.