Update
This commit is contained in:
parent
9e400686bb
commit
ccd1026819
|
@ -31,11 +31,23 @@ class ast_tree_node
|
|||
{
|
||||
return type;
|
||||
}
|
||||
int return_line()
|
||||
{
|
||||
return line;
|
||||
}
|
||||
void add_child(ast_tree_node& new_child)
|
||||
{
|
||||
children.push_back(new_child);
|
||||
return;
|
||||
}
|
||||
ast_tree_node& return_last_child()
|
||||
{
|
||||
std::list<ast_tree_node>::iterator i;
|
||||
for(i=children.begin();i!=children.end();++i)
|
||||
;
|
||||
--i;
|
||||
return *i;
|
||||
}
|
||||
void clear_tree()
|
||||
{
|
||||
line=0;
|
||||
|
@ -60,24 +72,24 @@ class ast_tree_node
|
|||
std::cout<<" ";
|
||||
if(type==__number)
|
||||
{
|
||||
std::cout<<"[";
|
||||
std::cout<<"[ number:";
|
||||
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;
|
||||
std::cout<<" ]"<<std::endl;
|
||||
return;
|
||||
}
|
||||
else if(type==__string)
|
||||
{
|
||||
std::cout<<"["<<str<<"]"<<std::endl;
|
||||
std::cout<<"[ string:"<<str<<" ]"<<std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout<<"{[";
|
||||
std::cout<<"{ [ node type:";
|
||||
print_token(type);
|
||||
std::cout<<"]"<<std::endl;
|
||||
std::cout<<" ]"<<std::endl;
|
||||
for(std::list<ast_tree_node>::iterator i=children.begin();i!=children.end();++i)
|
||||
i->print(tab_num+1);
|
||||
for(int i=0;i<tab_num;++i)
|
||||
|
|
|
@ -29,6 +29,13 @@ class ast_generator
|
|||
node_cache.top().add_child(t);
|
||||
return true;
|
||||
}
|
||||
else if((type==__add_operator || type==__sub_operator || type==__link_operator)
|
||||
&& (node_cache.top().return_last_child().return_type()==__mul_operator || node_cache.top().return_last_child().return_type()==__div_operator)
|
||||
&& node_cache.top().return_last_child().child_num()<2)
|
||||
{
|
||||
node_cache.top().return_last_child().add_child(t);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
node_cache.push(t);
|
||||
|
@ -56,6 +63,13 @@ class ast_generator
|
|||
node_cache.top().add_child(t);
|
||||
return true;
|
||||
}
|
||||
else if((type==__add_operator || type==__sub_operator || type==__link_operator)
|
||||
&& (node_cache.top().return_last_child().return_type()==__mul_operator || node_cache.top().return_last_child().return_type()==__div_operator)
|
||||
&& node_cache.top().return_last_child().child_num()<2)
|
||||
{
|
||||
node_cache.top().return_last_child().add_child(t);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
node_cache.push(t);
|
||||
|
@ -94,7 +108,27 @@ class ast_generator
|
|||
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)
|
||||
else if(cache_top_type==__add_operator || cache_top_type==__sub_operator || cache_top_type==__link_operator)
|
||||
{
|
||||
if(parse.top().type==__add_operator || parse.top().type==__sub_operator || parse.top().type==__link_operator)
|
||||
{
|
||||
t.add_child(node_cache.top());
|
||||
node_cache.pop();
|
||||
node_cache.push(t);
|
||||
}
|
||||
else if(parse.top().type==__mul_operator || parse.top().type==__div_operator)
|
||||
{
|
||||
if(node_cache.top().return_last_child().child_num()<2)
|
||||
{
|
||||
std::cout<<">>[Error] parse error:missing number or string at line "<<node_cache.top().return_line()<<"."<<std::endl;
|
||||
return false;
|
||||
}
|
||||
t.add_child(node_cache.top().return_last_child());
|
||||
node_cache.top().return_last_child()=t;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if((cache_top_type==__mul_operator || cache_top_type==__div_operator) && node_cache.top().child_num()<2)
|
||||
{
|
||||
t.add_child(node_cache.top());
|
||||
node_cache.pop();
|
||||
|
@ -125,7 +159,7 @@ class ast_generator
|
|||
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;
|
||||
case __semi:is_correct=true;root.add_child(node_cache.top());node_cache.pop();break;
|
||||
default:
|
||||
is_correct=false;
|
||||
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":";
|
||||
|
@ -150,6 +184,14 @@ class ast_generator
|
|||
}
|
||||
return;
|
||||
}
|
||||
void print_ast()
|
||||
{
|
||||
//if(can_run)
|
||||
root.print(0);
|
||||
//else
|
||||
// std::cout<<">>[Parse] Error(s) occurred,stop."<<std::endl;
|
||||
return;
|
||||
}
|
||||
void run()
|
||||
{
|
||||
if(can_run)
|
||||
|
|
|
@ -7,7 +7,7 @@ int main()
|
|||
ast_generator cod;
|
||||
std::string command;
|
||||
std::cout<<">> Nasal interpreter by ValKmjolnir"<<std::endl;
|
||||
std::cout<<">> input [help] to find help."<<std::endl;
|
||||
std::cout<<">> Input [help] to find help."<<std::endl;
|
||||
while(1)
|
||||
{
|
||||
std::cout<<">> ";
|
||||
|
@ -60,7 +60,7 @@ int main()
|
|||
pas.parse_process(lex.return_list());
|
||||
cod.input_token_stack(pas.return_stack());
|
||||
cod.gen_main_prog();
|
||||
// cod.print_gen_tree();
|
||||
cod.print_ast();
|
||||
}
|
||||
else if(command=="run")
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue