From f7f6eb9ea33ced028eaf60c4d893723cd46ae716 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Wed, 20 Nov 2019 23:31:07 +0800 Subject: [PATCH] Add for-loop --- balloon/abstract_syntax_tree.cpp | 90 ++++++++++++++++++++++----- balloon/balloon_parse.h | 101 ++++++++++++++++++++++++++----- balloon/balloon_scope.h | 20 ++++++ 3 files changed, 181 insertions(+), 30 deletions(-) diff --git a/balloon/abstract_syntax_tree.cpp b/balloon/abstract_syntax_tree.cpp index 322a4fd..f58acec 100644 --- a/balloon/abstract_syntax_tree.cpp +++ b/balloon/abstract_syntax_tree.cpp @@ -427,6 +427,8 @@ bool abstract_syntax_tree::condition_check() std::cout<<">>[Runtime-error] line "<::iterator j=i->children.begin(); std::string _name=j->name; - if(!scope.search_var(_name)) + if(!scope.check_redefine(_name)) { ++j; if(j!=i->children.end()) @@ -705,7 +707,7 @@ void abstract_syntax_tree::run_root() ; else if(i->type==__id) i->call_identifier(); - else if(i->type==__while) + else if(i->type==__while || i->type==__for) { scope.add_new_block_scope(); int ret_type=i->run_loop(); @@ -751,21 +753,77 @@ int abstract_syntax_tree::run_loop() return 0; } int ret=0; - - abstract_syntax_tree condition=children.front(); - abstract_syntax_tree blk=children.back(); - while(condition.condition_check()) + if(type==__while) { - int type=blk.run_block(); - if(type==__break) - break; - else if(type==__return) + abstract_syntax_tree condition=children.front(); + abstract_syntax_tree blk=children.back(); + while(condition.condition_check()) { - ret=__return; - break; + int type=blk.run_block(); + if(type==__break) + break; + else if(type==__return) + { + ret=__return; + break; + } + if(exit_type!=__process_exited_successfully) + break; } - if(exit_type!=__process_exited_successfully) - break; + } + else if(type==__for) + { + std::list::iterator iter=children.begin(); + abstract_syntax_tree def=*iter; + ++iter; + abstract_syntax_tree condition=*iter; + ++iter; + abstract_syntax_tree var_changement=*iter; + abstract_syntax_tree blk=children.back(); + + scope.add_new_local_scope(); + if(def.type==__definition) + { + var new_var; + std::list::iterator j=def.children.begin(); + std::string _name=j->name; + if(!scope.check_redefine(_name)) + { + ++j; + if(j!=def.children.end()) + new_var=j->calculation(); + new_var.set_name(_name); + scope.add_new_var(new_var); + } + else + { + std::cout<<">>[Runtime-error] line "<::iterator j=i->children.begin(); std::string _name=j->name; - if(!scope.search_var(_name)) + if(!scope.check_redefine(_name)) { ++j; if(j!=i->children.end()) @@ -941,7 +999,7 @@ int abstract_syntax_tree::run_block() i->assignment(); else if(i->type==__add_operator || i->type==__sub_operator || i->type==__mul_operator || i->type==__div_operator || i->type==__link_operator || i->type==__or_operator || i->type==__and_operator || i->type==__nor_operator) i->calculation(); - else if(i->type==__while) + else if(i->type==__while || i->type==__for) { int type=i->run_loop(); if(type) diff --git a/balloon/balloon_parse.h b/balloon/balloon_parse.h index 5a75ac1..f218cea 100644 --- a/balloon/balloon_parse.h +++ b/balloon/balloon_parse.h @@ -265,29 +265,102 @@ abstract_syntax_tree balloon_parse::choose() abstract_syntax_tree balloon_parse::loop() { abstract_syntax_tree new_node; - new_node.set_type(__while); + get_token(); - if(this_token.type!=__while) + if(this_token.type!=__while && this_token.type!=__for) { ++error; - std::cout<<">>[Parse-error] line "<>[Parse-error] line "<>[Parse-error] line "<>[Parse-error] line "<>[Parse-error] line "<>[Parse-error] line "<>[Parse-error] line "<>[Parse-error] line "<>[Parse-error] line "< >::iterator i=scope_list.back().end(); + --i; + for(std::list::iterator j=i->begin();j!=i->end();++j) + if(j->get_name()==name) + return true; + return false; + } + if(!global.empty()) + { + for(std::list::iterator i=global.begin();i!=global.end();++i) + if(i->get_name()==name) + return true; + return false; + } + return false; + } bool search_var(std::string name) { if(!scope_list.empty() && !scope_list.back().empty())