aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-03-27 18:40:55 +0800
committerLi Jin <dragon-fly@qq.com>2020-03-27 18:40:55 +0800
commitb3e7b8ebb3b43e02197683a8e46957bbb16b8ead (patch)
tree1ed982c80280534cf8da167cc1364d4121e7fa5c /src
parentcd26ef85ccd3b9ef59822f567d912ac7520efa9e (diff)
downloadyuescript-b3e7b8ebb3b43e02197683a8e46957bbb16b8ead.tar.gz
yuescript-b3e7b8ebb3b43e02197683a8e46957bbb16b8ead.tar.bz2
yuescript-b3e7b8ebb3b43e02197683a8e46957bbb16b8ead.zip
fix moonp repl issue.
Diffstat (limited to 'src')
-rw-r--r--src/MoonP/moon_compiler.cpp8
-rw-r--r--src/MoonP/moon_compiler.h2
-rw-r--r--src/linenoise.hpp9
-rw-r--r--src/moonp.cpp64
4 files changed, 69 insertions, 14 deletions
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) {
42 return std::string(sv); 42 return std::string(sv);
43} 43}
44 44
45const char* moonScriptVersion() { 45const char* version() {
46 return "0.5.0-r0.3.6"; 46 return "0.3.6";
47} 47}
48 48
49// name of table stored in lua registry 49// name of table stored in lua registry
50#define MOONP_MODULE "_modules_" 50#define MOONP_MODULE "__moon_modules__"
51 51
52class MoonCompilerImpl { 52class MoonCompilerImpl {
53public: 53public:
@@ -61,6 +61,7 @@ public:
61 BLOCK_START 61 BLOCK_START
62 BREAK_IF(!sameModule); 62 BREAK_IF(!sameModule);
63 BREAK_IF(!L); 63 BREAK_IF(!L);
64 _sameModule = true;
64 int top = lua_gettop(L); 65 int top = lua_gettop(L);
65 DEFER(lua_settop(L, top)); 66 DEFER(lua_settop(L, top));
66 lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE 67 lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE
@@ -69,7 +70,6 @@ public:
69 int idx = static_cast<int>(lua_objlen(L, -1)); // idx = #tb, tb 70 int idx = static_cast<int>(lua_objlen(L, -1)); // idx = #tb, tb
70 BREAK_IF(idx == 0); 71 BREAK_IF(idx == 0);
71 _useModule = true; 72 _useModule = true;
72 _sameModule = true;
73 BLOCK_END 73 BLOCK_END
74 } 74 }
75 75
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
17 17
18namespace MoonP { 18namespace MoonP {
19 19
20const char* moonScriptVersion(); 20const char* version();
21 21
22struct MoonConfig { 22struct MoonConfig {
23 bool lintGlobalVariable = false; 23 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) {
1864 /* Write the prompt and the current buffer content */ 1864 /* Write the prompt and the current buffer content */
1865 ab += l->prompt; 1865 ab += l->prompt;
1866 ab.append(buf, len); 1866 ab.append(buf, len);
1867 if (ab.empty()) return;
1868 /* Erase to right */ 1867 /* Erase to right */
1869 snprintf(seq,64,"\x1b[0K"); 1868 snprintf(seq,64,"\x1b[0K");
1870 ab += seq; 1869 ab += seq;
1871 /* Move cursor to original position. */ 1870 if (!l->prompt.empty() || len != 0) {
1872 snprintf(seq,64,"\r\x1b[%dC", (int)(unicodeColumnPos(buf, pos)+pcolwid)); 1871 /* Move cursor to original position. */
1873 ab += seq; 1872 snprintf(seq,64,"\r\x1b[%dC", (int)(unicodeColumnPos(buf, pos)+pcolwid));
1873 ab += seq;
1874 }
1874 if (write(fd,ab.c_str(), static_cast<int>(ab.length())) == -1) {} /* Can't recover from write error. */ 1875 if (write(fd,ab.c_str(), static_cast<int>(ab.length())) == -1) {} /* Can't recover from write error. */
1875} 1876}
1876 1877
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) {
96 return 1; 96 return 1;
97 } 97 }
98 int count = 0; 98 int count = 0;
99 bool quit = false;
100 linenoise::SetMultiLine(false); 99 linenoise::SetMultiLine(false);
101 while (!quit) { 100 linenoise::SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) {
101 std::string buf = editBuffer;
102 std::string tmp = buf;
103 MoonP::Utils::trim(tmp);
104 if (tmp.empty()) return;
105 std::string pre;
106 auto pos = buf.find_first_not_of(" \t\n");
107 if (pos != std::string::npos) {
108 pre = buf.substr(0, pos);
109 }
110 switch (tmp[0]) {
111 case 'b':
112 completions.push_back(pre + "break");
113 break;
114 case 'c':
115 completions.push_back(pre + "class ");
116 completions.push_back(pre + "continue");
117 break;
118 case 'e':
119 completions.push_back(pre + "else");
120 completions.push_back(pre + "export ");
121 break;
122 case 'i':
123 completions.push_back(pre + "import \"");
124 break;
125 case 'g':
126 completions.push_back(pre + "global ");
127 break;
128 case 'l':
129 completions.push_back(pre + "local ");
130 break;
131 case 'm':
132 completions.push_back(pre + "macro expr ");
133 completions.push_back(pre + "macro block ");
134 completions.push_back(pre + "macro lua ");
135 break;
136 case 's':
137 completions.push_back(pre + "switch ");
138 break;
139 case 'u':
140 completions.push_back(pre + "unless ");
141 break;
142 case 'w':
143 completions.push_back(pre + "with ");
144 completions.push_back(pre + "when ");
145 break;
146 }
147 });
148 std::cout << "Moonscript+ "sv << MoonP::version() << '\n';
149 while (true) {
102 count++; 150 count++;
103 std::string codes; 151 std::string codes;
104 quit = linenoise::Readline("moon> ", codes); 152 bool quit = linenoise::Readline("> ", codes);
153 if (quit) return 0;
105 linenoise::AddHistory(codes.c_str()); 154 linenoise::AddHistory(codes.c_str());
106 MoonP::Utils::trim(codes); 155 MoonP::Utils::trim(codes);
107 if (codes == "$"sv) { 156 if (codes == "$"sv) {
108 codes.clear(); 157 codes.clear();
109 for (std::string line; !linenoise::Readline("", line);) { 158 for (std::string line; !(quit = linenoise::Readline("", line));) {
110 auto temp = line; 159 auto temp = line;
111 MoonP::Utils::trim(temp); 160 MoonP::Utils::trim(temp);
112 if (temp == "$"sv) { 161 if (temp == "$"sv) {
@@ -117,6 +166,7 @@ int main(int narg, const char** args) {
117 linenoise::AddHistory(line.c_str()); 166 linenoise::AddHistory(line.c_str());
118 MoonP::Utils::trim(codes); 167 MoonP::Utils::trim(codes);
119 } 168 }
169 if (quit) return 0;
120 } 170 }
121 codes.insert(0, "global *\n"sv); 171 codes.insert(0, "global *\n"sv);
122 int top = lua_gettop(L); 172 int top = lua_gettop(L);
@@ -132,6 +182,10 @@ int main(int narg, const char** args) {
132 } 182 }
133 if (lua_isnil(L, -2) != 0) { 183 if (lua_isnil(L, -2) != 0) {
134 std::string err = lua_tostring(L, -1); 184 std::string err = lua_tostring(L, -1);
185 auto modName = std::string("(repl "sv) + std::to_string(count) + "):";
186 if (err.substr(0, modName.size()) == modName) {
187 err = err.substr(modName.size());
188 }
135 auto pos = err.find(':'); 189 auto pos = err.find(':');
136 if (pos != std::string::npos) { 190 if (pos != std::string::npos) {
137 int lineNum = std::stoi(err.substr(0, pos)); 191 int lineNum = std::stoi(err.substr(0, pos));
@@ -271,7 +325,7 @@ int main(int narg, const char** args) {
271 std::cout << help; 325 std::cout << help;
272 return 0; 326 return 0;
273 } else if (arg == "-v"sv) { 327 } else if (arg == "-v"sv) {
274 std::cout << "Moonscript version: "sv << MoonP::moonScriptVersion() << '\n'; 328 std::cout << "Moonscript+ version: "sv << MoonP::version() << '\n';
275 return 0; 329 return 0;
276 } else if (arg == "-o"sv) { 330 } else if (arg == "-o"sv) {
277 ++i; 331 ++i;