diff --git a/balloon/abstract_syntax_tree.h b/balloon/abstract_syntax_tree.h index f30e243..0a3c789 100644 --- a/balloon/abstract_syntax_tree.h +++ b/balloon/abstract_syntax_tree.h @@ -5,29 +5,185 @@ class abstract_syntax_tree { private: int type; - double number; - std::string str; - std::string name; + double var_number; + std::string var_string; + std::string var_name; std::list children; public: abstract_syntax_tree() { - type=__null_node; - number=0; - str=""; - name=""; + type=0; + var_number=0; + var_string=""; + var_name=""; children.clear(); return; } abstract_syntax_tree(const abstract_syntax_tree& p) { type=p.type; - number=p.number; - str=p.str; - name=p.name; + var_number=p.var_number; + var_string=p.var_string; + var_name=p.var_name; children=p.children; return; } + abstract_syntax_tree& operator=(const abstract_syntax_tree& p) + { + type=p.type; + var_number=p.var_number; + var_string=p.var_string; + var_name=p.var_name; + children.clear(); + children=p.children; + return *this; + } + void set_clear() + { + type=0; + var_number=0; + var_string=""; + var_name=""; + children.clear(); + return; + } + void print_tree(const int n) + { + std::string str=""; + for(int i=0;iprint_tree(n+1); + } + return; + } + void set_type(const int _type) + { + type=_type; + return; + } + void set_string(std::string str) + { + var_string=str; + return; + } + void set_number(std::string str) + { + if(str=="nil") + { + var_number=0; + return; + } + if((int)str.length()>2 && (str[1]=='x' || str[1]=='o')) + { + if(str[1]=='x') + { + int num=0; + int pw=1; + for(int i=(int)str.length()-1;i>1;--i) + { + if('0'<=str[i] && str[i]<='9') + num+=(str[i]-'0')*pw; + else if('a'<=str[i] && str[i]<='f') + num+=(10+str[i]-'a')*pw; + else if('A'<=str[i] && str[i]<='F') + num+=(10+str[i]-'A')*pw; + pw<<=4; + } + var_number=(double)num; + } + else + { + int num=0; + int pw=1; + for(int i=(int)str.length()-1;i>1;--i) + { + num+=(str[i]-'0')*pw; + pw<<=3; + } + var_number=(double)num; + } + return; + } + int dot_place=-1; + for(int i=0;i<(int)str.length();++i) + if(str[i]=='.') + { + dot_place=i; + break; + } + if(dot_place==-1) + { + var_number=0; + double pw=1; + for(int i=(int)str.length()-1;i>=0;--i) + { + var_number+=(str[i]-'0')*pw; + pw*=10; + } + } + else + { + var_number=0; + double pw=0.1; + for(int i=dot_place+1;i<(int)str.length();++i) + { + var_number+=(str[i]-'0')*pw; + pw/=10; + } + pw=1; + for(int i=dot_place-1;i>=0;--i) + { + var_number+=(str[i]-'0')*pw; + pw*=10; + } + } + return; + } + void set_name(std::string& str) + { + var_name=str; + return; + } + void add_child(abstract_syntax_tree p) + { + children.push_back(p); + return; + } + int get_type() + { + return type; + } + double get_number() + { + return var_number; + } + std::string get_string() + { + return var_string; + } + std::string get_name() + { + return var_name; + } + std::list& get_children() + { + return children; + } }; #endif diff --git a/balloon/balloon_lexer.h b/balloon/balloon_lexer.h index 7ce7b39..d509693 100644 --- a/balloon/balloon_lexer.h +++ b/balloon/balloon_lexer.h @@ -14,7 +14,54 @@ int is_reserve_word(std::string str) return __reserve_word; return __token_identifier; } - +bool check_number(std::string str) +{ + if(str.length()==1) + return true; + else if(str.length()==2 && '0'=3 && str[0]=='0' && str[1]=='x') + { + for(int i=2;i=3 && str[0]=='0' && str[1]=='o') + { + for(int i=2;i1) + return false; + if(str[0]=='.') + return false; + if(!dotcnt && str[0]=='0') + return false; + return true; + } + return false; +} class resource_file @@ -30,6 +77,11 @@ class resource_file void input_file(std::string filename) { std::ifstream fin(filename,std::ios::binary); + if(fin.fail()) + { + std::cout<<">>[Resource] cannot find a file named \'"<>[Lexer-error] line "<>[Lexer] complete scanning. "<>[Lexer] complete generating. "<& get_detail_token() { return detail_token; } + int get_error() + { + return error; + } }; #endif diff --git a/balloon/balloon_parse.h b/balloon/balloon_parse.h index 9b2b0fc..1532387 100644 --- a/balloon/balloon_parse.h +++ b/balloon/balloon_parse.h @@ -4,17 +4,17 @@ class balloon_parse { private: - std::stack detail; + std::stack parse; token this_token; int error; int warning; public: void get_token() { - if(!detail.empty()) + if(!parse.empty()) { - this_token=detail.top(); - detail.pop(); + this_token=parse.top(); + parse.pop(); } else this_token.type=0; @@ -22,16 +22,48 @@ class balloon_parse } void get_detail_token_stream(std::list& tk_list) { + while(!parse.empty()) + parse.pop(); + if(tk_list.empty()) + return; std::stack temp; for(std::list::iterator i=tk_list.begin();i!=tk_list.end();++i) temp.push(*i); while(!temp.empty()) { - detail.push(temp.top()); + parse.push(temp.top()); temp.pop(); } return; } + void print_parse_stack() + { + if(parse.empty()) + return; + std::stack temp; + int line=parse.top().line; + std::cout<> Balloon interpreter by ValKmjolnir"<> Input [help] to find help."<>[Lexer] error(s) found,stop."<>[Lexer] error(s) found,stop."<