Add 'else if' recognition

This commit is contained in:
Valk Richard Li 2019-09-28 09:01:14 -05:00 committed by GitHub
parent 5226188872
commit 40148872af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 4 deletions

View File

@ -8,6 +8,10 @@ elsif(this_token.type==__elsif)
parse.push(this_token); parse.push(this_token);
return; return;
} }
else if(this_token.type!=__else)
{
exit(0);
}
elsif(this_token.type==__elsif) elsif(this_token.type==__elsif)
{ {
parse.push(this_token); parse.push(this_token);

View File

@ -6,4 +6,5 @@ var f=func(n,m,dynamic...)
n+=m; n+=m;
return dynamic; return dynamic;
}; };
print(f(1,1,0,0,0,0,0)[3]); print(f(1,1,0,0,0,0,0)[3]);
function([0,1,2,3],{str:"str"});

View File

@ -18,7 +18,7 @@ int main()
std::cout<<">> 2. [cls ] |clear the screen."<<std::endl; std::cout<<">> 2. [cls ] |clear the screen."<<std::endl;
std::cout<<">> 3. [exit ] |shut down the interpreter."<<std::endl; std::cout<<">> 3. [exit ] |shut down the interpreter."<<std::endl;
std::cout<<">> 4. [lexer ] |run and show the lexer. (-lexer)"<<std::endl; std::cout<<">> 4. [lexer ] |run and show the lexer. (-lexer)"<<std::endl;
std::cout<<">> 5. [parser] |run parser. (-parser)"<<std::endl; std::cout<<">> 5. [parser] |run parser and see parse stack & parse result(s). (-parser)"<<std::endl;
std::cout<<">> 6. [ast ] |print the abstract syntax tree."<<std::endl; std::cout<<">> 6. [ast ] |print the abstract syntax tree."<<std::endl;
std::cout<<">> 7. [del ] |delete program in memory."<<std::endl; std::cout<<">> 7. [del ] |delete program in memory."<<std::endl;
std::cout<<">> 8. [run ] |run the programme in stack. (-lexer -parser)"<<std::endl; std::cout<<">> 8. [run ] |run the programme in stack. (-lexer -parser)"<<std::endl;

View File

@ -105,7 +105,7 @@ class nasal_parser
if(!error) if(!error)
root.run(); root.run();
else else
std::cout<<">>[Parse] "<<error<<"error(s) occurred,stop."<<std::endl; std::cout<<">>[Parse] "<<error<<" error(s) occurred,stop."<<std::endl;
return; return;
} }
void parse_main_work(); void parse_main_work();
@ -120,6 +120,7 @@ class nasal_parser
void definition_expr(); void definition_expr();
void assignment_expr(); void assignment_expr();
void loop_expr(); void loop_expr();
bool else_if_check();
void if_else_expr(); void if_else_expr();
void add_sub_operator_expr(); void add_sub_operator_expr();
void mul_div_operator_expr(); void mul_div_operator_expr();
@ -396,6 +397,24 @@ void nasal_parser::assignment_expr()
} }
return; return;
} }
bool nasal_parser::else_if_check()
{
token temp=this_token;
if(this_token.type!=__else)
return false;
else
{
get_token();
if(this_token.type!=__if)
{
parse.push(this_token);
this_token=temp;// to avoid when recognizing 'else' without 'if'
return false;
}
}
return true;
}
void nasal_parser::if_else_expr() void nasal_parser::if_else_expr()
{ {
get_token(); get_token();
@ -433,7 +452,7 @@ void nasal_parser::if_else_expr()
} }
statements_block(); statements_block();
get_token(); get_token();
while(this_token.type==__elsif) while(this_token.type==__elsif || else_if_check())
{ {
get_token(); get_token();
if(this_token.type!=__left_curve) if(this_token.type!=__left_curve)