aboutsummaryrefslogtreecommitdiff
path: root/src/yue.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/yue.cpp (renamed from src/moonp.cpp)102
1 files changed, 51 insertions, 51 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) {