aboutsummaryrefslogtreecommitdiff
path: root/src/moonp.cpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-03-27 11:21:31 +0800
committerLi Jin <dragon-fly@qq.com>2020-03-27 11:21:31 +0800
commitcd26ef85ccd3b9ef59822f567d912ac7520efa9e (patch)
treeba442dfe4285c25602a5c979ff108e1f3a6cf9ec /src/moonp.cpp
parent69091011685db34e5dc4012e9d2cf24a0ac77715 (diff)
downloadyuescript-cd26ef85ccd3b9ef59822f567d912ac7520efa9e.tar.gz
yuescript-cd26ef85ccd3b9ef59822f567d912ac7520efa9e.tar.bz2
yuescript-cd26ef85ccd3b9ef59822f567d912ac7520efa9e.zip
update REPL functions.
Diffstat (limited to 'src/moonp.cpp')
-rw-r--r--src/moonp.cpp33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/moonp.cpp b/src/moonp.cpp
index d1c565f..a67a5a7 100644
--- a/src/moonp.cpp
+++ b/src/moonp.cpp
@@ -22,6 +22,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
22#include <memory> 22#include <memory>
23using namespace std::string_view_literals; 23using namespace std::string_view_literals;
24#include "ghc/fs_std.hpp" 24#include "ghc/fs_std.hpp"
25#include "linenoise.hpp"
25 26
26#define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;}) 27#define _DEFER(code,line) std::shared_ptr<void> _defer_##line(nullptr, [&](auto){code;})
27#define DEFER(code) _DEFER(code,__LINE__) 28#define DEFER(code) _DEFER(code,__LINE__)
@@ -95,15 +96,17 @@ int main(int narg, const char** args) {
95 return 1; 96 return 1;
96 } 97 }
97 int count = 0; 98 int count = 0;
98 while (!std::cin.fail()) { 99 bool quit = false;
100 linenoise::SetMultiLine(false);
101 while (!quit) {
99 count++; 102 count++;
100 std::cout << "moon> "sv;
101 std::string codes; 103 std::string codes;
102 std::getline(std::cin, codes); 104 quit = linenoise::Readline("moon> ", codes);
105 linenoise::AddHistory(codes.c_str());
103 MoonP::Utils::trim(codes); 106 MoonP::Utils::trim(codes);
104 if (codes == "$"sv) { 107 if (codes == "$"sv) {
105 codes.clear(); 108 codes.clear();
106 for (std::string line; std::getline(std::cin, line);) { 109 for (std::string line; !linenoise::Readline("", line);) {
107 auto temp = line; 110 auto temp = line;
108 MoonP::Utils::trim(temp); 111 MoonP::Utils::trim(temp);
109 if (temp == "$"sv) { 112 if (temp == "$"sv) {
@@ -111,6 +114,8 @@ int main(int narg, const char** args) {
111 } 114 }
112 codes += '\n'; 115 codes += '\n';
113 codes += line; 116 codes += line;
117 linenoise::AddHistory(line.c_str());
118 MoonP::Utils::trim(codes);
114 } 119 }
115 } 120 }
116 codes.insert(0, "global *\n"sv); 121 codes.insert(0, "global *\n"sv);
@@ -118,7 +123,7 @@ int main(int narg, const char** args) {
118 DEFER(lua_settop(L, top)); 123 DEFER(lua_settop(L, top));
119 pushMoonp(L, "loadstring"sv); 124 pushMoonp(L, "loadstring"sv);
120 lua_pushlstring(L, codes.c_str(), codes.size()); 125 lua_pushlstring(L, codes.c_str(), codes.size());
121 lua_pushstring(L, (std::string("=(repl:") + std::to_string(count) + ')').c_str()); 126 lua_pushstring(L, (std::string("=(repl ") + std::to_string(count) + ')').c_str());
122 pushOptions(L, -1); 127 pushOptions(L, -1);
123 const std::string_view Err = "\033[35m"sv, Val = "\033[33m"sv, Stop = "\033[0m\n"sv; 128 const std::string_view Err = "\033[35m"sv, Val = "\033[33m"sv, Stop = "\033[0m\n"sv;
124 if (lua_pcall(L, 3, 2, 0) != 0) { 129 if (lua_pcall(L, 3, 2, 0) != 0) {
@@ -126,7 +131,13 @@ int main(int narg, const char** args) {
126 continue; 131 continue;
127 } 132 }
128 if (lua_isnil(L, -2) != 0) { 133 if (lua_isnil(L, -2) != 0) {
129 std::cout << Err << lua_tostring(L, -1) << Stop; 134 std::string err = lua_tostring(L, -1);
135 auto pos = err.find(':');
136 if (pos != std::string::npos) {
137 int lineNum = std::stoi(err.substr(0, pos));
138 err = std::to_string(lineNum - 1) + err.substr(pos);
139 }
140 std::cout << Err << err << Stop;
130 continue; 141 continue;
131 } 142 }
132 lua_pop(L, 1); 143 lua_pop(L, 1);
@@ -168,16 +179,16 @@ int main(int narg, const char** args) {
168 std::cout << help; 179 std::cout << help;
169 return 1; 180 return 1;
170 } 181 }
171 char ch; 182 std::string codes;
172 std::string s; 183 linenoise::SetMultiLine(false);
173 while ((ch = std::cin.get()) != EOF) { 184 for (std::string line; !linenoise::Readline("", line);) {
174 s += ch; 185 codes += line;
175 } 186 }
176 MoonP::MoonConfig conf; 187 MoonP::MoonConfig conf;
177 conf.implicitReturnRoot = true; 188 conf.implicitReturnRoot = true;
178 conf.lintGlobalVariable = false; 189 conf.lintGlobalVariable = false;
179 conf.reserveLineNumber = false; 190 conf.reserveLineNumber = false;
180 auto result = MoonP::MoonCompiler{nullptr, openlibs}.compile(s, conf); 191 auto result = MoonP::MoonCompiler{nullptr, openlibs}.compile(codes, conf);
181 if (std::get<1>(result).empty()) { 192 if (std::get<1>(result).empty()) {
182 std::cout << std::get<0>(result); 193 std::cout << std::get<0>(result);
183 return 0; 194 return 0;