Skip to content

Commit

Permalink
Aligned Lua inserter macro behavior with common Yue macro.
Browse files Browse the repository at this point in the history
pigpigyyy committed Jan 31, 2025
1 parent 8124020 commit d1878f2
Showing 10 changed files with 74 additions and 62 deletions.
30 changes: 0 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(yue CXX)

include(FetchContent)

execute_process(
COMMAND ${LUA} -v
OUTPUT_VARIABLE LUA_VERSION_OUTPUT
ERROR_VARIABLE LUA_VERSION_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE
)

string(REGEX MATCH "Lua ([0-9]+)\\.([0-9]+)\\.([0-9]+)" LUA_VERSION_MATCH ${LUA_VERSION_OUTPUT})
set(LUA_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")

set(LUA_TARBALL_URL "https://www.lua.org/ftp/lua-${LUA_VERSION}.tar.gz")

message(STATUS "Detected Lua version: ${LUA_VERSION}")
message(STATUS "Downloading Lua source from: ${LUA_TARBALL_URL}")

FetchContent_Declare(
lua_source
URL ${LUA_TARBALL_URL}
)

FetchContent_MakeAvailable(lua_source)

set(LUA_SOURCE_DIR "${lua_source_SOURCE_DIR}")

include_directories(${LUA_SOURCE_DIR}/src)

message(STATUS "Lua source directory: ${LUA_SOURCE_DIR}/src")

set(LUA_LIBDIR ${LUA_INCDIR}/../lib ${LUA_INCDIR}/../../lib)
set(LUA_INCLUDE_DIR "${LUA_INCDIR}")

12 changes: 4 additions & 8 deletions doc/docs/doc/README.md
Original file line number Diff line number Diff line change
@@ -282,16 +282,14 @@ A macro function can either return a YueScript string or a config table containi
```moonscript
macro yueFunc = (var) -> "local #{var} = ->"
$yueFunc funcA
funcA = -> "fail to assign to the Yue defined variable"
funcA = -> "fail to assign to the Yue macro defined variable"
-- YueScript knows the
-- local variables you declared in Lua code
macro luaFunc = (var) -> {
code: "local function #{var}() end"
type: "lua"
}
$luaFunc funcB
funcB = -> "assign to the Lua defined variable"
funcB = -> "fail to assign to the Lua macro defined variable"
macro lua = (code) -> {
:code
@@ -310,16 +308,14 @@ end
<pre>
macro yueFunc = (var) -> "local #{var} = ->"
$yueFunc funcA
funcA = -> "fail to assign to the Yue defined variable"
funcA = -> "fail to assign to the Yue macro defined variable"

-- YueScript knows the
-- local variables you declared in Lua codes
macro luaFunc = (var) -> {
code: "local function #{var}() end"
type: "lua"
}
$luaFunc funcB
funcB = -> "assign to the Lua defined variable"
funcB = -> "fail to assign to the Lua macro defined variable"

macro lua = (code) -> {
:code
8 changes: 3 additions & 5 deletions doc/docs/zh/doc/README.md
Original file line number Diff line number Diff line change
@@ -277,19 +277,18 @@ if $and f1!, f2!, f3!

### 直接插入代码

宏函数可以返回一个包含月之脚本代码的字符串,或是一个包含Lua代码字符串的配置表
宏函数可以返回一个包含月之脚本代码的字符串,或是一个包含 Lua 代码字符串的配置表
```moonscript
macro yueFunc = (var) -> "local #{var} = ->"
$yueFunc funcA
funcA = -> "无法访问宏生成月之脚本里定义的变量"
-- 月之脚本会知道你在 Lua 代码中声明的局部变量
macro luaFunc = (var) -> {
code: "local function #{var}() end"
type: "lua"
}
$luaFunc funcB
funcB = -> "访问宏生成Lua代码里定义的变量"
funcB = -> "无法访问宏生成 Lua 代码里定义的变量"
macro lua = (code) -> {
:code
@@ -310,13 +309,12 @@ macro yueFunc = (var) -> "local #{var} = ->"
$yueFunc funcA
funcA = -> "无法访问宏生成月之脚本里定义的变量"

-- 月之脚本会知道你在 Lua 代码中声明的局部变量
macro luaFunc = (var) -> {
code: "local function #{var}() end"
type: "lua"
}
$luaFunc funcB
funcB = -> "访问宏生成Lua代码里定义的变量"
funcB = -> "无法访问宏生成 Lua 代码里定义的变量"

macro lua = (code) -> {
:code
4 changes: 3 additions & 1 deletion spec/outputs/ambiguous.lua
Original file line number Diff line number Diff line change
@@ -39,9 +39,11 @@ end
do
print()
async_fn(function()
print();
print()
do
--[[a comment to insert]]
(haha)()
end
return nil
end)
end
18 changes: 14 additions & 4 deletions spec/outputs/codes_from_doc.lua
Original file line number Diff line number Diff line change
@@ -59,16 +59,21 @@ do
end
local funcA
funcA = function()
return "fail to assign to the Yue defined variable"
return "fail to assign to the Yue macro defined variable"
end
do
local function funcB() end
end
local funcB
funcB = function()
return "assign to the Lua defined variable"
return "fail to assign to the Lua macro defined variable"
end
do
-- raw Lua codes insertion
if cond then
print("output")
end
end
print("yuescript")
print(3)
print("Valid enum type:", "Static")
@@ -2065,16 +2070,21 @@ do
end
local funcA
funcA = function()
return "fail to assign to the Yue defined variable"
return "fail to assign to the Yue macro defined variable"
end
do
local function funcB() end
end
local funcB
funcB = function()
return "assign to the Lua defined variable"
return "fail to assign to the Lua macro defined variable"
end
do
-- raw Lua codes insertion
if cond then
print("output")
end
end
print("yuescript")
print(3)
print("Valid enum type:", "Static")
14 changes: 12 additions & 2 deletions spec/outputs/codes_from_doc_zh.lua
Original file line number Diff line number Diff line change
@@ -61,14 +61,19 @@ local funcA
funcA = function()
return "无法访问宏生成月之脚本里定义的变量"
end
do
local function funcB() end
end
local funcB
funcB = function()
return "访问宏生成Lua代码里定义的变量"
return "无法访问宏生成 Lua 代码里定义的变量"
end
do
-- 插入原始Lua代码
if cond then
print("输出")
end
end
print("yuescript")
print(3)
print("有效的枚举类型:", "Static")
@@ -2061,14 +2066,19 @@ local funcA
funcA = function()
return "无法访问宏生成月之脚本里定义的变量"
end
do
local function funcB() end
end
local funcB
funcB = function()
return "访问宏生成Lua代码里定义的变量"
return "无法访问宏生成 Lua 代码里定义的变量"
end
do
-- 插入原始Lua代码
if cond then
print("输出")
end
end
print("yuescript")
print(3)
print("有效的枚举类型:", "Static")
18 changes: 18 additions & 0 deletions spec/outputs/macro.lua
Original file line number Diff line number Diff line change
@@ -5,7 +5,9 @@ do
end
print(456)
do
do
-- TODO: "todo in a do block"
end
end
end
do
@@ -201,13 +203,17 @@ do
print(a)
end
local x = 0
do
local function f(a)
return a + 1
end
x = x + f(3)
end
do
function tb:func()
print(123)
end
end
print(x)
local sel
sel = function(a, b, c)
@@ -217,17 +223,23 @@ sel = function(a, b, c)
return c
end
end
do
local function sel(a, b, c)
if a then
return b
else
return c
end
end
end
do
local function dummy()

end
end
do
-- a comment here
end
local _ = require('underscore')
local a = ((((_({
1,
@@ -279,9 +291,11 @@ do
_6:Destroy()
end
end
do
origin.transform.root.gameObject:Parents():Descendants():SelectEnable():SelectVisible():TagEqual("fx"):Where(function(x)
return x.name:EndsWith("(Clone)")
end):Destroy()
end
print((setmetatable({
'abc',
a = 123,
@@ -313,7 +327,9 @@ print((setmetatable({
}))
print("current line: " .. tostring(323))
do
do
-- TODO
end
end
do
print(1)
@@ -336,7 +352,9 @@ do
end
local f1
f1 = function()
do
tb:func(123)
end
return
end
end
4 changes: 3 additions & 1 deletion spec/outputs/unicode/ambiguous.lua
Original file line number Diff line number Diff line change
@@ -39,9 +39,11 @@ end
do
_u6253_u5370()
_u5f02_u6b65_u51fd_u6570(function()
_u6253_u5370();
_u6253_u5370()
do
--[[a comment to insert]]
(haha)()
end
return nil
end)
end
14 changes: 14 additions & 0 deletions spec/outputs/unicode/macro.lua
Original file line number Diff line number Diff line change
@@ -179,13 +179,17 @@ do
_u6253_u5370(_u53d8_u91cfa)
end
local _u53d8_u91cfx = 0
do
local function f(a)
return a + 1
end
x = x + f(3)
end
do
function tb:func()
print(123)
end
end
_u6253_u5370(_u53d8_u91cfx)
local sel
sel = function(_u53c2_u6570a, _u53c2_u6570b, _u53c2_u6570c)
@@ -195,17 +199,23 @@ sel = function(_u53c2_u6570a, _u53c2_u6570b, _u53c2_u6570c)
return _u53c2_u6570c
end
end
do
local function sel(a, b, c)
if a then
return b
else
return c
end
end
end
do
local function dummy()

end
end
do
-- 这有个注释
end
local _ = require('下划线')
local _call_0 = (_({
1,
@@ -275,11 +285,13 @@ do
_6["摧毁"](_6)
end
end
do
local _call_0 = _u539f_u70b9["变换"]["根节点"]["游戏对象"]
_call_0["父节点"](_call_0):_u540e_u4ee3():_u9009_u62e9_u542f_u7528():_u9009_u62e9_u53ef_u89c1():_u6807_u7b7e_u7b49_u4e8e("fx"):_u5176_u4e2d(function(x)
local _call_0 = x["名称"]
return _call_0["结尾为"](_call_0, "(克隆)")
end):_u6467_u6bc1()
end
_u6253_u5370((setmetatable({
'abc',
["字段a"] = 123,
@@ -311,7 +323,9 @@ _u6253_u5370((setmetatable({
}))
_u6253_u5370("当前代码行数: " .. tostring(268))
do
do
-- 待实现
end
end
do
_u6253_u5370(1)
Loading

0 comments on commit d1878f2

Please sign in to comment.