Parsing for stmt
This commit is contained in:
@@ -135,14 +135,14 @@ Compound literals
|
||||
*/
|
||||
function Ast_Expr *parse_expr(S64 rbp = 0);
|
||||
|
||||
function Ast_Binary *
|
||||
function Ast_Expr *
|
||||
parse_init_stmt(Ast_Expr *expr){
|
||||
Token *token = token_get();
|
||||
if(token_match(TK_Colon)){
|
||||
if(expr->kind != AST_IDENT) parsing_error(token, "Failed to parse init stmt, expected left of [:] to be a token of kind [Identifier]");
|
||||
if(token_match(TK_Assign)){
|
||||
Ast_Expr *value = parse_expr();
|
||||
Ast_Binary *result = (Ast_Binary *)ast_expr_binary((Ast_Atom *)expr, value, token);
|
||||
Ast_Expr *result = ast_expr_binary((Ast_Atom *)expr, value, token);
|
||||
return result;
|
||||
}
|
||||
else not_implemented;
|
||||
@@ -210,17 +210,35 @@ parse_block(){
|
||||
Ast_Return *return_stmt = ast_return(token, expr);
|
||||
stmts.add(return_stmt);
|
||||
}
|
||||
|
||||
else if(token_match_keyword(keyword_for)){
|
||||
Ast_Expr *expr_first = parse_expr();
|
||||
Ast_Expr *init = parse_init_stmt(expr_first);
|
||||
Ast_Expr *actual_init = init ? init : expr_first;
|
||||
|
||||
Ast_Expr *cond = 0;
|
||||
Ast_Expr *iter = 0;
|
||||
if(token_match(TK_Comma)){
|
||||
cond = parse_expr();
|
||||
if(token_match(TK_Comma)){
|
||||
iter = parse_expr();
|
||||
}
|
||||
}
|
||||
|
||||
stmts.add(ast_for(token, actual_init, cond, iter, parse_block()));
|
||||
}
|
||||
|
||||
else if(token_match_keyword(keyword_if)){
|
||||
Array<Ast_If_Node *> if_nodes = {scratch};
|
||||
Ast_Expr *expr = parse_expr();
|
||||
Ast_Binary *init_val = parse_init_stmt(expr);
|
||||
Ast_Expr *init_val = parse_init_stmt(expr);
|
||||
if(init_val){
|
||||
if(token_match(TK_Comma)) expr = parse_expr();
|
||||
else expr = 0;
|
||||
}
|
||||
|
||||
Ast_Block *block = parse_block();
|
||||
Ast_If_Node *if_node = ast_if_node(token, init_val, expr, block);
|
||||
Ast_Block *if_block = parse_block();
|
||||
Ast_If_Node *if_node = ast_if_node(token, init_val, expr, if_block);
|
||||
if_nodes.add(if_node);
|
||||
|
||||
while(token_is(SAME_SCOPE) && token_is_keyword(keyword_else, 1)){
|
||||
@@ -228,13 +246,13 @@ parse_block(){
|
||||
token = token_next();
|
||||
if(token_match_keyword(keyword_if)){
|
||||
Ast_Expr *expr = parse_expr();
|
||||
Ast_Block *block = parse_block();
|
||||
Ast_If_Node *if_node = ast_if_node(token, 0, expr, block);
|
||||
Ast_Block *else_if_block = parse_block();
|
||||
Ast_If_Node *if_node = ast_if_node(token, 0, expr, else_if_block);
|
||||
if_nodes.add(if_node);
|
||||
}
|
||||
else{
|
||||
Ast_Block *block = parse_block();
|
||||
Ast_If_Node *if_node = ast_if_node(token, 0, 0, block);
|
||||
Ast_Block *else_block = parse_block();
|
||||
Ast_If_Node *if_node = ast_if_node(token, 0, 0, else_block);
|
||||
if_nodes.add(if_node);
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user