From afc8c544872a6e9faa2c184ba32492fd5099f39a Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Thu, 18 May 2023 18:45:56 +0800 Subject: [PATCH 01/34] :sparkles: gc::extend uses std::nothrow --- CMakeLists.txt | 4 +--- doc/vs.md | 4 ++-- nasal_gc.h | 26 ++++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 278dcd9..9fe5c8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,9 +27,7 @@ target_include_directories(nasock PRIVATE ${CMAKE_SOURCE_DIR}) add_executable(nasal main.cpp) -if(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows") - message("Ignore linking dl lib") -else() +if(NOT CMAKE_HOST_SYSTEM_NAME MATCHES "Windows") target_link_libraries(nasal dl) endif() diff --git a/doc/vs.md b/doc/vs.md index afcf8b3..b828435 100644 --- a/doc/vs.md +++ b/doc/vs.md @@ -2,9 +2,9 @@ ## First | 首先 -We give a __CMakeLists.txt__ for you to create new VS project from it. +We give a [__CMakeLists.txt__](../CMakeLists.txt) for you to create new VS project from it. -我们为你提供了 __CMakeLists.txt__ 用于创建新的 VS 工程。 +我们为你提供了 [__CMakeLists.txt__](../CMakeLists.txt) 用于创建新的 VS 工程。 If you are using this way, you will __not need__ to continue reading. diff --git a/nasal_gc.h b/nasal_gc.h index 271405a..baa673a 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -1,4 +1,6 @@ #pragma once + +// avoid MSVC warnings #ifdef _MSC_VER #pragma warning (disable:4244) #pragma warning (disable:4267) @@ -64,7 +66,7 @@ struct nas_vec; // vector struct nas_hash; // hashmap(dict) struct nas_func; // function(lambda) struct nas_upval; // upvalue -struct nas_obj; // special objects +struct nas_ghost; // objects struct nas_co; // coroutine struct nas_val; // nas_val includes gc-managed types @@ -117,7 +119,7 @@ public: nas_hash& hash(); nas_func& func(); nas_upval& upval(); - nas_obj& obj (); + nas_ghost& obj (); nas_co& co (); }; @@ -261,7 +263,7 @@ public: } }; -struct nas_obj { +struct nas_ghost { public: usize type; void* ptr; @@ -270,13 +272,13 @@ private: ghost_register_table* ghost_type_table; public: - nas_obj(): type(0), ptr(nullptr), ghost_type_table(nullptr) {} - ~nas_obj() {clear();} + nas_ghost(): type(0), ptr(nullptr), ghost_type_table(nullptr) {} + ~nas_ghost() {clear();} void set(usize, void*, ghost_register_table*); void clear(); public: - friend std::ostream& operator<<(std::ostream& out, nas_obj& ghost) { + friend std::ostream& operator<<(std::ostream& out, nas_ghost& ghost) { out<<"get_ghost_name(ghost.type); out<<" at 0x"<"; return out; @@ -313,7 +315,7 @@ struct nas_val { nas_hash* hash; nas_func* func; nas_upval* upval; - nas_obj* obj; + nas_ghost* obj; nas_co* co; } ptr; @@ -419,13 +421,13 @@ void nas_func::clear() { keys.clear(); } -void nas_obj::set(usize t, void* p, ghost_register_table* table) { +void nas_ghost::set(usize t, void* p, ghost_register_table* table) { type=t; ptr=p; ghost_type_table=table; } -void nas_obj::clear() { +void nas_ghost::clear() { if (!ptr) { return; } @@ -459,7 +461,7 @@ nas_val::nas_val(u8 val_type) { case vm_hash: ptr.hash=new nas_hash; break; case vm_func: ptr.func=new nas_func; break; case vm_upval:ptr.upval=new nas_upval;break; - case vm_obj: ptr.obj=new nas_obj; break; + case vm_obj: ptr.obj=new nas_ghost; break; case vm_co: ptr.co=new nas_co; break; } } @@ -561,7 +563,7 @@ nas_vec& var::vec () {return *val.gcobj->ptr.vec; } nas_hash& var::hash () {return *val.gcobj->ptr.hash; } nas_func& var::func () {return *val.gcobj->ptr.func; } nas_upval& var::upval() {return *val.gcobj->ptr.upval;} -nas_obj& var::obj () {return *val.gcobj->ptr.obj; } +nas_ghost& var::obj () {return *val.gcobj->ptr.obj; } nas_co& var::co () {return *val.gcobj->ptr.co; } const var zero=var::num(0); @@ -747,7 +749,7 @@ void gc::extend(u8 type) { size[index]+=incr[index]; for(u32 i=0;i Date: Mon, 5 Jun 2023 00:11:37 +0800 Subject: [PATCH 02/34] prepare to use new ast --- .github/workflows/c-cpp.yml | 2 +- ast/nasal_new_ast.cpp | 19 ++++ ast/nasal_new_ast.h | 112 +++++++++++++++++++++++ makefile | 5 +- nasal_dbg.h | 176 ++++++++++++++++++++++-------------- nasal_gc.h | 6 +- test/life.nas | 2 +- 7 files changed, 250 insertions(+), 72 deletions(-) create mode 100644 ast/nasal_new_ast.cpp create mode 100644 ast/nasal_new_ast.h diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 419659b..db27e23 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -4,7 +4,7 @@ on: schedule: - cron: "0 16 * * *" push: - branches: [ master ] + branches: [ master,develop ] pull_request: branches: [ master ] workflow_dispatch: diff --git a/ast/nasal_new_ast.cpp b/ast/nasal_new_ast.cpp new file mode 100644 index 0000000..266ed2d --- /dev/null +++ b/ast/nasal_new_ast.cpp @@ -0,0 +1,19 @@ +#include "nasal_new_ast.h" + +bool stmt::accept(ast_visitor* visitor) { + visitor->visit_stmt(this); + return true; +} + +bool expr::accept(ast_visitor* visitor) { + visitor->visit_expr(this); + return true; +} + +void ast_visitor::visit_stmt(stmt* node) { + node->accept(this); +} + +void ast_visitor::visit_expr(expr* node) { + node->accept(this); +} \ No newline at end of file diff --git a/ast/nasal_new_ast.h b/ast/nasal_new_ast.h new file mode 100644 index 0000000..154d901 --- /dev/null +++ b/ast/nasal_new_ast.h @@ -0,0 +1,112 @@ +#pragma once + +#include "nasal.h" +#include "nasal_err.h" + +enum class ast_node_type:u32 { + ast_null=0, // null node + ast_stmt, + ast_expr, + ast_root, // mark the root node of ast + ast_block, // expression block + ast_file, // used to store which file the sub-tree is on, only used in main block + ast_nil, // nil keyword + ast_num, // number, basic value type + ast_str, // string, basic value type + ast_id, // identifier + ast_bool, // bools + ast_func, // func keyword + ast_hash, // hash, basic value type + ast_vec, // vector, basic value type + ast_pair, // pair of key and value in hashmap + ast_call, // mark a sub-tree of calling an identifier + ast_callh, // id.name + ast_callv, // id[index] + ast_callf, // id() + ast_subvec, // id[index:index] + ast_params, // mark a sub-tree of function parameters + ast_default, // default parameter + ast_dynamic, // dynamic parameter + ast_and, // and keyword + ast_or, // or keyword + ast_equal, // = + ast_addeq, // += + ast_subeq, // -= + ast_multeq, // *= + ast_diveq, // /= + ast_lnkeq, // ~= + ast_btandeq, // &= + ast_btoreq, // |= + ast_btxoreq, // ^= + ast_cmpeq, // == + ast_neq, // != + ast_less, // < + ast_leq, // <= + ast_grt, // > + ast_geq, // >= + ast_add, // + + ast_sub, // - + ast_mult, // * + ast_div, // / + ast_link, // ~ + ast_neg, // unary - + ast_lnot, // unary ! + ast_bnot, // unary ~ bitwise not + ast_bitor, // bitwise or + ast_bitxor, // bitwise xor + ast_bitand, // bitwise and + ast_trino, // ?: + ast_for, // for keyword + ast_forindex, // forindex keyword + ast_foreach, // foreach keyword + ast_while, // while + ast_iter, // iterator, used in forindex/foreach + ast_cond, // mark a sub-tree of conditional expression + ast_if, // if keyword + ast_elsif, // elsif keyword + ast_else, // else keyword + ast_multi_id, // multi identifiers sub-tree + ast_tuple, // tuple, only used in multiple assignment + ast_def, // definition + ast_multi_assign,// multi assignment sub-tree + ast_continue, // continue keyword, only used in loop + ast_break, // break keyword, only used in loop + ast_ret // return keyword, only used in function block +}; + +class ast_visitor; + +class ast_node { +private: + span nd_loc; + ast_node_type nd_type; +public: + ast_node(const span& location,ast_node_type node_type): + nd_loc(location), nd_type(node_type) {} + ~ast_node() = default; + virtual bool accept(ast_visitor*) = 0; +}; + +class stmt:public ast_node { +public: + stmt(const span& location,ast_node_type node_type): + ast_node(location, node_type) {} + ~stmt() = default; + virtual bool accept(ast_visitor*); +}; + +class expr:public ast_node { +private: + +public: + expr(const span& location,ast_node_type node_type): + ast_node(location, node_type) {} + ~expr() = default; + virtual bool accept(ast_visitor*); +}; + +class ast_visitor { +public: + virtual void visit_stmt(stmt*); + virtual void visit_expr(expr*); +}; \ No newline at end of file diff --git a/makefile b/makefile index 6376f16..01e0999 100644 --- a/makefile +++ b/makefile @@ -18,11 +18,14 @@ SRC=\ STD=c++14 -nasal:$(SRC) +nasal:$(SRC) nasal_new_ast.o $(CXX) -std=$(STD) -O3 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall nasal.exe:$(SRC) $(CXX) -std=$(STD) -O3 main.cpp -o nasal.exe -fno-exceptions -Wshadow -Wall -static +nasal_new_ast.o: ast/nasal_new_ast.h ast/nasal_new_ast.cpp nasal.h + $(CXX) -std=$(STD) -c -O3 ast/nasal_new_ast.cpp -fno-exceptions -fPIC -o nasal_new_ast.o -I . + stable-release:$(SRC) $(CXX) -std=$(STD) -O2 main.cpp -o nasal -fno-exceptions -ldl -Wshadow -Wall stable-release-mingw:$(SRC) diff --git a/nasal_dbg.h b/nasal_dbg.h index 495c505..a10fc42 100644 --- a/nasal_dbg.h +++ b/nasal_dbg.h @@ -4,9 +4,61 @@ #include "nasal_err.h" #include "nasal_opcode.h" #include "nasal_vm.h" + +#include #include +#include class dbg:public vm { +private: + enum class dbg_cmd { + cmd_error, + cmd_help, + cmd_backtrace, + cmd_continue, + cmd_list_file, + cmd_global, + cmd_local, + cmd_upval, + cmd_register, + cmd_show_all, + cmd_next, + cmd_break_point, + cmd_exit + }; + +private: + const std::unordered_map command_table = { + {"h", dbg_cmd::cmd_help}, + {"help", dbg_cmd::cmd_help}, + {"bt", dbg_cmd::cmd_backtrace}, + {"backtrace", dbg_cmd::cmd_backtrace}, + {"c", dbg_cmd::cmd_continue}, + {"continue", dbg_cmd::cmd_continue}, + {"f", dbg_cmd::cmd_list_file}, + {"file", dbg_cmd::cmd_list_file}, + {"g", dbg_cmd::cmd_global}, + {"global", dbg_cmd::cmd_global}, + {"l", dbg_cmd::cmd_local}, + {"local", dbg_cmd::cmd_local}, + {"u", dbg_cmd::cmd_upval}, + {"upval", dbg_cmd::cmd_upval}, + {"r", dbg_cmd::cmd_register}, + {"register", dbg_cmd::cmd_register}, + {"a", dbg_cmd::cmd_show_all}, + {"all", dbg_cmd::cmd_show_all}, + {"n", dbg_cmd::cmd_next}, + {"next", dbg_cmd::cmd_next}, + {"bk", dbg_cmd::cmd_break_point}, + {"break", dbg_cmd::cmd_break_point}, + {"q", dbg_cmd::cmd_exit}, + {"exit", dbg_cmd::cmd_exit} + }; + dbg_cmd get_cmd_type(const string& cmd) const { + return command_table.count(cmd)? + command_table.at(cmd):dbg_cmd::cmd_error; + } + private: bool next; usize fsize; @@ -15,11 +67,12 @@ private: error& src; std::vector parse(const string&); - u16 fileindex(const string&); + u16 file_index(const string&) const; void err(); void help(); - void callsort(const u64*); - void stepinfo(); + void list_file() const; + void call_sort(const u64*) const; + void step_info(); void interact(); public: dbg(error& err): @@ -50,7 +103,7 @@ std::vector dbg::parse(const string& cmd) { return res; } -u16 dbg::fileindex(const string& filename) { +u16 dbg::file_index(const string& filename) const { for(u16 i=0;i\n" - <<"\th, help | get help\n" - <<"\tbt, backtrace | get function call trace\n" - <<"\tc, continue | run program until break point or exit\n" - <<"\tf, file | see all the compiled files\n" - <<"\tg, global | see global values\n" - <<"\tl, local | see local values\n" - <<"\tu, upval | see upvalue\n" - <<"\tr, register | show vm register detail\n" - <<"\ta, all | show global,local and upvalue\n" - <<"\tn, next | execute next bytecode\n" - <<"\tq, exit | exit debugger\n" + <<" h, help | get help\n" + <<" bt, backtrace | get function call trace\n" + <<" c, continue | run program until break point or exit\n" + <<" f, file | see all the compiled files\n" + <<" g, global | see global values\n" + <<" l, local | see local values\n" + <<" u, upval | see upvalue\n" + <<" r, register | show vm register detail\n" + <<" a, all | show global,local and upvalue\n" + <<" n, next | execute next bytecode\n" + <<" q, exit | exit debugger\n" <<"