From 3402b4fe4d362f1709e1d0bab7612532c14ee814 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 10 Jun 2022 15:05:44 +0200 Subject: [PATCH] Add DECL_TYPE --- ast.cpp | 2 +- ccodegen.cpp | 57 ++++++++++++++++++------------------------------ typechecking.cpp | 5 +++++ 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/ast.cpp b/ast.cpp index 95b42ac..053d231 100644 --- a/ast.cpp +++ b/ast.cpp @@ -485,7 +485,7 @@ ast_const(Token *pos, Intern_String name, Ast_Expr *expr){ function Ast_Decl * ast_type(Token *pos, Intern_String name, Ast_Resolved_Type *type){ - AST_NEW(Decl, CONST, pos, AST_DECL); + AST_NEW(Decl, TYPE, pos, AST_DECL); result->type = type_type; result->type_val = type; result->name = name; diff --git a/ccodegen.cpp b/ccodegen.cpp index a7a1388..80e6af4 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -72,8 +72,9 @@ gen_simple_decl(Ast_Resolved_Type *ast, Intern_String name){ } } -function void +function B32 gen_value(Value a){ + B32 result = true; switch(a.type->kind){ CASE_INT: { Scratch scratch; @@ -83,8 +84,9 @@ gen_value(Value a){ CASE_STRING: gen("LIT(\"%s\")", a.intern_val.str); break; CASE_BOOL: a.bool_val ? gen("true"):gen("false"); break; CASE_FLOAT: gen("%f", a.f64_val); break; - invalid_default_case; + default: result = false; } + return result; } function void @@ -96,37 +98,20 @@ gen_expr(Ast_Expr *ast){ } CASE(VALUE, Atom){ - gen_value(node->value); + B32 written = gen_value(node->value); + if(!written) gen("%s", node->value.intern_val.str); BREAK(); } - // CASE(INDEX, Index){ - // Sym *sym = resolved_get(node); - // if(is_array(sym->type)){ - // gen("("); - // gen("("); - - // gen("("); - // gen_simple_decl(sym->type->arr.base, {}); - // gen("*)"); - // gen_expr(node->expr); - // gen(".data)"); - - - // gen("["); - // gen_expr(node->index); - // gen("]"); - // gen(")"); - // } else{ - // gen("("); - // gen_expr(node->expr); - // gen("["); - // gen_expr(node->index); - // gen("]"); - // gen(")"); - // } - // BREAK(); - // } + CASE(INDEX, Index){ + gen("("); + gen_expr(node->expr); + gen("["); + gen_expr(node->index); + gen("]"); + gen(")"); + BREAK(); + } CASE(BINARY, Binary){ // if(node->op == TK_Dot){ @@ -348,6 +333,12 @@ gen_ast(Ast *ast){ BREAK(); } + CASE(TYPE, Decl){ + gen("// Type %s = ", node->name.str); + gen_expr(node->expr); + BREAK(); + } + CASE(CONST, Decl){ switch(node->type->kind){ CASE_FLOAT:{ @@ -367,12 +358,6 @@ gen_ast(Ast *ast){ gen_value(node->value); }break; - case TYPE_LAMBDA:{ - - }break; - - case TYPE_TYPE:{ - }break; // if(sym->type_val->kind == TYPE_STRUCT){ // Ast_Struct *agg = (Ast_Struct *)sym->type_val->ast; // if(node->value->kind == AST_STRUCT){ diff --git a/typechecking.cpp b/typechecking.cpp index 67a3f62..df68a05 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -966,6 +966,7 @@ resolve_decl(Ast_Decl *ast, B32 flags){ Ast_Resolved_Type *lambda_type = 0; Ast_Resolved_Type *ret_type = resolve_typespec(lambda->ret, AST_CANT_BE_NULL); Array args = {scratch}; + For(lambda->args){ Ast_Resolved_Type *type = resolve_typespec(it->typespec, AST_CANT_BE_NULL); Operand default_value = resolve_expr(it->expr, AST_CAN_BE_NULL); @@ -1008,7 +1009,11 @@ resolve_decl(Ast_Decl *ast, B32 flags){ if(!op.is_const){ compiler_error(node->pos, "Assigning a value that is not constant to a constant declaration"); } + node->value = op.value; + if(op.value.type == type_type){ + node->kind = AST_TYPE; + } BREAK(); } CASE(VAR, Decl){