This commit is contained in:
Valk Richard Li 2020-06-18 08:54:54 -07:00 committed by GitHub
parent eb66bbee20
commit 150135a733
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 1 deletions

View File

@ -111,7 +111,7 @@ while_loop::=
while '(' calculation ')' expressions
;
for_loop::=
for '(' [definition] ';' [calculation] ';' [calculation] ')' expressions
for '(' [definition|calculation] ';' [calculation] ';' [calculation] ')' expressions
;
forei_loop::=
(forindex | foreach) '(' (definition | assignment) ';' calculation ')' expressions

View File

@ -52,6 +52,7 @@ enum parse_error
lack_scalar,
lack_identifier,
lack_calculation,
lack_token,
};
void error_info(int line,int error_type,std::string error_str="")
@ -74,6 +75,7 @@ void error_info(int line,int error_type,std::string error_str="")
case lack_scalar: detail="expected scalar here."; break;
case lack_identifier: detail="expected identifier here."; break;
case lack_calculation: detail="expected arithmetic-expression here."; break;
case lack_token: detail="expected \'"+error_str+"\' here."; break;
}
std::cout<<detail<<std::endl;
return;

View File

@ -738,6 +738,12 @@ nasal_ast nasal_parse::multi_assgin()
nasal_ast nasal_parse::loop()
{
nasal_ast node;
if(ptr>tok_list_size)
{
++error;
error_info(tok_list.back().line,lack_token,"loop");
return node;
}
switch(tok_list[ptr].type)
{
case tok_while: node=while_loop(); break;
@ -750,21 +756,68 @@ nasal_ast nasal_parse::loop()
nasal_ast nasal_parse::while_loop()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
node.set_type(ast_while);
++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_left_curve)
{
++ptr;
node.add_child(calculation());
}
else
{
++error;
error_info(node.get_line(),lack_left_curve);
}
++ptr;
if(ptr>tok_list_size || tok_list[ptr].type!=tok_right_curve)
{
++error;
error_info(node.get_line(),lack_right_curve);
}
++ptr;
node.add_child(exprs_gen());
return node;
}
nasal_ast nasal_parse::for_loop()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
node.set_type(ast_for);
++ptr;
if(ptr>tok_list_size || tok_list[ptr].type!=tok_left_curve)
{
++error;
error_info(node.get_line(),lack_left_curve);
}
++ptr;
// unfinished
return node;
}
nasal_ast nasal_parse::forei_loop()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
switch(tok_list[ptr].type)
{
case tok_forindex: node.set_type(ast_forindex);break;
case tok_foreach: node.set_type(ast_foreach); break;
}
++ptr;
if(ptr>tok_list_size || tok_list[ptr].type!=tok_left_curve)
{
++error;
error_info(node.get_line(),lack_left_curve);
}
++ptr;
// unfinished
return node;
}
nasal_ast nasal_parse::conditional()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
node.set_type(ast_if);
return node;
}
nasal_ast nasal_parse::continue_expr()