This commit is contained in:
Valk Richard Li 2020-06-10 09:22:33 -07:00 committed by GitHub
parent 82f8e83dfe
commit 6fcd3ecdc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 51 deletions

View File

@ -25,17 +25,19 @@ function::=
argument_list::= argument_list::=
'(' [{id ','} ([id '...']|{id '=' calculation ','})] ')' '(' [{id ','} ([id '...']|{id '=' calculation ','})] ')'
; ;
expressions::= expr::=
'{' {definition|assignment|calculation|loop|conditional|return_expr|continue_expr|break_expr} '}' definition
|definition ';' |multi_assignment
|multi_assignment ';' |calculation
|calculation ';'
|loop |loop
|conditional |conditional
|return_expr |return_expr
|continue_expr |continue_expr
|break_expr |break_expr
; ;
expressions::=
'{' {expr} '}'
;
calculation::= calculation::=
'(' calculation ')' '(' calculation ')'
|scalar |scalar

View File

@ -37,6 +37,7 @@ enum parse_error
lack_left_curve, lack_left_curve,
lack_left_bracket, lack_left_bracket,
lack_left_brace, lack_left_brace,
lack_right_brace,
lack_semi, lack_semi,
lack_comma, lack_comma,
lack_colon, lack_colon,
@ -56,6 +57,7 @@ void error_info(int line,int error_type)
case lack_left_curve: detail="lack left curve."; break; case lack_left_curve: detail="lack left curve."; break;
case lack_left_bracket: detail="lack left bracket."; break; case lack_left_bracket: detail="lack left bracket."; break;
case lack_left_brace: detail="lack left brace."; break; case lack_left_brace: detail="lack left brace."; break;
case lack_right_brace: detail="lack right brace."; break;
case lack_semi: detail="lack \';\' here."; break; case lack_semi: detail="lack \';\' here."; break;
case lack_comma: detail="lack comma."; break; case lack_comma: detail="lack comma."; break;
case lack_colon: detail="lack colon."; break; case lack_colon: detail="lack colon."; break;

View File

@ -19,6 +19,7 @@ private:
nasal_ast hash_member_gen(); nasal_ast hash_member_gen();
nasal_ast func_gen(); nasal_ast func_gen();
nasal_ast args_list_gen(); nasal_ast args_list_gen();
nasal_ast expr();
nasal_ast exprs_gen(); nasal_ast exprs_gen();
nasal_ast calculation(); nasal_ast calculation();
nasal_ast trinocular(); nasal_ast trinocular();
@ -80,29 +81,8 @@ void nasal_parse::main_process()
root.set_type(ast_root); root.set_type(ast_root);
while(ptr<tok_list_size) while(ptr<tok_list_size)
{ {
switch(tok_list[ptr].type) root.add_child(expr());
{
case tok_nil:case tok_number:case tok_string:case tok_identifier:
case tok_func:
case tok_left_bracket:case tok_left_brace:
case tok_sub:
case tok_not: root.add_child(calculation()); break;
case tok_var: root.add_child(definition()); break;
case tok_for:case tok_forindex:case tok_foreach:
case tok_while: root.add_child(loop()); break;
case tok_if: root.add_child(conditional()); break;
defaut: error_info(tok_list[ptr].line,error_token);++error;break;
}
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_semi)
++ptr;
else if(ptr<tok_list_size && tok_list[ptr].type!=tok_semi
&& !root.get_children().empty() && !root.get_children().back().get_children().empty()
&& root.get_children().back().get_children().back().get_type()!=ast_function)
{
error_info(root.get_children().back().get_children().back().get_line(),lack_semi);
++error;
}
} }
std::cout<<">> [parse] complete generation. "<<error<<" error(s)."<<std::endl; std::cout<<">> [parse] complete generation. "<<error<<" error(s)."<<std::endl;
return; return;
@ -259,40 +239,60 @@ nasal_ast nasal_parse::args_list_gen()
} }
return node; return node;
} }
nasal_ast nasal_parse::exprs_gen() nasal_ast nasal_parse::expr()
{ {
nasal_ast node; nasal_ast node;
node.set_line(tok_list[ptr].line); node.set_line(tok_list[ptr].line);
node.set_type(ast_block);
while(ptr<tok_list_size)
{
switch(tok_list[ptr].type) switch(tok_list[ptr].type)
{ {
case tok_nil:case tok_number:case tok_string:case tok_identifier: case tok_nil:case tok_number:case tok_string:case tok_identifier:
case tok_func: case tok_func:
case tok_left_bracket:case tok_left_brace: case tok_left_bracket:case tok_left_brace:
case tok_sub: case tok_sub:
case tok_not: node.add_child(calculation()); break; case tok_not: node=calculation(); break;
case tok_var: node.add_child(definition()); break; case tok_var: node=definition(); break;
case tok_for:case tok_forindex:case tok_foreach: case tok_for:case tok_forindex:case tok_foreach:
case tok_while: node.add_child(loop()); break; case tok_while: node=loop(); break;
case tok_if: node.add_child(conditional()); break; case tok_if: node=conditional(); break;
case tok_continue:break; case tok_continue:break;
case tok_break:break; case tok_break:break;
case tok_return:break; case tok_return:break;
case tok_semi:--ptr;break;
defaut: error_info(tok_list[ptr].line,error_token);++error;break; defaut: error_info(tok_list[ptr].line,error_token);++error;break;
} }
++ptr; ++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_semi) if(ptr>=tok_list_size || (ptr<tok_list_size && tok_list[ptr].type!=tok_semi))
++ptr;
else if(ptr<tok_list_size && tok_list[ptr].type!=tok_semi
&& !node.get_children().empty() && !node.get_children().back().get_children().empty()
&& node.get_children().back().get_children().back().get_type()!=ast_function)
{ {
error_info(node.get_children().back().get_children().back().get_line(),lack_semi); bool tmp=node.get_children().empty();
if(tmp || (!tmp && node.get_children().back().get_type()!=ast_function))
{
error_info(node.get_children().back().get_line(),lack_semi);
++error;
}
if(ptr<tok_list_size) --ptr;
}
return node;
}
nasal_ast nasal_parse::exprs_gen()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
node.set_type(ast_block);
if(tok_list[ptr].type==tok_left_brace)
{
while(ptr<tok_list_size && tok_list[ptr].type!=tok_right_brace)
{
node.add_child(expr());
++ptr;
}
if(ptr>=tok_list_size)
{
error_info(node.get_line(),lack_right_brace);
++error; ++error;
} }
} }
else
node.add_child(expr());
return node; return node;
} }
nasal_ast nasal_parse::calculation() nasal_ast nasal_parse::calculation()