From ae62b6933e161c164234623c2850b00b54261b19 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 17 Jun 2022 10:35:05 +0200 Subject: [PATCH] fixing compiler bugs --- ast.cpp | 1 + ccodegen.cpp | 8 ++++++++ programs/base.kl | 29 ++++++++++++++++++++++++++++- programs/base_test.kl | 5 ++++- programs/gdi32.kl | 2 +- programs/main.kl | 9 +++++---- programs/user32.kl | 2 +- typechecking.cpp | 8 +++++--- typechecking.h | 2 +- 9 files changed, 54 insertions(+), 12 deletions(-) diff --git a/ast.cpp b/ast.cpp index c1ada89..087d7e8 100644 --- a/ast.cpp +++ b/ast.cpp @@ -196,6 +196,7 @@ How does current declaration order resolver works: enum Ast_Decl_State{ DECL_NOT_RESOLVED, DECL_RESOLVED, + DECL_RESOLVED_TYPE, DECL_RESOLVING, }; diff --git a/ccodegen.cpp b/ccodegen.cpp index 4b7e47c..909af50 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -164,8 +164,11 @@ gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name, Ast_Scope *scop function B32 gen_value(Value a){ B32 result = true; + if(is_enum(a.type)) + goto integer; switch(a.type->kind){ CASE_INT: { + integer: Scratch scratch; const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10); gen("%s", string); @@ -768,6 +771,11 @@ typedef struct String{ if(it->kind == AST_STRUCT){ genln("typedef struct %Q %Q;", it->name, it->name); } + + else if(it->kind == AST_LAMBDA){ + genln(""); + gen_lambda(it->name, it->lambda, false); + } } Scratch scratch; diff --git a/programs/base.kl b/programs/base.kl index 1d606b9..a4773f0 100644 --- a/programs/base.kl +++ b/programs/base.kl @@ -1,9 +1,17 @@ Os :: #import "os.kl" SizeU :: #strict U64 - arena_di: U64 +ALLOCATOR_ACTION :: enum + ALLOCATE + RESIZE + FREE_ALL + +Allocator :: struct + proc: (a: *Allocator, action: ALLOCATOR_ACTION, size: SizeU, old_pointer: *void): *void + Arena :: struct + allocator: Allocator di: U64 // @debug_id memory: Os.Memory alignment: U64 @@ -33,6 +41,7 @@ arena_init :: (a: *Arena) a.memory = Os.reserve(a.DEFAULT_RESERVE_SIZE) a.alignment = a.DEFAULT_ALIGNMENT a.di = arena_di++ + a.allocator.proc = arena_allocator_proc arena_push_size :: (a: *Arena, size: SizeU): *void generous_size := size + a.alignment @@ -47,3 +56,21 @@ arena_push_size :: (a: *Arena, size: SizeU): *void a.len += size return result +arena_release :: (a: *Arena) + Os.release(&a.memory) + +arena_allocator_proc :: (a: *Allocator, action: ALLOCATOR_ACTION, size: SizeU, old_pointer: *void): *void + arena: *Arena = a->*Arena + if action == ALLOCATOR_ACTION.ALLOCATE + return arena_push_size(arena, size) + elif action == ALLOCATOR_ACTION.RESIZE + pass + elif action == ALLOCATOR_ACTION.FREE_ALL + pass + else;; assert(false, "Invalid codepath") + return 0 + +allocate :: (a: *Allocator, size: SizeU): *void + return a.proc(a, ALLOCATOR_ACTION.ALLOCATE, size, 0) + + diff --git a/programs/base_test.kl b/programs/base_test.kl index 96b8c67..495e012 100644 --- a/programs/base_test.kl +++ b/programs/base_test.kl @@ -3,4 +3,7 @@ main :: (argc: int, argv: **char): int arena: Arena arena_init(&arena) - data: *S64 = arena_push_size(&arena, size_of(S64)) \ No newline at end of file + data: *S64 = arena_push_size(&arena, size_of(S64)) + *data = 10 + + arena_release(&arena) \ No newline at end of file diff --git a/programs/gdi32.kl b/programs/gdi32.kl index cdf8f09..a833bc7 100644 --- a/programs/gdi32.kl +++ b/programs/gdi32.kl @@ -1,4 +1,4 @@ -#load "Windows.kl" +#import "Windows.kl" RBGQUAD :: struct;; rgbBlue: BYTE; rgbGreen: BYTE; rgbRed: BYTE; rgbReserved: BYTE BITMAPINFOHEADER :: struct;; biSize: DWORD; biWidth: LONG; biHeight: LONG; biPlanes: WORD; biBitCount: WORD; biCompression: DWORD; biSizeImage: DWORD; biXPelsPerMeter: LONG; biYPelsPerMeter: LONG; biClrUsed: DWORD; biClrImportant: DWORD BITMAPINFO :: struct;; bmiHeader: BITMAPINFOHEADER; bmiColors: [1]RBGQUAD diff --git a/programs/main.kl b/programs/main.kl index 6802ce8..d71d288 100644 --- a/programs/main.kl +++ b/programs/main.kl @@ -1,7 +1,8 @@ -// #import "base.kl" -#load "gdi32.kl" -#load "user32.kl" -#load "os.kl" +#import "base.kl" +#import "gdi32.kl" +#import "user32.kl" +#import "os.kl" +#import "Windows.kl" question_mark16 :: 0x003f String32 :: struct;; str: *U32; len: S64 diff --git a/programs/user32.kl b/programs/user32.kl index 7c5ad20..d8df06e 100644 --- a/programs/user32.kl +++ b/programs/user32.kl @@ -1,4 +1,4 @@ -#load "Windows.kl" +#import "Windows.kl" WNDPROC :: (hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT WNDCLASSW :: struct;; style: UINT; lpfnWndProc: WNDPROC; cbClsExtra: int; cbWndExtra: int; hInstance: HINSTANCE; hIcon: HICON; hCursor: HCURSOR; hbrBackground: HBRUSH; lpszMenuName: LPCWSTR; lpszClassName: LPCWSTR MSG :: struct;; hwnd: HWND; message: UINT; wParam: WPARAM; lParam: LPARAM; time: DWORD; pt: POINT; lPrivate: DWORD diff --git a/typechecking.cpp b/typechecking.cpp index 926c612..93e9e58 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -75,12 +75,14 @@ make_sure_types_are_compatible_for_constant_evaluation(Token *pos, Value *a, Val function Value compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){ + if(is_enum(a.type) && (a.type == b.type)) + goto skip_eval; if(!(is_numeric(a.type) && is_numeric(b.type))) compiler_error(pos, "Constant application of binary %s on values of type %s and %s is not allowed", name(op), docname(a.type), docname(b.type)); make_sure_types_are_compatible_for_constant_evaluation(pos, &a, &b); - B32 result = 0; + skip_eval: B32 result = 0; if(is_const){ switch(a.type->kind){ CASE_INT:{ @@ -379,7 +381,7 @@ resolve_typespec(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context = Operand resolved = resolve_expr(ast, flags, compound_context); if(is_flag_set(flags, RESOLVE_TYPESPEC_COMPLETE)) - type_complete(resolved.type); + type_complete(resolved.type_val); if(resolved.type != type_type) compiler_error(ast->pos, "Expected [Type] got instead %s", docname(resolved.type)); return resolved.type_val; @@ -1115,7 +1117,7 @@ resolve_decl(Ast_Decl *ast){ value = bigint_as_signed(&op.big_int_val) + 1; } else{ it->state = DECL_RESOLVED; - op.type = untyped_int; + op.type = node->type_val; bigint_init_signed(&op.big_int_val, value); if(is_flag_set(node->flags, AST_FLAG)){ value = value << 1; diff --git a/typechecking.h b/typechecking.h index fdd47bd..3e5f30b 100644 --- a/typechecking.h +++ b/typechecking.h @@ -32,7 +32,7 @@ operand(Ast_Decl *decl){ result.value = decl->value; if(decl->kind == AST_LAMBDA){ result.is_const = false; - } else assert(!is_lambda(decl->type)); + } return result; }