From 99692899d1e793e2cbbaea03107cb0a5f5e5c452 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 27 Jul 2025 16:56:45 +0800 Subject: Fixed issue #222. --- src/yuescript/yue_ast.cpp | 11 +++++++++++ src/yuescript/yue_ast.h | 9 ++++++++- src/yuescript/yue_compiler.cpp | 29 ++++++++++++++++++++++++++++- src/yuescript/yue_parser.cpp | 4 +++- src/yuescript/yue_parser.h | 1 + 5 files changed, 51 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp index 0ad581b..945e1d7 100644 --- a/src/yuescript/yue_ast.cpp +++ b/src/yuescript/yue_ast.cpp @@ -304,6 +304,17 @@ std::string ImportAs_t::to_string(void* ud) const { } return join(temp, " "s); } +std::string ImportGlobal_t::to_string(void* ud) const { + str_list temp; + for (auto seg : segs.objects()) { + temp.emplace_back(seg->to_string(ud)); + } + auto item = join(temp, "."s); + if (target) { + return item + " as "s + target->to_string(ud); + } + return item; +} std::string Import_t::to_string(void* ud) const { if (ast_is(content)) { return content->to_string(ud); diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 388bc85..1937eb8 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -233,8 +233,15 @@ AST_NODE(ImportAs) AST_MEMBER(ImportAs, &literal, &target) AST_END(ImportAs) +AST_NODE(ImportGlobal) + ast_ptr sep; + ast_list segs; + ast_ptr target; + AST_MEMBER(ImportGlobal, &sep, &segs, &target) +AST_END(ImportGlobal) + AST_NODE(Import) - ast_sel content; + ast_sel content; AST_MEMBER(Import, &content) AST_END(Import) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 4f6e025..d676750 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.29.2"sv; +const std::string_view version = "0.29.3"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -11040,6 +11040,30 @@ private: } } + void transformImportGlobal(ImportGlobal_t* importNode, str_list& out) { + auto uname = static_cast(importNode->segs.front()); + auto var = _parser.toString(uname); + auto isNormal = _parser.match(var) && _parser.match(var); + auto varName = unicodeVariableFrom(uname); + str_list temp; + auto it = ++importNode->segs.objects().begin(); + for (; it != importNode->segs.objects().end(); ++it) { + temp.emplace_back(_parser.toString(*it)); + } + temp.emplace_front(var); + if (isLocal(varName) || !isNormal) { + temp.emplace_front("_G"s); + } + std::string stmt; + if (importNode->target) { + stmt = "const "s + _parser.toString(importNode->target) + '=' + join(temp, "."sv); + } else { + stmt = "const "s + temp.back() + '=' + join(temp, "."sv); + } + auto localAttrib = toAst(stmt, importNode); + transformLocalAttrib(localAttrib, out); + } + void transformImport(Import_t* import, str_list& out) { auto content = import->content.get(); switch (content->get_id()) { @@ -11052,6 +11076,9 @@ private: case id(): transformFromImport(static_cast(content), out); break; + case id(): + transformImportGlobal(static_cast(content), out); + break; default: YUEE("AST node mismatch", content); break; } } diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index aefa350..1942e23 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -349,7 +349,9 @@ YueParser::YueParser() { ImportAs = ImportLiteral >> -(space >> key("as") >> space >> (ImportTabLit | Variable | ImportAllMacro)); - Import = key("import") >> space >> (ImportAs | ImportFrom) | FromImport; + ImportGlobal = Seperator >> UnicodeName >> *('.' >> UnicodeName) >> -(space >> key("as") >> space >> Variable); + + Import = key("import") >> space >> (ImportAs | ImportFrom | ImportGlobal) | FromImport; Label = "::" >> LabelName >> "::"; diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index e905840..c91e530 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -320,6 +320,7 @@ private: AST_RULE(ImportAllMacro); AST_RULE(ImportTabLit); AST_RULE(ImportAs); + AST_RULE(ImportGlobal); AST_RULE(Import); AST_RULE(Label); AST_RULE(Goto); -- cgit v1.2.3-55-g6feb