Yuescript icon indicating copy to clipboard operation
Yuescript copied to clipboard

Allow lua 5.4 import

Open SlashScreen opened this issue 1 year ago • 8 comments

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

SlashScreen avatar Nov 10 '23 21:11 SlashScreen

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?

pigpigyyy avatar Nov 12 '23 08:11 pigpigyyy

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?

SlashScreen avatar Nov 12 '23 08:11 SlashScreen

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

pigpigyyy avatar Nov 12 '23 09:11 pigpigyyy

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 avatar Nov 12 '23 22:11 chrsm

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

SlashScreen avatar Nov 13 '23 02:11 SlashScreen

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.

SkyyySi avatar Nov 14 '23 15:11 SkyyySi

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 avatar Nov 15 '23 01:11 pigpigyyy

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

pigpigyyy avatar Nov 15 '23 04:11 pigpigyyy