From 73c9f98f4faf89d12f979f759ebcbfeb216781e3 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 14 Dec 2020 00:10:31 +0800 Subject: [PATCH] update --- nasal_ast.h | 8 +- nasal_lexer.h | 4 +- nasal_parse.h | 295 ++++++++++++-------------------------------------- 3 files changed, 77 insertions(+), 230 deletions(-) diff --git a/nasal_ast.h b/nasal_ast.h index 8d175c7..ad6f4c2 100644 --- a/nasal_ast.h +++ b/nasal_ast.h @@ -97,7 +97,7 @@ private: double num; std::vector children; public: - nasal_ast(); + nasal_ast(int,int); nasal_ast(const nasal_ast&); ~nasal_ast(); nasal_ast& operator=(const nasal_ast&); @@ -115,10 +115,10 @@ public: void print_ast(int); }; -nasal_ast::nasal_ast() +nasal_ast::nasal_ast(int init_line=0,int init_type=ast_null) { - this->line=0; - this->type=ast_null; + this->line=init_line; + this->type=init_type; return; } diff --git a/nasal_lexer.h b/nasal_lexer.h index 96ca37d..ed9d5bd 100644 --- a/nasal_lexer.h +++ b/nasal_lexer.h @@ -27,7 +27,7 @@ enum token_type tok_colon,tok_add,tok_sub,tok_mult,tok_div,tok_link,tok_not, tok_equal, tok_add_equal,tok_sub_equal,tok_mult_equal,tok_div_equal,tok_link_equal, - tok_cmp_equal,tok_cmp_not_equal,tok_less_than,tok_greater_than,tok_less_equal,tok_greater_equal + tok_cmp_equal,tok_cmp_not_equal,tok_less_than,tok_less_equal,tok_greater_than,tok_greater_equal }; struct @@ -78,8 +78,8 @@ struct {"==" ,tok_cmp_equal }, {"!=" ,tok_cmp_not_equal}, {"<" ,tok_less_than }, + {"<=" ,tok_less_equal }, {">" ,tok_greater_than }, - {"<=" ,tok_less_equal }, {">=" ,tok_greater_equal}, {NULL ,-1 } }; diff --git a/nasal_parse.h b/nasal_parse.h index f76e672..ab1f376 100644 --- a/nasal_parse.h +++ b/nasal_parse.h @@ -282,53 +282,40 @@ bool nasal_parse::need_semi_check(nasal_ast& node) nasal_ast nasal_parse::null_node_gen() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_null); + nasal_ast node(tok_list[ptr].line,ast_null); return node; } nasal_ast nasal_parse::nil_gen() { - nasal_ast node; - if(ptr>=tok_list_size) return node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_nil); + nasal_ast node(tok_list[ptr].line,ast_nil); return node; } nasal_ast nasal_parse::number_gen() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_number); + nasal_ast node(tok_list[ptr].line,ast_number); node.set_num(trans_string_to_number(tok_list[ptr].str)); return node; } nasal_ast nasal_parse::string_gen() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_string); + nasal_ast node(tok_list[ptr].line,ast_string); node.set_str(tok_list[ptr].str); return node; } nasal_ast nasal_parse::id_gen() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_identifier); + nasal_ast node(tok_list[ptr].line,ast_identifier); node.set_str(tok_list[ptr].str); return node; } nasal_ast nasal_parse::vector_gen() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_vector); + nasal_ast node(tok_list[ptr].line,ast_vector); ++ptr; while(ptr=tok_list_size) { @@ -416,19 +396,12 @@ nasal_ast nasal_parse::func_gen() nasal_ast null_argument_list; node.add_child(null_argument_list); } - if(ptr>=tok_list_size) - { - die(error_line,"expected expression block"); - return node; - } node.add_child(exprs_gen()); return node; } nasal_ast nasal_parse::args_list_gen() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_args); + nasal_ast node(tok_list[ptr].line,ast_args); ++ptr; while(ptr argname_table; for(int i=0;i=tok_list_size || tok_list[ptr].type!=tok_colon) { @@ -653,23 +605,10 @@ nasal_ast nasal_parse::calculation() return node; } ++ptr; - if(ptr=tok_list_size || tok_list[ptr].type!=tok_right_curve) die(error_line,"expected \")\""); @@ -1278,9 +1164,7 @@ nasal_ast nasal_parse::call_scalar() } nasal_ast nasal_parse::call_hash() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_call_hash); + nasal_ast node(tok_list[ptr].line,ast_call_hash); ++ptr; if(ptr=tok_list_size) + return node; + nasal_ast tmp(node.get_line(),ast_subvec); + tmp.add_child(node); + if(tok_list[ptr].type==tok_comma || tok_list[ptr].type==tok_right_bracket) { - tmp.set_line(node.get_line()); - tmp.set_type(ast_subvec); - tmp.add_child(node); - if(tok_list[ptr].type==tok_comma || tok_list[ptr].type==tok_right_bracket) - { - --ptr; - tmp.add_child(nil_gen()); - } - else - tmp.add_child(calculation()); - node=tmp; + --ptr; + tmp.add_child(nil_gen()); } + else + tmp.add_child(calculation()); + node=tmp; } else --ptr; @@ -1366,9 +1243,7 @@ nasal_ast nasal_parse::subvec() } nasal_ast nasal_parse::definition() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_definition); + nasal_ast node(tok_list[ptr].line,ast_definition); if(tok_list[ptr].type==tok_var) { ++ptr; @@ -1469,9 +1344,7 @@ nasal_ast nasal_parse::multi_id() nasal_ast nasal_parse::multi_scalar(bool check_call_memory) { // if check_call_memory is true,we will check if value called here can reach a memory space - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_multi_scalar); + nasal_ast node(tok_list[ptr].line,ast_multi_scalar); ++ptr; while(ptr=tok_list_size || tok_list[ptr].type!=tok_equal) @@ -1536,9 +1407,7 @@ nasal_ast nasal_parse::loop() } nasal_ast nasal_parse::while_loop() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_while); + nasal_ast node(tok_list[ptr].line,ast_while); ++ptr; if(ptr=tok_list_size || tok_list[ptr].type!=tok_left_curve) { @@ -1586,14 +1453,7 @@ nasal_ast nasal_parse::for_loop() else if(tok_list[ptr].type==tok_var) node.add_child(definition()); else if(tok_list[ptr].type==tok_left_curve) - { - if(check_multi_definition()) - node.add_child(definition()); - else if(check_multi_scalar()) - node.add_child(multi_assgin()); - else - node.add_child(calculation()); - } + node.add_child(check_multi_definition()?definition():(check_multi_scalar()?multi_assgin():calculation())); else node.add_child(calculation()); ++ptr; @@ -1648,8 +1508,7 @@ nasal_ast nasal_parse::for_loop() } nasal_ast nasal_parse::forei_loop() { - nasal_ast node; - node.set_line(tok_list[ptr].line); + nasal_ast node(tok_list[ptr].line); switch(tok_list[ptr].type) { case tok_forindex: node.set_type(ast_forindex);break; @@ -1696,10 +1555,9 @@ nasal_ast nasal_parse::forei_loop() nasal_ast nasal_parse::new_iter_gen() { - nasal_ast node; + nasal_ast node(tok_list[ptr].line); if(tok_list[ptr].type==tok_var) { - node.set_line(tok_list[ptr].line); node.set_type(ast_new_iter); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) @@ -1711,7 +1569,6 @@ nasal_ast nasal_parse::new_iter_gen() } else { - node.set_line(tok_list[ptr].line); node.set_type(ast_call); node.add_child(id_gen()); ++ptr; @@ -1727,12 +1584,8 @@ nasal_ast nasal_parse::new_iter_gen() nasal_ast nasal_parse::conditional() { - nasal_ast node; - nasal_ast tmp; - node.set_line(tok_list[ptr].line); - node.set_type(ast_conditional); - tmp.set_line(tok_list[ptr].line); - tmp.set_type(ast_if); + nasal_ast node(tok_list[ptr].line,ast_conditional); + nasal_ast tmp(tok_list[ptr].line,ast_if); ++ptr; if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) { @@ -1798,23 +1651,17 @@ nasal_ast nasal_parse::conditional() } nasal_ast nasal_parse::continue_expr() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_continue); + nasal_ast node(tok_list[ptr].line,ast_continue); return node; } nasal_ast nasal_parse::break_expr() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_break); + nasal_ast node(tok_list[ptr].line,ast_break); return node; } nasal_ast nasal_parse::return_expr() { - nasal_ast node; - node.set_line(tok_list[ptr].line); - node.set_type(ast_return); + nasal_ast node(tok_list[ptr].line,ast_return); ++ptr; if(ptr