From c0147bcdfae4bec75ad9745a518388a19e27c815 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Fri, 31 Jan 2020 17:55:17 +0800 Subject: [PATCH] update --- version2.0/abstract_syntax_tree.h | 19 +++++++- version2.0/main.cpp | 9 ++-- version2.0/nasal_symbol_table.h | 80 +++++++++++++++++++++++++++++-- 3 files changed, 100 insertions(+), 8 deletions(-) diff --git a/version2.0/abstract_syntax_tree.h b/version2.0/abstract_syntax_tree.h index 0ed3dea..ba7f59b 100644 --- a/version2.0/abstract_syntax_tree.h +++ b/version2.0/abstract_syntax_tree.h @@ -7,6 +7,7 @@ class abstract_syntax_tree // basic elements int line; int symbol_number; + bool symbol_is_global; int node_type; std::list children; @@ -35,6 +36,7 @@ class abstract_syntax_tree void set_var_string(std::string); void set_var_number(std::string); void set_var_name(std::string); + void set_var_scope(bool);// true means it exists in global scope void add_children(abstract_syntax_tree); // get @@ -44,6 +46,7 @@ class abstract_syntax_tree double get_var_number(); std::string get_var_string(); std::string get_var_name(); + bool get_var_scope(); std::list& get_children(); }; @@ -52,6 +55,7 @@ abstract_syntax_tree::abstract_syntax_tree() node_type=__null_type; line=0; symbol_number=-1; + symbol_is_global=false; var_number=0; var_string=""; var_name=""; @@ -64,6 +68,7 @@ abstract_syntax_tree::abstract_syntax_tree(const abstract_syntax_tree& tmp) node_type=tmp.node_type; line=tmp.line; symbol_number=tmp.symbol_number; + symbol_is_global=tmp.symbol_is_global; var_number=tmp.var_number; var_string=tmp.var_string; var_name=tmp.var_name; @@ -82,6 +87,7 @@ abstract_syntax_tree& abstract_syntax_tree::operator=(const abstract_syntax_tree node_type=tmp.node_type; line=tmp.line; symbol_number=tmp.symbol_number; + symbol_is_global=tmp.symbol_is_global; var_number=tmp.var_number; var_string=tmp.var_string; var_name=tmp.var_name; @@ -105,7 +111,7 @@ void abstract_syntax_tree::print_tree(const int n) case __dynamic_id: case __call_vector: case __call_hash: - case __call_function:std::cout<<": "<& abstract_syntax_tree::get_children() { return children; diff --git a/version2.0/main.cpp b/version2.0/main.cpp index 1fb4f88..be9770c 100644 --- a/version2.0/main.cpp +++ b/version2.0/main.cpp @@ -106,7 +106,11 @@ int main() parser.get_token_list(lexer.get_detail_token_list()); parser.main_generate(); if(!parser.get_error()) + { + symtable.set_scope_clear(); + symtable.symbol_table_main_generate(parser.get_root()); parser.get_root().print_tree(1); + } else std::cout<<">>[Parse] error occurred,stop."<>[Symbol] error occurred,stop."<get_children().front().get_var_name(); this->global_scope_add_symbol(tmp); i->get_children().front().set_symbol_number(this->search_symbol_id(tmp)); + i->get_children().front().set_var_scope(true); this->symbol_table_block_generate(i->get_children().back()); } else @@ -227,16 +228,60 @@ void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node) if(node.get_node_type()==__function) { this->local_list_add_scope(); - + if(node.get_children().front().get_node_type()==__parameters) + for(std::list::iterator i=node.get_children().front().get_children().begin();i!=node.get_children().front().get_children().end();++i) + { + if(i->get_node_type()==__id || i->get_node_type()==__dynamic_id) + { + symbol_table_unit tmp; + tmp.symbol_line=i->get_node_line(); + tmp.symbol_name=i->get_var_name(); + this->local_scope_add_symbol(tmp); + i->set_symbol_number(this->search_symbol_id(tmp)); + i->set_var_scope(false); + } + else + { + symbol_table_unit tmp; + tmp.symbol_line=i->get_children().front().get_node_line(); + tmp.symbol_name=i->get_children().front().get_var_name(); + this->local_scope_add_symbol(tmp); + i->get_children().front().set_symbol_number(this->search_symbol_id(tmp)); + i->get_children().front().set_var_scope(false); + this->symbol_table_block_generate(i->get_children().back()); + } + } + this->symbol_table_block_generate(node.get_children().back()); this->local_list_del_scope(); } else if(node.get_node_type()==__conditional) { - + for(std::list::iterator i=node.get_children().begin();i!=node.get_children().end();++i) + { + this->local_list_add_scope(); + if(i->get_node_type()==__if || i->get_node_type()==__elsif) + this->symbol_table_block_generate(i->get_children().front()); + this->symbol_table_block_generate(i->get_children().back()); + this->local_list_del_scope(); + } } else if(node.get_node_type()==__normal_statement_block) { - + for(std::list::iterator i=node.get_children().begin();i!=node.get_children().end();++i) + { + if(i->get_node_type()==__definition) + { + symbol_table_unit tmp; + tmp.symbol_line=i->get_children().front().get_node_line(); + tmp.symbol_name=i->get_children().front().get_var_name(); + this->local_scope_add_symbol(tmp); + i->get_children().front().set_symbol_number(this->search_symbol_id(tmp)); + i->get_children().front().set_var_scope(false); + this->symbol_table_block_generate(i->get_children().back()); + } + else + this->symbol_table_block_generate(*i); + } } else if(node.get_node_type()==__id) { @@ -246,6 +291,33 @@ void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node) int id_symbol_number=this->search_symbol_id(tmp); node.set_symbol_number(id_symbol_number); } + else if(node.get_node_type()==__hash) + { + this->local_list_add_scope(); + symbol_table_unit me; + me.symbol_line=node.get_node_line(); + me.symbol_name="me"; + this->local_scope_add_symbol(me); + for(std::list::iterator i=node.get_children().begin();i!=node.get_children().end();++i) + { + if(i->get_node_type()==__hash_member) + { + symbol_table_unit tmp; + tmp.symbol_line=i->get_children().front().get_node_line(); + if(i->get_children().front().get_node_type()==__id) + tmp.symbol_name=i->get_children().front().get_var_name(); + else if(i->get_children().front().get_node_type()==__string) + tmp.symbol_name=i->get_children().front().get_var_string(); + this->local_scope_add_symbol(tmp); + i->get_children().front().set_symbol_number(this->search_symbol_id(tmp)); + i->get_children().front().set_var_scope(false); + this->symbol_table_block_generate(i->get_children().back()); + } + else + this->symbol_table_block_generate(*i); + } + this->local_list_del_scope(); + } else { if(!node.get_children().empty()) @@ -255,4 +327,4 @@ void nasal_symbol_table::symbol_table_block_generate(abstract_syntax_tree& node) return; } -#endif \ No newline at end of file +#endif