diff options
| author | Li Jin <dragon-fly@qq.com> | 2025-07-27 16:56:45 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2025-07-27 16:56:45 +0800 |
| commit | 99692899d1e793e2cbbaea03107cb0a5f5e5c452 (patch) | |
| tree | 44745041f0aa184b8cafc98427c7b627b38c3c96 | |
| parent | c017493f56e98fcbc88f9459b9c10b3e1acc3e6e (diff) | |
| download | yuescript-99692899d1e793e2cbbaea03107cb0a5f5e5c452.tar.gz yuescript-99692899d1e793e2cbbaea03107cb0a5f5e5c452.tar.bz2 yuescript-99692899d1e793e2cbbaea03107cb0a5f5e5c452.zip | |
Fixed issue #222.v0.29.3
| -rw-r--r-- | spec/inputs/import.yue | 10 | ||||
| -rw-r--r-- | spec/outputs/import.lua | 10 | ||||
| -rw-r--r-- | src/yuescript/yue_ast.cpp | 11 | ||||
| -rw-r--r-- | src/yuescript/yue_ast.h | 9 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 29 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.cpp | 4 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.h | 1 |
7 files changed, 71 insertions, 3 deletions
diff --git a/spec/inputs/import.yue b/spec/inputs/import.yue index b8ffc24..8982e6c 100644 --- a/spec/inputs/import.yue +++ b/spec/inputs/import.yue | |||
| @@ -139,3 +139,13 @@ do | |||
| 139 | import "m" as {c: d} | 139 | import "m" as {c: d} |
| 140 | import "m" as {g, {<close>: i}} | 140 | import "m" as {g, {<close>: i}} |
| 141 | 141 | ||
| 142 | do | ||
| 143 | import require | ||
| 144 | import string as stringlib | ||
| 145 | import string.format | ||
| 146 | import io.read as io_read | ||
| 147 | |||
| 148 | type = -> | ||
| 149 | import type as tp | ||
| 150 | import 月 as yue | ||
| 151 | |||
diff --git a/spec/outputs/import.lua b/spec/outputs/import.lua index 83c99e2..7aa130f 100644 --- a/spec/outputs/import.lua +++ b/spec/outputs/import.lua | |||
| @@ -166,3 +166,13 @@ do | |||
| 166 | local _obj_1 = require("m") | 166 | local _obj_1 = require("m") |
| 167 | g, i = _obj_1[1], getmetatable(_obj_1[2]).__close | 167 | g, i = _obj_1[1], getmetatable(_obj_1[2]).__close |
| 168 | end | 168 | end |
| 169 | do | ||
| 170 | local require <const> = require | ||
| 171 | local stringlib <const> = string | ||
| 172 | local format <const> = string.format | ||
| 173 | local io_read <const> = io.read | ||
| 174 | local type | ||
| 175 | type = function() end | ||
| 176 | local tp <const> = _G.type | ||
| 177 | local yue <const> = _G["月"] | ||
| 178 | end | ||
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 { | |||
| 304 | } | 304 | } |
| 305 | return join(temp, " "s); | 305 | return join(temp, " "s); |
| 306 | } | 306 | } |
| 307 | std::string ImportGlobal_t::to_string(void* ud) const { | ||
| 308 | str_list temp; | ||
| 309 | for (auto seg : segs.objects()) { | ||
| 310 | temp.emplace_back(seg->to_string(ud)); | ||
| 311 | } | ||
| 312 | auto item = join(temp, "."s); | ||
| 313 | if (target) { | ||
| 314 | return item + " as "s + target->to_string(ud); | ||
| 315 | } | ||
| 316 | return item; | ||
| 317 | } | ||
| 307 | std::string Import_t::to_string(void* ud) const { | 318 | std::string Import_t::to_string(void* ud) const { |
| 308 | if (ast_is<FromImport_t>(content)) { | 319 | if (ast_is<FromImport_t>(content)) { |
| 309 | return content->to_string(ud); | 320 | 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) | |||
| 233 | AST_MEMBER(ImportAs, &literal, &target) | 233 | AST_MEMBER(ImportAs, &literal, &target) |
| 234 | AST_END(ImportAs) | 234 | AST_END(ImportAs) |
| 235 | 235 | ||
| 236 | AST_NODE(ImportGlobal) | ||
| 237 | ast_ptr<true, Seperator_t> sep; | ||
| 238 | ast_list<true, UnicodeName_t> segs; | ||
| 239 | ast_ptr<false, Variable_t> target; | ||
| 240 | AST_MEMBER(ImportGlobal, &sep, &segs, &target) | ||
| 241 | AST_END(ImportGlobal) | ||
| 242 | |||
| 236 | AST_NODE(Import) | 243 | AST_NODE(Import) |
| 237 | ast_sel<true, ImportAs_t, ImportFrom_t, FromImport_t> content; | 244 | ast_sel<true, ImportAs_t, ImportFrom_t, FromImport_t, ImportGlobal_t> content; |
| 238 | AST_MEMBER(Import, &content) | 245 | AST_MEMBER(Import, &content) |
| 239 | AST_END(Import) | 246 | AST_END(Import) |
| 240 | 247 | ||
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<std::string> Metamethods = { | |||
| 78 | "close"s // Lua 5.4 | 78 | "close"s // Lua 5.4 |
| 79 | }; | 79 | }; |
| 80 | 80 | ||
| 81 | const std::string_view version = "0.29.2"sv; | 81 | const std::string_view version = "0.29.3"sv; |
| 82 | const std::string_view extension = "yue"sv; | 82 | const std::string_view extension = "yue"sv; |
| 83 | 83 | ||
| 84 | class CompileError : public std::logic_error { | 84 | class CompileError : public std::logic_error { |
| @@ -11040,6 +11040,30 @@ private: | |||
| 11040 | } | 11040 | } |
| 11041 | } | 11041 | } |
| 11042 | 11042 | ||
| 11043 | void transformImportGlobal(ImportGlobal_t* importNode, str_list& out) { | ||
| 11044 | auto uname = static_cast<UnicodeName_t*>(importNode->segs.front()); | ||
| 11045 | auto var = _parser.toString(uname); | ||
| 11046 | auto isNormal = _parser.match<Name_t>(var) && _parser.match<Variable_t>(var); | ||
| 11047 | auto varName = unicodeVariableFrom(uname); | ||
| 11048 | str_list temp; | ||
| 11049 | auto it = ++importNode->segs.objects().begin(); | ||
| 11050 | for (; it != importNode->segs.objects().end(); ++it) { | ||
| 11051 | temp.emplace_back(_parser.toString(*it)); | ||
| 11052 | } | ||
| 11053 | temp.emplace_front(var); | ||
| 11054 | if (isLocal(varName) || !isNormal) { | ||
| 11055 | temp.emplace_front("_G"s); | ||
| 11056 | } | ||
| 11057 | std::string stmt; | ||
| 11058 | if (importNode->target) { | ||
| 11059 | stmt = "const "s + _parser.toString(importNode->target) + '=' + join(temp, "."sv); | ||
| 11060 | } else { | ||
| 11061 | stmt = "const "s + temp.back() + '=' + join(temp, "."sv); | ||
| 11062 | } | ||
| 11063 | auto localAttrib = toAst<LocalAttrib_t>(stmt, importNode); | ||
| 11064 | transformLocalAttrib(localAttrib, out); | ||
| 11065 | } | ||
| 11066 | |||
| 11043 | void transformImport(Import_t* import, str_list& out) { | 11067 | void transformImport(Import_t* import, str_list& out) { |
| 11044 | auto content = import->content.get(); | 11068 | auto content = import->content.get(); |
| 11045 | switch (content->get_id()) { | 11069 | switch (content->get_id()) { |
| @@ -11052,6 +11076,9 @@ private: | |||
| 11052 | case id<FromImport_t>(): | 11076 | case id<FromImport_t>(): |
| 11053 | transformFromImport(static_cast<FromImport_t*>(content), out); | 11077 | transformFromImport(static_cast<FromImport_t*>(content), out); |
| 11054 | break; | 11078 | break; |
| 11079 | case id<ImportGlobal_t>(): | ||
| 11080 | transformImportGlobal(static_cast<ImportGlobal_t*>(content), out); | ||
| 11081 | break; | ||
| 11055 | default: YUEE("AST node mismatch", content); break; | 11082 | default: YUEE("AST node mismatch", content); break; |
| 11056 | } | 11083 | } |
| 11057 | } | 11084 | } |
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() { | |||
| 349 | 349 | ||
| 350 | ImportAs = ImportLiteral >> -(space >> key("as") >> space >> (ImportTabLit | Variable | ImportAllMacro)); | 350 | ImportAs = ImportLiteral >> -(space >> key("as") >> space >> (ImportTabLit | Variable | ImportAllMacro)); |
| 351 | 351 | ||
| 352 | Import = key("import") >> space >> (ImportAs | ImportFrom) | FromImport; | 352 | ImportGlobal = Seperator >> UnicodeName >> *('.' >> UnicodeName) >> -(space >> key("as") >> space >> Variable); |
| 353 | |||
| 354 | Import = key("import") >> space >> (ImportAs | ImportFrom | ImportGlobal) | FromImport; | ||
| 353 | 355 | ||
| 354 | Label = "::" >> LabelName >> "::"; | 356 | Label = "::" >> LabelName >> "::"; |
| 355 | 357 | ||
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: | |||
| 320 | AST_RULE(ImportAllMacro); | 320 | AST_RULE(ImportAllMacro); |
| 321 | AST_RULE(ImportTabLit); | 321 | AST_RULE(ImportTabLit); |
| 322 | AST_RULE(ImportAs); | 322 | AST_RULE(ImportAs); |
| 323 | AST_RULE(ImportGlobal); | ||
| 323 | AST_RULE(Import); | 324 | AST_RULE(Import); |
| 324 | AST_RULE(Label); | 325 | AST_RULE(Label); |
| 325 | AST_RULE(Goto); | 326 | AST_RULE(Goto); |
