This commit is contained in:
Valk Richard Li 2020-11-13 00:02:12 +08:00
parent dc81773e7d
commit 5aabbd8d42
2 changed files with 160 additions and 300 deletions

View File

@ -104,76 +104,6 @@ std::string ast_str(int type)
return str; return str;
} }
enum parse_error
{
error_token,
error_expr,
lack_left_curve,
lack_right_curve,
lack_left_bracket,
lack_right_bracket,
lack_left_brace,
lack_right_brace,
exprs_lack_rbrace,
lack_semi,
lack_comma,
lack_colon,
lack_equal,
lack_scalar,
lack_identifier,
lack_calculation,
lack_exprs,
lack_token,
lack_args,
default_arg_not_end,
dynamic_id_not_end,
name_repetition,
definition_use_call,
multi_id_use_call,
multi_assign_lack_val,
lack_definition,
lack_loop_iter,
lack_func_content
};
void error_info(int line,int error_type,std::string error_str="")
{
std::string detail;
std::cout<<">> [parse] line "<<line<<": ";
switch(error_type)
{
case error_token: std::cout<<"error token \""+error_str+"\".\n"; break;
case error_expr: std::cout<<"error expression \""+error_str+"\".\n"; break;
case lack_left_curve: std::cout<<"expected \"(\".\n"; break;
case lack_right_curve: std::cout<<"expected \")\".\n"; break;
case lack_left_bracket: std::cout<<"expected \"[\".\n"; break;
case lack_right_bracket: std::cout<<"expected \"]\".\n"; break;
case lack_left_brace: std::cout<<"expected \"{\".\n"; break;
case lack_right_brace: std::cout<<"expected \"}\".\n"; break;
case exprs_lack_rbrace: std::cout<<"expected \"}\" with this line\'s \"{\".\n";break;
case lack_semi: std::cout<<"expected \";\".\n"; break;
case lack_comma: std::cout<<"expected \",\".\n"; break;
case lack_colon: std::cout<<"expected \":\".\n"; break;
case lack_equal: std::cout<<"expected \"=\".\n"; break;
case lack_scalar: std::cout<<"expected scalar here.\n"; break;
case lack_identifier: std::cout<<"expected identifier here.\n"; break;
case lack_calculation: std::cout<<"expected arithmetic-expression here.\n"; break;
case lack_exprs: std::cout<<"expected expression block here.\n"; break;
case lack_token: std::cout<<"expected \""+error_str+"\" here.\n"; break;
case lack_args: std::cout<<"expected arguments here.\n"; break;
case default_arg_not_end: std::cout<<"default argument missing for parameter of "+error_str+".\n";break;
case dynamic_id_not_end: std::cout<<"dynamic id must be the end of "+error_str+".\n";break;
case name_repetition: std::cout<<"this identifier name has existed.\n";break;
case definition_use_call: std::cout<<"should not use call_scalar in definition progress.\n";break;
case multi_id_use_call: std::cout<<"should not use call_scalar in multi_id progress.\n";break;
case multi_assign_lack_val:std::cout<<"multi-assignment lacks value list.\n";break;
case lack_definition: std::cout<<"expected a definition expression here.\n";break;
case lack_loop_iter: std::cout<<"expected an iterator to loop through.\n";break;
case lack_func_content: std::cout<<"expected arguments or expression block here.\n";break;
}
return;
}
enum runtime_scalar_type enum runtime_scalar_type
{ {
vm_nil=0, vm_nil=0,

View File

@ -49,6 +49,7 @@ private:
nasal_ast root; nasal_ast root;
std::vector<token> tok_list; std::vector<token> tok_list;
void reset(); void reset();
void die(int,std::string);
bool check_multi_definition(); bool check_multi_definition();
bool check_multi_scalar(); bool check_multi_scalar();
bool check_function_end(nasal_ast&); bool check_function_end(nasal_ast&);
@ -134,16 +135,15 @@ void nasal_parse::main_process()
{ {
root.add_child(expr()); root.add_child(expr());
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_semi) if(ptr>=tok_list_size)
break;
if(tok_list[ptr].type==tok_semi)
++ptr; ++ptr;
else if(root.get_children().empty() || need_semi_check(root.get_children().back())) else if(need_semi_check(root.get_children().back()))
{ {
// the last expression can be recognized without semi // the last expression can be recognized without semi
if(ptr<tok_list_size) if(ptr<tok_list_size)
{ die(error_line,"expected \";\"");
++error;
error_info(error_line-1,lack_semi);
}
} }
if(root.get_children().size()) if(root.get_children().size())
{ {
@ -151,7 +151,7 @@ void nasal_parse::main_process()
if(type==ast_continue || type==ast_break || type==ast_return) if(type==ast_continue || type==ast_break || type==ast_return)
{ {
++error; ++error;
error_info(root.get_children().back().get_line(),error_expr,ast_str(type)); die(root.get_children().back().get_line(),ast_str(type)+" is not allowed in main scope");
} }
} }
} }
@ -171,6 +171,13 @@ void nasal_parse::reset()
return; return;
} }
void nasal_parse::die(int line,std::string info)
{
++error;
std::cout<<">> [parse] line "<<line<<": "<<info<<".\n";
return;
}
bool nasal_parse::check_multi_definition() bool nasal_parse::check_multi_definition()
{ {
return ptr+1<tok_list_size && tok_list[ptr+1].type==tok_var; return ptr+1<tok_list_size && tok_list[ptr+1].type==tok_var;
@ -322,19 +329,15 @@ nasal_ast nasal_parse::vector_gen()
{ {
node.add_child(calculation()); node.add_child(calculation());
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_comma) ++ptr; if(ptr>=tok_list_size)
else if(ptr<tok_list_size && tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_bracket) break;
{ if(tok_list[ptr].type==tok_comma)
error_info(error_line,lack_comma); ++ptr;
++error; else if(tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_bracket)
break; break;
}
} }
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_bracket) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_bracket)
{ die(error_line,"expected \"]\"");
++error;
error_info(error_line,lack_right_bracket);
}
return node; return node;
} }
nasal_ast nasal_parse::hash_gen() nasal_ast nasal_parse::hash_gen()
@ -347,20 +350,15 @@ nasal_ast nasal_parse::hash_gen()
{ {
node.add_child(hash_member_gen()); node.add_child(hash_member_gen());
++ptr; ++ptr;
if(ptr>=tok_list_size) break; if(ptr>=tok_list_size)
if(tok_list[ptr].type==tok_comma) ++ptr; break;
else if(tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_brace) if(tok_list[ptr].type==tok_comma)
{ ++ptr;
error_info(error_line,lack_comma); else if(tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_brace)
++error;
break; break;
}
} }
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace)
{ die(error_line,"expected \"}\"");
++error;
error_info(error_line,lack_right_brace);
}
return node; return node;
} }
nasal_ast nasal_parse::hash_member_gen() nasal_ast nasal_parse::hash_member_gen()
@ -368,8 +366,7 @@ nasal_ast nasal_parse::hash_member_gen()
nasal_ast node; nasal_ast node;
if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_identifier && tok_list[ptr].type!=tok_string)) if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_identifier && tok_list[ptr].type!=tok_string))
{ {
error_info(error_line,lack_identifier); die(error_line,"expected identifier/string");
++error;
return node; return node;
} }
node.set_line(tok_list[ptr].line); node.set_line(tok_list[ptr].line);
@ -378,17 +375,14 @@ nasal_ast nasal_parse::hash_member_gen()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_colon) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_colon)
{ {
error_info(error_line,lack_colon); die(error_line,"expected \":\"");
++error;
return node; return node;
} }
++ptr; ++ptr;
if(ptr<tok_list_size) node.add_child(calculation()); if(ptr<tok_list_size)
node.add_child(calculation());
else else
{ die(error_line,"expected scalar");
error_info(error_line,lack_scalar);
++error;
}
return node; return node;
} }
nasal_ast nasal_parse::func_gen() nasal_ast nasal_parse::func_gen()
@ -399,14 +393,12 @@ nasal_ast nasal_parse::func_gen()
++ptr; ++ptr;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
error_info(error_line,lack_left_curve); die(error_line,"expected \"(\"");
++error;
return node; return node;
} }
if(tok_list[ptr].type!=tok_left_curve && tok_list[ptr].type!=tok_left_brace) if(tok_list[ptr].type!=tok_left_curve && tok_list[ptr].type!=tok_left_brace)
{ {
++error; die(error_line,"expected argument(s)/expression block");
error_info(error_line,lack_func_content);
return node; return node;
} }
if(tok_list[ptr].type==tok_left_curve) if(tok_list[ptr].type==tok_left_curve)
@ -421,8 +413,7 @@ nasal_ast nasal_parse::func_gen()
} }
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
error_info(error_line,lack_left_brace); die(error_line,"expected expression block");
++error;
return node; return node;
} }
node.add_child(exprs_gen()); node.add_child(exprs_gen());
@ -439,8 +430,7 @@ nasal_ast nasal_parse::args_list_gen()
nasal_ast tmp; nasal_ast tmp;
if(tok_list[ptr].type!=tok_identifier) if(tok_list[ptr].type!=tok_identifier)
{ {
++error; die(error_line,"expected identifier");
error_info(error_line,lack_args);
return node; return node;
} }
tmp=id_gen(); tmp=id_gen();
@ -469,19 +459,17 @@ nasal_ast nasal_parse::args_list_gen()
node.add_child(tmp); node.add_child(tmp);
} }
++ptr; ++ptr;
if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_curve)) if(ptr>=tok_list_size)
{ break;
++error;
error_info(error_line,lack_comma);
return node;
}
if(tok_list[ptr].type==tok_comma) if(tok_list[ptr].type==tok_comma)
++ptr; ++ptr;
else if(tok_list[ptr].type!=tok_right_curve)
break;
} }
if(ptr>=tok_list_size) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ {
++error; die(error_line,"expected \")\"");
error_info(error_line,lack_right_curve); return node;
} }
std::string args_format="func("; std::string args_format="func(";
@ -507,15 +495,9 @@ nasal_ast nasal_parse::args_list_gen()
else if(node.get_children()[i].get_type()==ast_dynamic_id) else if(node.get_children()[i].get_type()==ast_dynamic_id)
checked_dynamic_ids=true; checked_dynamic_ids=true;
if(checked_default_val && node.get_children()[i].get_type()!=ast_default_arg) if(checked_default_val && node.get_children()[i].get_type()!=ast_default_arg)
{ die(error_line,"default argument must be the end of argument list: "+args_format);
++error;
error_info(node.get_children()[i].get_line(),default_arg_not_end,args_format);
}
if(checked_dynamic_ids && i!=node_child_size-1) if(checked_dynamic_ids && i!=node_child_size-1)
{ die(error_line,"dynamic identifier must be the end of argument list: "+args_format);
++error;
error_info(node.get_children()[i].get_line(),dynamic_id_not_end,args_format);
}
} }
std::map<std::string,bool> argname_table; std::map<std::string,bool> argname_table;
for(int i=0;i<node_child_size;++i) for(int i=0;i<node_child_size;++i)
@ -529,10 +511,7 @@ nasal_ast nasal_parse::args_list_gen()
case ast_default_arg:new_name=node.get_children()[i].get_children()[0].get_str();break; case ast_default_arg:new_name=node.get_children()[i].get_children()[0].get_str();break;
} }
if(argname_table.find(new_name)!=argname_table.end()) if(argname_table.find(new_name)!=argname_table.end())
{ die(node.get_children()[i].get_line(),"argument name should not repeat");
error_info(node.get_children()[i].get_line(),name_repetition);
++error;
}
else else
argname_table[new_name]=true; argname_table[new_name]=true;
} }
@ -571,7 +550,7 @@ nasal_ast nasal_parse::expr()
case tok_break: node=break_expr(); break; case tok_break: node=break_expr(); break;
case tok_return: node=return_expr(); break; case tok_return: node=return_expr(); break;
case tok_semi: --ptr; break; case tok_semi: --ptr; break;
default: error_info(error_line,error_token,tok_list[ptr].str);++error;break; default: die(error_line,"error token \""+tok_list[ptr].str+"\"");break;
} }
return node; return node;
} }
@ -580,8 +559,7 @@ nasal_ast nasal_parse::exprs_gen()
nasal_ast node; nasal_ast node;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
++error; die(error_line,"expected expression block");
error_info(error_line,lack_exprs);
return node; return node;
} }
node.set_line(tok_list[ptr].line); node.set_line(tok_list[ptr].line);
@ -594,23 +572,29 @@ nasal_ast nasal_parse::exprs_gen()
{ {
node.add_child(expr()); node.add_child(expr());
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_semi) if(ptr>=tok_list_size)
break;
else if(tok_list[ptr].type==tok_semi)
++ptr; ++ptr;
else if(node.get_children().empty() || need_semi_check(node.get_children().back())) else if(need_semi_check(node.get_children().back()))
{ {
// the last expression can be recognized without semi // the last expression can be recognized without semi
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace)
{ {
++error; die(error_line,"expected \";\"");
error_info(error_line,lack_semi);
break; break;
} }
} }
} }
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_brace)
{ {
++error; std::string lb_line="";
error_info(left_brace_line,exprs_lack_rbrace); while(left_brace_line>1)
{
lb_line=(char)('0'+(left_brace_line%10))+lb_line;
left_brace_line/=10;
}
die(error_line,"expected \"}\" to match \"{\" in line "+lb_line);
} }
} }
else else
@ -627,14 +611,14 @@ nasal_ast nasal_parse::calculation()
nasal_ast node; nasal_ast node;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
++error; die(error_line,"expected calculation");
error_info(error_line,lack_calculation);
return node; return node;
} }
node=or_expr(); node=or_expr();
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_quesmark) if(ptr<tok_list_size && tok_list[ptr].type==tok_quesmark)
{ {
// trinocular calculation
nasal_ast tmp; nasal_ast tmp;
tmp.set_line(tok_list[ptr].line); tmp.set_line(tok_list[ptr].line);
tmp.set_type(ast_trinocular); tmp.set_type(ast_trinocular);
@ -643,20 +627,20 @@ nasal_ast nasal_parse::calculation()
if(ptr<tok_list_size) tmp.add_child(calculation()); if(ptr<tok_list_size) tmp.add_child(calculation());
else else
{ {
++error; die(error_line,"expected calculation");
error_info(error_line,lack_calculation);
return node; return node;
} }
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_colon) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_colon)
{ {
++error; die(error_line,"expected \":\"");
error_info(error_line,lack_colon);
return node; return node;
} }
++ptr; ++ptr;
if(ptr<tok_list_size) tmp.add_child(calculation()); if(ptr<tok_list_size)
else{ ++error; error_info(error_line,lack_calculation);} tmp.add_child(calculation());
else
die(error_line,"expected calculation");
node=tmp; node=tmp;
} }
else if( else if(
@ -671,6 +655,7 @@ nasal_ast nasal_parse::calculation()
) )
) )
{ {
// assignment
nasal_ast tmp; nasal_ast tmp;
tmp.set_line(tok_list[ptr].line); tmp.set_line(tok_list[ptr].line);
switch(tok_list[ptr].type) switch(tok_list[ptr].type)
@ -684,8 +669,10 @@ nasal_ast nasal_parse::calculation()
} }
tmp.add_child(node); tmp.add_child(node);
++ptr; ++ptr;
if(ptr<tok_list_size) tmp.add_child(calculation()); if(ptr<tok_list_size)
else{ ++error; error_info(error_line,lack_calculation);} tmp.add_child(calculation());
else
die(error_line,"expected calculation");
node=tmp; node=tmp;
} }
else --ptr; else --ptr;
@ -703,8 +690,10 @@ nasal_ast nasal_parse::or_expr()
tmp.set_type(ast_or); tmp.set_type(ast_or);
tmp.add_child(node); tmp.add_child(node);
++ptr; ++ptr;
if(ptr<tok_list_size) tmp.add_child(and_expr()); if(ptr<tok_list_size)
else{ ++error; error_info(error_line,lack_calculation);} tmp.add_child(and_expr());
else
die(error_line,"expected calculation");
node=tmp; node=tmp;
++ptr; ++ptr;
} }
@ -723,8 +712,10 @@ nasal_ast nasal_parse::and_expr()
tmp.set_type(ast_and); tmp.set_type(ast_and);
tmp.add_child(node); tmp.add_child(node);
++ptr; ++ptr;
if(ptr<tok_list_size) tmp.add_child(cmp_expr()); if(ptr<tok_list_size)
else{ ++error; error_info(error_line,lack_calculation);} tmp.add_child(cmp_expr());
else
die(error_line,"expected calculation");
node=tmp; node=tmp;
++ptr; ++ptr;
} }
@ -761,8 +752,10 @@ nasal_ast nasal_parse::cmp_expr()
} }
tmp.add_child(node); tmp.add_child(node);
++ptr; ++ptr;
if(ptr<tok_list_size) tmp.add_child(additive_expr()); if(ptr<tok_list_size)
else{ ++error; error_info(error_line,lack_calculation);} tmp.add_child(additive_expr());
else
die(error_line,"expected calculation");
node=tmp; node=tmp;
++ptr; ++ptr;
} }
@ -786,8 +779,11 @@ nasal_ast nasal_parse::additive_expr()
} }
tmp.add_child(node); tmp.add_child(node);
++ptr; ++ptr;
if(ptr<tok_list_size) tmp.add_child(multive_expr()); if(ptr<tok_list_size)
else{ ++error; error_info(error_line,lack_calculation);} tmp.add_child(multive_expr());
else
die(error_line,"expected calculation");
// pre-calculation
if(tmp.get_type()!=ast_link && tmp.get_children()[0].get_type()==ast_number && tmp.get_children()[1].get_type()==ast_number) if(tmp.get_type()!=ast_link && tmp.get_children()[0].get_type()==ast_number && tmp.get_children()[1].get_type()==ast_number)
{ {
double num1=tmp.get_children()[0].get_num(); double num1=tmp.get_children()[0].get_num();
@ -848,10 +844,10 @@ nasal_ast nasal_parse::multive_expr()
tmp.add_child((tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar()); tmp.add_child((tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar());
else else
{ {
++error; die(error_line,"expected calculation");
error_info(error_line,lack_calculation);
break; break;
} }
// pre-calculation
if(tmp.get_children()[0].get_type()==ast_number && tmp.get_children()[1].get_type()==ast_number) if(tmp.get_children()[0].get_type()==ast_number && tmp.get_children()[1].get_type()==ast_number)
{ {
double num1=tmp.get_children()[0].get_num(); double num1=tmp.get_children()[0].get_num();
@ -884,7 +880,9 @@ nasal_ast nasal_parse::unary()
else else
node.add_child(scalar()); node.add_child(scalar());
} }
else{ ++error; error_info(error_line,lack_calculation);} else
die(error_line,"expected calculation");
// pre-calculation
if(node.get_children()[0].get_type()==ast_number) if(node.get_children()[0].get_type()==ast_number)
{ {
double num=node.get_children()[0].get_num(); double num=node.get_children()[0].get_num();
@ -927,19 +925,17 @@ nasal_ast nasal_parse::scalar()
else if(tok_list[ptr].type==tok_left_curve) else if(tok_list[ptr].type==tok_left_curve)
{ {
++ptr; ++ptr;
if(ptr<tok_list_size) node=calculation(); if(ptr<tok_list_size)
else{ ++error; error_info(node.get_line(),lack_calculation);} node=calculation();
else
die(error_line,"expected calculation");
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ die(error_line,"expected \")\"");
++error;
error_info(error_line,lack_right_curve);
}
} }
else else
{ {
++error; die(error_line,"expected scalar");
error_info(error_line,lack_scalar);
return node; return node;
} }
++ptr; ++ptr;
@ -979,10 +975,7 @@ nasal_ast nasal_parse::call_hash()
if(ptr<tok_list_size && tok_list[ptr].type==tok_identifier) if(ptr<tok_list_size && tok_list[ptr].type==tok_identifier)
node.set_str(tok_list[ptr].str); node.set_str(tok_list[ptr].str);
else else
{ die(error_line,"expected identifier");
++error;
error_info(error_line,lack_identifier);
}
return node; return node;
} }
nasal_ast nasal_parse::call_vector() nasal_ast nasal_parse::call_vector()
@ -995,19 +988,15 @@ nasal_ast nasal_parse::call_vector()
{ {
node.add_child(subvec()); node.add_child(subvec());
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_comma) ++ptr; if(ptr>=tok_list_size)
else if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_bracket) break;
{ else if(tok_list[ptr].type==tok_comma)
++error; ++ptr;
error_info(error_line,lack_comma); else if(tok_list[ptr].type!=tok_right_bracket)
break; break;
}
} }
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_bracket) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_bracket)
{ die(error_line,"expected \"]\"");
++error;
error_info(error_line,lack_right_bracket);
}
return node; return node;
} }
nasal_ast nasal_parse::call_func() nasal_ast nasal_parse::call_func()
@ -1021,19 +1010,15 @@ nasal_ast nasal_parse::call_func()
{ {
node.add_child(special_call?hash_member_gen():calculation()); node.add_child(special_call?hash_member_gen():calculation());
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_comma) ++ptr; if(ptr>=tok_list_size)
else if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_curve)) break;
{ else if(tok_list[ptr].type==tok_comma)
++error; ++ptr;
error_info(error_line,lack_comma); else if(tok_list[ptr].type!=tok_right_curve)
break; break;
}
} }
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ die(error_line,"expected \")\"");
++error;
error_info(error_line,lack_right_curve);
}
return node; return node;
} }
nasal_ast nasal_parse::subvec() nasal_ast nasal_parse::subvec()
@ -1060,11 +1045,13 @@ nasal_ast nasal_parse::subvec()
--ptr; --ptr;
tmp.add_child(nil_gen()); tmp.add_child(nil_gen());
} }
else tmp.add_child(calculation()); else
tmp.add_child(calculation());
node=tmp; node=tmp;
} }
} }
else --ptr; else
--ptr;
return node; return node;
} }
nasal_ast nasal_parse::definition() nasal_ast nasal_parse::definition()
@ -1080,8 +1067,7 @@ nasal_ast nasal_parse::definition()
case tok_identifier:node.add_child(normal_def()); break; case tok_identifier:node.add_child(normal_def()); break;
case tok_left_curve:node.add_child(var_outcurve_def()); break; case tok_left_curve:node.add_child(var_outcurve_def()); break;
default: default:
++error; die(error_line,"expected identifier");
error_info(error_line,lack_identifier);
return node; return node;
} }
} }
@ -1090,18 +1076,13 @@ nasal_ast nasal_parse::definition()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_equal) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_equal)
{ {
++error; die(error_line,"expected \"=\" / don\'t call identifier in definition");
if(ptr<tok_list_size && (tok_list[ptr].type==tok_dot || tok_list[ptr].type==tok_left_bracket || tok_list[ptr].type==tok_left_curve) && !node.get_children().back().get_children().size())
error_info(error_line,definition_use_call);
else
error_info(error_line,lack_equal);
return node; return node;
} }
++ptr; ++ptr;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
++error; die(error_line,"expected scalar");
error_info(error_line,lack_scalar);
return node; return node;
} }
if(tok_list[ptr].type==tok_left_curve) if(tok_list[ptr].type==tok_left_curve)
@ -1125,22 +1106,15 @@ nasal_ast nasal_parse::var_incurve_def()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier)
{ {
++error; die(error_line,"expected identifier");
error_info(error_line,lack_identifier);
return node; return node;
} }
node=multi_id(); node=multi_id();
++ptr; ++ptr;
if(ptr<tok_list_size && (tok_list[ptr].type==tok_dot || tok_list[ptr].type==tok_left_bracket || tok_list[ptr].type==tok_left_curve)) if(ptr<tok_list_size && (tok_list[ptr].type==tok_dot || tok_list[ptr].type==tok_left_bracket || tok_list[ptr].type==tok_left_curve))
{ die(error_line,"don\'t call identifier in multi-definition");
++error;
error_info(error_line,multi_id_use_call);
}
else if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) else if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ die(error_line,"expected \")\"");
++error;
error_info(error_line,lack_right_curve);
}
return node; return node;
} }
nasal_ast nasal_parse::var_outcurve_def() nasal_ast nasal_parse::var_outcurve_def()
@ -1149,22 +1123,15 @@ nasal_ast nasal_parse::var_outcurve_def()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier)
{ {
++error; die(error_line,"expected identifier");
error_info(error_line,lack_identifier);
return node; return node;
} }
node=multi_id(); node=multi_id();
++ptr; ++ptr;
if(ptr<tok_list_size && (tok_list[ptr].type==tok_dot || tok_list[ptr].type==tok_left_bracket || tok_list[ptr].type==tok_left_curve)) if(ptr<tok_list_size && (tok_list[ptr].type==tok_dot || tok_list[ptr].type==tok_left_bracket || tok_list[ptr].type==tok_left_curve))
{ die(error_line,"don\'t call identifier in multi-definition");
++error;
error_info(error_line,multi_id_use_call);
}
else if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) else if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ die(error_line,"expected \")\"");
++error;
error_info(error_line,lack_right_curve);
}
return node; return node;
} }
nasal_ast nasal_parse::multi_id() nasal_ast nasal_parse::multi_id()
@ -1172,28 +1139,22 @@ nasal_ast nasal_parse::multi_id()
nasal_ast node; nasal_ast node;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier)
{ {
++error; die(error_line,"expected identifier");
error_info(error_line,lack_identifier);
return node; return node;
} }
node.set_line(tok_list[ptr].line); node.set_line(tok_list[ptr].line);
node.set_type(ast_multi_id); node.set_type(ast_multi_id);
node.add_child(id_gen()); while(ptr<tok_list_size)
++ptr;
while(ptr<tok_list_size && tok_list[ptr].type==tok_comma)
{ {
node.add_child(id_gen());
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_identifier) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_comma)
node.add_child(id_gen());
else
{ {
++error; --ptr;
error_info(error_line,lack_identifier);
break; break;
} }
++ptr; ++ptr;
} }
--ptr;
return node; return node;
} }
nasal_ast nasal_parse::multi_scalar() nasal_ast nasal_parse::multi_scalar()
@ -1206,20 +1167,15 @@ nasal_ast nasal_parse::multi_scalar()
{ {
node.add_child(calculation()); node.add_child(calculation());
++ptr; ++ptr;
if(ptr>=tok_list_size) break; if(ptr>=tok_list_size)
if(tok_list[ptr].type==tok_comma) ++ptr; break;
else if(tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_curve) if(tok_list[ptr].type==tok_comma)
{ ++ptr;
++error; else if(tok_list[ptr].type!=tok_right_curve)
error_info(error_line,lack_comma);
break; break;
}
} }
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ die(error_line,"expected \")\"");
++error;
error_info(error_line,lack_right_curve);
}
return node; return node;
} }
nasal_ast nasal_parse::multi_assgin() nasal_ast nasal_parse::multi_assgin()
@ -1231,15 +1187,13 @@ nasal_ast nasal_parse::multi_assgin()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_equal) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_equal)
{ {
++error; die(error_line,"expected \"=\"");
error_info(error_line,lack_equal);
return node; return node;
} }
++ptr; ++ptr;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
++error; die(error_line,"expected value list");
error_info(error_line,multi_assign_lack_val);
return node; return node;
} }
if(tok_list[ptr].type==tok_left_curve) if(tok_list[ptr].type==tok_left_curve)
@ -1251,12 +1205,6 @@ nasal_ast nasal_parse::multi_assgin()
nasal_ast nasal_parse::loop() nasal_ast nasal_parse::loop()
{ {
nasal_ast node; nasal_ast node;
if(ptr>=tok_list_size)
{
++error;
error_info(error_line,lack_token,"loop");
return node;
}
switch(tok_list[ptr].type) switch(tok_list[ptr].type)
{ {
case tok_while: node=while_loop(); break; case tok_while: node=while_loop(); break;
@ -1279,15 +1227,13 @@ nasal_ast nasal_parse::while_loop()
} }
else else
{ {
++error; die(error_line,"expected \"(\"");
error_info(error_line,lack_left_curve);
return node; return node;
} }
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ {
++error; die(error_line,"expected \")\"");
error_info(error_line,lack_right_curve);
return node; return node;
} }
++ptr; ++ptr;
@ -1302,16 +1248,14 @@ nasal_ast nasal_parse::for_loop()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve)
{ {
++error; die(error_line,"expected \"(\"");
error_info(error_line,lack_left_curve);
return node; return node;
} }
// first expression // first expression
++ptr; ++ptr;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
++error; die(error_line,"expected definition");
error_info(error_line,lack_definition);
return node; return node;
} }
if(tok_list[ptr].type==tok_semi) if(tok_list[ptr].type==tok_semi)
@ -1326,16 +1270,14 @@ nasal_ast nasal_parse::for_loop()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi)
{ {
++error; die(error_line,"expected \";\"");
error_info(error_line,lack_semi);
return node; return node;
} }
// conditional expression // conditional expression
++ptr; ++ptr;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
++error; die(error_line,"expected conditional expression");
error_info(error_line,lack_calculation);
return node; return node;
} }
if(tok_list[ptr].type==tok_semi) if(tok_list[ptr].type==tok_semi)
@ -1348,16 +1290,14 @@ nasal_ast nasal_parse::for_loop()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi)
{ {
++error; die(error_line,"expected \";\"");
error_info(error_line,lack_semi);
return node; return node;
} }
//after loop expression //after loop expression
++ptr; ++ptr;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
++error; die(error_line,"expected calculation");
error_info(error_line,lack_calculation);
return node; return node;
} }
if(tok_list[ptr].type==tok_right_curve) if(tok_list[ptr].type==tok_right_curve)
@ -1370,8 +1310,7 @@ nasal_ast nasal_parse::for_loop()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ {
++error; die(error_line,"expected \")\"");
error_info(error_line,lack_right_curve);
return node; return node;
} }
++ptr; ++ptr;
@ -1390,8 +1329,7 @@ nasal_ast nasal_parse::forei_loop()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve)
{ {
++error; die(error_line,"expected \"(\"");
error_info(error_line,lack_left_curve);
return node; return node;
} }
// first expression // first expression
@ -1399,31 +1337,27 @@ nasal_ast nasal_parse::forei_loop()
++ptr; ++ptr;
if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_var && tok_list[ptr].type!=tok_identifier)) if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_var && tok_list[ptr].type!=tok_identifier))
{ {
++error; die(error_line,"expected iterable value");
error_info(error_line,lack_loop_iter);
return node; return node;
} }
node.add_child(new_iter_gen()); node.add_child(new_iter_gen());
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_semi)
{ {
++error; die(error_line,"expected \";\"");
error_info(error_line,lack_semi);
return node; return node;
} }
++ptr; ++ptr;
if(ptr>=tok_list_size) if(ptr>=tok_list_size)
{ {
++error; die(error_line,"expected vector");
error_info(error_line,lack_calculation);
return node; return node;
} }
node.add_child(calculation()); node.add_child(calculation());
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ {
++error; die(error_line,"expected \")\"");
error_info(error_line,lack_right_curve);
return node; return node;
} }
++ptr; ++ptr;
@ -1441,8 +1375,7 @@ nasal_ast nasal_parse::new_iter_gen()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_identifier)
{ {
++error; die(error_line,"expected identifier");
error_info(error_line,lack_identifier);
return node; return node;
} }
node.add_child(id_gen()); node.add_child(id_gen());
@ -1474,8 +1407,7 @@ nasal_ast nasal_parse::conditional()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve)
{ {
++error; die(error_line,"expected \"(\"");
error_info(error_line,lack_left_curve);
return node; return node;
} }
++ptr; ++ptr;
@ -1483,8 +1415,7 @@ nasal_ast nasal_parse::conditional()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ {
++error; die(error_line,"expected \")\"");
error_info(error_line,lack_right_curve);
return node; return node;
} }
++ptr; ++ptr;
@ -1494,12 +1425,12 @@ nasal_ast nasal_parse::conditional()
++ptr; ++ptr;
while(ptr<tok_list_size && (tok_list[ptr].type==tok_elsif || (tok_list[ptr].type==tok_else && ptr+1<tok_list_size && tok_list[ptr+1].type==tok_if))) while(ptr<tok_list_size && (tok_list[ptr].type==tok_elsif || (tok_list[ptr].type==tok_else && ptr+1<tok_list_size && tok_list[ptr+1].type==tok_if)))
{ {
if(tok_list[ptr].type==tok_else) ++ptr; if(tok_list[ptr].type==tok_else)
++ptr;
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_left_curve)
{ {
++error; die(error_line,"expected \"(\"");
error_info(error_line,lack_left_curve);
return node; return node;
} }
tmp.set_line(tok_list[ptr].line); tmp.set_line(tok_list[ptr].line);
@ -1510,8 +1441,7 @@ nasal_ast nasal_parse::conditional()
++ptr; ++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve) if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{ {
++error; die(error_line,"expected \")\"");
error_info(error_line,lack_right_curve);
return node; return node;
} }
++ptr; ++ptr;