From b3e7b8ebb3b43e02197683a8e46957bbb16b8ead Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 27 Mar 2020 18:40:55 +0800 Subject: fix moonp repl issue. --- src/MoonP/moon_compiler.cpp | 8 +++--- src/MoonP/moon_compiler.h | 2 +- src/linenoise.hpp | 9 ++++--- src/moonp.cpp | 64 +++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 69 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index e961a92..d036645 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp @@ -42,12 +42,12 @@ inline std::string s(std::string_view sv) { return std::string(sv); } -const char* moonScriptVersion() { - return "0.5.0-r0.3.6"; +const char* version() { + return "0.3.6"; } // name of table stored in lua registry -#define MOONP_MODULE "_modules_" +#define MOONP_MODULE "__moon_modules__" class MoonCompilerImpl { public: @@ -61,6 +61,7 @@ public: BLOCK_START BREAK_IF(!sameModule); BREAK_IF(!L); + _sameModule = true; int top = lua_gettop(L); DEFER(lua_settop(L, top)); lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE @@ -69,7 +70,6 @@ public: int idx = static_cast(lua_objlen(L, -1)); // idx = #tb, tb BREAK_IF(idx == 0); _useModule = true; - _sameModule = true; BLOCK_END } diff --git a/src/MoonP/moon_compiler.h b/src/MoonP/moon_compiler.h index 573f130..d22a95a 100644 --- a/src/MoonP/moon_compiler.h +++ b/src/MoonP/moon_compiler.h @@ -17,7 +17,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI namespace MoonP { -const char* moonScriptVersion(); +const char* version(); struct MoonConfig { bool lintGlobalVariable = false; diff --git a/src/linenoise.hpp b/src/linenoise.hpp index 923a006..b680141 100644 --- a/src/linenoise.hpp +++ b/src/linenoise.hpp @@ -1864,13 +1864,14 @@ inline void refreshSingleLine(struct linenoiseState *l) { /* Write the prompt and the current buffer content */ ab += l->prompt; ab.append(buf, len); - if (ab.empty()) return; /* Erase to right */ snprintf(seq,64,"\x1b[0K"); ab += seq; - /* Move cursor to original position. */ - snprintf(seq,64,"\r\x1b[%dC", (int)(unicodeColumnPos(buf, pos)+pcolwid)); - ab += seq; + if (!l->prompt.empty() || len != 0) { + /* Move cursor to original position. */ + snprintf(seq,64,"\r\x1b[%dC", (int)(unicodeColumnPos(buf, pos)+pcolwid)); + ab += seq; + } if (write(fd,ab.c_str(), static_cast(ab.length())) == -1) {} /* Can't recover from write error. */ } diff --git a/src/moonp.cpp b/src/moonp.cpp index a67a5a7..18eb059 100644 --- a/src/moonp.cpp +++ b/src/moonp.cpp @@ -96,17 +96,66 @@ int main(int narg, const char** args) { return 1; } int count = 0; - bool quit = false; linenoise::SetMultiLine(false); - while (!quit) { + linenoise::SetCompletionCallback([](const char* editBuffer, std::vector& completions) { + std::string buf = editBuffer; + std::string tmp = buf; + MoonP::Utils::trim(tmp); + if (tmp.empty()) return; + std::string pre; + auto pos = buf.find_first_not_of(" \t\n"); + if (pos != std::string::npos) { + pre = buf.substr(0, pos); + } + switch (tmp[0]) { + case 'b': + completions.push_back(pre + "break"); + break; + case 'c': + completions.push_back(pre + "class "); + completions.push_back(pre + "continue"); + break; + case 'e': + completions.push_back(pre + "else"); + completions.push_back(pre + "export "); + break; + case 'i': + completions.push_back(pre + "import \""); + break; + case 'g': + completions.push_back(pre + "global "); + break; + case 'l': + completions.push_back(pre + "local "); + break; + case 'm': + completions.push_back(pre + "macro expr "); + completions.push_back(pre + "macro block "); + completions.push_back(pre + "macro lua "); + break; + case 's': + completions.push_back(pre + "switch "); + break; + case 'u': + completions.push_back(pre + "unless "); + break; + case 'w': + completions.push_back(pre + "with "); + completions.push_back(pre + "when "); + break; + } + }); + std::cout << "Moonscript+ "sv << MoonP::version() << '\n'; + while (true) { count++; std::string codes; - quit = linenoise::Readline("moon> ", codes); + bool quit = linenoise::Readline("> ", codes); + if (quit) return 0; linenoise::AddHistory(codes.c_str()); MoonP::Utils::trim(codes); if (codes == "$"sv) { codes.clear(); - for (std::string line; !linenoise::Readline("", line);) { + for (std::string line; !(quit = linenoise::Readline("", line));) { auto temp = line; MoonP::Utils::trim(temp); if (temp == "$"sv) { @@ -117,6 +166,7 @@ int main(int narg, const char** args) { linenoise::AddHistory(line.c_str()); MoonP::Utils::trim(codes); } + if (quit) return 0; } codes.insert(0, "global *\n"sv); int top = lua_gettop(L); @@ -132,6 +182,10 @@ int main(int narg, const char** args) { } if (lua_isnil(L, -2) != 0) { std::string err = lua_tostring(L, -1); + auto modName = std::string("(repl "sv) + std::to_string(count) + "):"; + if (err.substr(0, modName.size()) == modName) { + err = err.substr(modName.size()); + } auto pos = err.find(':'); if (pos != std::string::npos) { int lineNum = std::stoi(err.substr(0, pos)); @@ -271,7 +325,7 @@ int main(int narg, const char** args) { std::cout << help; return 0; } else if (arg == "-v"sv) { - std::cout << "Moonscript version: "sv << MoonP::moonScriptVersion() << '\n'; + std::cout << "Moonscript+ version: "sv << MoonP::version() << '\n'; return 0; } else if (arg == "-o"sv) { ++i; -- cgit v1.2.3-55-g6feb