From fd8d3acfed714aa2d8d809ad5877312a1a8cf548 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 9 Nov 2020 00:26:15 +0800 Subject: [PATCH] update --- nasal_ast.h | 25 +++++++++-- nasal_codegen.h | 7 ++- nasal_misc.h | 113 ++++++++++++++---------------------------------- nasal_parse.h | 2 +- nasal_runtime.h | 44 ++++++++----------- 5 files changed, 79 insertions(+), 112 deletions(-) diff --git a/nasal_ast.h b/nasal_ast.h index 6989ca5..8c3a45c 100644 --- a/nasal_ast.h +++ b/nasal_ast.h @@ -7,6 +7,7 @@ private: int line; int type; std::string str; + double num; std::vector children; public: nasal_ast(); @@ -17,10 +18,12 @@ public: void set_line(int); void set_type(int); void set_str(std::string&); + void set_num(double); void add_child(nasal_ast); int get_line(); int get_type(); std::string get_str(); + double get_num(); std::vector& get_children(); void print_ast(int); }; @@ -29,7 +32,6 @@ nasal_ast::nasal_ast() { this->line=0; this->type=ast_null; - this->str=""; return; } @@ -38,13 +40,13 @@ nasal_ast::nasal_ast(const nasal_ast& tmp) this->line=tmp.line; this->type=tmp.type; this->str=tmp.str; + this->num=tmp.num; this->children=tmp.children; return; } nasal_ast::~nasal_ast() { - this->str.clear(); this->children.clear(); return; } @@ -54,6 +56,7 @@ nasal_ast& nasal_ast::operator=(const nasal_ast& tmp) this->line=tmp.line; this->type=tmp.type; this->str=tmp.str; + this->num=tmp.num; this->children=tmp.children; return *this; } @@ -61,8 +64,9 @@ nasal_ast& nasal_ast::operator=(const nasal_ast& tmp) void nasal_ast::clear() { this->line=0; - this->type=ast_null; this->str=""; + this->num=0; + this->type=ast_null; this->children.clear(); return; } @@ -85,6 +89,12 @@ void nasal_ast::set_str(std::string& s) return; } +void nasal_ast::set_num(double n) +{ + this->num=n; + return; +} + void nasal_ast::add_child(nasal_ast ast) { children.push_back(ast); @@ -106,6 +116,11 @@ std::string nasal_ast::get_str() return this->str; } +double nasal_ast::get_num() +{ + return this->num; +} + std::vector& nasal_ast::get_children() { return this->children; @@ -117,8 +132,10 @@ void nasal_ast::print_ast(int depth) for(int i=0;itype); std::cout<type==ast_number || this->type==ast_string || this->type==ast_identifier || this->type==ast_dynamic_id || this->type==ast_call_hash) + if(this->type==ast_string || this->type==ast_identifier || this->type==ast_dynamic_id || this->type==ast_call_hash) std::cout<<":"<str; + else if(this->type==ast_number) + std::cout<<":"<num; std::cout<children.size(); for(int i=0;i'7') - return false; - return true; -} -inline bool check_dec_string(std::string str,int len) -{ - int i=0; - // check integer part - while('0'<=str[i] && str[i]<='9' && i'9') - return false; - } - return true; -} -bool check_numerable_string(std::string str) -{ - int len=str.length(); - if(!len) return false; - if(str[0]=='-' || str[0]=='+') - { - if(len==1) return false; - std::string tmp=""; - for(int i=1;i2 && str[0]=='0' && str[1]=='x') - return check_hex_string(str,len); - else if(len>2 && str[0]=='0' && str[1]=='o') - return check_oct_string(str,len); - else if('0'<=str[0] && str[0]<='9') - return check_dec_string(str,len); - return false; -} - -/* - trans_string_to_number: - convert string to number + if this string cannot be converted to a number,it will return nan */ inline double hex_to_double(std::string str,int len) { @@ -100,6 +28,8 @@ inline double hex_to_double(std::string str,int len) 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; @@ -109,7 +39,10 @@ inline double oct_to_double(std::string str,int len) double ret=0,num_pow=1; for(int i=len-1;i>1;--i) { - ret+=num_pow*(str[i]-'0'); + 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; @@ -124,9 +57,13 @@ inline double dec_to_double(std::string str,int len) ++i; } if(i==len) return ret; + if(str[i]!='.' && str[i]!='e' && str[i]!='E') + return (1/0.0)+(-1/0.0); if(str[i]=='.') { ++i; + if(i==len) + return (1/0.0)+(-1/0.0); double num_pow=0.1; 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 if('0'<=str[0] && str[0]<='9') + else ret_num=dec_to_double(str,len); return is_negative?-ret_num:ret_num; } diff --git a/nasal_parse.h b/nasal_parse.h index f2ae575..0317ab9 100644 --- a/nasal_parse.h +++ b/nasal_parse.h @@ -290,7 +290,7 @@ nasal_ast nasal_parse::number_gen() nasal_ast node; node.set_line(tok_list[ptr].line); node.set_type(ast_number); - node.set_str(tok_list[ptr].str); + node.set_num(trans_string_to_number(tok_list[ptr].str)); return node; } diff --git a/nasal_runtime.h b/nasal_runtime.h index 86bc7b7..ddd580a 100644 --- a/nasal_runtime.h +++ b/nasal_runtime.h @@ -24,10 +24,6 @@ private: // if error occurred,this value will add 1 int error; - // generate number and return gc place of this number - int number_generation(nasal_ast&); - // generate string and return gc place of this string - int string_generation(nasal_ast&); // generate vector and return gc place of this vector int vector_generation(nasal_ast&,int); // generate hash and return gc place of this hash @@ -206,20 +202,6 @@ void nasal_runtime::run() } // private functions -int nasal_runtime::number_generation(nasal_ast& node) -{ - int new_addr=nasal_vm.gc_alloc(); - nasal_vm.gc_get(new_addr).set_type(vm_number); - nasal_vm.gc_get(new_addr).set_number(trans_string_to_number(node.get_str())); - return new_addr; -} -int nasal_runtime::string_generation(nasal_ast& node) -{ - int new_addr=nasal_vm.gc_alloc(); - nasal_vm.gc_get(new_addr).set_type(vm_string); - nasal_vm.gc_get(new_addr).set_string(node.get_str()); - return new_addr; -} int nasal_runtime::vector_generation(nasal_ast& node,int local_scope_addr) { int new_addr=nasal_vm.gc_alloc(); @@ -1154,25 +1136,27 @@ int nasal_runtime::call_scalar_mem(nasal_ast& node,int local_scope_addr) int mem_address=-1; if(node.get_type()==ast_identifier) { + std::string id_name=node.get_str(); if(local_scope_addr>=0) - mem_address=nasal_vm.gc_get(local_scope_addr).get_closure().get_mem_address(node.get_str()); + mem_address=nasal_vm.gc_get(local_scope_addr).get_closure().get_mem_address(id_name); if(mem_address<0) - mem_address=nasal_vm.gc_get(global_scope_address).get_closure().get_mem_address(node.get_str()); + mem_address=nasal_vm.gc_get(global_scope_address).get_closure().get_mem_address(id_name); if(mem_address<0) { - std::cout<<">> [runtime] call_scalar_mem: cannot find value named \'"<> [runtime] call_scalar_mem: cannot find value named \'"<=0) - mem_address=nasal_vm.gc_get(local_scope_addr).get_closure().get_mem_address(node.get_children()[0].get_str()); + mem_address=nasal_vm.gc_get(local_scope_addr).get_closure().get_mem_address(id_name); if(mem_address<0) - mem_address=nasal_vm.gc_get(global_scope_address).get_closure().get_mem_address(node.get_children()[0].get_str()); + mem_address=nasal_vm.gc_get(global_scope_address).get_closure().get_mem_address(id_name); if(mem_address<0) { - std::cout<<">> [runtime] call_scalar_mem: cannot find value named \'"<> [runtime] call_scalar_mem: cannot find value named \'"<=0)