From 9eb72f8754365d60824b6418a49875d87a126fd5 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 14 Dec 2020 23:43:00 +0800 Subject: [PATCH] update --- nasal.h | 110 ++++++++++++++++++++++++- nasal_ast.h | 6 +- nasal_builtin.h | 4 +- nasal_bytecode_vm.h | 195 +++++++++++++++++++------------------------- nasal_codegen.h | 26 ++---- nasal_gc.h | 12 +-- nasal_misc.h | 191 ------------------------------------------- nasal_runtime.h | 2 +- 8 files changed, 209 insertions(+), 337 deletions(-) delete mode 100644 nasal_misc.h diff --git a/nasal.h b/nasal.h index e278d97..c4ef99b 100644 --- a/nasal.h +++ b/nasal.h @@ -1,11 +1,14 @@ #ifndef __NASAL_H__ #define __NASAL_H__ +#pragma GCC optimize(2) + #include #include #include #include #include +#include #include #include #include @@ -18,7 +21,112 @@ #include #include -#include "nasal_misc.h" +/* + check if a string can be converted to a number + if this string cannot be converted to a number,it will return nan +*/ +inline double hex_to_double(std::string str,int len) +{ + double ret=0; + for(int i=2;i2 && str[0]=='0' && str[1]=='x') + ret_num=hex_to_double(str,len); + else if(len>2 && str[0]=='0' && str[1]=='o') + ret_num=oct_to_double(str,len); + else + ret_num=dec_to_double(str,len); + return is_negative*ret_num; +} + +/* + trans_number_to_string: + convert number to string +*/ +std::string trans_number_to_string(double number) +{ + std::string res; + std::stringstream ss; + ss<>res; + return res; +} #include "nasal_lexer.h" #include "nasal_ast.h" #include "nasal_parse.h" diff --git a/nasal_ast.h b/nasal_ast.h index ad6f4c2..5772e77 100644 --- a/nasal_ast.h +++ b/nasal_ast.h @@ -107,10 +107,10 @@ public: void set_str(std::string&); void set_num(double); void add_child(nasal_ast); - int get_line(); - int get_type(); + int get_line(); + int get_type(); std::string get_str(); - double get_num(); + double get_num(); std::vector& get_children(); void print_ast(int); }; diff --git a/nasal_builtin.h b/nasal_builtin.h index d4aeca2..5af4b73 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -51,7 +51,7 @@ int builtin_contains(int,nasal_virtual_machine&); int builtin_delete(int,nasal_virtual_machine&); int builtin_getkeys(int,nasal_virtual_machine&); int builtin_import(int,nasal_virtual_machine&); -int builtin_die_state;// used in builtin_die +bool builtin_die_state;// used in builtin_die int builtin_die(int,nasal_virtual_machine&); int builtin_type(int,nasal_virtual_machine&); int builtin_substr(int,nasal_virtual_machine&); @@ -773,7 +773,7 @@ int builtin_die(int local_scope_addr,nasal_virtual_machine& nasal_vm) std::cout<<">> [runtime] builtin_die: \"str\" has wrong type(must be string).\n"; return -1; } - builtin_die_state=1; + builtin_die_state=true; std::cout<<">> [runtime] error: "< string_table; // number table std::vector number_table; - // opcode -> function address table - std::vector opr_table; // builtin function address table std::map builtin_func_hashmap; void die(std::string); @@ -89,92 +87,18 @@ private: void opr_return(); public: nasal_bytecode_vm(); - ~nasal_bytecode_vm(); void clear(); void run(std::vector&,std::vector&,std::vector&); + void nas_switch_threading(std::vector&,std::vector&,std::vector&); }; nasal_bytecode_vm::nasal_bytecode_vm() { local_scope_stack.push(-1); - - struct - { - int op; - void (nasal_bytecode_vm::*ptr)(); - }function_table[]= - { - {op_nop, nasal_bytecode_vm::opr_nop}, - {op_load, nasal_bytecode_vm::opr_load}, - {op_pushnum, nasal_bytecode_vm::opr_pushnum}, - {op_pushone, nasal_bytecode_vm::opr_pushone}, - {op_pushzero, nasal_bytecode_vm::opr_pushzero}, - {op_pushnil, nasal_bytecode_vm::opr_pushnil}, - {op_pushstr, nasal_bytecode_vm::opr_pushstr}, - {op_newvec, nasal_bytecode_vm::opr_newvec}, - {op_newhash, nasal_bytecode_vm::opr_newhash}, - {op_newfunc, nasal_bytecode_vm::opr_newfunc}, - {op_vecapp, nasal_bytecode_vm::opr_vecapp}, - {op_hashapp, nasal_bytecode_vm::opr_hashapp}, - {op_para, nasal_bytecode_vm::opr_para}, - {op_defpara, nasal_bytecode_vm::opr_defpara}, - {op_dynpara, nasal_bytecode_vm::opr_dynpara}, - {op_entry, nasal_bytecode_vm::opr_entry}, - {op_unot, nasal_bytecode_vm::opr_unot}, - {op_usub, nasal_bytecode_vm::opr_usub}, - {op_add, nasal_bytecode_vm::opr_add}, - {op_sub, nasal_bytecode_vm::opr_sub}, - {op_mul, nasal_bytecode_vm::opr_mul}, - {op_div, nasal_bytecode_vm::opr_div}, - {op_lnk, nasal_bytecode_vm::opr_lnk}, - {op_addeq, nasal_bytecode_vm::opr_addeq}, - {op_subeq, nasal_bytecode_vm::opr_subeq}, - {op_muleq, nasal_bytecode_vm::opr_muleq}, - {op_diveq, nasal_bytecode_vm::opr_diveq}, - {op_lnkeq, nasal_bytecode_vm::opr_lnkeq}, - {op_meq, nasal_bytecode_vm::opr_meq}, - {op_eq, nasal_bytecode_vm::opr_eq}, - {op_neq, nasal_bytecode_vm::opr_neq}, - {op_less, nasal_bytecode_vm::opr_less}, - {op_leq, nasal_bytecode_vm::opr_leq}, - {op_grt, nasal_bytecode_vm::opr_grt}, - {op_geq, nasal_bytecode_vm::opr_geq}, - {op_pop, nasal_bytecode_vm::opr_pop}, - {op_jmp, nasal_bytecode_vm::opr_jmp}, - {op_jmptrue, nasal_bytecode_vm::opr_jmptrue}, - {op_jmpfalse, nasal_bytecode_vm::opr_jmpfalse}, - {op_counter, nasal_bytecode_vm::opr_counter}, - {op_forindex, nasal_bytecode_vm::opr_forindex}, - {op_foreach, nasal_bytecode_vm::opr_foreach}, - {op_call, nasal_bytecode_vm::opr_call}, - {op_callv, nasal_bytecode_vm::opr_callv}, - {op_callvi, nasal_bytecode_vm::opr_callvi}, - {op_callh, nasal_bytecode_vm::opr_callh}, - {op_callf, nasal_bytecode_vm::opr_callf}, - {op_builtincall, nasal_bytecode_vm::opr_builtincall}, - {op_slicebegin, nasal_bytecode_vm::opr_slicebegin}, - {op_sliceend, nasal_bytecode_vm::opr_sliceend}, - {op_slice, nasal_bytecode_vm::opr_slice}, - {op_slice2, nasal_bytecode_vm::opr_slice2}, - {op_mcall, nasal_bytecode_vm::opr_mcall}, - {op_mcallv, nasal_bytecode_vm::opr_mcallv}, - {op_mcallh, nasal_bytecode_vm::opr_mcallh}, - {op_return, nasal_bytecode_vm::opr_return}, - {-1,NULL} - }; - for(int i=0;function_table[i].ptr;++i) - opr_table.push_back(NULL); - for(int i=0;function_table[i].ptr;++i) - opr_table[function_table[i].op]=function_table[i].ptr; for(int i=0;builtin_func_table[i].func_pointer;++i) builtin_func_hashmap[builtin_func_table[i].func_name]=builtin_func_table[i].func_pointer; return; } -nasal_bytecode_vm::~nasal_bytecode_vm() -{ - opr_table.clear(); - return; -} void nasal_bytecode_vm::clear() { vm.clear(); @@ -192,7 +116,6 @@ void nasal_bytecode_vm::clear() } void nasal_bytecode_vm::die(std::string str) { - ++error; std::string numinfo=""; int num=ptr; for(int i=0;i<8;++i) @@ -202,6 +125,7 @@ void nasal_bytecode_vm::die(std::string str) num>>=4; } std::cout<<">> [vm] 0x"<& strs,std::vector& { string_table=strs; number_table=nums; - int size=exec.size(); - for(int i=0;i*opr_table[exec_code[ptr].op])(); - if(error) - break; - } + time_t end_time=std::time(NULL); time_t total_run_time=end_time-begin_time; if(total_run_time>=1) diff --git a/nasal_codegen.h b/nasal_codegen.h index 6bdc3ca..0893901 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -77,9 +77,9 @@ struct {op_meq, "memeq "}, {op_eq, "eq "}, {op_neq, "neq "}, - {op_less, "l "}, + {op_less, "less "}, {op_leq, "leq "}, - {op_grt, "g "}, + {op_grt, "grt "}, {op_geq, "geq "}, {op_pop, "pop "}, {op_jmp, "jmp "}, @@ -115,7 +115,7 @@ struct opcode index=0; return; } - opcode& operator=(opcode& tmp) + opcode& operator=(const opcode& tmp) { op=tmp.op; index=tmp.index; @@ -1205,15 +1205,7 @@ void nasal_codegen::main_progress(nasal_ast& ast) void nasal_codegen::print_op(int index) { // print opcode ptr - std::string numinfo=""; - int num=index; - for(int i=0;i<8;++i) - { - int tmp=num&0x0f; - numinfo=(char)(tmp>9? 'a'+tmp-10:'0'+tmp)+numinfo; - num>>=4; - } - std::cout<<"0x"<9? 'a'+tmp-10:'0'+tmp)+numinfo; - num>>=4; - } - std::cout<<"0x"<collected) + if(0<=value_address && !garbage_collector_memory[value_address]->collected) return garbage_collector_memory[value_address]->elem; return error_returned_value; } void nasal_virtual_machine::add_reference(int value_address) { - if(0<=value_address && value_addresscollected) + if(0<=value_address && !garbage_collector_memory[value_address]->collected) ++garbage_collector_memory[value_address]->ref_cnt; return; } void nasal_virtual_machine::del_reference(int value_address) { - if(0<=value_address && value_addresscollected) + if(0<=value_address && !garbage_collector_memory[value_address]->collected) --garbage_collector_memory[value_address]->ref_cnt; else return; @@ -798,7 +798,7 @@ void nasal_virtual_machine::mem_free(int memory_address) { // mem_free has helped scalar to delete the reference // so don't need to delete reference again - if(0<=memory_address && memory_addressdel_reference(memory_manager_memory[memory_address]); memory_manager_free_space.push(memory_address); @@ -809,7 +809,7 @@ void nasal_virtual_machine::mem_change(int memory_address,int value_address) { // this progress is used to change a memory space's value address // be careful! this process doesn't check if this mem_space is in use. - if(0<=memory_address && memory_addressdel_reference(memory_manager_memory[memory_address]); memory_manager_memory[memory_address]=value_address; @@ -819,7 +819,7 @@ void nasal_virtual_machine::mem_change(int memory_address,int value_address) int nasal_virtual_machine::mem_get(int memory_address) { // be careful! this process doesn't check if this mem_space is in use. - if(0<=memory_address && memory_address1;--i) - { - if('0'<=str[i] && str[i]<='9') - ret+=num_pow*(str[i]-'0'); - else if('a'<=str[i] && str[i]<='f') - ret+=num_pow*(str[i]-'a'+10); - else if('A'<=str[i] && str[i]<='F') - ret+=num_pow*(str[i]-'A'+10); - else - return (1/0.0)+(-1/0.0); - num_pow*=16; - } - return ret; -} -inline double oct_to_double(std::string str,int len) -{ - double ret=0,num_pow=1; - for(int i=len-1;i>1;--i) - { - if('0'<=str[i] && str[i]<='8') - ret+=num_pow*(str[i]-'0'); - else - return (1/0.0)+(-1/0.0); - num_pow*=8; - } - return ret; -} -inline double dec_to_double(std::string str,int len) -{ - double ret=0; - int i=0; - while('0'<=str[i] && str[i]<='9' && i2 && str[0]=='0' && str[1]=='x') - ret_num=hex_to_double(str,len); - else if(len>2 && str[0]=='0' && str[1]=='o') - ret_num=oct_to_double(str,len); - else - ret_num=dec_to_double(str,len); - return is_negative?-ret_num:ret_num; -} - -/* - trans_number_to_string: - convert number to string -*/ -std::string trans_number_to_string(double number) -{ - std::string trans_num_string=""; - if(number<0) - { - trans_num_string+='-'; - number=-number; - } - if(number==0) - return "0"; - double integer_bit=1; - while(number>=integer_bit) - integer_bit*=10; - integer_bit/=10; - if(integer_bit==0.1) - trans_num_string+='0'; - while(integer_bit!=0.1) - { - trans_num_string+=(char)('0'+(int(number/integer_bit))); - number-=(double)(int(number/integer_bit))*integer_bit; - integer_bit/=10; - } - if(number>0.000000001) - trans_num_string+='.'; - while(number>0.000000001) - { - trans_num_string+=(char)('0'+int(number*10)); - number*=10; - number-=(double)(int(number)); - } - return trans_num_string; -} - -/* - prt_hex: - transform int to hex format and print it out (std::cout) -*/ -void prt_hex(const int ptr) -{ - char hex[9]; - hex[8]=0; - int tmp_plc=ptr; - if(tmp_plc<0) - { - tmp_plc=-tmp_plc; - std::cout<<"-0x"; - } - else - std::cout<<"0x"; - for(int j=7;j>=0;--j) - { - int tmp=(tmp_plc & 0x0000000f); - hex[j]=tmp<10? (char)('0'+tmp):(char)('a'+tmp-10); - tmp_plc>>=4; - } - std::cout<error=0; this->function_returned_address=-1;