Bug fixed

This commit is contained in:
Valk Richard Li 2019-09-11 07:42:25 -05:00 committed by GitHub
parent 2535a7f0c8
commit 9e400686bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 228 additions and 27 deletions

View File

@ -22,16 +22,52 @@ class ast_tree_node
fnum=0;
str="";
}
void set_line(int _line)
{
line=_line;
return;
}
int return_type()
{
return type;
}
void add_child(ast_tree_node& new_child)
{
children.push_back(new_child);
return;
}
void print()
void clear_tree()
{
line=0;
type=__root;
children.clear();
num=0;
fnum=0;
str="";
return;
}
int child_num()
{
int cnt=0;
for(std::list<ast_tree_node>::iterator i=children.begin();i!=children.end();++i)
++cnt;
return cnt;
}
void print(int tab_num)
{
for(int i=0;i<tab_num;++i)
std::cout<<" ";
if(type==__number)
{
std::cout<<"["<<num<<"]"<<std::endl;
std::cout<<"[";
if(num==0 && fnum!=0)
std::cout<<fnum;
else if(num!=0 && fnum==0)
std::cout<<num;
else
std::cout<<0;
std::cout<<"]"<<std::endl;
return;
}
else if(type==__string)
@ -39,15 +75,13 @@ class ast_tree_node
std::cout<<"["<<str<<"]"<<std::endl;
return;
}
if(type==__root)
{
std::cout<<"{[";
print_token(type);
std::cout<<"]"<<std::endl;
}
std::cout<<"{[";
print_token(type);
std::cout<<"]"<<std::endl;
for(std::list<ast_tree_node>::iterator i=children.begin();i!=children.end();++i)
i->print();
i->print(tab_num+1);
for(int i=0;i<tab_num;++i)
std::cout<<" ";
std::cout<<"}"<<std::endl;
return;
}
@ -72,16 +106,6 @@ class number_expr:public ast_tree_node
{
type=__number;
}
void setnum(long long int n)
{
num=n;
return;
}
void setfnum(double n)
{
fnum=n;
return;
}
long long int return_num()
{
return num;
@ -90,6 +114,46 @@ class number_expr:public ast_tree_node
{
return fnum;
}
void set_number(std::string& str)
{
type=__number;
double is_float=false;
int DotPlace=0;
for(int i=0;i<(int)str.length();++i)
if(str[i]=='.')
{
is_float=true;
DotPlace=i;
break;
}
if(!is_float)
{
num=0;
double acc=1;
for(int i=(int)str.length()-1;i>=0;--i)
{
num+=acc*((double)(str[i]-'0'));
acc*=10;
}
}
else
{
fnum=0;
double acc=1;
double aff=0.1;
for(int i=DotPlace+1;i<(int)str.length();++i)
{
fnum+=aff*((double)(str[i]-'0'));
aff*=0.1;
}
for(int i=DotPlace-1;i>=0;--i)
{
fnum+=acc*((double)(str[i]-'0'));
acc*=10;
}
}
return;
}
};
class string_expr:public ast_tree_node
{
@ -98,7 +162,7 @@ class string_expr:public ast_tree_node
{
type=__string;
}
void setstr(std::string& t)
void set_str(std::string& t)
{
str=t;
return;

View File

@ -6,10 +6,102 @@ class ast_generator
private:
ast_tree_node root;
std::stack<parse_unit> parse;
std::stack<ast_tree_node> node_cache;
bool can_run;
public:
ast_generator()
{
;
can_run=true;
}
bool number_expr_gen()
{
number_expr t;
t.set_line(parse.top().line);
t.set_number(parse.top().content);
if(node_cache.empty())
{
node_cache.push(t);
return true;
}
int type=node_cache.top().return_type();
if((type==__add_operator || type==__sub_operator || type==__mul_operator || type==__div_operator || type==__link_operator) && node_cache.top().child_num()<=1)
{
node_cache.top().add_child(t);
return true;
}
else
{
node_cache.push(t);
return true;
}
return true;
}
bool string_expr_gen()
{
string_expr t;
t.set_line(parse.top().line);
std::string tstr=parse.top().content;
std::string str="";
for(int i=1;i<(int)tstr.length()-1;++i)
str+=tstr[i];
t.set_str(str);
if(node_cache.empty())
{
node_cache.push(t);
return true;
}
int type=node_cache.top().return_type();
if((type==__add_operator || type==__sub_operator || type==__mul_operator || type==__div_operator || type==__link_operator) && node_cache.top().child_num()<2)
{
node_cache.top().add_child(t);
return true;
}
else
{
node_cache.push(t);
return true;
}
return true;
}
bool operator_expr_gen()
{
operator_expr t;
t.set_line(parse.top().line);
t.set_operator_type(parse.top().type);
if(node_cache.empty())
{
if(t.return_type()==__add_operator || t.return_type()==__sub_operator)
{
number_expr nullnum;
nullnum.set_line(parse.top().line);
std::string strnum="0";
nullnum.set_number(strnum);
t.add_child(nullnum);
node_cache.push(t);
return true;
}
else
{
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":missed number or string."<<std::endl;
return false;
}
}
int cache_top_type=node_cache.top().return_type();
if(cache_top_type==__number || cache_top_type==__string)
{
t.add_child(node_cache.top());
node_cache.pop();
node_cache.push(t);
return true;
}
else if(cache_top_type==__add_operator || cache_top_type==__sub_operator || cache_top_type==__mul_operator || cache_top_type==__div_operator || cache_top_type==__link_operator)
{
t.add_child(node_cache.top());
node_cache.pop();
node_cache.push(t);
return true;
}
return true;
}
void input_token_stack(std::stack<parse_unit>& temp)
{
@ -18,10 +110,52 @@ class ast_generator
}
void gen_main_prog()
{
can_run=true;
root.clear_tree();
while(!parse.empty())
{
int type=parse.top().type;
bool is_correct=false;
switch(type)
{
case __number:is_correct=number_expr_gen();break;
case __string:is_correct=string_expr_gen();break;
case __add_operator:is_correct=operator_expr_gen();break;
case __sub_operator:is_correct=operator_expr_gen();break;
case __mul_operator:is_correct=operator_expr_gen();break;
case __div_operator:is_correct=operator_expr_gen();break;
case __link_operator:is_correct=operator_expr_gen();break;
case __semi:is_correct=true;break;
default:
is_correct=false;
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":";
print_token(parse.top().type);
std::cout<<" at an incorrect place."<<std::endl;
break;
}
if(!is_correct)
can_run=false;
parse.pop();
}
std::stack<ast_tree_node> temp_cache;
while(!node_cache.empty())
{
temp_cache.push(node_cache.top());
node_cache.pop();
}
while(!temp_cache.empty())
{
root.add_child(temp_cache.top());
temp_cache.pop();
}
return;
}
void run()
{
if(can_run)
root.print(0);
else
std::cout<<">>[Parse] Error(s) occurred,stop."<<std::endl;
return;
}
};

View File

@ -59,7 +59,7 @@ int main()
lex.lexer_process(prog.use_file());
pas.parse_process(lex.return_list());
cod.input_token_stack(pas.return_stack());
// cod.gen_ast();
cod.gen_main_prog();
// cod.print_gen_tree();
}
else if(command=="run")
@ -67,8 +67,8 @@ int main()
lex.lexer_process(prog.use_file());
pas.parse_process(lex.return_list());
cod.input_token_stack(pas.return_stack());
// if(!cod.gen_ast())
// cod.run_gen_tree();
cod.gen_main_prog();
cod.run();
}
else
prog.input_file(command);

View File

@ -225,7 +225,7 @@ class nasal_lexer
}
else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' ||
temp=='}' || temp==',' || temp==';' || temp=='|' || temp==':' ||
temp=='?' || temp=='.' || temp=='`' || temp=='&'||
temp=='?' || temp=='.' || temp=='`' || temp=='&' || temp=='@' ||
temp=='%' || temp=='$' || temp=='^')
{
__token+=temp;

View File

@ -118,7 +118,7 @@ class nasal_parser
else if((*i).content=="<=")
temp_parse.type=__cmp_less_or_equal;
}
else if(((*i).content==";") || ((*i).content==",") || ((*i).content=="=") || ((*i).content==":") || ((*i).content=="."))
else if(((*i).content==";") || ((*i).content==",") || ((*i).content=="=") || ((*i).content==":") || ((*i).content==".") || ((*i).content=="?") || ((*i).content=="%") || ((*i).content=="$") || ((*i).content=="`") || ((*i).content=="^") || ((*i).content=="@"))
{
char c=(*i).content[0];
switch(c)
@ -128,6 +128,7 @@ class nasal_parser
case '=':temp_parse.type=__equal;break;
case ':':temp_parse.type=__colon;break;
case '.':temp_parse.type=__dot;break;
default:temp_parse.type=__unknown_operator;break;
}
}
else if(((*i).type==NUMBER) || ((*i).type==STRING) || ((*i).type==IDENTIFIER) || ((*i).type==DYNAMIC_ID))

View File

@ -19,6 +19,7 @@ enum token_type
__left_bracket,__right_bracket, // []
__left_curve,__right_curve, // ()
__semi,__comma,__colon,__dot, // ; , : .
__unknown_operator,
__var,__func,__return,
__if,__elsif,__else,
__continue,__break,
@ -67,6 +68,7 @@ void print_token(int type)
case __comma: context=",";break;
case __colon: context=":";break;
case __dot: context=".";break;
case __unknown_operator: context="un_optr";break;
case __var: context="var";break;
case __func: context="func";break;
case __continue: context="ctn";break;