diff --git a/balloon/abstract_syntax_tree.cpp b/balloon/abstract_syntax_tree.cpp index b81111d..6063bb7 100644 --- a/balloon/abstract_syntax_tree.cpp +++ b/balloon/abstract_syntax_tree.cpp @@ -2,6 +2,13 @@ #define __ABSTRACT_SYNTAX_TREE_CPP__ int exit_type=0; + +bool abstract_syntax_tree::check() +{ + + return true; +} + var abstract_syntax_tree::call_id() { var temp; @@ -71,6 +78,9 @@ void abstract_syntax_tree::run_root() std::string _name=j->name; if(!scope.search_var(_name)) { + ++j; + if(j!=i->children.end()) + new_var=j->get_value(); new_var.set_name(_name); scope.add_new_var(new_var); } @@ -89,11 +99,15 @@ void abstract_syntax_tree::run_root() std::cout<call_id().get_name()<type==__while) { - ; + scope.add_new_block_scope(); + i->run_loop(); + scope.pop_last_block_scope(); } else if(i->type==__ifelse) { - ; + scope.add_new_block_scope(); + i->run_ifelse(); + scope.pop_last_block_scope(); } if(exit_type!=__process_exited_successfully) break; @@ -107,6 +121,26 @@ void abstract_syntax_tree::run_root() return; } +void abstract_syntax_tree::run_loop() +{ + scope.add_new_local_scope(); + + abstract_syntax_tree condition=children.front(); + abstract_syntax_tree blk=children.back(); + while(condition.check()) + blk.run_block(__loop); + scope.pop_last_local_scope(); + return; +} + +void abstract_syntax_tree::run_ifelse() +{ + scope.add_new_local_scope(); + + scope.pop_last_local_scope(); + return; +} + void abstract_syntax_tree::run_func() { scope.add_new_block_scope(); @@ -119,13 +153,10 @@ void abstract_syntax_tree::run_func() void abstract_syntax_tree::run_block(int block_type) { scope.add_new_local_scope(); - + scope.pop_last_local_scope(); return; } - - - #endif diff --git a/balloon/abstract_syntax_tree.h b/balloon/abstract_syntax_tree.h index 3b67440..dd8733a 100644 --- a/balloon/abstract_syntax_tree.h +++ b/balloon/abstract_syntax_tree.h @@ -191,9 +191,12 @@ class abstract_syntax_tree { return children; } + bool check(); var call_id(); var get_value(); void run_root(); + void run_loop(); + void run_ifelse(); void run_func(); void run_block(int); }; diff --git a/balloon/balloon_parse.h b/balloon/balloon_parse.h index 86e5422..6a1a534 100644 --- a/balloon/balloon_parse.h +++ b/balloon/balloon_parse.h @@ -275,8 +275,9 @@ abstract_syntax_tree balloon_parse::definition() get_token(); if(this_token.type!=__equal) { - ++error; - std::cout<<">>[Parse-error] line "<>[Parse-warning] line "< scope; - var error_var; + std::list global; + std::list > > scope_list; public: void set_clear() { - scope.clear(); + global.clear(); + scope_list.clear(); return; } bool search_var(std::string name) { - for(std::list::iterator i=scope.begin();i!=scope.end();++i) - if(i->get_name()==name) - return true; + if(!scope_list.empty()) + { + std::list >::iterator i=scope_list.back().end(); + --i; + for(;;--i) + { + for(std::list::iterator j=i->begin();j!=i->end();++j) + if(j->get_name()==name) + return true; + if(i==scope_list.back().begin()) + break; + } + } + if(!global.empty()) + { + for(std::list::iterator i=global.begin();i!=global.end();++i) + if(i->get_name()==name) + return true; + } return false; } - void add_var(var t) + void add_new_var(var t) { - scope.push_back(t); + if(!scope_list.empty()) + { + std::list >::iterator i=scope_list.back().end(); + --i; + i->push_back(t); + return; + } + global.push_back(t); return; } var& get_var(std::string name) { - for(std::list::iterator i=scope.begin();i!=scope.end();++i) - if(i->get_name()==name) - return *i; - std::cout<<">>[Runtime-error] \'"< >::iterator i=scope_list.back().end(); + --i; + // get the last scope block(std::list >) + for(;;--i) + { + for(std::list::iterator j=i->begin();j!=i->end();++j) + if(j->get_name()==name) + return *j; + if(i==scope_list.back().begin()) + break; + } + } + if(!global.empty()) + { + for(std::list::iterator i=global.begin();i!=global.end();++i) + if(i->get_name()==name) + return *i; + } return error_var; } + void add_new_block_scope() + { + std::list > new_list; + scope_list.push_back(new_list); + return; + } + void add_new_local_scope() + { + if(!scope_list.empty()) + { + std::list > >::iterator i=scope_list.end(); + --i; + std::list new_list; + i->push_back(new_list); + } + return; + } + void pop_last_block_scope() + { + if(!scope_list.empty()) + scope_list.pop_back(); + return; + } + void pop_last_local_scope() + { + if(!scope_list.empty()) + { + std::list > temp=scope_list.back(); + temp.pop_back(); + } + return; + } }; +balloon_scope scope; + #endif diff --git a/balloon/loop.txt b/balloon/loop.txt new file mode 100644 index 0000000..45758d6 --- /dev/null +++ b/balloon/loop.txt @@ -0,0 +1,8 @@ +var a=1; +var sum=0; +while(a!=100) +{ + sum+=a; + a+=1; +} +print(sum); \ No newline at end of file diff --git a/balloon/main.cpp b/balloon/main.cpp index a39a081..b7f8314 100644 --- a/balloon/main.cpp +++ b/balloon/main.cpp @@ -86,9 +86,7 @@ int main() pas.get_detail_token_stream(lex.get_detail_token()); pas.parse_main(); if(!pas.get_error()) - { pas.run_tree(); - } else std::cout<<">>[Parse] error(s) found,stop."<