From 1b240b293e8cbc905be9b3eae7a688a041488bbe Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sat, 16 Oct 2021 14:07:55 +0800 Subject: [PATCH] update variable name in nasal_lexer --- .gitignore | 3 + nasal_lexer.h | 240 ++++++++++++++++++++++++-------------------------- stl/list.nas | 33 ++----- stl/queue.nas | 26 ++---- stl/stack.nas | 31 +++---- 5 files changed, 145 insertions(+), 188 deletions(-) diff --git a/.gitignore b/.gitignore index 259148f..af58d4b 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ *.exe *.out *.app + +nasal +.vscode \ No newline at end of file diff --git a/nasal_lexer.h b/nasal_lexer.h index 714048d..9508fae 100644 --- a/nasal_lexer.h +++ b/nasal_lexer.h @@ -1,11 +1,11 @@ #ifndef __NASAL_LEXER_H__ #define __NASAL_LEXER_H__ -#define IS_IDENTIFIER(c) ((c=='_')||('a'<=c && c<='z')||('A'<=c&&c<='Z')) -#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(c) (c=='\''||c=='\"'||c=='`') +#define IS_ID(c) ((c=='_')||('a'<=c && c<='z')||('A'<=c&&c<='Z')) +#define IS_HEX(c) (('0'<=c&&c<='9')||('a'<=c&&c<='f')||('A'<=c && c<='F')) +#define IS_OCT(c) ('0'<=c&&c<='7') +#define IS_DIGIT(c) ('0'<=c&&c<='9') +#define IS_STR(c) (c=='\''||c=='\"'||c=='`') // single operators have only one character #define IS_SINGLE_OPERATOR(c) (c=='('||c==')'||c=='['||c==']'||c=='{'||c=='}'||c==','||c==';'||c=='|'||c==':'||\ c=='?'||c=='`'||c=='&'||c=='@'||c=='%'||c=='$'||c=='^'||c=='\\') @@ -34,7 +34,7 @@ enum token_type struct { const char* str; - const int tok_type; + const uint32_t tok_type; }token_table[]= { {"for" ,tok_for }, @@ -82,7 +82,7 @@ struct {"<=" ,tok_leq }, {">" ,tok_grt }, {">=" ,tok_geq }, - {nullptr ,-1 } + {nullptr ,0 } }; struct token @@ -99,10 +99,10 @@ private: uint32_t error; uint32_t line; uint32_t ptr; - size_t res_size; - std::string line_code; + size_t size; + std::string code; std::string res; - std::vector token_list; + std::vector tokens; uint32_t get_type(const std::string&); void die(const char*); std::string id_gen(); @@ -113,7 +113,7 @@ public: void scan(); void print(); uint32_t err(){return error;} - const std::vector& get_tokens(){return token_list;} + const std::vector& get_tokens(){return tokens;} }; void nasal_lexer::open(const std::string& filename) @@ -125,19 +125,10 @@ void nasal_lexer::open(const std::string& filename) { ++error; std::cout<<"[lexer] cannot open file <"<.\n"; - return; } std::stringstream ss; ss< [0~9][0~9]*(.[0~9]*)(e|E(+|-)0|[1~9][0~9]*) - std::string token_str=""; - while(ptr=res_size) + if(ptr++>=size) die("get EOF when generating string."); - if(str_begin=='`' && token_str.length()!=1) + code+=res[ptr-1]; + if(begin=='`' && str.length()!=1) die("\'`\' is used for string that includes one character."); - return token_str; + return str; } void nasal_lexer::scan() { - token_list.clear(); + tokens.clear(); line=1; ptr=0; - line_code=""; - res_size=res.size(); + code=""; + size=res.size(); - std::string token_str; - while(ptr=res_size) break; - if(IS_IDENTIFIER(res[ptr])) + if(ptr>=size) break; + if(IS_ID(res[ptr])) { - token_str=id_gen(); - token_list.push_back({line,get_type(token_str),token_str}); - if(!token_list.back().type) - token_list.back().type=tok_id; + str=id_gen(); + tokens.push_back({line,get_type(str),str}); + if(!tokens.back().type) + tokens.back().type=tok_id; } else if(IS_DIGIT(res[ptr])) - token_list.push_back({line,tok_num,num_gen()}); - else if(IS_STRING(res[ptr])) - token_list.push_back({line,tok_str,str_gen()}); + tokens.push_back({line,tok_num,num_gen()}); + else if(IS_STR(res[ptr])) + tokens.push_back({line,tok_str,str_gen()}); else if(IS_SINGLE_OPERATOR(res[ptr])) { - token_str=res[ptr]; - line_code+=res[ptr]; - uint32_t type=get_type(token_str); + str=res[ptr]; + code+=res[ptr]; + uint32_t type=get_type(str); if(!type) die("incorrect operator."); - token_list.push_back({line,type,token_str}); + tokens.push_back({line,type,str}); ++ptr; } else if(res[ptr]=='.') { - if(ptr+2