diff options
Diffstat (limited to 'src/moonp.cpp')
-rw-r--r-- | src/moonp.cpp | 64 |
1 files changed, 59 insertions, 5 deletions
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; |