This commit is contained in:
Valk Richard Li 2019-11-12 23:23:42 +08:00 committed by GitHub
parent a61415fbed
commit 6aef19ce7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 469 additions and 37 deletions

View File

@ -1,63 +1,398 @@
#ifndef __ABSTRACT_SYNTAX_TREE_CPP__
#define __ABSTRACT_SYNTAX_TREE_CPP__
#include "abstract_syntax_tree.h"
int exit_type=0;
var abstract_syntax_tree::condition()
int exit_type=0;
std::stack<var> ret_stack;// for function ret use
var abstract_syntax_tree::calculation()
{
var cond;
cond.set_number(0);
if(this->type==__cmp_equal)
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;
}
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] must use a number to use \'!\' but use \'";
print_scalar(temp.get_type());
std::cout<<"\'."<<std::endl;
}
else
{
if(temp.get_number())
temp.set_number(0);
else
temp.set_number(1);
}
return temp;
}
else if(this->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] must use a number to use \'+\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->type==__sub_operator)
{
temp.set_type(__var_number);
std::list<abstract_syntax_tree>::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] must use a number to use \'-\' but use \'";
print_scalar(temp.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
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] must use a number to use \'-\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
}
else if(this->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] must use a number to use \'*\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] get number \'NaN\' or \'Inf\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to use \'/\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] must use a string to use \'~\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] must use number or string to use \'==\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'==\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] must use number or string to use \'!=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'!=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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()<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] must use number or string to use \'<\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'<\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] must use number or string to use \'>\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'>\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] must use number or string to use \'<=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'<=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] must use number or string to use \'>=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use same type to use \'>=\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] must use same type to use \'or\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else if(this->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] must use same type to use \'and\' but use \'";
print_scalar(left_child.get_type());
std::cout<<"\' and \'";
print_scalar(right_child.get_type());
std::cout<<"\'."<<std::endl;
}
}
else
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] error type occurred when doing calculation."<<std::endl;
}
return cond;
return temp;
}
bool abstract_syntax_tree::check()
bool abstract_syntax_tree::condition_check()
{
bool ret=false;
var cond=condition();
if(cond.get_number()==0)
ret=false;
var temp=calculation();
if(temp.get_type()==__var_number)
{
if(temp.get_number())
ret=true;
}
else
ret=true;
{
exit_type=__error_value_type;
std::cout<<">>[Runtime-error] must use a number to make a choice but use \'";
print_scalar(temp.get_type());
std::cout<<"\'."<<std::endl;
}
return ret;
}
var abstract_syntax_tree::call_id()
var abstract_syntax_tree::call_identifier()
{
var temp;
if(scope.search_var(name))
@ -70,7 +405,10 @@ var abstract_syntax_tree::call_id()
}
if(!children.empty())
{
;
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
{
}
}
return temp;
}
@ -80,7 +418,7 @@ var abstract_syntax_tree::get_value()
var temp;
temp.set_type(type);
if(type==__id)
temp=call_id();
temp=call_identifier();
else if(type==__number)
temp.set_number(number);
else if(type==__string)
@ -115,6 +453,7 @@ var abstract_syntax_tree::get_value()
}
void abstract_syntax_tree::run_root()
{
while(!ret_stack.empty())ret_stack.pop();
scope.set_clear();
int beg_time,end_time;
exit_type=__process_exited_successfully;
@ -141,12 +480,24 @@ void abstract_syntax_tree::run_root()
break;
}
}
else if(i->type==__equal || i->type==__add_equal || i->type==__sub_equal || i->type==__mul_equal || i->type==__div_equal || i->type==__link_equal)
{
;
}
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<<t.get_number()<<std::endl;
else if(t.get_type()==__var_string)
std::cout<<t.get_string()<<std::endl;
}
else if(i->type==__number)
std::cout<<i->number<<std::endl;
else if(i->type==__string)
std::cout<<i->str<<std::endl;
else if(i->type==__id)
std::cout<<i->call_id().get_name()<<std::endl;
std::cout<<i->call_identifier().get_name()<<std::endl;
else if(i->type==__while)
{
scope.add_new_block_scope();
@ -178,7 +529,7 @@ int abstract_syntax_tree::run_loop()
abstract_syntax_tree condition=children.front();
abstract_syntax_tree blk=children.back();
while(condition.check())
while(condition.condition_check())
{
int type=blk.run_block();
if(type==__break)
@ -199,7 +550,12 @@ int abstract_syntax_tree::run_ifelse()
scope.add_new_local_scope();
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
{
if(i->children.front().check())
if(i->type!=__else && i->children.front().condition_check())
{
ret=i->children.back().run_block();
break;
}
else
{
ret=i->children.back().run_block();
break;
@ -209,13 +565,17 @@ int abstract_syntax_tree::run_ifelse()
return ret;
}
void abstract_syntax_tree::run_func()
var abstract_syntax_tree::run_func()
{
var ret;
scope.add_new_block_scope();
scope.add_new_local_scope();
//get return
scope.pop_last_block_scope();
return;
ret=ret_stack.top();
ret_stack.pop();
return ret;
}
int abstract_syntax_tree::run_block()
@ -248,7 +608,19 @@ int abstract_syntax_tree::run_block()
else if(i->type==__string)
std::cout<<i->str<<std::endl;
else if(i->type==__id)
std::cout<<i->call_id().get_name()<<std::endl;
std::cout<<i->call_identifier().get_name()<<std::endl;
else if(i->type==__equal || i->type==__add_equal || i->type==__sub_equal || i->type==__mul_equal || i->type==__div_equal || i->type==__link_equal)
{
;
}
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<<t.get_number()<<std::endl;
else if(t.get_type()==__var_string)
std::cout<<t.get_string()<<std::endl;
}
else if(i->type==__while)
{
int type=i->run_loop();
@ -274,7 +646,16 @@ int abstract_syntax_tree::run_block()
else if(i->type==__break)
return __break;
else if(i->type==__return)
{
var temp;
temp.set_type(__null_type);
if(!(i->children.empty()))
{
}
ret_stack.push(temp);
return __return;
}
if(exit_type!=__process_exited_successfully)
break;
}

View File

@ -184,14 +184,14 @@ class abstract_syntax_tree
{
return children;
}
var condition();
bool check();
var call_id();
var calculation();
bool condition_check();
var call_identifier();
var get_value();
void run_root();
int run_loop();
int run_ifelse();
void run_func();
var run_func();
int run_block();
};

View File

@ -2,8 +2,10 @@
#define __BALLOON_H__
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <cmath>
#include <list>
#include <stack>
#include <ctime>

View File

@ -654,7 +654,7 @@ abstract_syntax_tree balloon_parse::call_identifier()
parse.push(this_token);
temp=new_node;
new_node=assignment();
new_node.add_child(temp);
new_node.get_children().push_front(temp);
}
else
parse.push(this_token);

View File

@ -145,7 +145,9 @@ enum runtime_error_type
__get_value_failure,
__find_var_failure,
__error_value_type,
__error_command_use
__error_command_use,
__sigfpe_arithmetic_exception,
__sigsegv_segmentation_error
};
void print_exit_type(int type)
@ -159,6 +161,8 @@ void print_exit_type(int type)
case __find_var_failure: context="find_var_failure";break;
case __error_value_type: context="value_type_error";break;
case __error_command_use: context="command_use_error(continue/break/return)";break;
case __sigfpe_arithmetic_exception: context="SIGFPE";break;
case __sigsegv_segmentation_error: context="SIGSEGV";break;
default: context="unknown";break;
}
std::cout<<context;

View File

@ -7,9 +7,27 @@ enum var_type
__var_number,
__var_string,
__var_array,
__var_hash
__var_hash,
__var_function
};
void print_scalar(int type)
{
std::string str="";
switch(type)
{
case __null_type: str="null";break;
case __var_number: str="number";break;
case __var_string: str="string";break;
case __var_array: str="array";break;
case __var_hash: str="hash";break;
case __var_function:str="function";break;
default: str="unknown";break;
}
std::cout<<str;
return;
}
class var
{
private:
@ -28,6 +46,7 @@ class var
name="";
balloon_array.clear();
balloon_hash.clear();
function.set_clear();
return;
}
var(const var& p)
@ -38,6 +57,7 @@ class var
name=p.name;
balloon_array=p.balloon_array;
balloon_hash=p.balloon_hash;
function=p.function;
return;
}
var& operator=(const var& p)
@ -48,8 +68,10 @@ class var
name=p.name;
balloon_array.clear();
balloon_hash.clear();
function.set_clear();
balloon_array=p.balloon_array;
balloon_hash=p.balloon_hash;
function=p.function;
return *this;
}
void set_type(int);
@ -59,6 +81,7 @@ class var
void set_function(abstract_syntax_tree&);
void append_array(var);
void append_hash(var);
int get_type();
std::string get_name();
double get_number();
std::string get_string();
@ -74,48 +97,63 @@ void var::set_type(int _type)
type=_type;
return;
}
void var::set_name(std::string _name)
{
name=_name;
return;
}
void var::set_number(double _num)
{
number=_num;
return;
}
void var::set_string(std::string _str)
{
str=_str;
return;
}
void var::set_function(abstract_syntax_tree& p)
{
function=p;
return;
}
void var::append_array(var _new_var)
{
balloon_array.push_back(_new_var);
return;
}
void var::append_hash(var _new_var)
{
balloon_hash.push_back(_new_var);
return;
}
int var::get_type()
{
return type;
}
std::string var::get_name()
{
return name;
}
double var::get_number()
{
return number;
}
std::string var::get_string()
{
return str;
}
var& var::get_array_member(int _place)
{
if(balloon_array.empty())
@ -130,6 +168,7 @@ var& var::get_array_member(int _place)
std::cout<<">>[Runtime-error] overflow when searching member "<<_place<<" but the max size of array \'"<<name<<"\' is "<<cnt<<"(this means 0-"<<cnt-1<<")."<<std::endl;
return error_var;
}
var& var::get_hash_member(std::string _name)
{
for(std::list<var>::iterator i=balloon_hash.begin();i!=balloon_hash.end();++i)
@ -138,6 +177,7 @@ var& var::get_hash_member(std::string _name)
std::cout<<">>[Runtime-error] hash \'"<<name<<"\' does not have a member named \'"<<_name<<"\'"<<std::endl;
return error_var;
}
abstract_syntax_tree& var::get_function()
{
return function;

View File

@ -16,4 +16,9 @@ a=2;
var i=func(v1,v2,v3){return [0,0,0];};
1073741824;
"str";
a;
a;
var m=[0,[0,1]];
1+1;
"hello "~"world!";
#10/0;
1+"h";