Generating for stmt

This commit is contained in:
Krzosa Karol
2022-05-31 18:50:41 +02:00
parent 0360086bab
commit 7ea0dfc7a6
4 changed files with 50 additions and 8 deletions

View File

@@ -108,16 +108,15 @@ gen_expr(Ast_Expr *ast){
else gen("."); else gen(".");
gen_expr(node->right); 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); Sym *sym = resolved_get(node);
Ast_Atom *atom = (Ast_Atom *)node->left; Ast_Atom *atom = (Ast_Atom *)node->left;
assert(atom->kind == AST_ATOM); assert(is_atom(atom));
gen_simple_decl(sym->type, atom->intern_val); gen_simple_decl(sym->type, atom->intern_val);
if(node->right){ if(node->right){
gen(" = "); gen(" = ");
gen_expr(node->right); gen_expr(node->right);
} }
gen(";");
} }
else{ else{
gen("("); gen("(");
@@ -249,7 +248,7 @@ gen_ast(Ast *ast){
CASE(IF, If){ CASE(IF, If){
For(node->ifs){ For(node->ifs){
if(it->init) gen_ast(it->init); if(it->init) gen_expr(it->init);
if(node->ifs.is_first(&it)){ if(node->ifs.is_first(&it)){
genln("if("); genln("if(");
gen_expr(it->expr); gen_expr(it->expr);
@@ -269,6 +268,24 @@ gen_ast(Ast *ast){
BREAK(); 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){ CASE(CONST, Const){
Sym *sym = resolved_get(node); Sym *sym = resolved_get(node);

View File

@@ -33,13 +33,13 @@ For now I don't thing it should be overloadable.
@todo @todo
[ ] - For loop [ ] - For loop
[ ] - Pass statement
[ ] - More operators [ ] - More operators
[ ] - Init statements [ ] - Init statements
[ ] - Fixing access to constants, in C we cant have constants inside of structs / functions so we need to rewrite the tree [ ] - 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 [ ] - Default values in structs??? Should compound stmts bring values from default values?? Maybe not? Whats the alternative
[ ] - Write up on order independent declarations [ ] - Write up on order independent declarations
[ ] - Switch [ ] - Switch
[ ] - Pass statement
[ ] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression [ ] - lvalue, rvalue concept so we cant assign value to some arbitrary weird expression
[ ] - More basic types [ ] - More basic types
[ ] - Array of inferred size [ ] - Array of inferred size

View File

@@ -27,6 +27,16 @@ resolve_type_pair(Token *pos, Ast_Resolved_Type *a, Ast_Resolved_Type *b){
return result; 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 function void
resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
if(!ast) return; if(!ast) return;
@@ -68,13 +78,26 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
BREAK(); 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){ CASE(IF, If){
For(node->ifs){ For(node->ifs){
resolve_stmt(it->init, ret); resolve_stmt(it->init, ret);
resolve_expr(it->expr); // @todo: typechecking resolve_expr(it->expr); // @todo: typechecking
S64 scope_index = scope_open(); resolve_stmt_block(it->block, ret);
For_It(it->block->stmts, jt) resolve_stmt(jt, ret);
scope_close(scope_index);
} }
BREAK(); BREAK();
} }

View File

@@ -176,6 +176,8 @@ scope_close(S64 local_sym_count){
pctx->local_syms.len = 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 function void
sym_associate(Ast *ast, Sym *sym){ sym_associate(Ast *ast, Sym *sym){
assert(ast); assert(ast);