From d1878f2b8eb1644205c5e0b641fd092e8b222766 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 31 Jan 2025 23:17:45 +0800 Subject: Aligned Lua inserter macro behavior with common Yue macro. --- CMakeLists.txt | 30 ------------------------------ doc/docs/doc/README.md | 12 ++++-------- doc/docs/zh/doc/README.md | 8 +++----- spec/outputs/ambiguous.lua | 4 +++- spec/outputs/codes_from_doc.lua | 18 ++++++++++++++---- spec/outputs/codes_from_doc_zh.lua | 14 ++++++++++++-- spec/outputs/macro.lua | 18 ++++++++++++++++++ spec/outputs/unicode/ambiguous.lua | 4 +++- spec/outputs/unicode/macro.lua | 14 ++++++++++++++ src/yuescript/yue_compiler.cpp | 14 +++----------- 10 files changed, 74 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9149ec5..613019e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}") diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 4eec2d5..89bd643 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md @@ -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
 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
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index c3a5b76..1a2da96 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -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
diff --git a/spec/outputs/ambiguous.lua b/spec/outputs/ambiguous.lua
index 3c82a03..fc43d63 100644
--- a/spec/outputs/ambiguous.lua
+++ b/spec/outputs/ambiguous.lua
@@ -39,9 +39,11 @@ end
 do
 	print()
 	async_fn(function()
-		print();
+		print()
+		do
 --[[a comment to insert]]
 		(haha)()
+		end
 		return nil
 	end)
 end
diff --git a/spec/outputs/codes_from_doc.lua b/spec/outputs/codes_from_doc.lua
index 5c262d3..74db469 100644
--- a/spec/outputs/codes_from_doc.lua
+++ b/spec/outputs/codes_from_doc.lua
@@ -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")
diff --git a/spec/outputs/codes_from_doc_zh.lua b/spec/outputs/codes_from_doc_zh.lua
index c6d5388..1a10555 100644
--- a/spec/outputs/codes_from_doc_zh.lua
+++ b/spec/outputs/codes_from_doc_zh.lua
@@ -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")
diff --git a/spec/outputs/macro.lua b/spec/outputs/macro.lua
index 7163077..aa74ed9 100644
--- a/spec/outputs/macro.lua
+++ b/spec/outputs/macro.lua
@@ -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,6 +223,7 @@ sel = function(a, b, c)
 		return c
 	end
 end
+do
 local function sel(a, b, c)
 	if a then
 		return b
@@ -224,10 +231,15 @@ local function sel(a, b, c)
 		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
diff --git a/spec/outputs/unicode/ambiguous.lua b/spec/outputs/unicode/ambiguous.lua
index ac90016..704b311 100644
--- a/spec/outputs/unicode/ambiguous.lua
+++ b/spec/outputs/unicode/ambiguous.lua
@@ -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
diff --git a/spec/outputs/unicode/macro.lua b/spec/outputs/unicode/macro.lua
index 069f429..e2195e4 100644
--- a/spec/outputs/unicode/macro.lua
+++ b/spec/outputs/unicode/macro.lua
@@ -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,6 +199,7 @@ 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
@@ -202,10 +207,15 @@ local function sel(a, b, c)
 		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)
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 446414d..e7fe0c0 100644
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -24,7 +24,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
 
 extern "C" {
 #include "lauxlib.h"
-#include "lparser.h"
 #include "lua.h"
 #include "lualib.h"
 } // extern "C"
@@ -76,7 +75,7 @@ static std::unordered_set Metamethods = {
 	"close"s // Lua 5.4
 };
 
-const std::string_view version = "0.26.3"sv;
+const std::string_view version = "0.27.0"sv;
 const std::string_view extension = "yue"sv;
 
 class CompileError : public std::logic_error {
@@ -6567,17 +6566,10 @@ private:
 				if (luaL_loadbuffer(L, codes.c_str(), codes.size(), macroChunk.c_str()) != 0) {
 					std::string err = lua_tostring(L, -1);
 					throw CompileError("lua macro is not expanding to valid block\n"s + err, x);
-				} else {
-					Proto* f = ((LClosure*)lua_topointer(L, -1))->p;
-					for (int i = 0; i < f->sizelocvars; i++) {
-						localVars.push_back(getstr(f->locvars[i].varname));
-					}
 				}
 				if (!codes.empty()) {
-					if (_config.reserveLineNumber) {
-						codes.insert(0, nll(chainValue).substr(1));
-					}
-					codes.append(nlr(chainValue));
+					codes.insert(0, indent() + "do"s + nll(chainValue));
+					codes.append(_newLine + indent() + "end"s + nlr(chainValue));
 				}
 				return {nullptr, nullptr, std::move(codes), std::move(localVars)};
 			} else {
-- 
cgit v1.2.3-55-g6feb