From 1ecd0a6912950d9a6c9d4f3f2a53bc6d175fe38f Mon Sep 17 00:00:00 2001 From: Sidi Liang <1467329765@qq.com> Date: Tue, 5 Nov 2024 01:24:18 +0800 Subject: [PATCH] [REPL] Modified to support web repl --- src/repl/repl.cpp | 38 +++++++++++++++++++++++++++++++++++++- src/repl/repl.h | 15 ++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/repl/repl.cpp b/src/repl/repl.cpp index b499d67..7970132 100644 --- a/src/repl/repl.cpp +++ b/src/repl/repl.cpp @@ -34,6 +34,14 @@ void repl::update_temp_file() { info::instance()->repl_file_source = content + " "; } +void repl::update_temp_file(const std::vector& 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& src) { + update_temp_file(src); + auto nasal_lexer = std::make_unique(); + if (nasal_lexer->scan("").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(); diff --git a/src/repl/repl.h b/src/repl/repl.h index e38ec3f..d9f844b 100644 --- a/src/repl/repl.h +++ b/src/repl/repl.h @@ -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& 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& src); + // Add method to access source + void set_source(const std::vector& src) { + source = src; + } + + // Add method to access runtime + vm& get_runtime() { + return runtime; + } }; }