diff --git a/version2.0/main.cpp b/version2.0/main.cpp index 46af052..f1769ca 100644 --- a/version2.0/main.cpp +++ b/version2.0/main.cpp @@ -3,6 +3,7 @@ resource_file resource; nasal_lexer lexer; nasal_parse parser; +nasal_symbol_table symtable; std::string command; @@ -36,6 +37,7 @@ int main() std::cout<<">> [lex ] turn code into tokens."<> [par ] turn tokens into abstract syntax tree."<> [ast ] check the abstract syntax tree."<> [sym ] generate symbol table and print the information of it."<> [run ] run code."<> [info ] print lexer,parser and ast on screen."<> [exit ] quit nasal interpreter."<>[Lexer] error occurred,stop."<>[Symbol] error occurred,stop."<>[Parse] error occurred,stop."<>[Lexer] error occurred,stop."<>[Symbol] error occurred,stop."<>[Parse] error occurred,stop."< #include "numeral_misc.h" -#include "nasal_scope.h" #include "nasal_enum.h" #include "abstract_syntax_tree.h" #include "nasal_lexer.h" #include "nasal_parse.h" +#include "nasal_symbol_table.h" #endif diff --git a/version2.0/nasal_parse.h b/version2.0/nasal_parse.h index d6ac6dc..73d62de 100644 --- a/version2.0/nasal_parse.h +++ b/version2.0/nasal_parse.h @@ -166,7 +166,7 @@ int nasal_parse::get_error() abstract_syntax_tree& nasal_parse::get_root() { - std::cout<<">>[Abstract-syntax-tree] get root address: "<<(&root)<<" ."<>[Parse] output root address: "<<(&root)<<" ."< global_symbol_dictionary; + std::list global_scope; + std::list local_symbol_dictionary; + std::list > block_list; + void symbol_table_block_generate(abstract_syntax_tree&); + public: + nasal_symbol_table(); + ~nasal_symbol_table(); + void set_scope_clear(); + void print_symbol_table(); + void global_scope_add_symbol(symbol_table_unit); + void block_list_add_scope(); + void block_list_del_scope(); + void block_scope_add_symbol(symbol_table_unit); + int search_symbol_id(symbol_table_unit); + int get_error(); + void symbol_table_main_generate(abstract_syntax_tree&); +}; + +nasal_symbol_table::nasal_symbol_table() +{ + error=0; + global_symbol_dictionary.clear(); + global_scope.clear(); + local_symbol_dictionary.clear(); + block_list.clear(); + return; +} + +nasal_symbol_table::~nasal_symbol_table() +{ + error=0; + global_symbol_dictionary.clear(); + global_scope.clear(); + local_symbol_dictionary.clear(); + block_list.clear(); + return; +} + +void nasal_symbol_table::set_scope_clear() +{ + error=0; + global_symbol_dictionary.clear(); + global_scope.clear(); + local_symbol_dictionary.clear(); + block_list.clear(); + return; +} + +void nasal_symbol_table::print_symbol_table() +{ + if(!global_symbol_dictionary.empty()) + { + std::cout<<">>[Symbol] global scope:"<::iterator i=global_symbol_dictionary.begin();i!=global_symbol_dictionary.end();++i) + std::cout<<" line "<symbol_line<<": "<symbol_name<<"|"<symbol_number<>[Symbol] empty global symbol list."<>[Symbol] local scope:"<::iterator i=local_symbol_dictionary.begin();i!=local_symbol_dictionary.end();++i) + std::cout<<" line "<symbol_line<<": "<symbol_name<<"|"<symbol_number<>[Symbol] empty local symbol list."<::iterator i=global_scope.begin();i!=global_scope.end();++i) + { + // this conditional expression is used to avoid wrong use of this function + if(i->symbol_name==tmp.symbol_name) + return; + symbol_number_gen=i->symbol_number; + } + tmp.symbol_number=symbol_number_gen+1; + global_scope.push_back(tmp); + global_symbol_dictionary.push_back(tmp); + return; +} + +void nasal_symbol_table::block_list_add_scope() +{ + std::list new_scope; + block_list.push_back(new_scope); + return; +} + +void nasal_symbol_table::block_list_del_scope() +{ + if(block_list.empty()) + { + ++error; + std::cout<<">>[Symbol-error] fatal error: empty block_scope_list."<::iterator i=block_list.back().begin();i!=block_list.back().end();++i) + { + // this conditional expression is used to avoid wrong use of this function + if(i->symbol_name==tmp.symbol_name) + return; + symbol_number_gen=i->symbol_number; + } + tmp.symbol_number=symbol_number_gen+1; + block_list.back().push_back(tmp); + local_symbol_dictionary.push_back(tmp); + } + else + { + ++error; + std::cout<<">>[Symbol-error] fatal error: empty block_scope_list."< >::iterator i=block_list.begin();i!=block_list.end();++i) + for(std::list::iterator j=i->begin();j!=i->end();++j) + if(j->symbol_name==tmp.symbol_name) + ret_number=j->symbol_number; + if(ret_number==-1) + for(std::list::iterator i=global_scope.begin();i!=global_scope.end();++i) + if(i->symbol_name==tmp.symbol_name) + ret_number=i->symbol_number; + if(ret_number==-1) + { + ++error; + std::cout<<">>[Symbol-error] line "<set_scope_clear(); + for(std::list::iterator i=root.get_children().begin();i!=root.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->global_scope_add_symbol(tmp); + this->symbol_table_block_generate(i->get_children().back()); + } + else + this->symbol_table_block_generate(*i); + } + std::cout<<">>[Symbol] symbol table generating complete. "<