From 7ea0dfc7a6e39970404759557710834ac751ff91 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 31 May 2022 18:50:41 +0200 Subject: [PATCH] Generating for stmt --- ccodegen.cpp | 25 +++++++++++++++++++++---- main.cpp | 2 +- typecheck.cpp | 29 ++++++++++++++++++++++++++--- typecheck.h | 2 ++ 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/ccodegen.cpp b/ccodegen.cpp index c19f456..f2af288 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -108,16 +108,15 @@ gen_expr(Ast_Expr *ast){ else gen("."); gen_expr(node->right); } - else if(node->op == TK_Comma){ + else if(node->op == TK_Colon){ // @todo: I think this needs to be a stmt Sym *sym = resolved_get(node); Ast_Atom *atom = (Ast_Atom *)node->left; - assert(atom->kind == AST_ATOM); + assert(is_atom(atom)); gen_simple_decl(sym->type, atom->intern_val); if(node->right){ gen(" = "); gen_expr(node->right); } - gen(";"); } else{ gen("("); @@ -249,7 +248,7 @@ gen_ast(Ast *ast){ CASE(IF, If){ For(node->ifs){ - if(it->init) gen_ast(it->init); + if(it->init) gen_expr(it->init); if(node->ifs.is_first(&it)){ genln("if("); gen_expr(it->expr); @@ -269,6 +268,24 @@ gen_ast(Ast *ast){ BREAK(); } + CASE(PASS, Pass){ + unused(node); + gen("//pass"); + BREAK(); + } + + CASE(FOR, For){ + gen("for("); + if(node->init) gen_expr(node->init); + gen(";"); + if(node->cond) gen_expr(node->cond); + gen(";"); + if(node->iter) gen_expr(node->iter); + gen(")"); + gen_block(node->block); + BREAK(); + } + CASE(CONST, Const){ Sym *sym = resolved_get(node); diff --git a/main.cpp b/main.cpp index dbc49cc..f2c4a4f 100644 --- a/main.cpp +++ b/main.cpp @@ -33,13 +33,13 @@ For now I don't thing it should be overloadable. @todo [ ] - For loop +[ ] - Pass statement [ ] - More operators [ ] - 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 [ ] - Switch -[ ] - Pass statement [ ] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression [ ] - More basic types [ ] - Array of inferred size diff --git a/typecheck.cpp b/typecheck.cpp index f0bd36a..387ec3f 100644 --- a/typecheck.cpp +++ b/typecheck.cpp @@ -27,6 +27,16 @@ resolve_type_pair(Token *pos, Ast_Resolved_Type *a, Ast_Resolved_Type *b){ return result; } +function void resolve_stmt(Ast *ast, Ast_Resolved_Type *ret); +function void +resolve_stmt_block(Ast_Block *block, Ast_Resolved_Type *ret){ + Scope(){ + For(block->stmts) resolve_stmt(it, ret); + } +} + +// @note: Ret is return value of function passed down the stack +// to check if type matches function void resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){ if(!ast) return; @@ -68,13 +78,26 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){ BREAK(); } + CASE(PASS, Pass){ + unused(node); + BREAK(); + } + + CASE(FOR, For){ + // @todo: I think we need to bring back the Ast_Init, it was not an expression + resolve_stmt(node->init, ret); + Operand cond = resolve_expr(node->cond); // @todo: typechecking + Operand iter = resolve_expr(node->iter); + unused(cond); unused(iter); + resolve_stmt_block(node->block, ret); + BREAK(); + } + CASE(IF, If){ For(node->ifs){ resolve_stmt(it->init, ret); resolve_expr(it->expr); // @todo: typechecking - S64 scope_index = scope_open(); - For_It(it->block->stmts, jt) resolve_stmt(jt, ret); - scope_close(scope_index); + resolve_stmt_block(it->block, ret); } BREAK(); } diff --git a/typecheck.h b/typecheck.h index d4c4610..70c96be 100644 --- a/typecheck.h +++ b/typecheck.h @@ -176,6 +176,8 @@ scope_close(S64 local_sym_count){ pctx->local_syms.len = local_sym_count; } +#define Scope() for(S64 scope_index = scope_open(); scope_index; (scope_close(scope_index), scope_index=0)) + function void sym_associate(Ast *ast, Sym *sym){ assert(ast);