From 9e400686bb9feb5cf1793d9a264041da1ee4356b Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Wed, 11 Sep 2019 07:42:25 -0500 Subject: [PATCH] Bug fixed --- .../version0.16.1/abstract_syntax_tree.h | 106 +++++++++++--- version0.16/version0.16.1/ast_generator.h | 136 +++++++++++++++++- version0.16/version0.16.1/main.cpp | 6 +- version0.16/version0.16.1/nasal_lexer.h | 2 +- version0.16/version0.16.1/nasal_parser.h | 3 +- version0.16/version0.16.1/nasal_token_type.h | 2 + 6 files changed, 228 insertions(+), 27 deletions(-) diff --git a/version0.16/version0.16.1/abstract_syntax_tree.h b/version0.16/version0.16.1/abstract_syntax_tree.h index 0beb619..cf03b19 100644 --- a/version0.16/version0.16.1/abstract_syntax_tree.h +++ b/version0.16/version0.16.1/abstract_syntax_tree.h @@ -22,16 +22,52 @@ class ast_tree_node fnum=0; str=""; } + void set_line(int _line) + { + line=_line; + return; + } + int return_type() + { + return type; + } void add_child(ast_tree_node& new_child) { children.push_back(new_child); return; } - void print() + void clear_tree() { + line=0; + type=__root; + children.clear(); + + num=0; + fnum=0; + str=""; + return; + } + int child_num() + { + int cnt=0; + for(std::list::iterator i=children.begin();i!=children.end();++i) + ++cnt; + return cnt; + } + void print(int tab_num) + { + for(int i=0;i::iterator i=children.begin();i!=children.end();++i) - i->print(); + i->print(tab_num+1); + for(int i=0;i=0;--i) + { + num+=acc*((double)(str[i]-'0')); + acc*=10; + } + } + else + { + fnum=0; + double acc=1; + double aff=0.1; + for(int i=DotPlace+1;i<(int)str.length();++i) + { + fnum+=aff*((double)(str[i]-'0')); + aff*=0.1; + } + for(int i=DotPlace-1;i>=0;--i) + { + fnum+=acc*((double)(str[i]-'0')); + acc*=10; + } + } + return; + } }; class string_expr:public ast_tree_node { @@ -98,7 +162,7 @@ class string_expr:public ast_tree_node { type=__string; } - void setstr(std::string& t) + void set_str(std::string& t) { str=t; return; diff --git a/version0.16/version0.16.1/ast_generator.h b/version0.16/version0.16.1/ast_generator.h index 8b46bcf..dd9a44e 100644 --- a/version0.16/version0.16.1/ast_generator.h +++ b/version0.16/version0.16.1/ast_generator.h @@ -6,10 +6,102 @@ class ast_generator private: ast_tree_node root; std::stack parse; + std::stack node_cache; + bool can_run; public: ast_generator() { - ; + can_run=true; + } + bool number_expr_gen() + { + number_expr t; + t.set_line(parse.top().line); + t.set_number(parse.top().content); + if(node_cache.empty()) + { + node_cache.push(t); + return true; + } + int type=node_cache.top().return_type(); + if((type==__add_operator || type==__sub_operator || type==__mul_operator || type==__div_operator || type==__link_operator) && node_cache.top().child_num()<=1) + { + node_cache.top().add_child(t); + return true; + } + else + { + node_cache.push(t); + return true; + } + return true; + } + bool string_expr_gen() + { + string_expr t; + t.set_line(parse.top().line); + std::string tstr=parse.top().content; + std::string str=""; + for(int i=1;i<(int)tstr.length()-1;++i) + str+=tstr[i]; + t.set_str(str); + if(node_cache.empty()) + { + node_cache.push(t); + return true; + } + int type=node_cache.top().return_type(); + if((type==__add_operator || type==__sub_operator || type==__mul_operator || type==__div_operator || type==__link_operator) && node_cache.top().child_num()<2) + { + node_cache.top().add_child(t); + return true; + } + else + { + node_cache.push(t); + return true; + } + return true; + } + bool operator_expr_gen() + { + operator_expr t; + t.set_line(parse.top().line); + t.set_operator_type(parse.top().type); + if(node_cache.empty()) + { + if(t.return_type()==__add_operator || t.return_type()==__sub_operator) + { + number_expr nullnum; + nullnum.set_line(parse.top().line); + std::string strnum="0"; + nullnum.set_number(strnum); + t.add_child(nullnum); + node_cache.push(t); + return true; + } + else + { + std::cout<<">>[Error] parse error in line "<& temp) { @@ -18,10 +110,52 @@ class ast_generator } void gen_main_prog() { + can_run=true; + root.clear_tree(); + while(!parse.empty()) + { + int type=parse.top().type; + bool is_correct=false; + switch(type) + { + case __number:is_correct=number_expr_gen();break; + case __string:is_correct=string_expr_gen();break; + case __add_operator:is_correct=operator_expr_gen();break; + case __sub_operator:is_correct=operator_expr_gen();break; + case __mul_operator:is_correct=operator_expr_gen();break; + case __div_operator:is_correct=operator_expr_gen();break; + case __link_operator:is_correct=operator_expr_gen();break; + case __semi:is_correct=true;break; + default: + is_correct=false; + std::cout<<">>[Error] parse error in line "< temp_cache; + while(!node_cache.empty()) + { + temp_cache.push(node_cache.top()); + node_cache.pop(); + } + while(!temp_cache.empty()) + { + root.add_child(temp_cache.top()); + temp_cache.pop(); + } return; } void run() { + if(can_run) + root.print(0); + else + std::cout<<">>[Parse] Error(s) occurred,stop."<