fixing compiler bugs

This commit is contained in:
Krzosa Karol
2022-06-17 10:35:05 +02:00
parent 6696fd80f0
commit ae62b6933e
9 changed files with 54 additions and 12 deletions

View File

@@ -196,6 +196,7 @@ How does current declaration order resolver works:
enum Ast_Decl_State{ enum Ast_Decl_State{
DECL_NOT_RESOLVED, DECL_NOT_RESOLVED,
DECL_RESOLVED, DECL_RESOLVED,
DECL_RESOLVED_TYPE,
DECL_RESOLVING, DECL_RESOLVING,
}; };

View File

@@ -164,8 +164,11 @@ gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name, Ast_Scope *scop
function B32 function B32
gen_value(Value a){ gen_value(Value a){
B32 result = true; B32 result = true;
if(is_enum(a.type))
goto integer;
switch(a.type->kind){ switch(a.type->kind){
CASE_INT: { CASE_INT: {
integer:
Scratch scratch; Scratch scratch;
const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10); const char *string = bigint_to_error_string(scratch, &a.big_int_val, 10);
gen("%s", string); gen("%s", string);
@@ -768,6 +771,11 @@ typedef struct String{
if(it->kind == AST_STRUCT){ if(it->kind == AST_STRUCT){
genln("typedef struct %Q %Q;", it->name, it->name); 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; Scratch scratch;

View File

@@ -1,9 +1,17 @@
Os :: #import "os.kl" Os :: #import "os.kl"
SizeU :: #strict U64 SizeU :: #strict U64
arena_di: 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 Arena :: struct
allocator: Allocator
di: U64 // @debug_id di: U64 // @debug_id
memory: Os.Memory memory: Os.Memory
alignment: U64 alignment: U64
@@ -33,6 +41,7 @@ arena_init :: (a: *Arena)
a.memory = Os.reserve(a.DEFAULT_RESERVE_SIZE) a.memory = Os.reserve(a.DEFAULT_RESERVE_SIZE)
a.alignment = a.DEFAULT_ALIGNMENT a.alignment = a.DEFAULT_ALIGNMENT
a.di = arena_di++ a.di = arena_di++
a.allocator.proc = arena_allocator_proc
arena_push_size :: (a: *Arena, size: SizeU): *void arena_push_size :: (a: *Arena, size: SizeU): *void
generous_size := size + a.alignment generous_size := size + a.alignment
@@ -47,3 +56,21 @@ arena_push_size :: (a: *Arena, size: SizeU): *void
a.len += size a.len += size
return result 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)

View File

@@ -4,3 +4,6 @@ main :: (argc: int, argv: **char): int
arena: Arena arena: Arena
arena_init(&arena) arena_init(&arena)
data: *S64 = arena_push_size(&arena, size_of(S64)) data: *S64 = arena_push_size(&arena, size_of(S64))
*data = 10
arena_release(&arena)

View File

@@ -1,4 +1,4 @@
#load "Windows.kl" #import "Windows.kl"
RBGQUAD :: struct;; rgbBlue: BYTE; rgbGreen: BYTE; rgbRed: BYTE; rgbReserved: BYTE 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 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 BITMAPINFO :: struct;; bmiHeader: BITMAPINFOHEADER; bmiColors: [1]RBGQUAD

View File

@@ -1,7 +1,8 @@
// #import "base.kl" #import "base.kl"
#load "gdi32.kl" #import "gdi32.kl"
#load "user32.kl" #import "user32.kl"
#load "os.kl" #import "os.kl"
#import "Windows.kl"
question_mark16 :: 0x003f question_mark16 :: 0x003f
String32 :: struct;; str: *U32; len: S64 String32 :: struct;; str: *U32; len: S64

View File

@@ -1,4 +1,4 @@
#load "Windows.kl" #import "Windows.kl"
WNDPROC :: (hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT 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 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 MSG :: struct;; hwnd: HWND; message: UINT; wParam: WPARAM; lParam: LPARAM; time: DWORD; pt: POINT; lPrivate: DWORD

View File

@@ -75,12 +75,14 @@ make_sure_types_are_compatible_for_constant_evaluation(Token *pos, Value *a, Val
function Value function Value
compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){ 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))) 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)); 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); make_sure_types_are_compatible_for_constant_evaluation(pos, &a, &b);
B32 result = 0; skip_eval: B32 result = 0;
if(is_const){ if(is_const){
switch(a.type->kind){ switch(a.type->kind){
CASE_INT:{ 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); Operand resolved = resolve_expr(ast, flags, compound_context);
if(is_flag_set(flags, RESOLVE_TYPESPEC_COMPLETE)) if(is_flag_set(flags, RESOLVE_TYPESPEC_COMPLETE))
type_complete(resolved.type); type_complete(resolved.type_val);
if(resolved.type != type_type) if(resolved.type != type_type)
compiler_error(ast->pos, "Expected [Type] got instead %s", docname(resolved.type)); compiler_error(ast->pos, "Expected [Type] got instead %s", docname(resolved.type));
return resolved.type_val; return resolved.type_val;
@@ -1115,7 +1117,7 @@ resolve_decl(Ast_Decl *ast){
value = bigint_as_signed(&op.big_int_val) + 1; value = bigint_as_signed(&op.big_int_val) + 1;
} else{ } else{
it->state = DECL_RESOLVED; it->state = DECL_RESOLVED;
op.type = untyped_int; op.type = node->type_val;
bigint_init_signed(&op.big_int_val, value); bigint_init_signed(&op.big_int_val, value);
if(is_flag_set(node->flags, AST_FLAG)){ if(is_flag_set(node->flags, AST_FLAG)){
value = value << 1; value = value << 1;

View File

@@ -32,7 +32,7 @@ operand(Ast_Decl *decl){
result.value = decl->value; result.value = decl->value;
if(decl->kind == AST_LAMBDA){ if(decl->kind == AST_LAMBDA){
result.is_const = false; result.is_const = false;
} else assert(!is_lambda(decl->type)); }
return result; return result;
} }