From 985dee800169fa81a6ac6939a9e7e95dc25af9b6 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Thu, 26 Dec 2024 21:02:35 +0800 Subject: [PATCH] :memo: format --- src/nasal_type.cpp | 37 --------------- src/nasal_type.h | 98 +++++++++++++++++++++++--------------- src/nasal_vm.cpp | 10 ++-- src/nasal_vm.h | 40 ++++++++-------- test/coroutine.nas | 116 +++++++++++++++++++++++---------------------- 5 files changed, 144 insertions(+), 157 deletions(-) diff --git a/src/nasal_type.cpp b/src/nasal_type.cpp index 588fbc2..8a38b11 100644 --- a/src/nasal_type.cpp +++ b/src/nasal_type.cpp @@ -317,41 +317,4 @@ std::ostream& operator<<(std::ostream& out, var& ref) { return out; } -bool var::object_check(const std::string& name) const { - return is_ghost() && ghost().type_name == name && ghost().pointer; -} - -var var::none() { - return {vm_type::vm_none, static_cast(0)}; -} - -var var::nil() { - return {vm_type::vm_nil, static_cast(0)}; -} - -var var::ret(u64 pc) { - return {vm_type::vm_ret, pc}; -} - -var var::cnt(i64 n) { - return {vm_type::vm_cnt, n}; -} - -var var::num(f64 n) { - return {vm_type::vm_num, n}; -} - -var var::gcobj(nas_val* p) { - return {p->type, p}; -} - -var var::addr(var* p) { - return {vm_type::vm_addr, p}; -} - -var nas_err(const std::string& error_function_name, const std::string& info) { - std::cerr << "[vm] " << error_function_name << ": " << info << "\n"; - return var::none(); -} - } \ No newline at end of file diff --git a/src/nasal_type.h b/src/nasal_type.h index 029f9da..3c240d5 100644 --- a/src/nasal_type.h +++ b/src/nasal_type.h @@ -86,31 +86,45 @@ public: } val; private: - var(vm_type t, u64 pc) {type = t; val.ret = pc;} - var(vm_type t, i64 ct) {type = t; val.cnt = ct;} - var(vm_type t, f64 n) {type = t; val.num = n;} - var(vm_type t, var* p) {type = t; val.addr = p;} - var(vm_type t, nas_val* p) {type = t; val.gcobj = p;} + var(vm_type t, u64 pc) { type = t; val.ret = pc; } + var(vm_type t, i64 ct) { type = t; val.cnt = ct; } + var(vm_type t, f64 n) { type = t; val.num = n; } + var(vm_type t, var* p) { type = t; val.addr = p; } + var(vm_type t, nas_val* p) { type = t; val.gcobj = p; } public: var() = default; var(const var&) = default; bool operator==(const var& nr) const { - return type==nr.type && val.gcobj==nr.val.gcobj; + return type == nr.type && val.gcobj == nr.val.gcobj; } bool operator!=(const var& nr) const { - return type!=nr.type || val.gcobj!=nr.val.gcobj; + return type != nr.type || val.gcobj != nr.val.gcobj; } public: // create new var object - static var none(); - static var nil(); - static var ret(u64); - static var cnt(i64); - static var num(f64); - static var gcobj(nas_val*); - static var addr(var*); + static var none() { + return var(vm_type::vm_none, static_cast(0)); + } + static var nil() { + return var(vm_type::vm_nil, static_cast(0)); + } + static var ret(u64 pc) { + return var(vm_type::vm_ret, pc); + } + static var cnt(i64 n) { + return var(vm_type::vm_cnt, n); + } + static var num(f64 n) { + return var(vm_type::vm_num, n); + } + static var gcobj(nas_val* p) { + return var(p->type, p); + } + static var addr(var* p) { + return var(vm_type::vm_addr, p); + } public: // get value @@ -143,26 +157,27 @@ public: const nas_map& map() const { return *val.gcobj->ptr.map; } public: - bool is_none() const { return type==vm_type::vm_none; } - bool is_cnt() const { return type==vm_type::vm_cnt; } - bool is_addr() const { return type==vm_type::vm_addr; } - bool is_ret() const { return type==vm_type::vm_ret; } - bool is_nil() const { return type==vm_type::vm_nil; } - bool is_num() const { return type==vm_type::vm_num; } - bool is_str() const { return type==vm_type::vm_str; } - bool is_vec() const { return type==vm_type::vm_vec; } - bool is_hash() const { return type==vm_type::vm_hash; } - bool is_func() const { return type==vm_type::vm_func; } - bool is_upval() const { return type==vm_type::vm_upval; } - bool is_ghost() const { return type==vm_type::vm_ghost; } - bool is_coroutine() const { return type==vm_type::vm_co; } - bool is_map() const { return type==vm_type::vm_map; } + bool is_none() const { return type == vm_type::vm_none; } + bool is_cnt() const { return type == vm_type::vm_cnt; } + bool is_addr() const { return type == vm_type::vm_addr; } + bool is_ret() const { return type == vm_type::vm_ret; } + bool is_nil() const { return type == vm_type::vm_nil; } + bool is_num() const { return type == vm_type::vm_num; } + bool is_str() const { return type == vm_type::vm_str; } + bool is_vec() const { return type == vm_type::vm_vec; } + bool is_hash() const { return type == vm_type::vm_hash; } + bool is_func() const { return type == vm_type::vm_func; } + bool is_upval() const { return type == vm_type::vm_upval; } + bool is_ghost() const { return type == vm_type::vm_ghost; } + bool is_coroutine() const { return type == vm_type::vm_co; } + bool is_map() const { return type == vm_type::vm_map; } public: // number and string can be translated to each other f64 to_num() const; std::string to_str(); - bool object_check(const std::string&) const; + inline bool object_check(const std::string&) const; + friend std::ostream& operator<<(std::ostream&, var&); }; struct nas_vec { @@ -174,6 +189,7 @@ struct nas_vec { auto size() const { return elems.size(); } var get_value(const i32); var* get_memory(const i32); + friend std::ostream& operator<<(std::ostream&, nas_vec&); }; struct nas_hash { @@ -185,6 +201,7 @@ struct nas_hash { auto size() const { return elems.size(); } var get_value(const std::string&); var* get_memory(const std::string&); + friend std::ostream& operator<<(std::ostream&, nas_hash&); }; struct nas_func { @@ -206,6 +223,7 @@ struct nas_func { parameter_size(0), local_size(0), dynamic_parameter_name("") {} void clear(); + friend std::ostream& operator<<(std::ostream&, nas_func&); }; struct nas_upval { @@ -222,7 +240,7 @@ public: nas_upval(): on_stack(true), size(0), stack_frame_offset(nullptr) {} var& operator[](usize n) { - return on_stack? stack_frame_offset[n]:elems[n]; + return on_stack? stack_frame_offset[n] : elems[n]; } void clear() { @@ -250,6 +268,7 @@ public: ~nas_ghost() { clear(); } void set(const std::string&, destructor, marker, void*); void clear(); + friend std::ostream& operator<<(std::ostream&, const nas_ghost&); public: const auto& get_ghost_name() const { return type_name; } @@ -290,6 +309,7 @@ struct nas_co { delete[] ctx.stack; } void clear(); + friend std::ostream& operator<<(std::ostream&, const nas_co&); }; struct nas_map { @@ -304,21 +324,21 @@ public: var get_value(const std::string&); var* get_memory(const std::string&); + friend std::ostream& operator<<(std::ostream&, nas_map&); }; -std::ostream& operator<<(std::ostream&, nas_vec&); -std::ostream& operator<<(std::ostream&, nas_hash&); -std::ostream& operator<<(std::ostream&, nas_func&); -std::ostream& operator<<(std::ostream&, nas_map&); -std::ostream& operator<<(std::ostream&, const nas_ghost&); -std::ostream& operator<<(std::ostream&, const nas_co&); -std::ostream& operator<<(std::ostream&, var&); - const var zero = var::num(0); const var one = var::num(1); const var nil = var::nil(); +inline bool var::object_check(const std::string& name) const { + return is_ghost() && ghost().type_name == name && ghost().pointer; +} + // use to print error log and return error value -var nas_err(const std::string&, const std::string&); +static var nas_err(const std::string& func, const std::string& info) { + std::cerr << "[vm] " << func << ": " << info << "\n"; + return var::none(); +} } \ No newline at end of file diff --git a/src/nasal_vm.cpp b/src/nasal_vm.cpp index 70bc2cc..3a57ec4 100644 --- a/src/nasal_vm.cpp +++ b/src/nasal_vm.cpp @@ -536,12 +536,12 @@ void vm::die(const std::string& str) { if (!ngc.cort) { // in main context, exit directly std::exit(1); - } else { - // in coroutine, shut down the coroutine and return to main context - ctx.pc = 0; // mark coroutine 'dead' - ngc.context_reserve(); // switch context to main - ctx.top[0] = nil; // generate return value 'nil' } + + // in coroutine, shut down the coroutine and return to main context + ctx.pc = 0; // mark coroutine 'dead' + ngc.context_reserve(); // switch context to main + ctx.top[0] = nil; // generate return value 'nil' } void vm::run(const codegen& gen, diff --git a/src/nasal_vm.h b/src/nasal_vm.h index 0829bce..6221208 100644 --- a/src/nasal_vm.h +++ b/src/nasal_vm.h @@ -359,8 +359,8 @@ inline void vm::o_loadl() { } inline void vm::o_loadu() { - ctx.funcr.func().upval[(imm[ctx.pc]>>16)&0xffff] - .upval()[imm[ctx.pc]&0xffff] = (ctx.top--)[0]; + ctx.funcr.func().upval[(imm[ctx.pc]>>16) & 0xffff] + .upval()[imm[ctx.pc] & 0xffff] = (ctx.top--)[0]; } inline void vm::o_dup() { @@ -452,7 +452,7 @@ inline void vm::o_lnot() { var val = ctx.top[0]; switch(val.type) { case vm_type::vm_nil: ctx.top[0] = one; break; - case vm_type::vm_num: ctx.top[0] = val.num()? zero:one; break; + case vm_type::vm_num: ctx.top[0] = val.num()? zero : one; break; case vm_type::vm_str: { const f64 num = util::str_to_num(val.str().c_str()); if (std::isnan(num)) { @@ -462,7 +462,7 @@ inline void vm::o_lnot() { } } break; default: - die("cannot do not-operation on "+type_name_string(val)); + die("cannot do not-operation on " + type_name_string(val)); return; } } @@ -477,7 +477,7 @@ inline void vm::o_bnot() { inline void vm::o_btor() { ctx.top[-1] = var::num( - static_cast(ctx.top[-1].to_num())| + static_cast(ctx.top[-1].to_num()) | static_cast(ctx.top[0].to_num()) ); --ctx.top; @@ -485,7 +485,7 @@ inline void vm::o_btor() { inline void vm::o_btxor() { ctx.top[-1] = var::num( - static_cast(ctx.top[-1].to_num())^ + static_cast(ctx.top[-1].to_num()) ^ static_cast(ctx.top[0].to_num()) ); --ctx.top; @@ -493,7 +493,7 @@ inline void vm::o_btxor() { inline void vm::o_btand() { ctx.top[-1] = var::num( - static_cast(ctx.top[-1].to_num())& + static_cast(ctx.top[-1].to_num()) & static_cast(ctx.top[0].to_num()) ); --ctx.top; @@ -503,10 +503,10 @@ inline void vm::o_btand() { ctx.top[-1] = var::num(ctx.top[-1].to_num() type ctx.top[0].to_num());\ --ctx.top; -inline void vm::o_add() {op_calc(+);} -inline void vm::o_sub() {op_calc(-);} -inline void vm::o_mul() {op_calc(*);} -inline void vm::o_div() {op_calc(/);} +inline void vm::o_add() { op_calc(+); } +inline void vm::o_sub() { op_calc(-); } +inline void vm::o_mul() { op_calc(*); } +inline void vm::o_div() { op_calc(/); } inline void vm::o_lnk() { // concat two vectors into one if (ctx.top[-1].is_vec() && ctx.top[0].is_vec()) { @@ -535,7 +535,7 @@ inline void vm::o_subc() {op_calc_const(-);} inline void vm::o_mulc() {op_calc_const(*);} inline void vm::o_divc() {op_calc_const(/);} inline void vm::o_lnkc() { - ctx.top[0] = ngc.newstr(ctx.top[0].to_str()+const_string[imm[ctx.pc]]); + ctx.top[0] = ngc.newstr(ctx.top[0].to_str() + const_string[imm[ctx.pc]]); } // top[0] stores the value of memr[0], to avoid being garbage-collected @@ -575,7 +575,7 @@ inline void vm::o_lnkeq() { ctx.memr[0].to_str()+ctx.top[-1].to_str() ); ctx.memr = nullptr; - ctx.top -= imm[ctx.pc]+1; + ctx.top -= imm[ctx.pc] + 1; } inline void vm::o_bandeq() { @@ -598,11 +598,11 @@ inline void vm::o_boreq() { inline void vm::o_bxoreq() { ctx.top[-1] = ctx.memr[0] = var::num( - static_cast(ctx.memr[0].to_num())^ + static_cast(ctx.memr[0].to_num()) ^ static_cast(ctx.top[-1].to_num()) ); ctx.memr = nullptr; - ctx.top -= imm[ctx.pc]+1; + ctx.top -= imm[ctx.pc] + 1; } // top[0] stores the value of memr[0], to avoid being garbage-collected @@ -1083,8 +1083,8 @@ inline void vm::o_mcalll() { inline void vm::o_mupval() { ctx.memr = &( ctx.funcr.func() - .upval[(imm[ctx.pc]>>16)&0xffff] - .upval()[imm[ctx.pc]&0xffff] + .upval[(imm[ctx.pc]>>16) & 0xffff] + .upval()[imm[ctx.pc] & 0xffff] ); (++ctx.top)[0] = ctx.memr[0]; // push value in this memory space on stack @@ -1102,7 +1102,7 @@ inline void vm::o_mcallv() { } } else if (vec.is_hash()) { // do mcallh but use the mcallv way if (!val.is_str()) { - die("must use string as the key but get "+type_name_string(val)); + die("must use string as the key but get " + type_name_string(val)); return; } auto& ref = vec.hash(); @@ -1114,7 +1114,7 @@ inline void vm::o_mcallv() { } } else if (vec.is_map()) { if (!val.is_str()) { - die("must use string as the key but get "+type_name_string(val)); + die("must use string as the key but get " + type_name_string(val)); return; } auto& ref = vec.map(); @@ -1192,7 +1192,7 @@ inline void vm::o_ret() { auto size = func.func().local_size; upval.on_stack = false; upval.elems.resize(size); - for(u64 i = 0; i