aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-02-17 11:22:07 +0800
committerLi Jin <dragon-fly@qq.com>2021-02-17 11:22:07 +0800
commit7066392d1c974065181d95d93274136dcd625d43 (patch)
treecf51eafc2c52cbc12246a306bca172d799193d30 /src
parent90cd12ad9ef465f3e435e1bd034dcfbe4e19d016 (diff)
downloadyuescript-7066392d1c974065181d95d93274136dcd625d43.tar.gz
yuescript-7066392d1c974065181d95d93274136dcd625d43.tar.bz2
yuescript-7066392d1c974065181d95d93274136dcd625d43.zip
stop reusing variables, rename project.
Diffstat (limited to 'src')
-rw-r--r--src/yue.cpp (renamed from src/moonp.cpp)102
-rw-r--r--src/yuescript/ast.cpp (renamed from src/MoonP/ast.cpp)2
-rw-r--r--src/yuescript/ast.hpp (renamed from src/MoonP/ast.hpp)2
-rw-r--r--src/yuescript/parser.cpp (renamed from src/MoonP/parser.cpp)2
-rw-r--r--src/yuescript/parser.hpp (renamed from src/MoonP/parser.hpp)0
-rw-r--r--src/yuescript/stacktraceplus.h (renamed from src/MoonP/stacktraceplus.h)32
-rw-r--r--src/yuescript/yue_ast.h (renamed from src/MoonP/moon_ast.h)2
-rw-r--r--src/yuescript/yue_compiler.cpp (renamed from src/MoonP/moon_compiler.cpp)288
-rw-r--r--src/yuescript/yue_compiler.h (renamed from src/MoonP/moon_compiler.h)18
-rw-r--r--src/yuescript/yue_parser.cpp (renamed from src/MoonP/moon_parser.cpp)22
-rw-r--r--src/yuescript/yue_parser.h (renamed from src/MoonP/moon_parser.h)12
-rw-r--r--src/yuescript/yuescript.cpp (renamed from src/MoonP/moonplus.cpp)50
-rw-r--r--src/yuescript/yuescript.h (renamed from src/MoonP/moonplus.h)90
13 files changed, 306 insertions, 316 deletions
diff --git a/src/moonp.cpp b/src/yue.cpp
index 27c6016..b5f91ff 100644
--- a/src/moonp.cpp
+++ b/src/yue.cpp
@@ -6,8 +6,8 @@ The above copyright notice and this permission notice shall be included in all c
6 6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 8
9#include "MoonP/moon_compiler.h" 9#include "yuescript/yue_compiler.h"
10#include "MoonP/moon_parser.h" 10#include "yuescript/yue_parser.h"
11 11
12#include <iostream> 12#include <iostream>
13#include <iomanip> 13#include <iomanip>
@@ -24,29 +24,29 @@ using namespace std::string_view_literals;
24#include "ghc/fs_std.hpp" 24#include "ghc/fs_std.hpp"
25#include "linenoise.hpp" 25#include "linenoise.hpp"
26 26
27#if not (defined MOONP_NO_MACRO && defined MOONP_COMPILER_ONLY) 27#if not (defined YUE_NO_MACRO && defined YUE_COMPILER_ONLY)
28#define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;}) 28#define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;})
29#define DEFER(code) _DEFER(code,__LINE__) 29#define DEFER(code) _DEFER(code,__LINE__)
30extern "C" { 30extern "C" {
31#include "lua.h" 31#include "lua.h"
32#include "lauxlib.h" 32#include "lauxlib.h"
33#include "lualib.h" 33#include "lualib.h"
34int luaopen_moonp(lua_State* L); 34int luaopen_yue(lua_State* L);
35} // extern "C" 35} // extern "C"
36 36
37static void openlibs(void* state) { 37static void openlibs(void* state) {
38 lua_State* L = static_cast<lua_State*>(state); 38 lua_State* L = static_cast<lua_State*>(state);
39 luaL_openlibs(L); 39 luaL_openlibs(L);
40 luaopen_moonp(L); 40 luaopen_yue(L);
41} 41}
42 42
43void pushMoonp(lua_State* L, std::string_view name) { 43void pushYue(lua_State* L, std::string_view name) {
44 lua_getglobal(L, "package"); // package 44 lua_getglobal(L, "package"); // package
45 lua_getfield(L, -1, "loaded"); // package loaded 45 lua_getfield(L, -1, "loaded"); // package loaded
46 lua_getfield(L, -1, "moonp"); // package loaded moonp 46 lua_getfield(L, -1, "yue"); // package loaded yue
47 lua_pushlstring(L, &name.front(), name.size()); // package loaded moonp name 47 lua_pushlstring(L, &name.front(), name.size()); // package loaded yue name
48 lua_gettable(L, -2); // loaded[name], package loaded moonp item 48 lua_gettable(L, -2); // loaded[name], package loaded yue item
49 lua_insert(L, -4); // item package loaded moonp 49 lua_insert(L, -4); // item package loaded yue
50 lua_pop(L, 3); // item 50 lua_pop(L, 3); // item
51} 51}
52 52
@@ -71,15 +71,15 @@ void pushOptions(lua_State* L, int lineOffset) {
71 lua_pushinteger(L, lineOffset); 71 lua_pushinteger(L, lineOffset);
72 lua_rawset(L, -3); 72 lua_rawset(L, -3);
73} 73}
74#endif // not (defined MOONP_NO_MACRO && defined MOONP_COMPILER_ONLY) 74#endif // not (defined YUE_NO_MACRO && defined YUE_COMPILER_ONLY)
75 75
76#ifndef MOONP_NO_MACRO 76#ifndef YUE_NO_MACRO
77#define MOONP_ARGS nullptr,openlibs 77#define YUE_ARGS nullptr,openlibs
78#else 78#else
79#define MOONP_ARGS 79#define YUE_ARGS
80#endif // MOONP_NO_MACRO 80#endif // YUE_NO_MACRO
81 81
82#ifndef MOONP_COMPILER_ONLY 82#ifndef YUE_COMPILER_ONLY
83static const char luaminifyCodes[] = 83static const char luaminifyCodes[] =
84#include "LuaMinify.h" 84#include "LuaMinify.h"
85 85
@@ -92,16 +92,16 @@ static void pushLuaminify(lua_State* L) {
92 luaL_error(L, err.c_str()); 92 luaL_error(L, err.c_str());
93 } 93 }
94} 94}
95#endif // MOONP_COMPILER_ONLY 95#endif // YUE_COMPILER_ONLY
96 96
97int main(int narg, const char** args) { 97int main(int narg, const char** args) {
98 const char* help = 98 const char* help =
99"Usage: moonp [options|files|directories] ...\n\n" 99"Usage: yue [options|files|directories] ...\n\n"
100" -h Print this message\n" 100" -h Print this message\n"
101#ifndef MOONP_COMPILER_ONLY 101#ifndef YUE_COMPILER_ONLY
102" -e str Execute a file or raw codes\n" 102" -e str Execute a file or raw codes\n"
103" -m Generate minified codes\n" 103" -m Generate minified codes\n"
104#endif // MOONP_COMPILER_ONLY 104#endif // YUE_COMPILER_ONLY
105" -t path Specify where to place compiled files\n" 105" -t path Specify where to place compiled files\n"
106" -o file Write output to file\n" 106" -o file Write output to file\n"
107" -s Use spaces in generated codes instead of tabs\n" 107" -s Use spaces in generated codes instead of tabs\n"
@@ -109,19 +109,19 @@ int main(int narg, const char** args) {
109" -b Dump compile time (doesn't write output)\n" 109" -b Dump compile time (doesn't write output)\n"
110" -l Write line numbers from source codes\n" 110" -l Write line numbers from source codes\n"
111" -v Print version\n" 111" -v Print version\n"
112#ifndef MOONP_COMPILER_ONLY 112#ifndef YUE_COMPILER_ONLY
113" -- Read from standard in, print to standard out\n" 113" -- Read from standard in, print to standard out\n"
114" (Must be first and only argument)\n\n" 114" (Must be first and only argument)\n\n"
115" Execute without options to enter REPL, type symbol '$'\n" 115" Execute without options to enter REPL, type symbol '$'\n"
116" in a single line to start/stop multi-line mode\n" 116" in a single line to start/stop multi-line mode\n"
117#endif // MOONP_COMPILER_ONLY 117#endif // YUE_COMPILER_ONLY
118; 118;
119#ifndef MOONP_COMPILER_ONLY 119#ifndef YUE_COMPILER_ONLY
120 if (narg == 1) { 120 if (narg == 1) {
121 lua_State* L = luaL_newstate(); 121 lua_State* L = luaL_newstate();
122 openlibs(L); 122 openlibs(L);
123 DEFER(lua_close(L)); 123 DEFER(lua_close(L));
124 pushMoonp(L, "insert_loader"sv); 124 pushYue(L, "insert_loader"sv);
125 if (lua_pcall(L, 0, 0, 0) != 0) { 125 if (lua_pcall(L, 0, 0, 0) != 0) {
126 std::cout << lua_tostring(L, -1) << '\n'; 126 std::cout << lua_tostring(L, -1) << '\n';
127 return 1; 127 return 1;
@@ -131,7 +131,7 @@ int main(int narg, const char** args) {
131 linenoise::SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) { 131 linenoise::SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) {
132 std::string buf = editBuffer; 132 std::string buf = editBuffer;
133 std::string tmp = buf; 133 std::string tmp = buf;
134 MoonP::Utils::trim(tmp); 134 yue::Utils::trim(tmp);
135 if (tmp.empty()) return; 135 if (tmp.empty()) return;
136 std::string pre; 136 std::string pre;
137 auto pos = buf.find_first_not_of(" \t\n"); 137 auto pos = buf.find_first_not_of(" \t\n");
@@ -176,33 +176,33 @@ int main(int narg, const char** args) {
176 break; 176 break;
177 } 177 }
178 }); 178 });
179 std::cout << "Moonscript+ "sv << MoonP::version << '\n'; 179 std::cout << "Yuescript "sv << yue::version << '\n';
180 while (true) { 180 while (true) {
181 count++; 181 count++;
182 std::string codes; 182 std::string codes;
183 bool quit = linenoise::Readline("> ", codes); 183 bool quit = linenoise::Readline("> ", codes);
184 if (quit) return 0; 184 if (quit) return 0;
185 linenoise::AddHistory(codes.c_str()); 185 linenoise::AddHistory(codes.c_str());
186 MoonP::Utils::trim(codes); 186 yue::Utils::trim(codes);
187 if (codes == "$"sv) { 187 if (codes == "$"sv) {
188 codes.clear(); 188 codes.clear();
189 for (std::string line; !(quit = linenoise::Readline("", line));) { 189 for (std::string line; !(quit = linenoise::Readline("", line));) {
190 auto temp = line; 190 auto temp = line;
191 MoonP::Utils::trim(temp); 191 yue::Utils::trim(temp);
192 if (temp == "$"sv) { 192 if (temp == "$"sv) {
193 break; 193 break;
194 } 194 }
195 codes += '\n'; 195 codes += '\n';
196 codes += line; 196 codes += line;
197 linenoise::AddHistory(line.c_str()); 197 linenoise::AddHistory(line.c_str());
198 MoonP::Utils::trim(codes); 198 yue::Utils::trim(codes);
199 } 199 }
200 if (quit) return 0; 200 if (quit) return 0;
201 } 201 }
202 codes.insert(0, "global *\n"sv); 202 codes.insert(0, "global *\n"sv);
203 int top = lua_gettop(L); 203 int top = lua_gettop(L);
204 DEFER(lua_settop(L, top)); 204 DEFER(lua_settop(L, top));
205 pushMoonp(L, "loadstring"sv); 205 pushYue(L, "loadstring"sv);
206 lua_pushlstring(L, codes.c_str(), codes.size()); 206 lua_pushlstring(L, codes.c_str(), codes.size());
207 lua_pushstring(L, (std::string("=(repl ") + std::to_string(count) + ')').c_str()); 207 lua_pushstring(L, (std::string("=(repl ") + std::to_string(count) + ')').c_str());
208 pushOptions(L, -1); 208 pushOptions(L, -1);
@@ -226,7 +226,7 @@ int main(int narg, const char** args) {
226 continue; 226 continue;
227 } 227 }
228 lua_pop(L, 1); 228 lua_pop(L, 1);
229 pushMoonp(L, "pcall"sv); 229 pushYue(L, "pcall"sv);
230 lua_insert(L, -2); 230 lua_insert(L, -2);
231 int last = lua_gettop(L) - 2; 231 int last = lua_gettop(L) - 2;
232 if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) { 232 if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) {
@@ -251,8 +251,8 @@ int main(int narg, const char** args) {
251 return 0; 251 return 0;
252 } 252 }
253 bool minify = false; 253 bool minify = false;
254#endif // MOONP_COMPILER_ONLY 254#endif // YUE_COMPILER_ONLY
255 MoonP::MoonConfig config; 255 yue::YueConfig config;
256 config.implicitReturnRoot = true; 256 config.implicitReturnRoot = true;
257 config.lintGlobalVariable = false; 257 config.lintGlobalVariable = false;
258 config.reserveLineNumber = false; 258 config.reserveLineNumber = false;
@@ -274,12 +274,12 @@ int main(int narg, const char** args) {
274 while ((ch = std::cin.get()) != EOF) { 274 while ((ch = std::cin.get()) != EOF) {
275 codes += ch; 275 codes += ch;
276 } 276 }
277 MoonP::MoonConfig conf; 277 yue::YueConfig conf;
278 conf.implicitReturnRoot = true; 278 conf.implicitReturnRoot = true;
279 conf.lintGlobalVariable = false; 279 conf.lintGlobalVariable = false;
280 conf.reserveLineNumber = false; 280 conf.reserveLineNumber = false;
281 conf.useSpaceOverTab = true; 281 conf.useSpaceOverTab = true;
282 auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(codes, conf); 282 auto result = yue::YueCompiler{YUE_ARGS}.compile(codes, conf);
283 if (result.error.empty()) { 283 if (result.error.empty()) {
284 std::cout << result.codes; 284 std::cout << result.codes;
285 return 0; 285 return 0;
@@ -288,14 +288,14 @@ int main(int narg, const char** args) {
288 std::cout << result.error << '\n'; 288 std::cout << result.error << '\n';
289 return 1; 289 return 1;
290 } 290 }
291#ifndef MOONP_COMPILER_ONLY 291#ifndef YUE_COMPILER_ONLY
292 } else if (arg == "-e"sv) { 292 } else if (arg == "-e"sv) {
293 ++i; 293 ++i;
294 if (i < narg) { 294 if (i < narg) {
295 lua_State* L = luaL_newstate(); 295 lua_State* L = luaL_newstate();
296 openlibs(L); 296 openlibs(L);
297 DEFER(lua_close(L)); 297 DEFER(lua_close(L));
298 pushMoonp(L, "insert_loader"sv); 298 pushYue(L, "insert_loader"sv);
299 if (lua_pcall(L, 0, 0, 0) != 0) { 299 if (lua_pcall(L, 0, 0, 0) != 0) {
300 std::cout << lua_tostring(L, -1) << '\n'; 300 std::cout << lua_tostring(L, -1) << '\n';
301 return 1; 301 return 1;
@@ -308,7 +308,7 @@ int main(int narg, const char** args) {
308 if (ext == ".lua") { 308 if (ext == ".lua") {
309 lua_getglobal(L, "load"); 309 lua_getglobal(L, "load");
310 } else { 310 } else {
311 pushMoonp(L, "loadstring"sv); 311 pushYue(L, "loadstring"sv);
312 } 312 }
313 std::string s( 313 std::string s(
314 (std::istreambuf_iterator<char>(input)), 314 (std::istreambuf_iterator<char>(input)),
@@ -316,7 +316,7 @@ int main(int narg, const char** args) {
316 lua_pushlstring(L, s.c_str(), s.size()); 316 lua_pushlstring(L, s.c_str(), s.size());
317 lua_pushlstring(L, evalStr.c_str(), evalStr.size()); 317 lua_pushlstring(L, evalStr.c_str(), evalStr.size());
318 } else { 318 } else {
319 pushMoonp(L, "loadstring"sv); 319 pushYue(L, "loadstring"sv);
320 lua_pushlstring(L, evalStr.c_str(), evalStr.size()); 320 lua_pushlstring(L, evalStr.c_str(), evalStr.size());
321 lua_pushliteral(L, "=(eval str)"); 321 lua_pushliteral(L, "=(eval str)");
322 } 322 }
@@ -329,7 +329,7 @@ int main(int narg, const char** args) {
329 return 1; 329 return 1;
330 } 330 }
331 lua_pop(L, 1); 331 lua_pop(L, 1);
332 pushMoonp(L, "pcall"sv); 332 pushYue(L, "pcall"sv);
333 lua_insert(L, -2); 333 lua_insert(L, -2);
334 int argCount = 0; 334 int argCount = 0;
335 i++; 335 i++;
@@ -354,7 +354,7 @@ int main(int narg, const char** args) {
354 } 354 }
355 } else if (arg == "-m"sv) { 355 } else if (arg == "-m"sv) {
356 minify = true; 356 minify = true;
357#endif // MOONP_COMPILER_ONLY 357#endif // YUE_COMPILER_ONLY
358 } else if (arg == "-s"sv) { 358 } else if (arg == "-s"sv) {
359 config.useSpaceOverTab = true; 359 config.useSpaceOverTab = true;
360 } else if (arg == "-l"sv) { 360 } else if (arg == "-l"sv) {
@@ -375,7 +375,7 @@ int main(int narg, const char** args) {
375 std::cout << help; 375 std::cout << help;
376 return 0; 376 return 0;
377 } else if (arg == "-v"sv) { 377 } else if (arg == "-v"sv) {
378 std::cout << "Moonscript+ version: "sv << MoonP::version << '\n'; 378 std::cout << "Yuescript version: "sv << yue::version << '\n';
379 return 0; 379 return 0;
380 } else if (arg == "-o"sv) { 380 } else if (arg == "-o"sv) {
381 ++i; 381 ++i;
@@ -397,7 +397,7 @@ int main(int narg, const char** args) {
397 if (!item.is_directory()) { 397 if (!item.is_directory()) {
398 auto ext = item.path().extension().string(); 398 auto ext = item.path().extension().string();
399 for (char& ch : ext) ch = std::tolower(ch); 399 for (char& ch : ext) ch = std::tolower(ch);
400 if (!ext.empty() && ext.substr(1) == MoonP::extension) { 400 if (!ext.empty() && ext.substr(1) == yue::extension) {
401 files.emplace_back(item.path().string(), item.path().lexically_relative(arg).string()); 401 files.emplace_back(item.path().string(), item.path().lexically_relative(arg).string());
402 } 402 }
403 } 403 }
@@ -425,12 +425,12 @@ int main(int narg, const char** args) {
425 std::istreambuf_iterator<char>()); 425 std::istreambuf_iterator<char>());
426 if (dumpCompileTime) { 426 if (dumpCompileTime) {
427 auto start = std::chrono::high_resolution_clock::now(); 427 auto start = std::chrono::high_resolution_clock::now();
428 auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(s, config); 428 auto result = yue::YueCompiler{YUE_ARGS}.compile(s, config);
429 auto end = std::chrono::high_resolution_clock::now(); 429 auto end = std::chrono::high_resolution_clock::now();
430 if (!result.codes.empty()) { 430 if (!result.codes.empty()) {
431 std::chrono::duration<double> diff = end - start; 431 std::chrono::duration<double> diff = end - start;
432 start = std::chrono::high_resolution_clock::now(); 432 start = std::chrono::high_resolution_clock::now();
433 MoonP::MoonParser{}.parse<MoonP::File_t>(s); 433 yue::YueParser{}.parse<yue::File_t>(s);
434 end = std::chrono::high_resolution_clock::now(); 434 end = std::chrono::high_resolution_clock::now();
435 std::chrono::duration<double> parseDiff = end - start; 435 std::chrono::duration<double> parseDiff = end - start;
436 std::ostringstream buf; 436 std::ostringstream buf;
@@ -445,7 +445,7 @@ int main(int narg, const char** args) {
445 return std::tuple{1, file.first, buf.str()}; 445 return std::tuple{1, file.first, buf.str()};
446 } 446 }
447 } 447 }
448 auto result = MoonP::MoonCompiler{MOONP_ARGS}.compile(s, config); 448 auto result = yue::YueCompiler{YUE_ARGS}.compile(s, config);
449 if (result.error.empty()) { 449 if (result.error.empty()) {
450 if (!writeToFile) { 450 if (!writeToFile) {
451 return std::tuple{0, file.first, result.codes + '\n'}; 451 return std::tuple{0, file.first, result.codes + '\n'};
@@ -475,7 +475,7 @@ int main(int narg, const char** args) {
475 if (output) { 475 if (output) {
476 const auto& codes = result.codes; 476 const auto& codes = result.codes;
477 if (config.reserveLineNumber) { 477 if (config.reserveLineNumber) {
478 auto head = std::string("-- [moonp]: "sv) + file.first + '\n'; 478 auto head = std::string("-- [yue]: "sv) + file.first + '\n';
479 output.write(head.c_str(), head.size()); 479 output.write(head.c_str(), head.size());
480 } 480 }
481 output.write(codes.c_str(), codes.size()); 481 output.write(codes.c_str(), codes.size());
@@ -497,7 +497,7 @@ int main(int narg, const char** args) {
497 results.push_back(std::move(task)); 497 results.push_back(std::move(task));
498 } 498 }
499 int ret = 0; 499 int ret = 0;
500#ifndef MOONP_COMPILER_ONLY 500#ifndef YUE_COMPILER_ONLY
501 lua_State* L = nullptr; 501 lua_State* L = nullptr;
502 DEFER({ 502 DEFER({
503 if (L) lua_close(L); 503 if (L) lua_close(L);
@@ -507,7 +507,7 @@ int main(int narg, const char** args) {
507 luaL_openlibs(L); 507 luaL_openlibs(L);
508 pushLuaminify(L); 508 pushLuaminify(L);
509 } 509 }
510#endif // MOONP_COMPILER_ONLY 510#endif // YUE_COMPILER_ONLY
511 std::list<std::string> errs; 511 std::list<std::string> errs;
512 for (auto& result : results) { 512 for (auto& result : results) {
513 int val = 0; 513 int val = 0;
@@ -518,7 +518,7 @@ int main(int narg, const char** args) {
518 ret = val; 518 ret = val;
519 errs.push_back(msg); 519 errs.push_back(msg);
520 } else { 520 } else {
521#ifndef MOONP_COMPILER_ONLY 521#ifndef YUE_COMPILER_ONLY
522 if (minify) { 522 if (minify) {
523 std::ifstream input(file, std::ios::in); 523 std::ifstream input(file, std::ios::in);
524 if (input) { 524 if (input) {
@@ -560,7 +560,7 @@ int main(int narg, const char** args) {
560 } 560 }
561#else 561#else
562 std::cout << msg; 562 std::cout << msg;
563#endif // MOONP_COMPILER_ONLY 563#endif // YUE_COMPILER_ONLY
564 } 564 }
565 } 565 }
566 for (const auto& err : errs) { 566 for (const auto& err : errs) {
diff --git a/src/MoonP/ast.cpp b/src/yuescript/ast.cpp
index 4929021..3deefb1 100644
--- a/src/MoonP/ast.cpp
+++ b/src/yuescript/ast.cpp
@@ -11,7 +11,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
11 11
12#include <cassert> 12#include <cassert>
13 13
14#include "MoonP/ast.hpp" 14#include "yuescript/ast.hpp"
15 15
16 16
17namespace parserlib { 17namespace parserlib {
diff --git a/src/MoonP/ast.hpp b/src/yuescript/ast.hpp
index 104202d..c88fcf9 100644
--- a/src/MoonP/ast.hpp
+++ b/src/yuescript/ast.hpp
@@ -17,7 +17,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17#include <stdexcept> 17#include <stdexcept>
18#include <type_traits> 18#include <type_traits>
19 19
20#include "MoonP/parser.hpp" 20#include "yuescript/parser.hpp"
21 21
22 22
23namespace parserlib { 23namespace parserlib {
diff --git a/src/MoonP/parser.cpp b/src/yuescript/parser.cpp
index 8dc2ff9..a44ae34 100644
--- a/src/MoonP/parser.cpp
+++ b/src/yuescript/parser.cpp
@@ -16,7 +16,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16#include <unordered_map> 16#include <unordered_map>
17#include <unordered_set> 17#include <unordered_set>
18 18
19#include "MoonP/parser.hpp" 19#include "yuescript/parser.hpp"
20 20
21 21
22namespace parserlib { 22namespace parserlib {
diff --git a/src/MoonP/parser.hpp b/src/yuescript/parser.hpp
index f70475f..f70475f 100644
--- a/src/MoonP/parser.hpp
+++ b/src/yuescript/parser.hpp
diff --git a/src/MoonP/stacktraceplus.h b/src/yuescript/stacktraceplus.h
index e884a2c..b165754 100644
--- a/src/MoonP/stacktraceplus.h
+++ b/src/yuescript/stacktraceplus.h
@@ -36,7 +36,7 @@ local string_gmatch = string.gmatch
36local string_sub = string.sub 36local string_sub = string.sub
37local table_concat = table.concat 37local table_concat = table.concat
38 38
39local moonp = require("moonp") 39local yue = require("yue")
40 40
41local _M = { 41local _M = {
42 max_tb_output_len = 70, -- controls the maximum length of the 'stringified' table before cutting with ' (more...)' 42 max_tb_output_len = 70, -- controls the maximum length of the 'stringified' table before cutting with ' (more...)'
@@ -161,8 +161,8 @@ local function GuessFunctionName(info)
161 if type(info.source) == "string" and info.source:sub(1,1) == "@" then 161 if type(info.source) == "string" and info.source:sub(1,1) == "@" then
162 local fname = info.source:sub(2) 162 local fname = info.source:sub(2)
163 local text 163 local text
164 if moonp.file_exist(fname) then 164 if yue.file_exist(fname) then
165 text = moonp.read_file(fname) 165 text = yue.read_file(fname)
166 end 166 end
167 if not text then 167 if not text then
168 -- print("file not found: "..tostring(err)) -- whoops! 168 -- print("file not found: "..tostring(err)) -- whoops!
@@ -317,27 +317,27 @@ function Dumper:DumpLocals (level)
317 end 317 end
318end 318end
319 319
320local function getMoonLineNumber(fname, line) 320local function getYueLineNumber(fname, line)
321 local moonCompiled = require("moonp").moon_compiled 321 local yueCompiled = require("yue").yue_compiled
322 local source = moonCompiled["@"..fname] 322 local source = yueCompiled["@"..fname]
323 if not source then 323 if not source then
324 source = moonCompiled["@="..fname] 324 source = yueCompiled["@="..fname]
325 end 325 end
326 if not source then 326 if not source then
327 local name_path = fname:gsub("%.", moonp.dirsep) 327 local name_path = fname:gsub("%.", yue.dirsep)
328 local file_exist, file_path 328 local file_exist, file_path
329 for path in package.path:gmatch("[^;]+") do 329 for path in package.path:gmatch("[^;]+") do
330 file_path = path:gsub("?", name_path) 330 file_path = path:gsub("?", name_path)
331 file_exist = moonp.file_exist(file_path) 331 file_exist = yue.file_exist(file_path)
332 if file_exist then 332 if file_exist then
333 break 333 break
334 end 334 end
335 end 335 end
336 if file_exist then 336 if file_exist then
337 local codes = moonp.read_file(file_path) 337 local codes = yue.read_file(file_path)
338 local moonFile = codes:match("^%s*--%s*%[moonp%]:%s*([^\n]*)") 338 local yueFile = codes:match("^%s*--%s*%[yue%]:%s*([^\n]*)")
339 if moonFile then 339 if yueFile then
340 fname = moonFile:gsub("^%s*(.-)%s*$", "%1") 340 fname = yueFile:gsub("^%s*(.-)%s*$", "%1")
341 source = codes 341 source = codes
342 end 342 end
343 end 343 end
@@ -409,12 +409,12 @@ function _M.stacktrace(thread, message, level)
409 local fn = fname:match("%[string \"(.-)\"%]") 409 local fn = fname:match("%[string \"(.-)\"%]")
410 if fn then fname = fn end 410 if fn then fname = fn end
411 fname = fname:gsub("^%s*(.-)%s*$", "%1") 411 fname = fname:gsub("^%s*(.-)%s*$", "%1")
412 fname, line = getMoonLineNumber(fname, line) 412 fname, line = getYueLineNumber(fname, line)
413 if _M.simplified then 413 if _M.simplified then
414 message = table.concat({ 414 message = table.concat({
415 "", fname, ":", 415 "", fname, ":",
416 line, ": ", msg}) 416 line, ": ", msg})
417 message = message:gsub("^%(moonplus%):%s*%d+:%s*", "") 417 message = message:gsub("^%(yuescript%):%s*%d+:%s*", "")
418 message = message:gsub("%s(%d+):", "%1:") 418 message = message:gsub("%s(%d+):", "%1:")
419 else 419 else
420 message = table.concat({ 420 message = table.concat({
@@ -441,7 +441,7 @@ Stack Traceback
441 elseif info.what == "main" or info.what == "Lua" then 441 elseif info.what == "main" or info.what == "Lua" then
442 info.source = info.source 442 info.source = info.source
443 end 443 end
444 info.source, info.currentline = getMoonLineNumber(info.source, info.currentline) 444 info.source, info.currentline = getYueLineNumber(info.source, info.currentline)
445 if info.what == "main" then 445 if info.what == "main" then
446 if _M.simplified then 446 if _M.simplified then
447 dumper:add_f("(%d) '%s':%d\r\n", level_to_show, info.source, info.currentline) 447 dumper:add_f("(%d) '%s':%d\r\n", level_to_show, info.source, info.currentline)
diff --git a/src/MoonP/moon_ast.h b/src/yuescript/yue_ast.h
index 6b738d4..090765c 100644
--- a/src/MoonP/moon_ast.h
+++ b/src/yuescript/yue_ast.h
@@ -8,7 +8,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
8 8
9#pragma once 9#pragma once
10 10
11#include "MoonP/ast.hpp" 11#include "yuescript/ast.hpp"
12 12
13namespace parserlib { 13namespace parserlib {
14 14
diff --git a/src/MoonP/moon_compiler.cpp b/src/yuescript/yue_compiler.cpp
index 24e4a97..22311f4 100644
--- a/src/MoonP/moon_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -12,12 +12,11 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
12#include <stack> 12#include <stack>
13#include <vector> 13#include <vector>
14#include <memory> 14#include <memory>
15#include <cassert>
16 15
17#include "MoonP/moon_parser.h" 16#include "yuescript/yue_parser.h"
18#include "MoonP/moon_compiler.h" 17#include "yuescript/yue_compiler.h"
19 18
20#ifndef MOONP_NO_MACRO 19#ifndef YUE_NO_MACRO
21 20
22extern "C" { 21extern "C" {
23#include "lua.h" 22#include "lua.h"
@@ -26,7 +25,7 @@ extern "C" {
26} // extern "C" 25} // extern "C"
27 26
28// name of table stored in lua registry 27// name of table stored in lua registry
29#define MOONP_MODULE "__moon_modules__" 28#define YUE_MODULE "__yue_modules__"
30 29
31#if LUA_VERSION_NUM > 501 30#if LUA_VERSION_NUM > 501
32 #ifndef LUA_COMPAT_5_1 31 #ifndef LUA_COMPAT_5_1
@@ -34,9 +33,9 @@ extern "C" {
34 #endif // LUA_COMPAT_5_1 33 #endif // LUA_COMPAT_5_1
35#endif // LUA_VERSION_NUM 34#endif // LUA_VERSION_NUM
36 35
37#endif // MOONP_NO_MACRO 36#endif // YUE_NO_MACRO
38 37
39namespace MoonP { 38namespace yue {
40using namespace std::string_view_literals; 39using namespace std::string_view_literals;
41using namespace parserlib; 40using namespace parserlib;
42 41
@@ -46,6 +45,12 @@ using namespace parserlib;
46 45
47#define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;}) 46#define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;})
48#define DEFER(code) _DEFER(code,__LINE__) 47#define DEFER(code) _DEFER(code,__LINE__)
48#define YUEE(msg,node) throw std::logic_error( \
49 _info.errorMessage( \
50 std::string("[File] ") + __FILE__ \
51 + ", [Func] " + __FUNCTION__ \
52 + ", [Line] " + std::to_string(__LINE__) \
53 + ", [Error] " + msg, node))
49 54
50typedef std::list<std::string> str_list; 55typedef std::list<std::string> str_list;
51 56
@@ -53,13 +58,13 @@ inline std::string s(std::string_view sv) {
53 return std::string(sv); 58 return std::string(sv);
54} 59}
55 60
56const std::string_view version = "0.6.3"sv; 61const std::string_view version = "0.6.5"sv;
57const std::string_view extension = "mp"sv; 62const std::string_view extension = "yue"sv;
58 63
59class MoonCompilerImpl { 64class YueCompilerImpl {
60public: 65public:
61#ifndef MOONP_NO_MACRO 66#ifndef YUE_NO_MACRO
62 MoonCompilerImpl(lua_State* sharedState, 67 YueCompilerImpl(lua_State* sharedState,
63 const std::function<void(void*)>& luaOpen, 68 const std::function<void(void*)>& luaOpen,
64 bool sameModule, 69 bool sameModule,
65 std::string_view moduleName = {}): 70 std::string_view moduleName = {}):
@@ -72,8 +77,8 @@ public:
72 _sameModule = true; 77 _sameModule = true;
73 int top = lua_gettop(L); 78 int top = lua_gettop(L);
74 DEFER(lua_settop(L, top)); 79 DEFER(lua_settop(L, top));
75 lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE 80 lua_pushliteral(L, YUE_MODULE); // YUE_MODULE
76 lua_rawget(L, LUA_REGISTRYINDEX); // reg[MOONP_MODULE], tb 81 lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE], tb
77 BREAK_IF(lua_istable(L, -1) == 0); 82 BREAK_IF(lua_istable(L, -1) == 0);
78 int idx = static_cast<int>(lua_objlen(L, -1)); // idx = #tb, tb 83 int idx = static_cast<int>(lua_objlen(L, -1)); // idx = #tb, tb
79 BREAK_IF(idx == 0); 84 BREAK_IF(idx == 0);
@@ -81,19 +86,19 @@ public:
81 BLOCK_END 86 BLOCK_END
82 } 87 }
83 88
84 ~MoonCompilerImpl() { 89 ~YueCompilerImpl() {
85 if (L && _stateOwner) { 90 if (L && _stateOwner) {
86 lua_close(L); 91 lua_close(L);
87 L = nullptr; 92 L = nullptr;
88 } 93 }
89 } 94 }
90#endif // MOONP_NO_MACRO 95#endif // YUE_NO_MACRO
91 96
92 CompileInfo compile(std::string_view codes, const MoonConfig& config) { 97 CompileInfo compile(std::string_view codes, const YueConfig& config) {
93 _config = config; 98 _config = config;
94#ifndef MOONP_NO_MACRO 99#ifndef YUE_NO_MACRO
95 if (L) passOptions(); 100 if (L) passOptions();
96#endif // MOONP_NO_MACRO 101#endif // YUE_NO_MACRO
97 _info = _parser.parse<File_t>(codes); 102 _info = _parser.parse<File_t>(codes);
98 std::unique_ptr<GlobalVars> globals; 103 std::unique_ptr<GlobalVars> globals;
99 std::unique_ptr<Options> options; 104 std::unique_ptr<Options> options;
@@ -119,14 +124,14 @@ public:
119 globals->push_back({var.first, line, col}); 124 globals->push_back({var.first, line, col});
120 } 125 }
121 } 126 }
122#ifndef MOONP_NO_MACRO 127#ifndef YUE_NO_MACRO
123 if (L) { 128 if (L) {
124 int top = lua_gettop(L); 129 int top = lua_gettop(L);
125 DEFER(lua_settop(L, top)); 130 DEFER(lua_settop(L, top));
126 if (!options) { 131 if (!options) {
127 options = std::make_unique<Options>(); 132 options = std::make_unique<Options>();
128 } 133 }
129 pushMoonp("options"sv); 134 pushYue("options"sv);
130 lua_pushnil(L); // options startKey 135 lua_pushnil(L); // options startKey
131 while (lua_next(L, -2) != 0) { // options key value 136 while (lua_next(L, -2) != 0) { // options key value
132 size_t len = 0; 137 size_t len = 0;
@@ -138,7 +143,7 @@ public:
138 lua_pop(L, 1); // options key 143 lua_pop(L, 1); // options key
139 } 144 }
140 } 145 }
141#endif // MOONP_NO_MACRO 146#endif // YUE_NO_MACRO
142 return {std::move(out.back()), Empty, std::move(globals), std::move(options)}; 147 return {std::move(out.back()), Empty, std::move(globals), std::move(options)};
143 } catch (const std::logic_error& error) { 148 } catch (const std::logic_error& error) {
144 return {Empty, error.what(), std::move(globals), std::move(options)}; 149 return {Empty, error.what(), std::move(globals), std::move(options)};
@@ -162,31 +167,31 @@ public:
162 _withVars = {}; 167 _withVars = {};
163 _continueVars = {}; 168 _continueVars = {};
164 _enableReturn = {}; 169 _enableReturn = {};
165#ifndef MOONP_NO_MACRO 170#ifndef YUE_NO_MACRO
166 if (_useModule) { 171 if (_useModule) {
167 _useModule = false; 172 _useModule = false;
168 if (!_sameModule) { 173 if (!_sameModule) {
169 int top = lua_gettop(L); 174 int top = lua_gettop(L);
170 DEFER(lua_settop(L, top)); 175 DEFER(lua_settop(L, top));
171 lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE 176 lua_pushliteral(L, YUE_MODULE); // YUE_MODULE
172 lua_rawget(L, LUA_REGISTRYINDEX); // reg[MOONP_MODULE], tb 177 lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE], tb
173 int idx = static_cast<int>(lua_objlen(L, -1)); 178 int idx = static_cast<int>(lua_objlen(L, -1));
174 lua_pushnil(L); // tb nil 179 lua_pushnil(L); // tb nil
175 lua_rawseti(L, -2, idx); // tb[idx] = nil, tb 180 lua_rawseti(L, -2, idx); // tb[idx] = nil, tb
176 } 181 }
177 } 182 }
178#endif // MOONP_NO_MACRO 183#endif // YUE_NO_MACRO
179 } 184 }
180private: 185private:
181#ifndef MOONP_NO_MACRO 186#ifndef YUE_NO_MACRO
182 bool _stateOwner = false; 187 bool _stateOwner = false;
183 bool _useModule = false; 188 bool _useModule = false;
184 bool _sameModule = false; 189 bool _sameModule = false;
185 lua_State* L = nullptr; 190 lua_State* L = nullptr;
186 std::function<void(void*)> _luaOpen; 191 std::function<void(void*)> _luaOpen;
187#endif // MOONP_NO_MACRO 192#endif // YUE_NO_MACRO
188 MoonConfig _config; 193 YueConfig _config;
189 MoonParser _parser; 194 YueParser _parser;
190 ParseInfo _info; 195 ParseInfo _info;
191 int _indentOffset = 0; 196 int _indentOffset = 0;
192 std::stack<bool> _varArgs; 197 std::stack<bool> _varArgs;
@@ -523,7 +528,7 @@ private:
523 case id<Statement_t>(): { 528 case id<Statement_t>(): {
524 return static_cast<Statement_t*>(body); 529 return static_cast<Statement_t*>(body);
525 } 530 }
526 default: assert(false); break; 531 default: YUEE("AST node mismatch", body); break;
527 } 532 }
528 return nullptr; 533 return nullptr;
529 } 534 }
@@ -875,7 +880,7 @@ private:
875 statement->appendix.set(nullptr); 880 statement->appendix.set(nullptr);
876 break; 881 break;
877 } 882 }
878 default: assert(false); break; 883 default: YUEE("AST node mismatch", appendix->item.get()); break;
879 } 884 }
880 } 885 }
881 auto content = statement->content.get(); 886 auto content = statement->content.get();
@@ -947,7 +952,7 @@ private:
947 } 952 }
948 break; 953 break;
949 } 954 }
950 default: assert(false); break; 955 default: YUEE("AST node mismatch", content); break;
951 } 956 }
952 if (statement->needSep && !out.back().empty()) { 957 if (statement->needSep && !out.back().empty()) {
953 auto index = std::string::npos; 958 auto index = std::string::npos;
@@ -1287,7 +1292,7 @@ private:
1287 case id<Switch_t>(): transformSwitch(static_cast<Switch_t*>(value), out, ExpUsage::Closure); break; 1292 case id<Switch_t>(): transformSwitch(static_cast<Switch_t*>(value), out, ExpUsage::Closure); break;
1288 case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(value), out); break; 1293 case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(value), out); break;
1289 case id<Exp_t>(): transformExp(static_cast<Exp_t*>(value), out, ExpUsage::Closure); break; 1294 case id<Exp_t>(): transformExp(static_cast<Exp_t*>(value), out, ExpUsage::Closure); break;
1290 default: assert(false); break; 1295 default: YUEE("AST node mismatch", value); break;
1291 } 1296 }
1292 } 1297 }
1293 1298
@@ -1411,7 +1416,7 @@ private:
1411 } 1416 }
1412 break; 1417 break;
1413 } 1418 }
1414 default: assert(false); break; 1419 default: YUEE("AST node mismatch", pair); break;
1415 } 1420 }
1416 } 1421 }
1417 return pairs; 1422 return pairs;
@@ -1574,7 +1579,7 @@ private:
1574 } 1579 }
1575 break; 1580 break;
1576 } 1581 }
1577 default: assert(false); break; 1582 default: YUEE("AST node mismatch", action); break;
1578 } 1583 }
1579 } 1584 }
1580 1585
@@ -1632,7 +1637,7 @@ private:
1632 ifCondPairs.back().second = node; 1637 ifCondPairs.back().second = node;
1633 ifCondPairs.emplace_back(); 1638 ifCondPairs.emplace_back();
1634 break; 1639 break;
1635 default: assert(false); break; 1640 default: YUEE("AST node mismatch", node); break;
1636 } 1641 }
1637 } 1642 }
1638 auto assign = ifCondPairs.front().first->assign.get(); 1643 auto assign = ifCondPairs.front().first->assign.get();
@@ -1853,7 +1858,7 @@ private:
1853 transformExp(arg, out, ExpUsage::Closure); 1858 transformExp(arg, out, ExpUsage::Closure);
1854 return; 1859 return;
1855 } 1860 }
1856 default: assert(false); return; 1861 default: YUEE("invalid expression usage", x); return;
1857 } 1862 }
1858 } 1863 }
1859 } 1864 }
@@ -1863,7 +1868,9 @@ private:
1863 transform_backcall_exp(exp->backcalls.objects(), out, usage, assignList); 1868 transform_backcall_exp(exp->backcalls.objects(), out, usage, assignList);
1864 return; 1869 return;
1865 } 1870 }
1866 assert(usage == ExpUsage::Closure); 1871 if (usage != ExpUsage::Closure) {
1872 YUEE("invalid expression usage", exp);
1873 }
1867 str_list temp; 1874 str_list temp;
1868 transform_backcall_exp(exp->backcalls.objects(), temp, ExpUsage::Closure); 1875 transform_backcall_exp(exp->backcalls.objects(), temp, ExpUsage::Closure);
1869 for (auto _opValue : exp->opValues.objects()) { 1876 for (auto _opValue : exp->opValues.objects()) {
@@ -1881,7 +1888,7 @@ private:
1881 case id<simple_table_t>(): transform_simple_table(static_cast<simple_table_t*>(item), out); break; 1888 case id<simple_table_t>(): transform_simple_table(static_cast<simple_table_t*>(item), out); break;
1882 case id<ChainValue_t>(): transformChainValue(static_cast<ChainValue_t*>(item), out, ExpUsage::Closure); break; 1889 case id<ChainValue_t>(): transformChainValue(static_cast<ChainValue_t*>(item), out, ExpUsage::Closure); break;
1883 case id<String_t>(): transformString(static_cast<String_t*>(item), out); break; 1890 case id<String_t>(): transformString(static_cast<String_t*>(item), out); break;
1884 default: assert(false); break; 1891 default: YUEE("AST node mismatch", value); break;
1885 } 1892 }
1886 } 1893 }
1887 1894
@@ -1916,7 +1923,7 @@ private:
1916 out.push_back(s("..."sv)); 1923 out.push_back(s("..."sv));
1917 break; 1924 break;
1918 case id<Parens_t>(): transformParens(static_cast<Parens_t*>(item), out); break; 1925 case id<Parens_t>(): transformParens(static_cast<Parens_t*>(item), out); break;
1919 default: assert(false); break; 1926 default: YUEE("AST node mismatch", item); break;
1920 } 1927 }
1921 } 1928 }
1922 1929
@@ -1945,7 +1952,7 @@ private:
1945 case id<Comprehension_t>(): transformComprehension(static_cast<Comprehension_t*>(value), out, ExpUsage::Closure); break; 1952 case id<Comprehension_t>(): transformComprehension(static_cast<Comprehension_t*>(value), out, ExpUsage::Closure); break;
1946 case id<FunLit_t>(): transformFunLit(static_cast<FunLit_t*>(value), out); break; 1953 case id<FunLit_t>(): transformFunLit(static_cast<FunLit_t*>(value), out); break;
1947 case id<Num_t>(): transformNum(static_cast<Num_t*>(value), out); break; 1954 case id<Num_t>(): transformNum(static_cast<Num_t*>(value), out); break;
1948 default: assert(false); break; 1955 default: YUEE("AST node mismatch", value); break;
1949 } 1956 }
1950 } 1957 }
1951 1958
@@ -2304,10 +2311,10 @@ private:
2304 } 2311 }
2305 } 2312 }
2306 2313
2307#ifndef MOONP_NO_MACRO 2314#ifndef YUE_NO_MACRO
2308 void passOptions() { 2315 void passOptions() {
2309 if (!_config.options.empty()) { 2316 if (!_config.options.empty()) {
2310 pushMoonp("options"sv); // options 2317 pushYue("options"sv); // options
2311 for (const auto& option : _config.options) { 2318 for (const auto& option : _config.options) {
2312 lua_pushlstring(L, option.second.c_str(), option.second.size()); 2319 lua_pushlstring(L, option.second.c_str(), option.second.size());
2313 lua_setfield(L, -2, option.first.c_str()); 2320 lua_setfield(L, -2, option.first.c_str());
@@ -2318,8 +2325,8 @@ private:
2318 2325
2319 void pushCurrentModule() { 2326 void pushCurrentModule() {
2320 if (_useModule) { 2327 if (_useModule) {
2321 lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE 2328 lua_pushliteral(L, YUE_MODULE); // YUE_MODULE
2322 lua_rawget(L, LUA_REGISTRYINDEX); // reg[MOONP_MODULE], tb 2329 lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE], tb
2323 int idx = static_cast<int>(lua_objlen(L, -1)); // idx = #tb, tb 2330 int idx = static_cast<int>(lua_objlen(L, -1)); // idx = #tb, tb
2324 lua_rawgeti(L, -1, idx); // tb[idx], tb cur 2331 lua_rawgeti(L, -1, idx); // tb[idx], tb cur
2325 lua_remove(L, -2); // cur 2332 lua_remove(L, -2); // cur
@@ -2334,14 +2341,14 @@ private:
2334 passOptions(); 2341 passOptions();
2335 _stateOwner = true; 2342 _stateOwner = true;
2336 } 2343 }
2337 lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE 2344 lua_pushliteral(L, YUE_MODULE); // YUE_MODULE
2338 lua_rawget(L, LUA_REGISTRYINDEX); // reg[MOONP_MODULE], tb 2345 lua_rawget(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE], tb
2339 if (lua_isnil(L, -1) != 0) { // tb == nil 2346 if (lua_isnil(L, -1) != 0) { // tb == nil
2340 lua_pop(L, 1); 2347 lua_pop(L, 1);
2341 lua_newtable(L); // tb 2348 lua_newtable(L); // tb
2342 lua_pushliteral(L, MOONP_MODULE); // tb MOONP_MODULE 2349 lua_pushliteral(L, YUE_MODULE); // tb YUE_MODULE
2343 lua_pushvalue(L, -2); // tb MOONP_MODULE tb 2350 lua_pushvalue(L, -2); // tb YUE_MODULE tb
2344 lua_rawset(L, LUA_REGISTRYINDEX); // reg[MOONP_MODULE] = tb, tb 2351 lua_rawset(L, LUA_REGISTRYINDEX); // reg[YUE_MODULE] = tb, tb
2345 } // tb 2352 } // tb
2346 int idx = static_cast<int>(lua_objlen(L, -1)); // idx = #tb, tb 2353 int idx = static_cast<int>(lua_objlen(L, -1)); // idx = #tb, tb
2347 lua_newtable(L); // tb cur 2354 lua_newtable(L); // tb cur
@@ -2350,20 +2357,20 @@ private:
2350 lua_remove(L, -2); // cur 2357 lua_remove(L, -2); // cur
2351 } 2358 }
2352 2359
2353 void pushMoonp(std::string_view name) { 2360 void pushYue(std::string_view name) {
2354 lua_getglobal(L, "package"); // package 2361 lua_getglobal(L, "package"); // package
2355 lua_getfield(L, -1, "loaded"); // package loaded 2362 lua_getfield(L, -1, "loaded"); // package loaded
2356 lua_getfield(L, -1, "moonp"); // package loaded moonp 2363 lua_getfield(L, -1, "yue"); // package loaded yue
2357 lua_pushlstring(L, &name.front(), name.size()); // package loaded moonp name 2364 lua_pushlstring(L, &name.front(), name.size()); // package loaded yue name
2358 lua_gettable(L, -2); // loaded[name], package loaded moonp item 2365 lua_gettable(L, -2); // loaded[name], package loaded yue item
2359 lua_insert(L, -4); // item package loaded moonp 2366 lua_insert(L, -4); // item package loaded yue
2360 lua_pop(L, 3); // item 2367 lua_pop(L, 3); // item
2361 } 2368 }
2362 2369
2363 bool isModuleLoaded(std::string_view name) { 2370 bool isModuleLoaded(std::string_view name) {
2364 int top = lua_gettop(L); 2371 int top = lua_gettop(L);
2365 DEFER(lua_settop(L, top)); 2372 DEFER(lua_settop(L, top));
2366 lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE 2373 lua_pushliteral(L, YUE_MODULE); // YUE_MODULE
2367 lua_rawget(L, LUA_REGISTRYINDEX); // modules 2374 lua_rawget(L, LUA_REGISTRYINDEX); // modules
2368 lua_pushlstring(L, &name.front(), name.size()); 2375 lua_pushlstring(L, &name.front(), name.size());
2369 lua_rawget(L, -2); // modules module 2376 lua_rawget(L, -2); // modules module
@@ -2374,7 +2381,7 @@ private:
2374 } 2381 }
2375 2382
2376 void pushModuleTable(std::string_view name) { 2383 void pushModuleTable(std::string_view name) {
2377 lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE 2384 lua_pushliteral(L, YUE_MODULE); // YUE_MODULE
2378 lua_rawget(L, LUA_REGISTRYINDEX); // modules 2385 lua_rawget(L, LUA_REGISTRYINDEX); // modules
2379 lua_pushlstring(L, &name.front(), name.size()); 2386 lua_pushlstring(L, &name.front(), name.size());
2380 lua_rawget(L, -2); // modules module 2387 lua_rawget(L, -2); // modules module
@@ -2442,7 +2449,7 @@ private:
2442 pushCurrentModule(); // cur 2449 pushCurrentModule(); // cur
2443 int top = lua_gettop(L) - 1; 2450 int top = lua_gettop(L) - 1;
2444 DEFER(lua_settop(L, top)); 2451 DEFER(lua_settop(L, top));
2445 pushMoonp("loadstring"sv); // cur loadstring 2452 pushYue("loadstring"sv); // cur loadstring
2446 lua_pushlstring(L, macroCodes.c_str(), macroCodes.size()); // cur loadstring codes 2453 lua_pushlstring(L, macroCodes.c_str(), macroCodes.size()); // cur loadstring codes
2447 lua_pushlstring(L, chunkName.c_str(), chunkName.size()); // cur loadstring codes chunk 2454 lua_pushlstring(L, chunkName.c_str(), chunkName.size()); // cur loadstring codes chunk
2448 pushOptions(macro->m_begin.m_line - 1); // cur loadstring codes chunk options 2455 pushOptions(macro->m_begin.m_line - 1); // cur loadstring codes chunk options
@@ -2455,7 +2462,7 @@ private:
2455 throw std::logic_error(_info.errorMessage(s("failed to load macro codes, at (macro "sv) + macroName + s("): "sv) + err, macro->macroLit)); 2462 throw std::logic_error(_info.errorMessage(s("failed to load macro codes, at (macro "sv) + macroName + s("): "sv) + err, macro->macroLit));
2456 } 2463 }
2457 lua_pop(L, 1); // cur f 2464 lua_pop(L, 1); // cur f
2458 pushMoonp("pcall"sv); // cur f pcall 2465 pushYue("pcall"sv); // cur f pcall
2459 lua_insert(L, -2); // cur pcall f 2466 lua_insert(L, -2); // cur pcall f
2460 if (lua_pcall(L, 1, 2, 0) != 0) { // f(), cur success macro 2467 if (lua_pcall(L, 1, 2, 0) != 0) { // f(), cur success macro
2461 std::string err = lua_tostring(L, -1); 2468 std::string err = lua_tostring(L, -1);
@@ -2482,7 +2489,7 @@ private:
2482 void transformMacro(Macro_t* macro, str_list&, bool) { 2489 void transformMacro(Macro_t* macro, str_list&, bool) {
2483 throw std::logic_error(_info.errorMessage("macro feature not supported"sv, macro)); 2490 throw std::logic_error(_info.errorMessage("macro feature not supported"sv, macro));
2484 } 2491 }
2485#endif // MOONP_NO_MACRO 2492#endif // YUE_NO_MACRO
2486 2493
2487 void transformReturn(Return_t* returnNode, str_list& out) { 2494 void transformReturn(Return_t* returnNode, str_list& out) {
2488 if (!_enableReturn.top()) { 2495 if (!_enableReturn.top()) {
@@ -2613,11 +2620,11 @@ private:
2613 case id<self_t>(): 2620 case id<self_t>():
2614 arg.name = "self"sv; 2621 arg.name = "self"sv;
2615 break; 2622 break;
2616 default: assert(false); break; 2623 default: YUEE("AST node mismatch", selfName->name.get()); break;
2617 } 2624 }
2618 break; 2625 break;
2619 } 2626 }
2620 default: assert(false); break; 2627 default: YUEE("AST node mismatch", def->name.get()); break;
2621 } 2628 }
2622 forceAddToScope(arg.name); 2629 forceAddToScope(arg.name);
2623 if (def->defaultValue) { 2630 if (def->defaultValue) {
@@ -2710,7 +2717,7 @@ private:
2710 case id<self_t>(): 2717 case id<self_t>():
2711 out.push_back(s("self"sv)); 2718 out.push_back(s("self"sv));
2712 break; 2719 break;
2713 default: assert(false); break; 2720 default: YUEE("AST node mismatch", name); break;
2714 } 2721 }
2715 } 2722 }
2716 2723
@@ -3170,24 +3177,24 @@ private:
3170 temp.back() = s(temp.back().front() == '[' ? "[ "sv : "["sv) + temp.back() + s("]"sv); 3177 temp.back() = s(temp.back().front() == '[' ? "[ "sv : "["sv) + temp.back() + s("]"sv);
3171 break; 3178 break;
3172 case id<InvokeArgs_t>(): transformInvokeArgs(static_cast<InvokeArgs_t*>(item), temp); break; 3179 case id<InvokeArgs_t>(): transformInvokeArgs(static_cast<InvokeArgs_t*>(item), temp); break;
3173 default: assert(false); break; 3180 default: YUEE("AST node mismatch", item); break;
3174 } 3181 }
3175 } 3182 }
3176 switch (usage) { 3183 switch (usage) {
3177 case ExpUsage::Common: 3184 case ExpUsage::Common:
3178 out.push_back(indent() + join(temp) + nll(chainList.front())); 3185 out.push_back(indent() + join(temp) + nll(x));
3179 break; 3186 break;
3180 case ExpUsage::Return: 3187 case ExpUsage::Return:
3181 out.push_back(indent() + s("return "sv) + join(temp) + nll(chainList.front())); 3188 out.push_back(indent() + s("return "sv) + join(temp) + nll(x));
3182 break; 3189 break;
3183 case ExpUsage::Assignment: assert(false); break; 3190 case ExpUsage::Assignment: YUEE("invalid expression usage", x); break;
3184 default: 3191 default:
3185 out.push_back(join(temp)); 3192 out.push_back(join(temp));
3186 break; 3193 break;
3187 } 3194 }
3188 } 3195 }
3189 3196
3190#ifndef MOONP_NO_MACRO 3197#ifndef YUE_NO_MACRO
3191 std::tuple<std::string,std::string,str_list> expandMacroStr(ChainValue_t* chainValue) { 3198 std::tuple<std::string,std::string,str_list> expandMacroStr(ChainValue_t* chainValue) {
3192 const auto& chainList = chainValue->items.objects(); 3199 const auto& chainList = chainValue->items.objects();
3193 auto x = ast_to<Callable_t>(chainList.front())->item.to<MacroName_t>(); 3200 auto x = ast_to<Callable_t>(chainList.front())->item.to<MacroName_t>();
@@ -3212,7 +3219,7 @@ private:
3212 } 3219 }
3213 auto fcodes = _parser.toString(args->back()); 3220 auto fcodes = _parser.toString(args->back());
3214 Utils::trim(fcodes); 3221 Utils::trim(fcodes);
3215 pushMoonp("loadstring"sv); // loadstring 3222 pushYue("loadstring"sv); // loadstring
3216 lua_pushlstring(L, fcodes.c_str(), fcodes.size()); // loadstring codes 3223 lua_pushlstring(L, fcodes.c_str(), fcodes.size()); // loadstring codes
3217 lua_pushliteral(L, "=(macro in-place)"); // loadstring codes chunk 3224 lua_pushliteral(L, "=(macro in-place)"); // loadstring codes chunk
3218 pushOptions(args->back()->m_begin.m_line - 1); // loadstring codes chunk options 3225 pushOptions(args->back()->m_begin.m_line - 1); // loadstring codes chunk options
@@ -3225,7 +3232,7 @@ private:
3225 throw std::logic_error(_info.errorMessage(s("failed to load macro codes, at (macro in-place): "sv) + err, x)); 3232 throw std::logic_error(_info.errorMessage(s("failed to load macro codes, at (macro in-place): "sv) + err, x));
3226 } 3233 }
3227 lua_pop(L, 1); // f 3234 lua_pop(L, 1); // f
3228 pushMoonp("pcall"sv); // f pcall 3235 pushYue("pcall"sv); // f pcall
3229 lua_insert(L, -2); // pcall f 3236 lua_insert(L, -2); // pcall f
3230 if (lua_pcall(L, 1, 2, 0) != 0) { // f(), success macroFunc 3237 if (lua_pcall(L, 1, 2, 0) != 0) { // f(), success macroFunc
3231 std::string err = lua_tostring(L, -1); 3238 std::string err = lua_tostring(L, -1);
@@ -3236,7 +3243,7 @@ private:
3236 throw std::logic_error(_info.errorMessage(s("failed to generate macro function\n"sv) + err, x)); 3243 throw std::logic_error(_info.errorMessage(s("failed to generate macro function\n"sv) + err, x));
3237 } // true macroFunc 3244 } // true macroFunc
3238 lua_remove(L, -2); // macroFunc 3245 lua_remove(L, -2); // macroFunc
3239 pushMoonp("pcall"sv); // macroFunc pcall 3246 pushYue("pcall"sv); // macroFunc pcall
3240 lua_insert(L, -2); // pcall macroFunc 3247 lua_insert(L, -2); // pcall macroFunc
3241 bool success = lua_pcall(L, 1, 2, 0) == 0; 3248 bool success = lua_pcall(L, 1, 2, 0) == 0;
3242 if (!success) { // err 3249 if (!success) { // err
@@ -3254,7 +3261,7 @@ private:
3254 if (lua_isfunction(L, -1) == 0) { 3261 if (lua_isfunction(L, -1) == 0) {
3255 throw std::logic_error(_info.errorMessage("can not resolve macro"sv, x)); 3262 throw std::logic_error(_info.errorMessage("can not resolve macro"sv, x));
3256 } // cur macroFunc 3263 } // cur macroFunc
3257 pushMoonp("pcall"sv); // cur macroFunc pcall 3264 pushYue("pcall"sv); // cur macroFunc pcall
3258 lua_insert(L, -2); // cur pcall macroFunc 3265 lua_insert(L, -2); // cur pcall macroFunc
3259 auto item = *(++chainList.begin()); 3266 auto item = *(++chainList.begin());
3260 const node_container* args = nullptr; 3267 const node_container* args = nullptr;
@@ -3457,11 +3464,11 @@ private:
3457 } 3464 }
3458 } 3465 }
3459 } 3466 }
3460#endif // MOONP_NO_MACRO 3467#endif // YUE_NO_MACRO
3461 3468
3462 void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr, bool allowBlockMacroReturn = false) { 3469 void transformChainValue(ChainValue_t* chainValue, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr, bool allowBlockMacroReturn = false) {
3463 if (isMacroChain(chainValue)) { 3470 if (isMacroChain(chainValue)) {
3464#ifndef MOONP_NO_MACRO 3471#ifndef YUE_NO_MACRO
3465 ast_ptr<false,ast_node> node; 3472 ast_ptr<false,ast_node> node;
3466 std::unique_ptr<input> codes; 3473 std::unique_ptr<input> codes;
3467 std::string luaCodes; 3474 std::string luaCodes;
@@ -3515,7 +3522,7 @@ private:
3515#else 3522#else
3516 (void)allowBlockMacroReturn; 3523 (void)allowBlockMacroReturn;
3517 throw std::logic_error(_info.errorMessage("macro feature not supported"sv, chainValue)); 3524 throw std::logic_error(_info.errorMessage("macro feature not supported"sv, chainValue));
3518#endif // MOONP_NO_MACRO 3525#endif // YUE_NO_MACRO
3519 } 3526 }
3520 const auto& chainList = chainValue->items.objects(); 3527 const auto& chainList = chainValue->items.objects();
3521 if (transformChainEndWithEOP(chainList, out, usage, assignList)) { 3528 if (transformChainEndWithEOP(chainList, out, usage, assignList)) {
@@ -3561,7 +3568,7 @@ private:
3561 case id<DoubleString_t>(): transformDoubleString(static_cast<DoubleString_t*>(arg), temp); break; 3568 case id<DoubleString_t>(): transformDoubleString(static_cast<DoubleString_t*>(arg), temp); break;
3562 case id<LuaString_t>(): transformLuaString(static_cast<LuaString_t*>(arg), temp); break; 3569 case id<LuaString_t>(): transformLuaString(static_cast<LuaString_t*>(arg), temp); break;
3563 case id<TableLit_t>(): transformTableLit(static_cast<TableLit_t*>(arg), temp); break; 3570 case id<TableLit_t>(): transformTableLit(static_cast<TableLit_t*>(arg), temp); break;
3564 default: assert(false); break; 3571 default: YUEE("AST node mismatch", arg); break;
3565 } 3572 }
3566 } 3573 }
3567 out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv)); 3574 out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv));
@@ -3624,7 +3631,7 @@ private:
3624 temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item); 3631 temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item);
3625 pushScope(); 3632 pushScope();
3626 break; 3633 break;
3627 default: assert(false); break; 3634 default: YUEE("AST node mismatch", item); break;
3628 } 3635 }
3629 } 3636 }
3630 if (auto stmt = comp->value.as<Statement_t>()) { 3637 if (auto stmt = comp->value.as<Statement_t>()) {
@@ -3680,7 +3687,7 @@ private:
3680 temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item); 3687 temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item);
3681 pushScope(); 3688 pushScope();
3682 break; 3689 break;
3683 default: assert(false); break; 3690 default: YUEE("AST node mismatch", item); break;
3684 } 3691 }
3685 } 3692 }
3686 { 3693 {
@@ -3760,13 +3767,13 @@ private:
3760 varAfter.push_back(desVar); 3767 varAfter.push_back(desVar);
3761 break; 3768 break;
3762 } 3769 }
3763 default: assert(false); break; 3770 default: YUEE("AST node mismatch", item); break;
3764 } 3771 }
3765 } 3772 }
3766 switch (loopTarget->getId()) { 3773 switch (loopTarget->getId()) {
3767 case id<star_exp_t>(): { 3774 case id<star_exp_t>(): {
3768 auto star_exp = static_cast<star_exp_t*>(loopTarget); 3775 auto star_exp = static_cast<star_exp_t*>(loopTarget);
3769 auto listVar = singleVariableFrom(star_exp->value); 3776 std::string listVar;
3770 auto indexVar = getUnusedName("_index_"sv); 3777 auto indexVar = getUnusedName("_index_"sv);
3771 varAfter.push_back(indexVar); 3778 varAfter.push_back(indexVar);
3772 auto value = singleValueFrom(star_exp->value); 3779 auto value = singleValueFrom(star_exp->value);
@@ -3779,12 +3786,6 @@ private:
3779 auto slice = ast_cast<Slice_t>(chainList.back()); 3786 auto slice = ast_cast<Slice_t>(chainList.back());
3780 BREAK_IF(!slice); 3787 BREAK_IF(!slice);
3781 endWithSlice = true; 3788 endWithSlice = true;
3782 if (listVar.empty() && chainList.size() == 2 &&
3783 ast_is<Callable_t>(chainList.front())) {
3784 transformCallable(static_cast<Callable_t*>(chainList.front()), temp);
3785 listVar = temp.back();
3786 temp.pop_back();
3787 }
3788 chainList.pop_back(); 3789 chainList.pop_back();
3789 auto chain = x->new_ptr<ChainValue_t>(); 3790 auto chain = x->new_ptr<ChainValue_t>();
3790 for (auto item : chainList) { 3791 for (auto item : chainList) {
@@ -3808,12 +3809,10 @@ private:
3808 stepValue = temp.back(); 3809 stepValue = temp.back();
3809 temp.pop_back(); 3810 temp.pop_back();
3810 } 3811 }
3811 if (listVar.empty()) { 3812 listVar = getUnusedName("_list_"sv);
3812 listVar = getUnusedName("_list_"sv); 3813 varBefore.push_back(listVar);
3813 varBefore.push_back(listVar); 3814 transformChainValue(chain, temp, ExpUsage::Closure);
3814 transformChainValue(chain, temp, ExpUsage::Closure); 3815 _buf << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList);
3815 _buf << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList);
3816 }
3817 std::string maxVar; 3816 std::string maxVar;
3818 if (!stopValue.empty()) { 3817 if (!stopValue.empty()) {
3819 maxVar = getUnusedName("_max_"sv); 3818 maxVar = getUnusedName("_max_"sv);
@@ -3859,7 +3858,7 @@ private:
3859 _buf << indent() << "for "sv << join(vars, ", "sv) << " in "sv << temp.back() << " do"sv << nlr(loopTarget); 3858 _buf << indent() << "for "sv << join(vars, ", "sv) << " in "sv << temp.back() << " do"sv << nlr(loopTarget);
3860 out.push_back(clearBuf()); 3859 out.push_back(clearBuf());
3861 break; 3860 break;
3862 default: assert(false); break; 3861 default: YUEE("AST node mismatch", loopTarget); break;
3863 } 3862 }
3864 for (auto& var : varBefore) addToScope(var); 3863 for (auto& var : varBefore) addToScope(var);
3865 pushScope(); 3864 pushScope();
@@ -3926,7 +3925,7 @@ private:
3926 switch (arg->getId()) { 3925 switch (arg->getId()) {
3927 case id<Exp_t>(): transformExp(static_cast<Exp_t*>(arg), temp, ExpUsage::Closure); break; 3926 case id<Exp_t>(): transformExp(static_cast<Exp_t*>(arg), temp, ExpUsage::Closure); break;
3928 case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(arg), temp); break; 3927 case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(arg), temp); break;
3929 default: assert(false); break; 3928 default: YUEE("AST node mismatch", arg); break;
3930 } 3929 }
3931 } 3930 }
3932 out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv)); 3931 out.push_back(s("("sv) + join(temp, ", "sv) + s(")"sv));
@@ -3963,7 +3962,7 @@ private:
3963 transformBlock(newBlock, out, usage, assignList); 3962 transformBlock(newBlock, out, usage, assignList);
3964 break; 3963 break;
3965 } 3964 }
3966 default: assert(false); break; 3965 default: YUEE("AST node mismatch", body); break;
3967 } 3966 }
3968 } 3967 }
3969 3968
@@ -4172,13 +4171,13 @@ private:
4172 case id<LuaString_t>(): transformLuaString(static_cast<LuaString_t*>(key), temp); 4171 case id<LuaString_t>(): transformLuaString(static_cast<LuaString_t*>(key), temp);
4173 temp.back() = s("[ "sv) + temp.back() + s("]"sv); 4172 temp.back() = s("[ "sv) + temp.back() + s("]"sv);
4174 break; 4173 break;
4175 default: assert(false); break; 4174 default: YUEE("AST node mismatch", key); break;
4176 } 4175 }
4177 auto value = pair->value.get(); 4176 auto value = pair->value.get();
4178 switch (value->getId()) { 4177 switch (value->getId()) {
4179 case id<Exp_t>(): transformExp(static_cast<Exp_t*>(value), temp, ExpUsage::Closure); break; 4178 case id<Exp_t>(): transformExp(static_cast<Exp_t*>(value), temp, ExpUsage::Closure); break;
4180 case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(value), temp); break; 4179 case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(value), temp); break;
4181 default: assert(false); break; 4180 default: YUEE("AST node mismatch", value); break;
4182 } 4181 }
4183 out.push_back(temp.front() + s(" = "sv) + temp.back()); 4182 out.push_back(temp.front() + s(" = "sv) + temp.back());
4184 } 4183 }
@@ -4188,7 +4187,7 @@ private:
4188 switch (name->getId()) { 4187 switch (name->getId()) {
4189 case id<SelfName_t>(): transformSelfName(static_cast<SelfName_t*>(name), out); break; 4188 case id<SelfName_t>(): transformSelfName(static_cast<SelfName_t*>(name), out); break;
4190 case id<Name_t>(): out.push_back(_parser.toString(name)); break; 4189 case id<Name_t>(): out.push_back(_parser.toString(name)); break;
4191 default: assert(false); break; 4190 default: YUEE("AST node mismatch", name); break;
4192 } 4191 }
4193 } 4192 }
4194 4193
@@ -4232,7 +4231,7 @@ private:
4232 } 4231 }
4233 break; 4232 break;
4234 } 4233 }
4235 default: assert(false); break; 4234 default: YUEE("AST node mismatch", content); break;
4236 } 4235 }
4237 } 4236 }
4238 out.push_back(temp.empty() ? s("\"\""sv) : join(temp, " .. "sv)); 4237 out.push_back(temp.empty() ? s("\"\""sv) : join(temp, " .. "sv));
@@ -4244,7 +4243,7 @@ private:
4244 case id<SingleString_t>(): transformSingleString(static_cast<SingleString_t*>(str), out); break; 4243 case id<SingleString_t>(): transformSingleString(static_cast<SingleString_t*>(str), out); break;
4245 case id<DoubleString_t>(): transformDoubleString(static_cast<DoubleString_t*>(str), out); break; 4244 case id<DoubleString_t>(): transformDoubleString(static_cast<DoubleString_t*>(str), out); break;
4246 case id<LuaString_t>(): transformLuaString(static_cast<LuaString_t*>(str), out); break; 4245 case id<LuaString_t>(): transformLuaString(static_cast<LuaString_t*>(str), out); break;
4247 default: assert(false); break; 4246 default: YUEE("AST node mismatch", str); break;
4248 } 4247 }
4249 } 4248 }
4250 4249
@@ -4396,7 +4395,7 @@ private:
4396 case id<Statement_t>(): 4395 case id<Statement_t>():
4397 transformStatement(static_cast<Statement_t*>(content), statements); 4396 transformStatement(static_cast<Statement_t*>(content), statements);
4398 break; 4397 break;
4399 default: assert(false); break; 4398 default:YUEE("AST node mismatch", content); break;
4400 } 4399 }
4401 } 4400 }
4402 for (auto& member : members) { 4401 for (auto& member : members) {
@@ -4602,7 +4601,7 @@ private:
4602 case id<normal_pair_t>(): 4601 case id<normal_pair_t>():
4603 transform_normal_pair(static_cast<normal_pair_t*>(keyValue), temp); 4602 transform_normal_pair(static_cast<normal_pair_t*>(keyValue), temp);
4604 break; 4603 break;
4605 default: assert(false); break; 4604 default: YUEE("AST node mismatch", keyValue); break;
4606 } 4605 }
4607 if (type == MemType::Property) { 4606 if (type == MemType::Property) {
4608 incIndentOffset(); 4607 incIndentOffset();
@@ -4621,7 +4620,7 @@ private:
4621 case id<AssignableChain_t>(): transformAssignableChain(static_cast<AssignableChain_t*>(item), out); break; 4620 case id<AssignableChain_t>(): transformAssignableChain(static_cast<AssignableChain_t*>(item), out); break;
4622 case id<Variable_t>(): transformVariable(static_cast<Variable_t*>(item), out); break; 4621 case id<Variable_t>(): transformVariable(static_cast<Variable_t*>(item), out); break;
4623 case id<SelfName_t>(): transformSelfName(static_cast<SelfName_t*>(item), out); break; 4622 case id<SelfName_t>(): transformSelfName(static_cast<SelfName_t*>(item), out); break;
4624 default: assert(false); break; 4623 default: YUEE("AST node mismatch", item); break;
4625 } 4624 }
4626 } 4625 }
4627 4626
@@ -4646,14 +4645,8 @@ private:
4646 checkAssignable(with->valueList); 4645 checkAssignable(with->valueList);
4647 auto vars = getAssignVars(with); 4646 auto vars = getAssignVars(with);
4648 if (vars.front().empty()) { 4647 if (vars.front().empty()) {
4649 if (with->assigns->values.objects().size() == 1) { 4648 withVar = getUnusedName("_with_"sv);
4650 auto var = singleVariableFrom(with->assigns->values.objects().front()); 4649 {
4651 if (!var.empty()) {
4652 withVar = var;
4653 }
4654 }
4655 if (withVar.empty()) {
4656 withVar = getUnusedName("_with_"sv);
4657 auto assignment = x->new_ptr<ExpListAssign_t>(); 4650 auto assignment = x->new_ptr<ExpListAssign_t>();
4658 assignment->expList.set(toAst<ExpList_t>(withVar, x)); 4651 assignment->expList.set(toAst<ExpList_t>(withVar, x));
4659 auto assign = x->new_ptr<Assign_t>(); 4652 auto assign = x->new_ptr<Assign_t>();
@@ -4693,21 +4686,18 @@ private:
4693 transformAssignment(assignment, temp); 4686 transformAssignment(assignment, temp);
4694 } 4687 }
4695 } else { 4688 } else {
4696 withVar = singleVariableFrom(with->valueList); 4689 withVar = getUnusedName("_with_"sv);
4697 if (withVar.empty()) { 4690 auto assignment = x->new_ptr<ExpListAssign_t>();
4698 withVar = getUnusedName("_with_"sv); 4691 assignment->expList.set(toAst<ExpList_t>(withVar, x));
4699 auto assignment = x->new_ptr<ExpListAssign_t>(); 4692 auto assign = x->new_ptr<Assign_t>();
4700 assignment->expList.set(toAst<ExpList_t>(withVar, x)); 4693 assign->values.dup(with->valueList->exprs);
4701 auto assign = x->new_ptr<Assign_t>(); 4694 assignment->action.set(assign);
4702 assign->values.dup(with->valueList->exprs); 4695 if (!returnValue) {
4703 assignment->action.set(assign); 4696 scoped = true;
4704 if (!returnValue) { 4697 temp.push_back(indent() + s("do"sv) + nll(with));
4705 scoped = true; 4698 pushScope();
4706 temp.push_back(indent() + s("do"sv) + nll(with));
4707 pushScope();
4708 }
4709 transformAssignment(assignment, temp);
4710 } 4699 }
4700 transformAssignment(assignment, temp);
4711 } 4701 }
4712 if (!with->eop && !scoped && !returnValue) { 4702 if (!with->eop && !scoped && !returnValue) {
4713 pushScope(); 4703 pushScope();
@@ -4843,7 +4833,7 @@ private:
4843 } 4833 }
4844 break; 4834 break;
4845 } 4835 }
4846 default: assert(false); break; 4836 default: YUEE("AST node mismatch", item); break;
4847 } 4837 }
4848 } 4838 }
4849 4839
@@ -4942,7 +4932,7 @@ private:
4942 case id<normal_pair_t>(): transform_normal_pair(static_cast<normal_pair_t*>(pair), temp); break; 4932 case id<normal_pair_t>(): transform_normal_pair(static_cast<normal_pair_t*>(pair), temp); break;
4943 case id<TableBlockIndent_t>(): transformTableBlockIndent(static_cast<TableBlockIndent_t*>(pair), temp); break; 4933 case id<TableBlockIndent_t>(): transformTableBlockIndent(static_cast<TableBlockIndent_t*>(pair), temp); break;
4944 case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(pair), temp); break; 4934 case id<TableBlock_t>(): transformTableBlock(static_cast<TableBlock_t*>(pair), temp); break;
4945 default: assert(false); break; 4935 default: YUEE("AST node mismatch", pair); break;
4946 } 4936 }
4947 temp.back() = indent() + temp.back() + (pair == pairs.back() ? Empty : s(","sv)) + nll(pair); 4937 temp.back() = indent() + temp.back() + (pair == pairs.back() ? Empty : s(","sv)) + nll(pair);
4948 } 4938 }
@@ -4986,7 +4976,7 @@ private:
4986 temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item); 4976 temp.back() = indent() + s("if "sv) + temp.back() + s(" then"sv) + nll(item);
4987 pushScope(); 4977 pushScope();
4988 break; 4978 break;
4989 default: assert(false); break; 4979 default: YUEE("AST node mismatch", item); break;
4990 } 4980 }
4991 } 4981 }
4992 transformExp(comp->key, kv, ExpUsage::Closure); 4982 transformExp(comp->key, kv, ExpUsage::Closure);
@@ -5158,7 +5148,7 @@ private:
5158 expList->exprs.push_back(exp); 5148 expList->exprs.push_back(exp);
5159 break; 5149 break;
5160 } 5150 }
5161 default: assert(false); break; 5151 default: YUEE("AST node mismatch", name); break;
5162 } 5152 }
5163 } 5153 }
5164 if (objAssign) { 5154 if (objAssign) {
@@ -5196,7 +5186,7 @@ private:
5196 } 5186 }
5197 if (auto tabLit = import->target.as<ImportTabLit_t>()) { 5187 if (auto tabLit = import->target.as<ImportTabLit_t>()) {
5198 auto newTab = x->new_ptr<ImportTabLit_t>(); 5188 auto newTab = x->new_ptr<ImportTabLit_t>();
5199#ifndef MOONP_NO_MACRO 5189#ifndef YUE_NO_MACRO
5200 bool importAllMacro = false; 5190 bool importAllMacro = false;
5201 std::list<std::pair<std::string,std::string>> macroPairs; 5191 std::list<std::pair<std::string,std::string>> macroPairs;
5202 for (auto item : tabLit->items.objects()) { 5192 for (auto item : tabLit->items.objects()) {
@@ -5220,7 +5210,7 @@ private:
5220 case id<normal_pair_t>(): 5210 case id<normal_pair_t>():
5221 newTab->items.push_back(item); 5211 newTab->items.push_back(item);
5222 break; 5212 break;
5223 default: assert(false); break; 5213 default: YUEE("AST node mismatch", item); break;
5224 } 5214 }
5225 } 5215 }
5226 if (importAllMacro || !macroPairs.empty()) { 5216 if (importAllMacro || !macroPairs.empty()) {
@@ -5231,7 +5221,7 @@ private:
5231 pushCurrentModule(); // cur 5221 pushCurrentModule(); // cur
5232 int top = lua_gettop(L) - 1; // Lua state may be setup by pushCurrentModule() 5222 int top = lua_gettop(L) - 1; // Lua state may be setup by pushCurrentModule()
5233 DEFER(lua_settop(L, top)); 5223 DEFER(lua_settop(L, top));
5234 pushMoonp("find_modulepath"sv); // cur find_modulepath 5224 pushYue("find_modulepath"sv); // cur find_modulepath
5235 lua_pushlstring(L, moduleName.c_str(), moduleName.size()); // cur find_modulepath moduleName 5225 lua_pushlstring(L, moduleName.c_str(), moduleName.size()); // cur find_modulepath moduleName
5236 if (lua_pcall(L, 1, 1, 0) != 0) { 5226 if (lua_pcall(L, 1, 1, 0) != 0) {
5237 std::string err = lua_tostring(L, -1); 5227 std::string err = lua_tostring(L, -1);
@@ -5243,7 +5233,7 @@ private:
5243 std::string moduleFullName = lua_tostring(L, -1); 5233 std::string moduleFullName = lua_tostring(L, -1);
5244 lua_pop(L, 1); // cur 5234 lua_pop(L, 1); // cur
5245 if (!isModuleLoaded(moduleFullName)) { 5235 if (!isModuleLoaded(moduleFullName)) {
5246 pushMoonp("read_file"sv); // cur read_file 5236 pushYue("read_file"sv); // cur read_file
5247 lua_pushlstring(L, moduleFullName.c_str(), moduleFullName.size()); // cur load_text moduleFullName 5237 lua_pushlstring(L, moduleFullName.c_str(), moduleFullName.size()); // cur load_text moduleFullName
5248 if (lua_pcall(L, 1, 1, 0) != 0) { 5238 if (lua_pcall(L, 1, 1, 0) != 0) {
5249 std::string err = lua_tostring(L, -1); 5239 std::string err = lua_tostring(L, -1);
@@ -5253,8 +5243,8 @@ private:
5253 throw std::logic_error(_info.errorMessage("failed to get module text"sv, x)); 5243 throw std::logic_error(_info.errorMessage("failed to get module text"sv, x));
5254 } // cur text 5244 } // cur text
5255 std::string text = lua_tostring(L, -1); 5245 std::string text = lua_tostring(L, -1);
5256 auto compiler = MoonCompilerImpl(L, _luaOpen, false, moduleFullName); 5246 auto compiler = YueCompilerImpl(L, _luaOpen, false, moduleFullName);
5257 MoonConfig config; 5247 YueConfig config;
5258 config.lineOffset = 0; 5248 config.lineOffset = 0;
5259 config.lintGlobalVariable = false; 5249 config.lintGlobalVariable = false;
5260 config.reserveLineNumber = false; 5250 config.reserveLineNumber = false;
@@ -5279,7 +5269,7 @@ private:
5279 lua_setfield(L, -3, pair.second.c_str()); // cur[second] = val, cur mod 5269 lua_setfield(L, -3, pair.second.c_str()); // cur[second] = val, cur mod
5280 } 5270 }
5281 } 5271 }
5282#else // MOONP_NO_MACRO 5272#else // YUE_NO_MACRO
5283 for (auto item : tabLit->items.objects()) { 5273 for (auto item : tabLit->items.objects()) {
5284 switch (item->getId()) { 5274 switch (item->getId()) {
5285 case id<MacroName_t>(): 5275 case id<MacroName_t>():
@@ -5292,10 +5282,10 @@ private:
5292 case id<normal_pair_t>(): 5282 case id<normal_pair_t>():
5293 newTab->items.push_back(item); 5283 newTab->items.push_back(item);
5294 break; 5284 break;
5295 default: assert(false); break; 5285 default: YUEE("AST node mismatch", item); break;
5296 } 5286 }
5297 } 5287 }
5298#endif // MOONP_NO_MACRO 5288#endif // YUE_NO_MACRO
5299 if (newTab->items.empty()) { 5289 if (newTab->items.empty()) {
5300 out.push_back(Empty); 5290 out.push_back(Empty);
5301 return; 5291 return;
@@ -5339,7 +5329,7 @@ private:
5339 case id<ImportFrom_t>(): 5329 case id<ImportFrom_t>():
5340 transformImportFrom(static_cast<ImportFrom_t*>(content), out); 5330 transformImportFrom(static_cast<ImportFrom_t*>(content), out);
5341 break; 5331 break;
5342 default: assert(false); break; 5332 default: YUEE("AST node mismatch", content); break;
5343 } 5333 }
5344 } 5334 }
5345 5335
@@ -5598,25 +5588,25 @@ private:
5598 } 5588 }
5599}; 5589};
5600 5590
5601const std::string MoonCompilerImpl::Empty; 5591const std::string YueCompilerImpl::Empty;
5602 5592
5603MoonCompiler::MoonCompiler(void* sharedState, 5593YueCompiler::YueCompiler(void* sharedState,
5604 const std::function<void(void*)>& luaOpen, 5594 const std::function<void(void*)>& luaOpen,
5605 bool sameModule): 5595 bool sameModule):
5606#ifndef MOONP_NO_MACRO 5596#ifndef YUE_NO_MACRO
5607_compiler(std::make_unique<MoonCompilerImpl>(static_cast<lua_State*>(sharedState), luaOpen, sameModule)) {} 5597_compiler(std::make_unique<YueCompilerImpl>(static_cast<lua_State*>(sharedState), luaOpen, sameModule)) {}
5608#else 5598#else
5609_compiler(std::make_unique<MoonCompilerImpl>()) { 5599_compiler(std::make_unique<YueCompilerImpl>()) {
5610 (void)sharedState; 5600 (void)sharedState;
5611 (void)luaOpen; 5601 (void)luaOpen;
5612 (void)sameModule; 5602 (void)sameModule;
5613} 5603}
5614#endif // MOONP_NO_MACRO 5604#endif // YUE_NO_MACRO
5615 5605
5616MoonCompiler::~MoonCompiler() {} 5606YueCompiler::~YueCompiler() {}
5617 5607
5618CompileInfo MoonCompiler::compile(std::string_view codes, const MoonConfig& config) { 5608CompileInfo YueCompiler::compile(std::string_view codes, const YueConfig& config) {
5619 return _compiler->compile(codes, config); 5609 return _compiler->compile(codes, config);
5620} 5610}
5621 5611
5622} // namespace MoonP 5612} // namespace yue
diff --git a/src/MoonP/moon_compiler.h b/src/yuescript/yue_compiler.h
index 5855b2d..2e8c86a 100644
--- a/src/MoonP/moon_compiler.h
+++ b/src/yuescript/yue_compiler.h
@@ -16,14 +16,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
16#include <unordered_map> 16#include <unordered_map>
17#include <functional> 17#include <functional>
18 18
19namespace MoonP { 19namespace yue {
20 20
21extern const std::string_view version; 21extern const std::string_view version;
22extern const std::string_view extension; 22extern const std::string_view extension;
23 23
24using Options = std::unordered_map<std::string,std::string>; 24using Options = std::unordered_map<std::string,std::string>;
25 25
26struct MoonConfig { 26struct YueConfig {
27 bool lintGlobalVariable = false; 27 bool lintGlobalVariable = false;
28 bool implicitReturnRoot = true; 28 bool implicitReturnRoot = true;
29 bool reserveLineNumber = true; 29 bool reserveLineNumber = true;
@@ -47,17 +47,17 @@ struct CompileInfo {
47 std::unique_ptr<Options> options; 47 std::unique_ptr<Options> options;
48}; 48};
49 49
50class MoonCompilerImpl; 50class YueCompilerImpl;
51 51
52class MoonCompiler { 52class YueCompiler {
53public: 53public:
54 MoonCompiler(void* luaState = nullptr, 54 YueCompiler(void* luaState = nullptr,
55 const std::function<void(void*)>& luaOpen = nullptr, 55 const std::function<void(void*)>& luaOpen = nullptr,
56 bool sameModule = false); 56 bool sameModule = false);
57 virtual ~MoonCompiler(); 57 virtual ~YueCompiler();
58 CompileInfo compile(std::string_view codes, const MoonConfig& config = {}); 58 CompileInfo compile(std::string_view codes, const YueConfig& config = {});
59private: 59private:
60 std::unique_ptr<MoonCompilerImpl> _compiler; 60 std::unique_ptr<YueCompilerImpl> _compiler;
61}; 61};
62 62
63} // namespace MoonP 63} // namespace yue
diff --git a/src/MoonP/moon_parser.cpp b/src/yuescript/yue_parser.cpp
index a581e15..cf3da01 100644
--- a/src/MoonP/moon_parser.cpp
+++ b/src/yuescript/yue_parser.cpp
@@ -6,11 +6,11 @@ The above copyright notice and this permission notice shall be included in all c
6 6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 8
9#include "MoonP/moon_parser.h" 9#include "yuescript/yue_parser.h"
10 10
11namespace pl = parserlib; 11namespace pl = parserlib;
12 12
13namespace MoonP { 13namespace yue {
14using namespace std::string_view_literals; 14using namespace std::string_view_literals;
15 15
16std::unordered_set<std::string> LuaKeywords = { 16std::unordered_set<std::string> LuaKeywords = {
@@ -29,10 +29,10 @@ std::unordered_set<std::string> Keywords = {
29 "until", "while", // Lua keywords 29 "until", "while", // Lua keywords
30 "as", "class", "continue", "export", "extends", 30 "as", "class", "continue", "export", "extends",
31 "from", "global", "import", "macro", "switch", 31 "from", "global", "import", "macro", "switch",
32 "unless", "using", "when", "with" // Moon keywords 32 "unless", "using", "when", "with" // Yue keywords
33}; 33};
34 34
35MoonParser::MoonParser() { 35YueParser::YueParser() {
36 plain_space = *set(" \t"); 36 plain_space = *set(" \t");
37 Break = nl(-expr('\r') >> '\n'); 37 Break = nl(-expr('\r') >> '\n');
38 Any = Break | any(); 38 Any = Break | any();
@@ -245,7 +245,7 @@ MoonParser::MoonParser() {
245 IfElse = -(Break >> *EmptyLine >> CheckIndent) >> key("else") >> plain_body; 245 IfElse = -(Break >> *EmptyLine >> CheckIndent) >> key("else") >> plain_body;
246 If = key("if") >> Seperator >> IfCond >> plain_body_with("then") >> *IfElseIf >> -IfElse; 246 If = key("if") >> Seperator >> IfCond >> plain_body_with("then") >> *IfElseIf >> -IfElse;
247 Unless = key("unless") >> Seperator >> IfCond >> plain_body_with("then") >> *IfElseIf >> -IfElse; 247 Unless = key("unless") >> Seperator >> IfCond >> plain_body_with("then") >> *IfElseIf >> -IfElse;
248 248
249 While = key("while") >> disable_do_chain(Exp) >> plain_body_with("do"); 249 While = key("while") >> disable_do_chain(Exp) >> plain_body_with("do");
250 Repeat = key("repeat") >> Body >> Break >> *EmptyLine >> CheckIndent >> key("until") >> Exp; 250 Repeat = key("repeat") >> Body >> Break >> *EmptyLine >> CheckIndent >> key("until") >> Exp;
251 251
@@ -611,7 +611,7 @@ MoonParser::MoonParser() {
611 File = White >> -Shebang >> Block >> eof(); 611 File = White >> -Shebang >> Block >> eof();
612} 612}
613 613
614ParseInfo MoonParser::parse(std::string_view codes, rule& r) { 614ParseInfo YueParser::parse(std::string_view codes, rule& r) {
615 ParseInfo res; 615 ParseInfo res;
616 try { 616 try {
617 res.codes = std::make_unique<input>(); 617 res.codes = std::make_unique<input>();
@@ -650,19 +650,19 @@ ParseInfo MoonParser::parse(std::string_view codes, rule& r) {
650 return res; 650 return res;
651} 651}
652 652
653std::string MoonParser::toString(ast_node* node) { 653std::string YueParser::toString(ast_node* node) {
654 return _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it)); 654 return _converter.to_bytes(std::wstring(node->m_begin.m_it, node->m_end.m_it));
655} 655}
656 656
657std::string MoonParser::toString(input::iterator begin, input::iterator end) { 657std::string YueParser::toString(input::iterator begin, input::iterator end) {
658 return _converter.to_bytes(std::wstring(begin, end)); 658 return _converter.to_bytes(std::wstring(begin, end));
659} 659}
660 660
661input MoonParser::encode(std::string_view codes) { 661input YueParser::encode(std::string_view codes) {
662 return _converter.from_bytes(&codes.front(), &codes.back() + 1); 662 return _converter.from_bytes(&codes.front(), &codes.back() + 1);
663} 663}
664 664
665std::string MoonParser::decode(const input& codes) { 665std::string YueParser::decode(const input& codes) {
666 return _converter.to_bytes(codes); 666 return _converter.to_bytes(codes);
667} 667}
668 668
@@ -716,4 +716,4 @@ std::string ParseInfo::errorMessage(std::string_view msg, const input_range* loc
716 return buf.str(); 716 return buf.str();
717} 717}
718 718
719} // namespace MoonP 719} // namespace yue
diff --git a/src/MoonP/moon_parser.h b/src/yuescript/yue_parser.h
index b165070..f9a0d54 100644
--- a/src/MoonP/moon_parser.h
+++ b/src/yuescript/yue_parser.h
@@ -16,10 +16,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
16#include <sstream> 16#include <sstream>
17#include <string_view> 17#include <string_view>
18 18
19#include "MoonP/ast.hpp" 19#include "yuescript/ast.hpp"
20#include "MoonP/moon_ast.h" 20#include "yuescript/yue_ast.h"
21 21
22namespace MoonP { 22namespace yue {
23using namespace parserlib; 23using namespace parserlib;
24 24
25struct ParseInfo { 25struct ParseInfo {
@@ -42,9 +42,9 @@ struct identity { typedef T type; };
42extern std::unordered_set<std::string> LuaKeywords; 42extern std::unordered_set<std::string> LuaKeywords;
43extern std::unordered_set<std::string> Keywords; 43extern std::unordered_set<std::string> Keywords;
44 44
45class MoonParser { 45class YueParser {
46public: 46public:
47 MoonParser(); 47 YueParser();
48 48
49 template<class AST> 49 template<class AST>
50 ParseInfo parse(std::string_view codes) { 50 ParseInfo parse(std::string_view codes) {
@@ -314,4 +314,4 @@ namespace Utils {
314 void trim(std::string& str); 314 void trim(std::string& str);
315}; 315};
316 316
317} // namespace MoonP 317} // namespace yue
diff --git a/src/MoonP/moonplus.cpp b/src/yuescript/yuescript.cpp
index 9b9689c..9928f4d 100644
--- a/src/MoonP/moonplus.cpp
+++ b/src/yuescript/yuescript.cpp
@@ -5,28 +5,28 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8#include "MoonP/moon_compiler.h" 8#include "yuescript/yue_compiler.h"
9 9
10extern "C" { 10extern "C" {
11 11
12#include "lua.h" 12#include "lua.h"
13#include "lauxlib.h" 13#include "lauxlib.h"
14 14
15static const char moonplusCodes[] = 15static const char yuescriptCodes[] =
16#include "MoonP/moonplus.h" 16#include "yuescript/yuescript.h"
17 17
18static void init_moonplus(lua_State* L) { 18static void init_yuescript(lua_State* L) {
19 if (luaL_loadbuffer(L, moonplusCodes, sizeof(moonplusCodes) / sizeof(moonplusCodes[0]) - 1, "=(moonplus)") != 0) { 19 if (luaL_loadbuffer(L, yuescriptCodes, sizeof(yuescriptCodes) / sizeof(yuescriptCodes[0]) - 1, "=(yuescript)") != 0) {
20 std::string err = std::string("failed to load moonplus module.\n") + lua_tostring(L, -1); 20 std::string err = std::string("failed to load yuescript module.\n") + lua_tostring(L, -1);
21 luaL_error(L, err.c_str()); 21 luaL_error(L, err.c_str());
22 } else if (lua_pcall(L, 0, 0, 0) != 0) { 22 } else if (lua_pcall(L, 0, 0, 0) != 0) {
23 std::string err = std::string("failed to init moonplus module.\n") + lua_tostring(L, -1); 23 std::string err = std::string("failed to init yuescript module.\n") + lua_tostring(L, -1);
24 luaL_error(L, err.c_str()); 24 luaL_error(L, err.c_str());
25 } 25 }
26} 26}
27 27
28static const char stpCodes[] = 28static const char stpCodes[] =
29#include "MoonP/stacktraceplus.h" 29#include "yuescript/stacktraceplus.h"
30 30
31static int init_stacktraceplus(lua_State* L) { 31static int init_stacktraceplus(lua_State* L) {
32 if (luaL_loadbuffer(L, stpCodes, sizeof(stpCodes) / sizeof(stpCodes[0]) - 1, "=(stacktraceplus)") != 0) { 32 if (luaL_loadbuffer(L, stpCodes, sizeof(stpCodes) / sizeof(stpCodes[0]) - 1, "=(stacktraceplus)") != 0) {
@@ -39,10 +39,10 @@ static int init_stacktraceplus(lua_State* L) {
39 return 1; 39 return 1;
40} 40}
41 41
42static int moontolua(lua_State* L) { 42static int yuetolua(lua_State* L) {
43 size_t size = 0; 43 size_t size = 0;
44 const char* input = luaL_checklstring(L, 1, &size); 44 const char* input = luaL_checklstring(L, 1, &size);
45 MoonP::MoonConfig config; 45 yue::YueConfig config;
46 bool sameModule = false; 46 bool sameModule = false;
47 if (lua_gettop(L) == 2) { 47 if (lua_gettop(L) == 2) {
48 luaL_checktype(L, 2, LUA_TTABLE); 48 luaL_checktype(L, 2, LUA_TTABLE);
@@ -84,7 +84,7 @@ static int moontolua(lua_State* L) {
84 lua_pop(L, 1); 84 lua_pop(L, 1);
85 } 85 }
86 std::string s(input, size); 86 std::string s(input, size);
87 auto result = MoonP::MoonCompiler(L, nullptr, sameModule).compile(s, config); 87 auto result = yue::YueCompiler(L, nullptr, sameModule).compile(s, config);
88 if (result.codes.empty() && !result.error.empty()) { 88 if (result.codes.empty() && !result.error.empty()) {
89 lua_pushnil(L); 89 lua_pushnil(L);
90 } else { 90 } else {
@@ -115,23 +115,23 @@ static int moontolua(lua_State* L) {
115 return 3; 115 return 3;
116} 116}
117 117
118int luaopen_moonp(lua_State* L) { 118int luaopen_yue(lua_State* L) {
119 lua_getglobal(L, "package"); // package 119 lua_getglobal(L, "package"); // package
120 lua_getfield(L, -1, "loaded"); // package loaded 120 lua_getfield(L, -1, "loaded"); // package loaded
121 lua_createtable(L, 0, 0); // package loaded moonp 121 lua_createtable(L, 0, 0); // package loaded yue
122 lua_pushcfunction(L, moontolua); // package loaded moonp func 122 lua_pushcfunction(L, yuetolua); // package loaded yue func
123 lua_setfield(L, -2, "to_lua"); // moonp["to_lua"] = func, package loaded moonp 123 lua_setfield(L, -2, "to_lua"); // yue["to_lua"] = func, package loaded yue
124 lua_pushlstring(L, &MoonP::version.front(), MoonP::version.size()); // package loaded moonp version 124 lua_pushlstring(L, &yue::version.front(), yue::version.size()); // package loaded yue version
125 lua_setfield(L, -2, "version"); // moonp["version"] = version, package loaded moonp 125 lua_setfield(L, -2, "version"); // yue["version"] = version, package loaded yue
126 lua_createtable(L, 0, 0); // package loaded moonp options 126 lua_createtable(L, 0, 0); // package loaded yue options
127 lua_pushlstring(L, &MoonP::extension.front(), MoonP::extension.size()); // package loaded moonp options ext 127 lua_pushlstring(L, &yue::extension.front(), yue::extension.size()); // package loaded yue options ext
128 lua_setfield(L, -2, "extension"); // options["extension"] = ext, package loaded moonp options 128 lua_setfield(L, -2, "extension"); // options["extension"] = ext, package loaded yue options
129 lua_setfield(L, -2, "options"); // moonp["options"] = options, package loaded moonp 129 lua_setfield(L, -2, "options"); // yue["options"] = options, package loaded yue
130 lua_pushcfunction(L, init_stacktraceplus); // package loaded moonp func1 130 lua_pushcfunction(L, init_stacktraceplus); // package loaded yue func1
131 lua_setfield(L, -2, "load_stacktraceplus"); // moonp["load_stacktraceplus"] = func1, package loaded moonp 131 lua_setfield(L, -2, "load_stacktraceplus"); // yue["load_stacktraceplus"] = func1, package loaded yue
132 lua_setfield(L, -2, "moonp"); // loaded["moonp"] = moonp, package loaded 132 lua_setfield(L, -2, "yue"); // loaded["yue"] = yue, package loaded
133 lua_pop(L, 2); // empty 133 lua_pop(L, 2); // empty
134 init_moonplus(L); 134 init_yuescript(L);
135 return 0; 135 return 0;
136} 136}
137 137
diff --git a/src/MoonP/moonplus.h b/src/yuescript/yuescript.h
index ff82e9f..433751e 100644
--- a/src/MoonP/moonplus.h
+++ b/src/yuescript/yuescript.h
@@ -1,6 +1,6 @@
1R"moonplus_codes( 1R"yuescript_codes(
2--[[ 2--[[
3Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin 3Copyright (C) 2020 by Leaf Corcoran, modified by Li Jin, 2021
4 4
5Permission is hereby granted, free of charge, to any person obtaining a copy 5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal 6of this software and associated documentation files (the "Software"), to deal
@@ -20,17 +20,17 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.]] 21THE SOFTWARE.]]
22 22
23local moonp = require("moonp") 23local yue = require("yue")
24local concat, insert, remove = table.concat, table.insert, table.remove 24local concat, insert, remove = table.concat, table.insert, table.remove
25local unpack = unpack or table.unpack 25local unpack = unpack or table.unpack
26local lua = { 26local lua = {
27 loadstring = loadstring, 27 loadstring = loadstring,
28 load = load 28 load = load
29} 29}
30local split, get_options, create_moonpath, moon_loader, load_text, moon_call, loadstring, loadfile, dofile, insert_loader, remove_loader, moon_require, find_modulepath 30local split, get_options, create_yuepath, yue_loader, load_text, yue_call, loadstring, loadfile, dofile, insert_loader, remove_loader, yue_require, find_modulepath
31moonp.dirsep = "/" 31yue.dirsep = "/"
32moonp.moon_compiled = { } 32yue.yue_compiled = { }
33moonp.file_exist = function(fname) 33yue.file_exist = function(fname)
34 local file = io.open(fname) 34 local file = io.open(fname)
35 if file then 35 if file then
36 file:close() 36 file:close()
@@ -39,7 +39,7 @@ moonp.file_exist = function(fname)
39 return false 39 return false
40 end 40 end
41end 41end
42moonp.read_file = function(fname) 42yue.read_file = function(fname)
43 local file, err = io.open(fname) 43 local file, err = io.open(fname)
44 if not file then 44 if not file then
45 return nil, err 45 return nil, err
@@ -72,9 +72,9 @@ get_options = function(...)
72 return { }, ... 72 return { }, ...
73 end 73 end
74end 74end
75create_moonpath = function(package_path) 75create_yuepath = function(package_path)
76 local extension = moonp.options.extension 76 local extension = yue.options.extension
77 local moonpaths 77 local yuepaths
78 do 78 do
79 local _accum_0 = { } 79 local _accum_0 = { }
80 local _len_0 = 1 80 local _len_0 = 1
@@ -96,19 +96,19 @@ create_moonpath = function(package_path)
96 break 96 break
97 end 97 end
98 end 98 end
99 moonpaths = _accum_0 99 yuepaths = _accum_0
100 end 100 end
101 return concat(moonpaths, ";") 101 return concat(yuepaths, ";")
102end 102end
103find_modulepath = function(name) 103find_modulepath = function(name)
104 if not package.moonpath then 104 if not package.yuepath then
105 package.moonpath = create_moonpath(package.path) 105 package.yuepath = create_yuepath(package.path)
106 end 106 end
107 local name_path = name:gsub("%.", moonp.dirsep) 107 local name_path = name:match("[\\/]") and name or name:gsub("%.", yue.dirsep)
108 local file_exist, file_path 108 local file_exist, file_path
109 for path in package.moonpath:gmatch("[^;]+") do 109 for path in package.yuepath:gmatch("[^;]+") do
110 file_path = path:gsub("?", name_path) 110 file_path = path:gsub("?", name_path)
111 file_exist = moonp.file_exist(file_path) 111 file_exist = yue.file_exist(file_path)
112 if file_exist then 112 if file_exist then
113 break 113 break
114 end 114 end
@@ -122,11 +122,11 @@ end
122load_text = function(name) 122load_text = function(name)
123 local file_path = find_modulepath(name) 123 local file_path = find_modulepath(name)
124 if file_path then 124 if file_path then
125 return moonp.read_file(file_path), file_path 125 return yue.read_file(file_path), file_path
126 end 126 end
127 return nil, nil 127 return nil, nil
128end 128end
129moon_loader = function(name) 129yue_loader = function(name)
130 local text, file_path = load_text(name) 130 local text, file_path = load_text(name)
131 if text then 131 if text then
132 local res, err = loadstring(text, file_path) 132 local res, err = loadstring(text, file_path)
@@ -135,27 +135,27 @@ moon_loader = function(name)
135 end 135 end
136 return res 136 return res
137 end 137 end
138 return nil, "Could not find moonp file" 138 return nil, "Could not find yue file"
139end 139end
140moon_call = function(f, ...) 140yue_call = function(f, ...)
141 local args = { 141 local args = {
142 ... 142 ...
143 } 143 }
144 return xpcall((function() 144 return xpcall((function()
145 return f(unpack(args)) 145 return f(unpack(args))
146 end), function(err) 146 end), function(err)
147 return moonp.stp.stacktrace(err, 1) 147 return yue.stp.stacktrace(err, 1)
148 end) 148 end)
149end 149end
150loadstring = function(...) 150loadstring = function(...)
151 local options, str, chunk_name, mode, env = get_options(...) 151 local options, str, chunk_name, mode, env = get_options(...)
152 chunk_name = chunk_name or "=(moonplus.loadstring)" 152 chunk_name = chunk_name or "=(yuescript.loadstring)"
153 local code, err = moonp.to_lua(str, options) 153 local code, err = yue.to_lua(str, options)
154 if not code then 154 if not code then
155 return nil, err 155 return nil, err
156 end 156 end
157 if chunk_name then 157 if chunk_name then
158 moonp.moon_compiled["@" .. chunk_name] = code 158 yue.yue_compiled["@" .. chunk_name] = code
159 end 159 end
160 return (lua.loadstring or lua.load)(code, chunk_name, unpack({ 160 return (lua.loadstring or lua.load)(code, chunk_name, unpack({
161 mode, 161 mode,
@@ -163,7 +163,7 @@ loadstring = function(...)
163 })) 163 }))
164end 164end
165loadfile = function(fname, ...) 165loadfile = function(fname, ...)
166 local text = moonp.read_file(fname) 166 local text = yue.read_file(fname)
167 return loadstring(text, tostring(fname), ...) 167 return loadstring(text, tostring(fname), ...)
168end 168end
169dofile = function(...) 169dofile = function(...)
@@ -174,35 +174,35 @@ insert_loader = function(pos)
174 if pos == nil then 174 if pos == nil then
175 pos = 2 175 pos = 2
176 end 176 end
177 if not package.moonpath then 177 if not package.yuepath then
178 package.moonpath = create_moonpath(package.path) 178 package.yuepath = create_yuepath(package.path)
179 end 179 end
180 local loaders = package.loaders or package.searchers 180 local loaders = package.loaders or package.searchers
181 for _index_0 = 1, #loaders do 181 for _index_0 = 1, #loaders do
182 local loader = loaders[_index_0] 182 local loader = loaders[_index_0]
183 if loader == moon_loader then 183 if loader == yue_loader then
184 return false 184 return false
185 end 185 end
186 end 186 end
187 insert(loaders, pos, moon_loader) 187 insert(loaders, pos, yue_loader)
188 return true 188 return true
189end 189end
190remove_loader = function() 190remove_loader = function()
191 local loaders = package.loaders or package.searchers 191 local loaders = package.loaders or package.searchers
192 for i, loader in ipairs(loaders) do 192 for i, loader in ipairs(loaders) do
193 if loader == moon_loader then 193 if loader == yue_loader then
194 remove(loaders, i) 194 remove(loaders, i)
195 return true 195 return true
196 end 196 end
197 end 197 end
198 return false 198 return false
199end 199end
200moon_require = function(name) 200yue_require = function(name)
201 insert_loader() 201 insert_loader()
202 local success, res = xpcall((function() 202 local success, res = xpcall((function()
203 return require(name) 203 return require(name)
204 end), function(err) 204 end), function(err)
205 local msg = moonp.stp.stacktrace(err, 1) 205 local msg = yue.stp.stacktrace(err, 1)
206 print(msg) 206 print(msg)
207 return msg 207 return msg
208 end) 208 end)
@@ -212,21 +212,21 @@ moon_require = function(name)
212 return nil 212 return nil
213 end 213 end
214end 214end
215setmetatable(moonp, { 215setmetatable(yue, {
216 __index = function(self, key) 216 __index = function(self, key)
217 if not (key == "stp") then 217 if not (key == "stp") then
218 return nil 218 return nil
219 end 219 end
220 local stp = rawget(moonp, "stp") 220 local stp = rawget(yue, "stp")
221 if not stp then 221 if not stp then
222 do 222 do
223 local _with_0 = moonp.load_stacktraceplus() 223 local _with_0 = yue.load_stacktraceplus()
224 _with_0.dump_locals = false 224 _with_0.dump_locals = false
225 _with_0.simplified = true 225 _with_0.simplified = true
226 stp = _with_0 226 stp = _with_0
227 end 227 end
228 rawset(moonp, "stp", stp) 228 rawset(yue, "stp", stp)
229 rawset(moonp, "load_stacktraceplus", nil) 229 rawset(yue, "load_stacktraceplus", nil)
230 end 230 end
231 return stp 231 return stp
232 end, 232 end,
@@ -237,15 +237,15 @@ setmetatable(moonp, {
237for k, v in pairs({ 237for k, v in pairs({
238 insert_loader = insert_loader, 238 insert_loader = insert_loader,
239 remove_loader = remove_loader, 239 remove_loader = remove_loader,
240 loader = moon_loader, 240 loader = yue_loader,
241 dofile = dofile, 241 dofile = dofile,
242 loadfile = loadfile, 242 loadfile = loadfile,
243 loadstring = loadstring, 243 loadstring = loadstring,
244 create_moonpath = create_moonpath, 244 create_yuepath = create_yuepath,
245 find_modulepath = find_modulepath, 245 find_modulepath = find_modulepath,
246 pcall = moon_call, 246 pcall = yue_call,
247 require = moon_require 247 require = yue_require
248}) do 248}) do
249 moonp[k] = v 249 yue[k] = v
250end 250end
251)moonplus_codes"; 251)yuescript_codes";