#ifndef __ABSTRACT_SYNTAX_TREE_CPP__ #define __ABSTRACT_SYNTAX_TREE_CPP__ #include "abstract_syntax_tree.h" int exit_type=0; std::stack ret_stack; // for function ret use std::list parameter; // for function call use var abstract_syntax_tree::calculation() { var temp; temp.set_type(__null_type); if(this->type==__number) { temp.set_type(__var_number); temp.set_number(number); return temp; } else if(this->type==__string) { temp.set_type(__var_string); temp.set_string(str); return temp; } else if(this->type==__id) { temp=this->call_identifier(); return temp; } else if(this->type==__array) { temp.set_type(__var_array); if(!children.empty()) for(std::list::iterator i=children.begin();i!=children.end();++i) temp.append_array(i->calculation()); return temp; } else if(this->type==__hash) { temp.set_type(__var_hash); if(!children.empty()) for(std::list::iterator i=children.begin();i!=children.end();++i) { var t; t=i->children.front().calculation(); t.set_name(i->name); temp.append_hash(t); } return temp; } else if(this->type==__function) { temp.set_type(__var_function); temp.set_function(*this); return temp; } if(this->type==__nor_operator) { temp.set_type(__var_number); temp=children.front().calculation(); if(temp.get_type()!=__var_number) { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<type==__add_operator) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==__var_number && right_child.get_type()==__var_number) temp.set_number(left_child.get_number()+right_child.get_number()); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<type==__sub_operator) { temp.set_type(__var_number); std::list::iterator i=children.begin(); ++i; if(i==children.end()) { temp=children.front().calculation(); if(temp.get_type()==__var_number) temp.set_number(temp.get_number()*(-1)); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "<type==__mul_operator) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==__var_number && right_child.get_type()==__var_number) temp.set_number(left_child.get_number()*right_child.get_number()); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<type==__div_operator) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==__var_number && right_child.get_type()==__var_number) { temp.set_number(left_child.get_number()/right_child.get_number()); if(std::isnan(temp.get_number()) || std::isinf(temp.get_number())) { exit_type=__sigfpe_arithmetic_exception; std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "<type==__link_operator) { temp.set_type(__var_string); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==__var_string && right_child.get_type()==__var_string) temp.set_string(left_child.get_string()+right_child.get_string()); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<type==__cmp_equal) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==right_child.get_type()) { if(left_child.get_type()==__var_number) temp.set_number((double)(left_child.get_number()==right_child.get_number())); else if(left_child.get_type()==__var_string) temp.set_number((double)(left_child.get_string()==right_child.get_string())); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "<type==__cmp_not_equal) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==right_child.get_type()) { if(left_child.get_type()==__var_number) temp.set_number((double)(left_child.get_number()!=right_child.get_number())); else if(left_child.get_type()==__var_string) temp.set_number((double)(left_child.get_string()!=right_child.get_string())); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "<type==__cmp_less) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==right_child.get_type()) { if(left_child.get_type()==__var_number) temp.set_number((double)(left_child.get_number()>[Runtime-error] line "<>[Runtime-error] line "<type==__cmp_more) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==right_child.get_type()) { if(left_child.get_type()==__var_number) temp.set_number((double)(left_child.get_number()>right_child.get_number())); else if(left_child.get_type()==__var_string) temp.set_number((double)(left_child.get_string()>right_child.get_string())); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<\' but use \'"; print_scalar(left_child.get_type()); std::cout<<"\'."<>[Runtime-error] line "<\' but use \'"; print_scalar(left_child.get_type()); std::cout<<"\' and \'"; print_scalar(right_child.get_type()); std::cout<<"\'."<type==__cmp_less_or_equal) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==right_child.get_type()) { if(left_child.get_type()==__var_number) temp.set_number((double)(left_child.get_number()<=right_child.get_number())); else if(left_child.get_type()==__var_string) temp.set_number((double)(left_child.get_string()<=right_child.get_string())); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "<type==__cmp_more_or_equal) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==right_child.get_type()) { if(left_child.get_type()==__var_number) temp.set_number((double)(left_child.get_number()>=right_child.get_number())); else if(left_child.get_type()==__var_string) temp.set_number((double)(left_child.get_string()>=right_child.get_string())); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<=\' but use \'"; print_scalar(left_child.get_type()); std::cout<<"\'."<>[Runtime-error] line "<=\' but use \'"; print_scalar(left_child.get_type()); std::cout<<"\' and \'"; print_scalar(right_child.get_type()); std::cout<<"\'."<type==__or_operator) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==__var_number && right_child.get_type()==__var_number) temp.set_number((double)(left_child.get_number() || right_child.get_number())); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<type==__and_operator) { temp.set_type(__var_number); var left_child=children.front().calculation(); var right_child=children.back().calculation(); if(left_child.get_type()==__var_number && right_child.get_type()==__var_number) temp.set_number((double)(left_child.get_number() && right_child.get_number())); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "<type); std::cout<<"\' occurred when doing calculation."<>[Runtime-error] line "<>[Runtime-error] line "<::iterator i=children.begin();i!=children.end();++i) { if(i->type==__call_array && temp.get_type()==__var_array) { var place=i->children.front().calculation(); if(place.get_type()==__var_number) temp=temp.get_array_member((int)place.get_number()); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<type); std::cout<<": incorrect type \'"; print_scalar(temp.get_type()); std::cout<<"\'."<type==__call_hash && temp.get_type()==__var_hash) { temp=temp.get_hash_member(i->name); if(temp.get_type()==__null_type) { exit_type=__get_value_failure; std::cout<<">>[Runtime-error] line "<name<<"\' or this value is set to __null_type. detail: "; temp.print_var(); std::cout<<"."<type==__call_function && temp.get_type()==__var_function) { parameter.clear(); for(std::list::iterator j=i->children.begin();j!=i->children.end();++j) parameter.push_back(j->calculation()); temp=temp.get_function().run_func(); } else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<type); std::cout<<": incorrect type \'"; print_scalar(temp.get_type()); std::cout<<"\'."<>[Runtime-error] line "<::iterator i=children.begin();i!=children.end();++i) { if(i->type==__call_array && addr->get_type()==__var_array) { var place=i->children.front().calculation(); if(place.get_type()==__var_number) addr=addr->get_array_member_addr((int)place.get_number()); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<type); std::cout<<": incorrect type \'"; print_scalar(addr->get_type()); std::cout<<"\'."<type==__call_hash && addr->get_type()==__var_hash) { addr=addr->get_hash_member_addr(i->name); if(addr->get_type()==__null_type) { exit_type=__get_value_failure; std::cout<<">>[Runtime-error] line "<name<<"\'."<type==__call_function && addr->get_type()==__var_function) { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "<type); std::cout<<": incorrect type \'"; print_scalar(addr->get_type()); std::cout<<"\'."<get_name(); *addr=temp; addr->set_name(tname); } else if(type==__add_equal || type==__sub_equal || type==__mul_equal || type==__div_equal) { if(addr->get_type()==__var_number && temp.get_type()==__var_number) { switch(type) { case __add_equal:addr->set_number(addr->get_number()+temp.get_number());break; case __sub_equal:addr->set_number(addr->get_number()-temp.get_number());break; case __mul_equal:addr->set_number(addr->get_number()*temp.get_number());break; case __div_equal:addr->set_number(addr->get_number()/temp.get_number());break; } if(std::isnan(addr->get_number()) || std::isinf(addr->get_number())) { exit_type=__sigfpe_arithmetic_exception; std::cout<<">>[Runtime-error] line "<>[Runtime-error] line "<get_type()); std::cout<<"\' and \'"; print_scalar(temp.get_type()); std::cout<<"\' but this operator "; print_detail_token(type); std::cout<<" must use \'number\' and \'number\'."<get_type()==__var_string && temp.get_type()==__string) addr->set_string(addr->get_string()+temp.get_string()); else { exit_type=__error_value_type; std::cout<<">>[Runtime-error] line "<get_type()); std::cout<<"\' and \'"; print_scalar(temp.get_type()); std::cout<<"\' but this operator ~ must use \'string\' and \'string\'."<::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->calculation(); new_var.set_name(_name); scope.add_new_var(new_var); } else { std::cout<<">>[Runtime-error] line "<type==__equal || i->type==__add_equal || i->type==__sub_equal || i->type==__mul_equal || i->type==__div_equal || i->type==__link_equal) 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) { var t=i->calculation(); if(t.get_type()==__var_number) std::cout<type==__number) std::cout<number<type==__string) std::cout<str<type==__id) { var t=i->call_identifier(); if(t.get_type()==__var_number) std::cout<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; } end_time=time(NULL); std::cout<<"------------------------------------------------------------------------------"<>[Runtime] process exited after "<::iterator i=children.begin();i!=children.end();++i) { if((i->type==__if || i->type==__elsif) && i->children.front().condition_check()) { ret=i->children.back().run_block(); break; } else if(i->type==__else) { ret=i->children.back().run_block(); break; } } scope.pop_last_local_scope(); return ret; } var abstract_syntax_tree::run_func() { var ret; scope.add_new_block_scope(); scope.add_new_local_scope(); abstract_syntax_tree para=children.front(); abstract_syntax_tree blk=children.back(); std::list::iterator para_name=para.children.begin(); std::list::iterator para_value=parameter.begin(); for(;para_name!=para.children.end();++para_name,++para_value) { if(para_value==parameter.end() && para_name!=para.children.end()) { exit_type=__lack_parameter; std::cout<<">>[Runtime-error] line "<name); scope.add_new_var(new_var); } if(exit_type==__process_exited_successfully) { int _t=blk.run_block(); if(_t!=__return) ret_stack.push(error_var); } else ret_stack.push(error_var); //get return scope.pop_last_block_scope(); ret=ret_stack.top(); ret_stack.pop(); return ret; } int abstract_syntax_tree::run_block() { scope.add_new_local_scope(); for(std::list::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->calculation(); new_var.set_name(_name); scope.add_new_var(new_var); } else { std::cout<<">>[Runtime-error] line "<type==__number) std::cout<number<type==__string) std::cout<str<type==__id) { var t=i->call_identifier(); if(t.get_type()==__var_number) std::cout<type==__equal || i->type==__add_equal || i->type==__sub_equal || i->type==__mul_equal || i->type==__div_equal || i->type==__link_equal) 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) { var t=i->calculation(); if(t.get_type()==__var_number) std::cout<type==__while) { int type=i->run_loop(); if(type) { if(type==__return) return type; else { std::cout<<"[Runtime-error] line "<type==__ifelse) { int type=i->run_ifelse(); if(type) return type; } else if(i->type==__continue) return __continue; else if(i->type==__break) return __break; else if(i->type==__return) { var temp; temp.set_type(__null_type); if(!(i->children.empty())) temp=i->children.front().calculation(); ret_stack.push(temp); return __return; } if(exit_type!=__process_exited_successfully) break; } scope.pop_last_local_scope(); return 0; } #endif