From 5134f4eea28549cb55cad271ff8fcfb061e36c38 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Sun, 10 Nov 2019 23:44:36 +0800 Subject: [PATCH] fixed bug when meeting }}}}}}}}}}}} --- balloon/abstract_syntax_tree.cpp | 86 ++++++++++++++++++++++++++------ balloon/abstract_syntax_tree.h | 15 ++---- balloon/balloon_parse.h | 9 +++- balloon/balloon_type.h | 6 ++- 4 files changed, 88 insertions(+), 28 deletions(-) diff --git a/balloon/abstract_syntax_tree.cpp b/balloon/abstract_syntax_tree.cpp index 6063bb7..94e196e 100644 --- a/balloon/abstract_syntax_tree.cpp +++ b/balloon/abstract_syntax_tree.cpp @@ -1,6 +1,6 @@ #ifndef __ABSTRACT_SYNTAX_TREE_CPP__ #define __ABSTRACT_SYNTAX_TREE_CPP__ - +#include "abstract_syntax_tree.h" int exit_type=0; bool abstract_syntax_tree::check() @@ -12,15 +12,17 @@ bool abstract_syntax_tree::check() var abstract_syntax_tree::call_id() { var temp; - if(children.empty()) + if(scope.search_var(name)) + temp=scope.get_var(name); + else { - if(scope.search_var(name)) - temp=scope.get_var(name); - else - { - std::cout<<">>[Runtime-error] cannot find a var named \'"<>[Runtime-error] cannot find a var named \'"<>[Runtime] process exited after "<::iterator i=children.begin();i!=children.end();++i) + { + if(i->type==__definition) + { + var new_var; + std::list::iterator j=i->children.begin(); + 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); + } + else + { + std::cout<<">>[Runtime-error] redeclaration of \'"<<_name<<"\'."<type==__number) + std::cout<number<type==__string) + std::cout<str<type==__id) + std::cout<call_id().get_name()<type==__while) + i->run_loop(); + else if(i->type==__ifelse) + i->run_ifelse(); + else if(i->type==__continue) + { + return __continue; +// else +// { +// std::cout<<">>[Runtime-error] must use \'continue\' in a loop."<type==__break) + { + return __loop; +// else +// { +// std::cout<<">>[Runtime-error] must use \'continue\' in a loop."<type==__return) + { + break; + } + if(exit_type!=__process_exited_successfully) + break; + } scope.pop_last_local_scope(); - return; + return 0; } diff --git a/balloon/abstract_syntax_tree.h b/balloon/abstract_syntax_tree.h index dd8733a..a60f303 100644 --- a/balloon/abstract_syntax_tree.h +++ b/balloon/abstract_syntax_tree.h @@ -97,10 +97,9 @@ class abstract_syntax_tree } if((int)_str.length()>2 && (_str[1]=='x' || _str[1]=='o')) { + double num=0; + double pw=1; if(_str[1]=='x') - { - double num=0; - double pw=1; for(int i=(int)_str.length()-1;i>1;--i) { if('0'<=_str[i] && _str[i]<='9') @@ -111,19 +110,13 @@ class abstract_syntax_tree num+=(10+_str[i]-'A')*pw; pw*=16; } - number=num; - } else - { - double num=0; - double pw=1; for(int i=(int)_str.length()-1;i>1;--i) { num+=(_str[i]-'0')*pw; pw*=8; } - number=num; - } + number=num; return; } int dot_place=-1; @@ -198,7 +191,7 @@ class abstract_syntax_tree void run_loop(); void run_ifelse(); void run_func(); - void run_block(int); + int run_block(); }; #endif diff --git a/balloon/balloon_parse.h b/balloon/balloon_parse.h index 6a1a534..4ad3dd8 100644 --- a/balloon/balloon_parse.h +++ b/balloon/balloon_parse.h @@ -162,7 +162,10 @@ abstract_syntax_tree balloon_parse::choose() temp.set_clear(); get_token(); if(this_token.type!=__elsif && this_token.type!=__else) + { + parse.push(this_token); break; + } if(this_token.type==__else) { get_token(); @@ -478,7 +481,7 @@ abstract_syntax_tree balloon_parse::block() std::cout<<">>[Parse-error] line "<>[Parse-error] line "<