From c2ebe97ff7c20a95f920b74be8d8d14c5f130a20 Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 4 Nov 2019 23:31:04 +0800 Subject: [PATCH] Update --- balloon/abstract_syntax_tree.h | 123 +++++++++++++++++---------------- balloon/balloon_lexer.h | 20 ++++-- balloon/balloon_parse.h | 36 +++++++++- 3 files changed, 114 insertions(+), 65 deletions(-) diff --git a/balloon/abstract_syntax_tree.h b/balloon/abstract_syntax_tree.h index 0a3c789..5313ec9 100644 --- a/balloon/abstract_syntax_tree.h +++ b/balloon/abstract_syntax_tree.h @@ -5,35 +5,40 @@ class abstract_syntax_tree { private: int type; - double var_number; - std::string var_string; - std::string var_name; + double number; + std::string str; + std::string name; std::list children; public: abstract_syntax_tree() { type=0; - var_number=0; - var_string=""; - var_name=""; + number=0; + str=""; + name=""; children.clear(); return; } abstract_syntax_tree(const abstract_syntax_tree& p) { type=p.type; - var_number=p.var_number; - var_string=p.var_string; - var_name=p.var_name; + number=p.number; + str=p.str; + name=p.name; children=p.children; return; } + ~abstract_syntax_tree() + { + children.clear(); + 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; + number=p.number; + str=p.str; + name=p.name; children.clear(); children=p.children; return *this; @@ -41,27 +46,27 @@ class abstract_syntax_tree void set_clear() { type=0; - var_number=0; - var_string=""; - var_name=""; + number=0; + str=""; + name=""; children.clear(); return; } void print_tree(const int n) { - std::string str=""; + std::string _str=""; for(int i=0;i2 && (str[1]=='x' || str[1]=='o')) + if((int)_str.length()>2 && (_str[1]=='x' || _str[1]=='o')) { - if(str[1]=='x') + if(_str[1]=='x') { - int num=0; - int pw=1; - for(int i=(int)str.length()-1;i>1;--i) + double num=0; + double 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; + 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*=16; } - var_number=(double)num; + number=num; } else { - int num=0; - int pw=1; - for(int i=(int)str.length()-1;i>1;--i) + double num=0; + double pw=1; + for(int i=(int)_str.length()-1;i>1;--i) { - num+=(str[i]-'0')*pw; - pw<<=3; + num+=(_str[i]-'0')*pw; + pw*=8; } - var_number=(double)num; + number=num; } return; } int dot_place=-1; - for(int i=0;i<(int)str.length();++i) - if(str[i]=='.') + for(int i=0;i<(int)_str.length();++i) + if(_str[i]=='.') { dot_place=i; break; } if(dot_place==-1) { - var_number=0; + number=0; double pw=1; - for(int i=(int)str.length()-1;i>=0;--i) + for(int i=(int)_str.length()-1;i>=0;--i) { - var_number+=(str[i]-'0')*pw; + number+=(_str[i]-'0')*pw; pw*=10; } } else { - var_number=0; + number=0; double pw=0.1; - for(int i=dot_place+1;i<(int)str.length();++i) + for(int i=dot_place+1;i<(int)_str.length();++i) { - var_number+=(str[i]-'0')*pw; + number+=(_str[i]-'0')*pw; pw/=10; } pw=1; for(int i=dot_place-1;i>=0;--i) { - var_number+=(str[i]-'0')*pw; + number+=(_str[i]-'0')*pw; pw*=10; } } return; } - void set_name(std::string& str) + void set_name(std::string _str) { - var_name=str; + name=_str; return; } void add_child(abstract_syntax_tree p) @@ -170,15 +175,15 @@ class abstract_syntax_tree } double get_number() { - return var_number; + return number; } std::string get_string() { - return var_string; + return str; } std::string get_name() { - return var_name; + return name; } std::list& get_children() { diff --git a/balloon/balloon_lexer.h b/balloon/balloon_lexer.h index d509693..dcb6ca4 100644 --- a/balloon/balloon_lexer.h +++ b/balloon/balloon_lexer.h @@ -88,7 +88,10 @@ class resource_file c=fin.get(); if(fin.eof()) break; - resource.push_back(c); + if(0<=c && c<128) + resource.push_back(c); + else + resource.push_back(' '); } resource.push_back('\n'); return; @@ -103,7 +106,8 @@ class resource_file std::cout<::iterator i=resource.begin();i!=resource.end();++i) { - std::cout<<*i; + if(32<=*i && *i<128 || *i=='\n') + std::cout<<*i; if(*i=='\n') { ++line; @@ -163,7 +167,7 @@ class balloon_lexer int line=1; std::string token_str; std::list::iterator ptr=res.begin(); - while(1) + while(ptr!=res.end()) { while(*ptr==' ' || *ptr=='\n' || *ptr=='\t' || *ptr=='\r' || *ptr<0 || *ptr>127) { @@ -318,8 +322,14 @@ class balloon_lexer if(ptr==res.end()) break; } + else + { + ++error; + std::cout<<">>[Lexer-error] line "<>[Lexer] complete scanning. "<>[Lexer] complete scanning."<>[Lexer] complete generating. "<>[Lexer] complete generating."<& get_detail_token() diff --git a/balloon/balloon_parse.h b/balloon/balloon_parse.h index 1532387..87d37d0 100644 --- a/balloon/balloon_parse.h +++ b/balloon/balloon_parse.h @@ -8,6 +8,7 @@ class balloon_parse token this_token; int error; int warning; + abstract_syntax_tree root; public: void get_token() { @@ -22,6 +23,7 @@ class balloon_parse } void get_detail_token_stream(std::list& tk_list) { + root.set_clear(); while(!parse.empty()) parse.pop(); if(tk_list.empty()) @@ -63,7 +65,39 @@ class balloon_parse std::cout<>[Parse-error] line "<>[Parse] complete generating."<