From f66b155e405cb1d6c02356b5f07f84d817a83feb Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 22 Jun 2022 14:01:15 +0200 Subject: [PATCH] Rewriting constant casts into literals of said type --- parsing.cpp | 2 +- programs/main.kl | 31 +++++++++++++++++++++---------- typechecking.cpp | 29 +++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/parsing.cpp b/parsing.cpp index 90ba7fa..31dfce2 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -3,7 +3,7 @@ function Ast_Decl *parse_decl(B32 is_global); function void print_token_context(Token *token){ if(!token) return; - printf(" :: %s:%d\n", token->file.str, (S32)token->line + 1); + printf(" :: tdi:%u %s:%d\n", token->di, token->file.str, (S32)token->line + 1); // @Note(Krzosa): Print error line { int i = 0; diff --git a/programs/main.kl b/programs/main.kl index d6fb80f..5417e13 100644 --- a/programs/main.kl +++ b/programs/main.kl @@ -89,17 +89,15 @@ print :: (a: Any) // print(a[i]) -app_is_running := true -window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT - if msg == WM_DESTROY - PostQuitMessage(0) - app_is_running = false - return 0 - else;; return DefWindowProcW(hwnd, msg, wparam, lparam) +little_untyped_test :: () + if true == false;; pass + if true;; pass + cast_value1 := 32->S64 + cast_value2 := true->Bool -WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int - if good_scheduling := false, timeBeginPeriod(1) == TIMERR_NOERROR - good_scheduling = true + switch 4 + 4;; OutputDebugStringA("4") + 3;; OutputDebugStringA("3") some_type: Type = Vec2 char_info := get_type_info(char) @@ -113,6 +111,19 @@ WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nS #assert(int != char) #assert(*char == *char) + +app_is_running := true +window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT + if msg == WM_DESTROY + PostQuitMessage(0) + app_is_running = false + return 0 + else;; return DefWindowProcW(hwnd, msg, wparam, lparam) + +WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int + if good_scheduling := false, timeBeginPeriod(1) == TIMERR_NOERROR + good_scheduling = true + arena: Arena window_name := string_to_string16(&arena, "Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ") w := WNDCLASSW{ diff --git a/typechecking.cpp b/typechecking.cpp index c765606..9338ef3 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -473,6 +473,7 @@ insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){ // function void try_propagating_resolved_type_to_untyped_literals(Ast *ast, Ast_Type *type, Ast_Type *additional_not_bool_type = 0){ + if(!type) compiler_error(ast->pos, "Internal compiler error: Type passed to try_propagating_resolved_type_to_untyped_literals is null"); if(!ast) return; if(is_untyped(type)) return; @@ -678,10 +679,10 @@ resolve_stmt(Ast *ast, Ast_Type *ret){ BREAK(); } + // @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL CASE(IF, If){ For(node->ifs){ resolve_stmt(it->init, ret); - // @todo: maybe add else kind ?? and then make sure other then else are AST_CANT_BE_NULL resolve_and_require_bool("Conditional in a if condition", it->expr, AST_CAN_BE_NULL); try_propagating_resolved_type_to_untyped_literals(it->expr, type_bool); For_Named(it->scope->stmts, jt) @@ -691,16 +692,26 @@ resolve_stmt(Ast *ast, Ast_Type *ret){ } CASE(SWITCH, Switch){ - // @todo better typechecking - resolve_expr(node->value, AST_CANT_BE_NULL); + Operand base = resolve_expr(node->value, AST_CANT_BE_NULL); + if(!is_int(base.type) && !is_enum(base.type)) + compiler_error(node->pos, "You can only switch on integer values(enums included), the type of expression in switch statement is instead %Q", typestring(base.type)); + + try_converting_untyped_to_default_type(&base.value); + try_propagating_resolved_type_to_untyped_literals(node->value, base.type); For(node->cases){ For_Named(it->labels, label){ Operand op = resolve_expr(label, AST_CANT_BE_NULL); if(!op.is_const) compiler_error(label->pos, "Switch label required to be constant"); + make_sure_value_is_compatible_with_type(label->pos, &op, base.type, TYPE_AND_EXPR_REQUIRED); + try_propagating_resolved_type_to_untyped_literals(label, base.type); } - For_Named(it->scope->stmts, stmt) resolve_stmt(stmt, ret); + + For_Named(it->scope->stmts, stmt) + resolve_stmt(stmt, ret); } - if(node->default_scope) For_Named(node->default_scope->stmts, stmt) resolve_stmt(stmt, ret); + if(node->default_scope) + For_Named(node->default_scope->stmts, stmt) + resolve_stmt(stmt, ret); BREAK(); } @@ -758,6 +769,7 @@ resolve_lambda_type(Ast_Lambda *lambda){ Ast_Type *type = resolve_typespec(it->typespec, AST_CANT_BE_NULL); Operand default_value = resolve_expr(it->expr, AST_CAN_BE_NULL, type); make_sure_value_is_compatible_with_type(it->pos, &default_value, type, EXPR_CAN_BE_NULL); + it->type = type; try_propagating_resolved_type_to_untyped_literals(it->expr, it->type); } @@ -833,9 +845,14 @@ resolve_cast(Ast_Binary *node){ } assert(original_type != type ? expr.type == type : 1); - if(expr.is_const) check_value_bounds(node->pos, &expr.value); + if(expr.is_const) { + check_value_bounds(node->pos, &expr.value); + rewrite_into_const(node, Ast_Binary, expr.value); + } node->resolved_type = expr.type; + if(!expr.is_const) + try_propagating_resolved_type_to_untyped_literals(node->left, node->resolved_type); return expr; }