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

Allow lua 5.4 import #156

Open
SlashScreen opened this issue Nov 10, 2023 · 8 comments
Open

Allow lua 5.4 import #156

SlashScreen opened this issue Nov 10, 2023 · 8 comments

Comments

@SlashScreen
Copy link

Lua 5.4 uses import instead of require. Is there a way we can use import "module" instead of require("module") in generated code?

@pigpigyyy
Copy link
Owner

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?

@SlashScreen
Copy link
Author

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?

@pigpigyyy
Copy link
Owner

pigpigyyy commented Nov 12, 2023

"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.

@chrsm
Copy link

chrsm commented Nov 12, 2023

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")
    ]]

@SlashScreen
Copy link
Author

@chrsm Macros would work, but what about import from and import as?

@SkyyySi
Copy link

SkyyySi commented Nov 14, 2023

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.

@pigpigyyy
Copy link
Owner

pigpigyyy commented Nov 15, 2023

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"

@pigpigyyy
Copy link
Owner

@SlashScreen

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?

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".

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

4 participants