[REPL] Modified to support web repl

This commit is contained in:
Sidi Liang 2024-11-05 01:24:18 +08:00
parent 8dc06c085c
commit 1ecd0a6912
No known key found for this signature in database
GPG Key ID: 9785F5EECFFA5311
2 changed files with 51 additions and 2 deletions

View File

@ -34,6 +34,14 @@ void repl::update_temp_file() {
info::instance()->repl_file_source = content + " ";
}
void repl::update_temp_file(const std::vector<std::string>& src) {
auto content = std::string("");
for(const auto& i : src) {
content += i + "\n";
}
info::instance()->repl_file_source = content + " ";
}
bool repl::check_need_more_input() {
while(true) {
update_temp_file();
@ -67,6 +75,34 @@ bool repl::check_need_more_input() {
return true;
}
int repl::check_need_more_input(std::vector<std::string>& src) {
update_temp_file(src);
auto nasal_lexer = std::make_unique<lexer>();
if (nasal_lexer->scan("<nasal-repl>").geterr()) {
return -1;
}
i64 in_curve = 0;
i64 in_bracket = 0;
i64 in_brace = 0;
for(const auto& t : nasal_lexer->result()) {
switch(t.type) {
case tok::tk_lcurve: ++in_curve; break;
case tok::tk_rcurve: --in_curve; break;
case tok::tk_lbracket: ++in_bracket; break;
case tok::tk_rbracket: --in_bracket; break;
case tok::tk_lbrace: ++in_brace; break;
case tok::tk_rbrace: --in_brace; break;
default: break;
}
}
if (in_curve > 0 || in_bracket > 0 || in_brace > 0) {
return 1; // More input needed
}
return 0; // Input is complete
}
void repl::help() {
std::cout << ".h, .help | show help\n";
std::cout << ".e, .exit | quit the REPL\n";
@ -150,7 +186,7 @@ void repl::execute() {
std::cout << "\", input \".help\" for help\n";
continue;
}
source.push_back(line);
if (!check_need_more_input()) {
source.pop_back();

View File

@ -36,8 +36,8 @@ private:
std::string readline(const std::string&);
bool check_need_more_input();
void update_temp_file();
void update_temp_file(const std::vector<std::string>& src);
void help();
bool run();
public:
repl() {
@ -48,7 +48,20 @@ public:
// set empty history
command_history = {""};
}
// Make these methods public for web REPL
bool run();
void execute();
int check_need_more_input(std::vector<std::string>& src);
// Add method to access source
void set_source(const std::vector<std::string>& src) {
source = src;
}
// Add method to access runtime
vm& get_runtime() {
return runtime;
}
};
}