Finished parser.

This commit is contained in:
Valk Richard Li 2019-09-24 10:00:55 -05:00 committed by GitHub
parent 70ce284cab
commit 7fe919950f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 5 deletions

View File

@ -16,3 +16,15 @@ i=i.i[0].i(0);
var hash={ var hash={
f:func {var e=1;return 0;}, f:func {var e=1;return 0;},
}; };
for(var i=0;i<1024;i+=1)
{
print(i);
}
foreach(var i;[1,2,3,4])
{
print(i);
}
forindex(var i=list;[1,2,3,4])
{
print(i[0]);
}

View File

@ -497,15 +497,115 @@ void nasal_parser::loop_expr()
std::cout<<">>[Error] line "<<this_token.line<<": expect a ')' after 'while'."<<std::endl; std::cout<<">>[Error] line "<<this_token.line<<": expect a ')' after 'while'."<<std::endl;
return; return;
} }
statements_block();
} }
else if(this_token.type==__for) else if(this_token.type==__for)
{ {
; get_token();
if(this_token.type!=__left_curve)
{
++error;
std::cout<<">>[Error] line "<<this_token.line<<": expect a '(' after 'for'."<<std::endl;
return;
}
get_token();
switch(this_token.type)
{
case __var:definition_expr();break;
case __id:identifier_begin_expr();break;
case __semi:parse.push(this_token);break;
default:
std::cout<<">>[Error] line "<<this_token.line<<": \'";
print_token(this_token.type);
std::cout<<"\' in an incorrect place."<<std::endl;
++error;
break;
}
check_semi_at_end();
get_token();
switch(this_token.type)
{
case __id:identifier_begin_expr();break;
case __number:number_begin_expr();break;
case __string:string_begin_expr();break;
case __semi:parse.push(this_token);break;
default:
std::cout<<">>[Error] line "<<this_token.line<<": \'";
print_token(this_token.type);
std::cout<<"\' in an incorrect place."<<std::endl;
++error;
break;
}
check_semi_at_end();
get_token();
switch(this_token.type)
{
case __id:identifier_begin_expr();break;
case __number:number_begin_expr();break;
case __string:string_begin_expr();break;
case __right_curve:parse.push(this_token);break;
default:
std::cout<<">>[Error] line "<<this_token.line<<": \'";
print_token(this_token.type);
std::cout<<"\' in an incorrect place."<<std::endl;
++error;
break;
}
get_token();
if(this_token.type!=__right_curve)
{
++error;
std::cout<<">>[Error] line "<<this_token.line<<": expect a ')' after 'for('."<<std::endl;
return;
}
statements_block();
} }
else if(this_token.type==__forindex || this_token.type==__foreach) else if(this_token.type==__forindex || this_token.type==__foreach)
{ {
; get_token();
if(this_token.type!=__left_curve)
{
++error;
std::cout<<">>[Error] line "<<this_token.line<<": expect a '(' after 'forindex' or 'foreach'."<<std::endl;
return;
}
get_token();
switch(this_token.type)
{
case __var:definition_expr();break;
case __id:identifier_begin_expr();break;
case __semi:parse.push(this_token);break;
default:
std::cout<<">>[Error] line "<<this_token.line<<": \'";
print_token(this_token.type);
std::cout<<"\' in an incorrect place."<<std::endl;
++error;
break;
}
check_semi_at_end();
get_token();
switch(this_token.type)
{
case __id:identifier_begin_expr();break;
case __number:number_begin_expr();break;
case __string:string_begin_expr();break;
case __left_bracket:list_generate_expr();break;
case __right_curve:parse.push(this_token);break;
default:
std::cout<<">>[Error] line "<<this_token.line<<": \'";
print_token(this_token.type);
std::cout<<"\' in an incorrect place."<<std::endl;
++error;
break;
}
get_token();
if(this_token.type!=__right_curve)
{
++error;
std::cout<<">>[Error] line "<<this_token.line<<": expect a ')' after 'for('."<<std::endl;
return;
}
statements_block();
} }
else else
{ {
@ -515,7 +615,6 @@ void nasal_parser::loop_expr()
std::cout<<"' when creating a new loop."<<std::endl; std::cout<<"' when creating a new loop."<<std::endl;
return; return;
} }
statements_block();
return; return;
} }
void nasal_parser::add_sub_operator_expr() void nasal_parser::add_sub_operator_expr()