Add if statements

This commit is contained in:
Krzosa Karol
2022-05-26 18:13:37 +02:00
parent 3cd79040bc
commit 3d9a38494c
7 changed files with 134 additions and 15 deletions

View File

@@ -59,6 +59,17 @@ token_is(Token_Kind kind, S64 lookahead = 0){
return 0;
}
function Token *
token_is_keyword(Intern_String keyword, S64 lookahead = 0){
Token *token = token_get(lookahead);
if(token->kind == TK_Keyword){
if(keyword.str == token->intern_val.str){
return token;
}
}
return 0;
}
function Token *
token_match(Token_Kind kind){
Token *token = token_get();
@@ -182,10 +193,37 @@ parse_block(){
if(!token_is_scope()) result->expr = parse_expr();
stmts.add(result);
}
else if(token_match_keyword(keyword_if)){
Array<Ast_If_Node *> if_nodes = {scratch};
Ast_Expr *expr = parse_expr();
Ast_Block *block = parse_block();
Ast_If_Node *if_node = ast_if_node(token, expr, block);
if_nodes.add(if_node);
while(token_is(SAME_SCOPE) && token_is_keyword(keyword_else, 1)){
token_next();
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, expr, block);
if_nodes.add(if_node);
}
else{
Ast_Block *block = parse_block();
Ast_If_Node *if_node = ast_if_node(token, 0, block);
if_nodes.add(if_node);
break;
}
}
Ast_If *result_if = ast_if(token, if_nodes);
stmts.add(result_if);
}
else{
Ast_Decl *result = parse_decl(false);
if(result) stmts.add(result);
else parsing_error(token, "Unexpected token while parsing statement");
else parsing_error(token, "Unexpected token [%s] while parsing statement", token_kind_string(token->kind).str);
}
} while(token_match(SAME_SCOPE));
token_expect(CLOSE_SCOPE);