diff --git a/ccodegen.cpp b/ccodegen.cpp index b37af48..708c593 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -2,6 +2,7 @@ #define gen(...) pctx->gen.addf(__VA_ARGS__) #define genln(...) do{gen("\n"); gen_indent(); gen(__VA_ARGS__); }while(0) global S32 global_indent; +global S32 is_inside_struct; function void gen_indent(){ @@ -253,6 +254,40 @@ gen_block(Ast_Block *block){ genln("}"); } +enum { + ALWAYS_EMIT_VALUE = 0, + DONT_EMIT_VALUE = 1, +}; + +function void +gen_var(Intern_String name, Ast_Resolved_Type *type, Ast_Expr *expr, B32 emit_value){ + if(is_array(type)){ + gen("Slice %s", name.str); + } else{ + gen_simple_decl(type, name); + } + + if(emit_value == DONT_EMIT_VALUE){ + return; + } + + if(expr){ + gen(" = "); + gen_expr(expr); + } else if(is_array(type)){ + gen(" = (Slice){%d, (", type->arr.size); + gen_simple_decl(type, {}); + gen("){}"); + gen("}"); + } else { // Default zero + if(is_numeric(type)){ + gen(" = 0"); + } else { + gen(" = {}"); + } + } +} + function void gen_ast(Ast *ast){ switch(ast->kind){ @@ -280,24 +315,7 @@ gen_ast(Ast *ast){ CASE(VAR, Var){ Sym *sym = resolved_get(node); - - if(is_array(sym->type)){ - gen("Slice %s", node->name.str); - } else{ - gen_simple_decl(sym->type, node->name); - } - - if(node->expr){ - gen(" = "); - gen_expr(node->expr); - } else if(is_array(sym->type)){ - gen(" = (Slice){%d, (", sym->type->arr.size); - gen_simple_decl(sym->type, {}); - gen("){}"); - gen("}"); - } else { // Default zero - gen(" = {}"); - } + gen_var(sym->name, sym->type, node->expr, is_inside_struct ? DONT_EMIT_VALUE : ALWAYS_EMIT_VALUE); gen(";"); BREAK(); } @@ -381,7 +399,7 @@ gen_ast(Ast *ast){ For(lambda->args){ assert(it->kind == AST_LAMBDA_ARG); Ast_Resolved_Type *type = resolved_type_get(it->typespec); - gen_simple_decl(type, it->name); + gen_var(it->name, type, 0, DONT_EMIT_VALUE); if(&it != (lambda->args.end() - 1)) gen(", "); } gen(")"); @@ -405,6 +423,7 @@ gen_ast(Ast *ast){ if(node->value->kind == AST_STRUCT){ gen("typedef struct %s{", node->name.str); global_indent++; + is_inside_struct++; For(agg->members){ genln(""); gen_ast(it); @@ -414,6 +433,7 @@ gen_ast(Ast *ast){ genln(""); gen_ast(it); } + is_inside_struct--; global_indent--; genln("}%s;", node->name.str); } diff --git a/program.c b/program.c index 3c89d2d..c08bdc0 100644 --- a/program.c +++ b/program.c @@ -35,10 +35,7 @@ int main(){ entry(); } -#line 0 "program.kl" -#line 1 -#line 3 typedef struct Token{ U8 kind; U8 *str; @@ -47,61 +44,40 @@ typedef struct Token{ Number = 0, };*/ }Token; -#line 12 String kind_name(U8 kind){ - #line 15 if((kind==0)){ - #line 14 return LIT(""); } else{ - #line 16 return LIT(""); } } -#line 18 Bool is_numeric(U8 c){ - #line 19 Bool result = ((c>=48)&&(c<=57)); - #line 20 return result; } -#line 22 +void print_tokens(Slice tokens){ + for(S64 i = 0;(ilen), t->str); + } +} void entry(){ - #line 23 String string_to_lex = LIT("Identifier 2425525 Not_Number"); - #line 24 Slice token_array = (Slice){32, (Token [32]){}}; - #line 25 S64 token_count = 0; - #line 27 - Token t; - #line 28 + Token t = {}; for(S64 i = 0;(ilen), tk->str); - } + print_tokens(token_array); } \ No newline at end of file diff --git a/program.kl b/program.kl index a15c827..c2ea5bb 100644 --- a/program.kl +++ b/program.kl @@ -19,6 +19,11 @@ is_numeric :: (c: U8): Bool result := c >= '0 && c <= '9 return result +print_tokens :: (tokens: []Token) + for i := 0, i < tokens.len, i++ + t := &tokens[i] + printf("%d. %.*s", i, cast(t.len: S32), t.str) + entry :: () string_to_lex := "Identifier 2425525 Not_Number" token_array: [32]Token @@ -34,7 +39,5 @@ entry :: () i+=1 t.len = i - t.len token_array[token_count++] = t + print_tokens(token_array) - for i := 0, i < token_count, i++ - tk := &token_array[i] - printf("%.*s", cast(tk.len: S32), tk.str) diff --git a/typecheck.cpp b/typecheck.cpp index 9c8898f..bd480d7 100644 --- a/typecheck.cpp +++ b/typecheck.cpp @@ -313,6 +313,10 @@ make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Resolved_ else if(is_untyped(expr->type)){ expr->value = convert_untyped_to_typed(pos, expr->value, type); } + else if(is_array(type) && type->arr.size == ARRAY_SIZE_INFERRED){ + if(type->arr.inferred_size_hash == expr->type->arr.inferred_size_hash) + expr->type = type; + } if(type && expr->type != type){ parsing_error(pos, "Assigning but incompatible types, expression: %s expected var type: %s", docname(expr->type), docname(type));