From ec773c08be9721b3cc667fd4a48d8a8854517dbc Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 26 May 2022 18:57:15 +0200 Subject: [PATCH] Add init statement to if --- cgenerate.cpp | 12 ++++++++++++ lambdas.kl | 4 ++-- new_ast.cpp | 22 ++++++++++++++++++++-- new_parse.cpp | 25 ++++++++++++++++++++++--- new_resolve.cpp | 15 +++++++++++++++ 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/cgenerate.cpp b/cgenerate.cpp index e6ec256..d31a137 100644 --- a/cgenerate.cpp +++ b/cgenerate.cpp @@ -208,8 +208,20 @@ gen_ast(Ast *ast){ Ast_End(); } + Ast_Begin(AST_INIT, Ast_Init){ + Sym *sym = resolved_get(node); + gen_simple_decl(sym->type, node->ident->intern_val); + if(node->expr){ + gen(" = "); + gen_expr(node->expr); + } + gen(";"); + Ast_End(); + } + Ast_Begin(AST_IF, Ast_If){ For(node->ifs){ + if(it[0]->init) gen_ast(it[0]->init); if(node->ifs.is_first(it)){ genln("if("); gen_expr(it[0]->expr); diff --git a/lambdas.kl b/lambdas.kl index f6a7acc..d1ad82e 100644 --- a/lambdas.kl +++ b/lambdas.kl @@ -2,8 +2,8 @@ if_stmt :: (cond: int): int CONSTANT :: 10 - if cond + CONSTANT - return cond + CONSTANT + if i := cond, cond + CONSTANT + return i + CONSTANT else if cond - CONSTANT return cond - CONSTANT else diff --git a/new_ast.cpp b/new_ast.cpp index 39deacd..dd25d3d 100644 --- a/new_ast.cpp +++ b/new_ast.cpp @@ -89,6 +89,7 @@ enum Ast_Kind{ AST_COMPOUND_ITEM, AST_COMPOUND, + AST_INIT, AST_IF, AST_IF_NODE, AST_RETURN, @@ -167,9 +168,16 @@ struct Ast_Return: Ast{ Ast_Expr *expr; }; +struct Ast_Init: Ast{ + Token_Kind op; + Ast_Atom *ident; + Ast_Expr *expr; +}; + struct Ast_If_Node: Ast{ Ast_Expr *expr ; Ast_Block *block; + Ast_Init *init; }; struct Ast_If: Ast{ @@ -338,9 +346,19 @@ ast_if(Token *pos, Array ifs){ } function Ast_If_Node * -ast_if_node(Token *pos, Ast_Expr *expr, Ast_Block *block){ +ast_if_node(Token *pos, Ast_Init *init, Ast_Expr *expr, Ast_Block *block){ AST_NEW(If_Node, AST_IF_NODE, pos); - result->block = block; + result->block = block; + result->expr = expr; + result->init = init; + return result; +} + +function Ast_Init * +ast_init(Token *pos, Token_Kind op, Ast_Atom *ident, Ast_Expr *expr){ + AST_NEW(Init, AST_INIT, pos); + result->op = op; + result->ident = ident; result->expr = expr; return result; } diff --git a/new_parse.cpp b/new_parse.cpp index 40c400a..7140cdf 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -196,8 +196,27 @@ parse_block(){ else if(token_match_keyword(keyword_if)){ Array 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; } diff --git a/new_resolve.cpp b/new_resolve.cpp index 8b7b1ec..c676d77 100644 --- a/new_resolve.cpp +++ b/new_resolve.cpp @@ -212,12 +212,27 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){ Ast_End(); } + Ast_Begin(AST_INIT, Ast_Init){ + switch(node->op){ + case TK_Comma:{ + Operand op = eval_expr(node->expr); + Sym *sym = sym_new(SYM_Var, node->ident->intern_val, op.type, node); + sym_insert(sym); + }break; + invalid_default_case; + } + Ast_End(); + } + Ast_Begin(AST_IF, Ast_If){ For(node->ifs){ if(it[0]->expr) eval_expr(it[0]->expr); + if(it[0]->init) eval_stmt(it[0]->init, ret); + S64 scope_index = scope_push(); For_It(it[0]->block->stmts, jt){ eval_stmt(jt[0], ret); } + scope_pop(scope_index); } Ast_End(); }