From 2cfbbcb653554c60b6eedc522609cc0c5bb1743e Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Fri, 16 Oct 2020 07:59:52 -0700 Subject: [PATCH] update --- version3.0/nasal_lexer.h | 110 +++++++++++++++++++++++++++---------- version3.0/nasal_misc.h | 80 ++++++++++++--------------- version3.0/nasal_runtime.h | 20 ++++--- 3 files changed, 128 insertions(+), 82 deletions(-) diff --git a/version3.0/nasal_lexer.h b/version3.0/nasal_lexer.h index ffacc49..23954a9 100644 --- a/version3.0/nasal_lexer.h +++ b/version3.0/nasal_lexer.h @@ -1,15 +1,15 @@ #ifndef __NASAL_LEXER_H__ #define __NASAL_LEXER_H__ -#define IS_IDENTIFIER_HEAD(c) (c=='_')||('a'<=c && c<='z')||('A'<=c&&c<='Z') -#define IS_IDENTIFIER_BODY(c) (c=='_')||('a'<=c && c<='z')||('A'<=c&&c<='Z')||('0'<=c&&c<='9') -#define IS_NUMBER_HEAD(c) ('0'<=c&&c<='9') -#define IS_NUMBER_BODY(c) ('0'<=c&&c<='9')||('a'<=c&&c<='f')||('A'<=c&&c<='F')||(c=='e'||c=='E'||c=='.'||c=='x'||c=='o') +#define IS_IDENTIFIER_HEAD(c) ((c=='_')||('a'<=c && c<='z')||('A'<=c&&c<='Z')) +#define IS_IDENTIFIER_BODY(c) ((c=='_')||('a'<=c && c<='z')||('A'<=c&&c<='Z')||('0'<=c&&c<='9')) +#define IS_HEX_NUMBER(c) (('0'<=c&&c<='9')||('a'<=c&&c<='f')||('A'<=c && c<='F')) +#define IS_OCT_NUMEBR(c) ('0'<=c&&c<='7') +#define IS_DIGIT(c) ('0'<=c&&c<='9') #define IS_STRING_HEAD(c) (c=='\''||c=='\"') // single operators have only one character #define IS_SINGLE_OPRATOR(c) (c=='('||c==')'||c=='['||c==']'||c=='{'||c=='}'||c==','||c==';'||c=='|'||c==':'||\ c=='?'||c=='`'||c=='&'||c=='@'||c=='%'||c=='$'||c=='^'||c=='\\') -#define IS_DOT(c) (c=='.') // calculation operators may have two chars, for example: += -= *= /= ~= != == >= <= #define IS_CALC_OPERATOR(c) (c=='='||c=='+'||c=='-'||c=='*'||c=='!'||c=='/'||c=='<'||c=='>'||c=='~') #define IS_NOTE_HEAD(c) (c=='#') @@ -83,6 +83,7 @@ private: int error; std::vector token_list; std::string identifier_gen(std::vector&,int&,int&); + void generate_number_error(int,std::string); std::string number_gen(std::vector&,int&,int&); std::string string_gen(std::vector&,int&,int&); public: @@ -109,42 +110,95 @@ std::string nasal_lexer::identifier_gen(std::vector& res,int& ptr,int& lin // after running this process, ptr will point to the next token's beginning character } +void nasal_lexer::generate_number_error(int line,std::string token_str) +{ + ++error; + std::cout<<">> [lexer] line "<& res,int& ptr,int& line) { int res_size=res.size(); bool scientific_notation=false;// numbers like 1e8 are scientific_notation - bool is_hex=(ptr 0|[1~9][0~9]*(.[0~9]*)(e|E(+|-)0|[1~9][0~9]*) + if(ptr=res_size) { - token_str+=res[ptr]; - ++ptr; + generate_number_error(line,token_str); + return "0"; + } + while(ptr> [lexer] line "<=res_size) + { + generate_number_error(line,token_str); + return "0"; + } + if(ptr=res_size) + { + generate_number_error(line,token_str); + return "0"; + } + if(ptr& res) new_token.type=tok_identifier; token_list.push_back(new_token); } - else if(IS_NUMBER_HEAD(res[ptr])) + else if(IS_DIGIT(res[ptr])) { token_str=number_gen(res,ptr,line); token new_token; @@ -252,9 +306,9 @@ void nasal_lexer::scanner(std::vector& res) token_list.push_back(new_token); ++ptr; } - else if(IS_DOT(res[ptr])) + else if(res[ptr]=='.') { - if(ptr+2'9' ) return false; + ++i; + if(i==len) return false; + while('0'<=str[i] && str[i]<='9' && i'9') return false; } - if(dot_cnt>1) return false; return true; } @@ -67,18 +67,16 @@ bool check_numerable_string(std::string str) { int len=str.length(); if(!len) return false; - if(str[0]=='-' && len>1) + if(str[0]=='-' || str[0]=='+') { + if(len==1) return false; std::string tmp=""; for(int i=1;i2 && str[0]=='0' && str[1]=='x') + if(len>2 && 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); @@ -93,8 +91,7 @@ bool check_numerable_string(std::string str) */ inline double hex_to_double(std::string str,int len) { - double ret=0; - double num_pow=1; + double ret=0,num_pow=1; for(int i=len-1;i>1;--i) { if('0'<=str[i] && str[i]<='9') @@ -109,8 +106,7 @@ inline double hex_to_double(std::string str,int len) } inline double oct_to_double(std::string str,int len) { - double ret=0; - double num_pow=1; + double ret=0,num_pow=1; for(int i=len-1;i>1;--i) { ret+=num_pow*(str[i]-'0'); @@ -122,34 +118,29 @@ inline double dec_to_double(std::string str,int len) { double ret=0; int i=0; - for(;i1) + if(str[0]=='-' || str[0]=='+') { - is_negative=true; + is_negative=(str[0]=='-'); std::string tmp=""; for(int i=1;i2 && str[0]=='0' && str[1]=='x') + if(len>2 && 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); diff --git a/version3.0/nasal_runtime.h b/version3.0/nasal_runtime.h index 38159b4..a6e8d64 100644 --- a/version3.0/nasal_runtime.h +++ b/version3.0/nasal_runtime.h @@ -56,7 +56,7 @@ private: int call_vector(nasal_ast&,int,int); int call_hash(nasal_ast&,int,int); int call_function(nasal_ast&,std::string,int,int,int); - int call_builtin_function(nasal_ast&,int); + int call_builtin_function(std::string,int); // get scalars' memory place in complex data structure like vector/hash/function/closure(scope) int call_scalar_mem(nasal_ast&,int); int call_vector_mem(nasal_ast&,int,int); @@ -193,7 +193,9 @@ void nasal_runtime::run() nasal_vm.del_reference(global_scope_address); nasal_vm.clear(); - std::cout<<">> [runtime] process exited after "<=1) + std::cout<<">> [runtime] process exited after "<=0) return value_address; } if(value_address<0) { - std::cout<<">> [runtime] call_nasal_scalar: cannot find value named \'"<> [runtime] call_scalar: call "<> [runtime] call_scalar: cannot find value named \""<*builtin_func_hashmap[builtin_name])(local_scope_addr); + if(builtin_func_hashmap.find(val_name)!=builtin_func_hashmap.end()) + ret_value_addr=(this->*builtin_func_hashmap[val_name])(local_scope_addr); return ret_value_addr; } int nasal_runtime::call_scalar_mem(nasal_ast& node,int local_scope_addr)