Add init statement to if

This commit is contained in:
Krzosa Karol
2022-05-26 18:57:15 +02:00
parent 8e4942f5ae
commit ec773c08be
5 changed files with 71 additions and 7 deletions

View File

@@ -196,8 +196,27 @@ parse_block(){
else if(token_match_keyword(keyword_if)){
Array<Ast_If_Node *> if_nodes = {scratch};
Ast_Expr *expr = parse_expr();
Ast_Init *init_val = 0;
{
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();
assert(expr->kind == AST_IDENT);
init_val = ast_init(token, TK_Comma, (Ast_Atom *)expr, value);
}
else not_implemented;
if(token_match(TK_Comma)){
expr = parse_expr();
}
}
}
Ast_Block *block = parse_block();
Ast_If_Node *if_node = ast_if_node(token, expr, block);
Ast_If_Node *if_node = ast_if_node(token, init_val, expr, block);
if_nodes.add(if_node);
while(token_is(SAME_SCOPE) && token_is_keyword(keyword_else, 1)){
@@ -206,12 +225,12 @@ parse_block(){
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, expr, block);
Ast_If_Node *if_node = ast_if_node(token, 0, expr, block);
if_nodes.add(if_node);
}
else{
Ast_Block *block = parse_block();
Ast_If_Node *if_node = ast_if_node(token, 0, block);
Ast_If_Node *if_node = ast_if_node(token, 0, 0, block);
if_nodes.add(if_node);
break;
}