From 41697dec8019e90c0e7b06756c4e82d12f578bb7 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 31 May 2022 16:24:04 +0200 Subject: [PATCH] Ast_Init is not Ast_Binary with TK_Comma --- ccodegen.cpp | 22 +++++++++++----------- main.cpp | 6 +++--- new_ast.cpp | 21 ++------------------- new_parse.cpp | 6 +++--- order2.kl | 5 +++-- typechecking.cpp | 10 +++++++--- 6 files changed, 29 insertions(+), 41 deletions(-) diff --git a/ccodegen.cpp b/ccodegen.cpp index 5a692fd..c19f456 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -108,6 +108,17 @@ gen_expr(Ast_Expr *ast){ else gen("."); gen_expr(node->right); } + else if(node->op == TK_Comma){ + Sym *sym = resolved_get(node); + Ast_Atom *atom = (Ast_Atom *)node->left; + assert(atom->kind == AST_ATOM); + gen_simple_decl(sym->type, atom->intern_val); + if(node->right){ + gen(" = "); + gen_expr(node->right); + } + gen(";"); + } else{ gen("("); gen_expr(node->left); @@ -236,17 +247,6 @@ gen_ast(Ast *ast){ BREAK(); } - CASE(INIT, Init){ - Sym *sym = resolved_get(node); - gen_simple_decl(sym->type, node->ident->intern_val); - if(node->expr){ - gen(" = "); - gen_expr(node->expr); - } - gen(";"); - BREAK(); - } - CASE(IF, If){ For(node->ifs){ if(it->init) gen_ast(it->init); diff --git a/main.cpp b/main.cpp index cadbaef..5b24722 100644 --- a/main.cpp +++ b/main.cpp @@ -40,13 +40,13 @@ For now I don't thing it should be overloadable. ------------------------------------------------------------------------------- @todo +[ ] - For loop +[ ] - Init statements [ ] - Fixing access to constants, in C we cant have constants inside of structs / functions so we need to rewrite the tree [ ] - Default values in structs??? Should compound stmts bring values from default values?? Maybe not? Whats the alternative [ ] - Write up on order independent declarations -[ ] - Init statements -[ ] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression -[ ] - For loop [ ] - Switch +[ ] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression [ ] - More basic types [ ] - Array of inferred size [ ] - Lexer: Need to insert scope endings when hitting End of file diff --git a/new_ast.cpp b/new_ast.cpp index 88b0fb9..2e5ffe5 100644 --- a/new_ast.cpp +++ b/new_ast.cpp @@ -178,16 +178,10 @@ 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; + Ast_Binary*init; }; struct Ast_If: Ast{ @@ -405,7 +399,7 @@ ast_return(Token *pos, Ast_Expr *expr){ } function Ast_If_Node * -ast_if_node(Token *pos, Ast_Init *init, Ast_Expr *expr, Ast_Block *block){ +ast_if_node(Token *pos, Ast_Binary *init, Ast_Expr *expr, Ast_Block *block){ AST_NEW(If_Node, IF_NODE, pos, AST_STMT); result->block = block; result->expr = expr; @@ -416,17 +410,6 @@ ast_if_node(Token *pos, Ast_Init *init, Ast_Expr *expr, Ast_Block *block){ return result; } -function Ast_Init * -ast_init(Token *pos, Token_Kind op, Ast_Atom *ident, Ast_Expr *expr){ - AST_NEW(Init, INIT, pos, AST_STMT); - result->op = op; - result->ident = ident; - result->expr = expr; - result->ident->parent = result; - result->expr->parent = result; - return result; -} - function Ast_Array * ast_array(Token *pos, Ast_Expr *expr){ AST_NEW(Array, ARRAY, pos, AST_EXPR); diff --git a/new_parse.cpp b/new_parse.cpp index e94a389..cc74674 100644 --- a/new_parse.cpp +++ b/new_parse.cpp @@ -135,14 +135,14 @@ Compound literals */ function Ast_Expr *parse_expr(S64 rbp = 0); -function Ast_Init * +function Ast_Binary * 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_Init *result = ast_init(token, TK_Comma, (Ast_Atom *)expr, value); + Ast_Binary *result = (Ast_Binary *)ast_expr_binary((Ast_Atom *)expr, value, token); return result; } else not_implemented; @@ -213,7 +213,7 @@ parse_block(){ else if(token_match_keyword(keyword_if)){ Array if_nodes = {scratch}; Ast_Expr *expr = parse_expr(); - Ast_Init *init_val = parse_init_stmt(expr); + Ast_Binary *init_val = parse_init_stmt(expr); if(init_val){ if(token_match(TK_Comma)) expr = parse_expr(); else expr = 0; diff --git a/order2.kl b/order2.kl index 9c1d6c1..dedb8db 100644 --- a/order2.kl +++ b/order2.kl @@ -23,14 +23,15 @@ Arena :: struct Sub :: struct len: int + Sub_Sub :: struct + len: int get_len :: (s: *Arena): int // @todo return s.next.len constant_inside :: 10000 - - +// subarena: Arena.Sub // @todo string16: Str16 String16 :: struct diff --git a/typechecking.cpp b/typechecking.cpp index 6c85eb8..7926e47 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -234,11 +234,15 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){ BREAK(); } - CASE(INIT, Init){ + CASE(BINARY, Binary){ switch(node->op){ case TK_Comma:{ - Operand op = resolve_expr(node->expr); - Sym *sym = sym_new_resolved(SYM_VAR, node->ident->intern_val, op.type, op.value, node); + Operand left = resolve_expr(node->left); // needs to be lvalue + unused(left); + Operand right = resolve_expr(node->right); + assert(node->left->kind == AST_IDENT); + Ast_Atom *atom = (Ast_Atom *)node->left; // @todo use left operand + Sym *sym = sym_new_resolved(SYM_VAR, atom->intern_val, right.type, right.value, node); sym_insert(sym); }break; invalid_default_case;