From 3e01239722ae36e4fae577112e71d0737b5e036a Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sun, 17 Sep 2023 17:56:59 +0800 Subject: [PATCH] :sparkles: finish basic function of repl --- src/main.cpp | 3 ++- src/nasal_dbg.cpp | 3 ++- src/nasal_gc.cpp | 5 +---- src/nasal_gc.h | 6 +++--- src/nasal_vm.cpp | 49 ++++++++++++++++++++++++++++++----------------- src/nasal_vm.h | 17 +++++++++++++--- src/repl.cpp | 7 +++++-- src/repl.h | 5 +++++ 8 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b90fad2..c3d538b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -154,7 +154,8 @@ void execute( debugger->run(gen, ld, argv, cmd&VM_PROFILE, cmd&VM_PROF_ALL); } else if (cmd&VM_TIME || cmd&VM_EXEC) { auto runtime = std::unique_ptr(new nasal::vm); - runtime->run(gen, ld, argv, cmd&VM_DETAIL); + runtime->set_detail_report_info(cmd&VM_DETAIL); + runtime->run(gen, ld, argv); } // get running time diff --git a/src/nasal_dbg.cpp b/src/nasal_dbg.cpp index 72c7325..e73fc46 100644 --- a/src/nasal_dbg.cpp +++ b/src/nasal_dbg.cpp @@ -237,7 +237,8 @@ void dbg::run( const std::vector& argv, bool profile, bool show_all_prof_result) { - verbose = true; + + set_detail_report_info(true); do_profiling = profile || show_all_prof_result; const auto& file_list = linker.get_file_list(); diff --git a/src/nasal_gc.cpp b/src/nasal_gc.cpp index a2046fe..b1bec6a 100644 --- a/src/nasal_gc.cpp +++ b/src/nasal_gc.cpp @@ -537,11 +537,8 @@ void gc::extend(u8 type) { void gc::init( const std::vector& s, const std::vector& argv) { - // initialize function register - rctx->funcr = nil; - worktime = 0; - // initialize counters + worktime = 0; for(u8 i = 0; i strs; // reserved address for const vm_str - std::vector env_argv; // command line arguments - std::vector memory; // gc memory + std::vector strs = {}; // reserved address for const vm_str + std::vector env_argv = {}; // command line arguments + std::vector memory; // gc memory std::vector unused[gc_type_size]; // gc free list /* heap increase size */ diff --git a/src/nasal_vm.cpp b/src/nasal_vm.cpp index 0fa761c..49999a4 100644 --- a/src/nasal_vm.cpp +++ b/src/nasal_vm.cpp @@ -20,19 +20,10 @@ void vm::init( /* set native functions */ native = natives; - /* set canary and program counter */ - ctx.pc = 0; - ctx.localr = nullptr; - ctx.memr = nullptr; - ctx.funcr = nil; - ctx.upvalr = nil; - ctx.canary = ctx.stack+STACK_DEPTH-1; // stack[STACK_DEPTH-1] - ctx.top = ctx.stack; // nothing is on stack - - /* clear main stack and global */ - for(u32 i = 0; i(val.val.gcobj); switch(val.type) { @@ -240,9 +252,8 @@ void vm::die(const std::string& str) { void vm::run( const codegen& gen, const linker& linker, - const std::vector& argv, - const bool detail) { - verbose = detail; + const std::vector& argv +) { init(gen.strs(), gen.nums(), gen.natives(), gen.codes(), gen.globals(), linker.get_file_list(), argv); #ifndef _MSC_VER @@ -341,11 +352,13 @@ void vm::run( #endif vmexit: - if (detail) { + if (verbose) { ngc.info(); } - ngc.clear(); imm.clear(); + if (!is_repl_mode) { + ngc.clear(); + } return; #ifndef _MSC_VER diff --git a/src/nasal_vm.h b/src/nasal_vm.h index e99d74a..e96b2f6 100644 --- a/src/nasal_vm.h +++ b/src/nasal_vm.h @@ -38,6 +38,10 @@ protected: const std::string* files = nullptr; // file name list const opcode* bytecode = nullptr; // bytecode buffer address + /* variables for repl mode */ + bool is_repl_mode = false; + bool first_exec_flag = true; + /* vm initializing function */ void init( const std::vector&, @@ -46,7 +50,9 @@ protected: const std::vector&, const std::unordered_map&, const std::vector&, - const std::vector&); + const std::vector& + ); + void context_and_global_init(); /* debug functions */ bool verbose = false; @@ -167,8 +173,13 @@ public: void run( const codegen&, const linker&, - const std::vector&, - const bool); + const std::vector& + ); + + /* set detail report info flag */ + void set_detail_report_info(bool flag) {verbose = flag;} + /* set repl mode flag */ + void set_repl_mode_flag(bool flag) {is_repl_mode = flag;} }; inline bool vm::cond(var& val) { diff --git a/src/repl.cpp b/src/repl.cpp index a9f3ae1..d59207d 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -75,7 +75,6 @@ bool repl::run() { auto nasal_linker = std::unique_ptr(new linker); auto nasal_opt = std::unique_ptr(new optimizer); auto nasal_codegen = std::unique_ptr(new codegen); - auto nasal_runtime = std::unique_ptr(new vm); if (nasal_lexer->scan("").geterr()) { return false; @@ -96,7 +95,11 @@ bool repl::run() { auto end = clk::now(); std::clog << "[compile time: " << (end-start).count()*1000.0/den << " ms]\n"; - nasal_runtime->run(*nasal_codegen, *nasal_linker, {}, false); + runtime->set_detail_report_info(false); + // TODO: gc init stage in this run may cause memory leak, + // because constant strings will be generated again. + // but we could not delete old strings, they maybe still on stack. + runtime->run(*nasal_codegen, *nasal_linker, {}); return true; } diff --git a/src/repl.h b/src/repl.h index ffa0d6e..3aa5b81 100644 --- a/src/repl.h +++ b/src/repl.h @@ -1,6 +1,7 @@ #pragma once #include "nasal.h" +#include "nasal_vm.h" #include #include @@ -26,6 +27,7 @@ struct info { class repl { private: std::vector source; + std::unique_ptr runtime; private: std::string readline(std::string); @@ -35,6 +37,9 @@ private: bool run(); public: + repl(): runtime(std::unique_ptr(new vm)) { + runtime->set_repl_mode_flag(true); + } void execute(); };