From 6f5143657e76d149f6f7c8c0f150c6de17dc6192 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 2 Nov 2020 12:24:12 +0800 Subject: [PATCH] update --- lib.nas | 2 +- nasal_enum.h | 2 +- nasal_lexer.h | 131 +++++++++++++++++++++++++++++--------------------- 3 files changed, 78 insertions(+), 57 deletions(-) diff --git a/lib.nas b/lib.nas index 2c5c070..1ef83a7 100644 --- a/lib.nas +++ b/lib.nas @@ -86,7 +86,7 @@ var die=func(str) nasal_call_builtin_die(str); return nil; } -var type=func(object) +var typeof=func(object) { return nasal_call_builtin_type(object); } diff --git a/nasal_enum.h b/nasal_enum.h index 33db5ce..9ffa2f6 100644 --- a/nasal_enum.h +++ b/nasal_enum.h @@ -140,7 +140,7 @@ enum parse_error void error_info(int line,int error_type,std::string error_str="") { std::string detail; - std::cout<<">> [parse] error: [line "<> [parse] line "<= <= #define IS_CALC_OPERATOR(c) (c=='='||c=='+'||c=='-'||c=='*'||c=='!'||c=='/'||c=='<'||c=='>'||c=='~') -#define IS_NOTE_HEAD(c) (c=='#') +#define IS_NOTE(c) (c=='#') -#ifndef TOKEN_TABLE_SIZE -#define TOKEN_TABLE_SIZE 45 -struct token_table +struct { - std::string str; + const char* str; int tok_type; -}tok_tbl[TOKEN_TABLE_SIZE]= +}token_table[]= { {"for" ,tok_for }, {"forindex",tok_forindex }, @@ -67,8 +64,8 @@ struct token_table {">" ,tok_greater_than }, {"<=" ,tok_less_equal }, {">=" ,tok_greater_equal}, + {NULL ,-1 } }; -#endif struct token { @@ -84,15 +81,16 @@ private: int res_size; int line; int ptr; + std::string line_code; std::vector res; std::vector token_list; std::string identifier_gen(); - void generate_number_error(int,std::string); std::string number_gen(); std::string string_gen(); public: void clear(); void openfile(std::string); + void die(std::string,int,int); void scanner(); void print_token(); int get_error(); @@ -105,6 +103,7 @@ void nasal_lexer::clear() res_size=0; line=0; ptr=0; + line_code=""; res.clear(); token_list.clear(); return; @@ -134,21 +133,23 @@ void nasal_lexer::openfile(std::string filename) return; } +void nasal_lexer::die(std::string error_info,int line=-1,int column=-1) +{ + ++error; + std::cout<<">> [lexer] line "<> [lexer] line "< 0|[1~9][0~9]*(.[0~9]*)(e|E(+|-)0|[1~9][0~9]*) - if(ptr [0~9][0~9]*(.[0~9]*)(e|E(+|-)0|[1~9][0~9]*) while(ptr=res_size) { - generate_number_error(line,token_str); + line_code+=token_str; + die("["+line_code+"_] incorrect number.",line,line_code.length()); return "0"; } while(ptr=res_size) { - generate_number_error(line,token_str); + line_code+=token_str; + die("["+line_code+"_] incorrect number.",line,line_code.length()); return "0"; } if(ptr=res_size) { - generate_number_error(line,token_str); + line_code+=token_str; + die("["+line_code+"_] incorrect number.",line,line_code.length()); return "0"; } if(ptr=res_size) return token_str; while(ptr=res_size) - { - ++error; - std::cout<<">> [lexer] line "<=res_size) break; - if(IS_IDENTIFIER_HEAD(res[ptr])) + if(IS_IDENTIFIER(res[ptr])) { token_str=identifier_gen(); token new_token; new_token.line=line; new_token.str=token_str; new_token.type=0; - for(int i=0;i> [lexer] line "<