diff --git a/ast.cpp b/ast.cpp index 2e59e92..964d14f 100644 --- a/ast.cpp +++ b/ast.cpp @@ -113,7 +113,8 @@ struct Ast_Var_Unpack: Ast_Expr{ struct Ast_Unary: Ast_Expr{ Token_Kind op; Ast_Expr *expr; - U64 padding[2]; // For folding constants into atoms + Ast_Type *resolved_type_val; + U64 padding[1]; // For folding constants into atoms }; struct Ast_Index: Ast_Expr{ diff --git a/ccodegen.cpp b/ccodegen.cpp index fef8411..a4c0740 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -284,6 +284,11 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ BREAK(); } + CASE(ARRAY, Array){ + gen("%d", node->resolved_type->type_id); + BREAK(); + } + CASE(INDEX, Index){ gen("("); gen_expr(node->expr); @@ -332,7 +337,19 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ CASE(UNARY, Unary){ gen("("); - if(node->op != TK_PostIncrement && node->op != TK_PostDecrement) gen("%s", name(node->op)); + if(node->op != TK_PostIncrement && node->op != TK_PostDecrement){ + if(node->op == TK_Pointer){ + // Normal types are usually generated with gen_simple_decl + // if they are part of the expression they are a value + // which means we need to shortcircuit here with proper type_id + Ast_Type *base = get_type_base(node->resolved_type); + if(base == type_type){ + gen("%d)", node->resolved_type_val->type_id); + return true; + } + } + gen("%s", name(node->op)); + } gen_expr(node->expr); if(node->op == TK_PostIncrement || node->op == TK_PostDecrement) gen("%s", name(node->op)); gen(")"); diff --git a/parsing.cpp b/parsing.cpp index da67417..ed0ad64 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -167,16 +167,15 @@ parse_expr_call(Ast_Expr *left, Token_Kind close_kind){ Token *token = token_get(); Ast_Atom *name = 0; Ast_Expr *index = 0; - if(token_match(TK_OpenBracket)){ - index = parse_expr(0); - token_expect(TK_CloseBracket); - token_expect(TK_Assign); - } Ast_Expr *item = parse_expr(); if(token_match(TK_Assign)){ assert(is_flag_set(item->flags, AST_ATOM)); - name = (Ast_Atom *)item; + if(item->kind != AST_IDENT){ + index = item; + } else{ + name = (Ast_Atom *)item; + } item = parse_expr(); } diff --git a/programs/main.kl b/programs/main.kl index 1859ac8..be6713d 100644 --- a/programs/main.kl +++ b/programs/main.kl @@ -42,11 +42,22 @@ create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap return result print :: (type: Type) - switch type - int, S64, S32, S16, S8 - OutputDebugStringA("int") - default - OutputDebugStringA("unknown_type") + type_info := get_type_info(type) + if !type_info + return + + switch type_info.kind + S64, S32, S16, S8, int + OutputDebugStringA("Integer") + U64, U32, U16, U8 + OutputDebugStringA("Unsigned") + Type_Info_Kind.POINTER + OutputDebugStringA("*") + print(type_info.base_type) + Type_Info_Kind.SLICE + OutputDebugStringA("[]") + print(type_info.base_type) + default;; OutputDebugStringA("Unknown") app_is_running := true window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT @@ -67,6 +78,7 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS if good_scheduling := false, timeBeginPeriod(1) == TIMERR_NOERROR good_scheduling = true + print([]**S64) char_info := get_type_info(char) assert(char_info.kind == Type_Info_Kind.CHAR) #assert(int == int) diff --git a/typechecking.cpp b/typechecking.cpp index 39a1d91..aff6d6c 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -829,7 +829,8 @@ resolve_field_access(Ast_Expr *node, Ast_Scope *context){ } Ast_Type *type = decl->type; - if(type == type_type && (is_enum(decl->type_val) || is_struct(decl->type_val))) type = decl->type_val; + if(type == type_type && decl->type_val && (is_enum(decl->type_val) || is_struct(decl->type_val))) + type = decl->type_val; if(current) current->resolved_type = type; if(is_pointer(type)) type = type->base; @@ -973,8 +974,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ return operand_lvalue(node->resolved_type); } else if(value.type->kind == TYPE_TYPE){ - node->resolved_type = type_pointer(value.type_val); - return operand_type(node->resolved_type); + node->resolved_type = type_type; + node->resolved_type_val = type_pointer(value.type_val); + return operand_type(node->resolved_type_val); } else{ compiler_error(node->pos, "Dereferencing expression %Q that is not a [Pointer] or [Type]", typestring(value.type)); return {}; } } @@ -1139,6 +1141,14 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ invalid_return; } +function Ast_Type * +get_type_base(Ast_Type *type){ + switch(type->kind){ + case TYPE_POINTER: case TYPE_SLICE: case TYPE_ARRAY: return get_type_base(type->base); + default: return type; + } +} + function void resolve_decl(Ast_Decl *ast){ if(ast->state == DECL_RESOLVED) diff --git a/types.h b/types.h index f1b4b1f..38684d1 100644 --- a/types.h +++ b/types.h @@ -220,4 +220,4 @@ force_inline B32 is_numeric(Ast_Type *type){ return (type->kind >= TYPE_UNTYPED_FIRST_NUMERIC && type->kind <= TYPE_UNTYPED_LAST_NUMERIC) || (type->kind >= TYPE_FIRST_NUMERIC && type->kind <= TYPE_LAST_NUMERIC); -} +} \ No newline at end of file