From 2c431e32072b441f19112cd7fb2cb913c84285ed Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 13 Jun 2022 20:22:40 +0200 Subject: [PATCH] Writing more real program and squashing bugs --- Windows.kl | 19 ++++++++++++++----- ast.cpp | 1 + base.kl | 41 ++++++++++++++++++++++++++++++++++++++--- ccodegen.cpp | 5 +++-- main.cpp | 7 +++++-- parsing.cpp | 10 +++++++--- 6 files changed, 68 insertions(+), 15 deletions(-) diff --git a/Windows.kl b/Windows.kl index 103adbf..31bf452 100644 --- a/Windows.kl +++ b/Windows.kl @@ -5,6 +5,7 @@ HMENU :: *void HINSTANCE :: *void LPVOID :: *void SIZE_T :: U64 +BOOL :: int CreateWindowA :: #foreign (dwExStyle: DWORD, lpClassName: *char, lpWindowName: *char, dwStyle: DWORD, X: int, Y: int, nWidth: int, nHeight: int, hWndParent: HWND, hMenu: HMENU, hInstance: HINSTANCE, lpParam: *void): HWND @@ -14,9 +15,17 @@ MEM_RESERVE :: 2//0x00002000 MEM_RESET :: 3//0x00080000 MEM_RESET_UNDO :: 4//0x1000000 -PAGE_EXECUTE :: 1//0x10 -PAGE_EXECUTE_READ :: 2//0x20 -PAGE_EXECUTE_READWRITE :: 3//0x40 -PAGE_EXECUTE_WRITECOPY :: 4//0x80 +MEM_DECOMMIT :: 5//0x00004000 +MEM_RELEASE :: 6//0x00008000 -VirtualAlloc :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD): LPVOID \ No newline at end of file +PAGE_NOACCESS :: 1 +PAGE_READONLY :: 2 +PAGE_READWRITE :: 4 +PAGE_WRITECOPY :: 8 +PAGE_EXECUTE :: 10//0x10 +PAGE_EXECUTE_READ :: 20//0x20 +PAGE_EXECUTE_READWRITE :: 30//0x40 +PAGE_EXECUTE_WRITECOPY :: 40//0x80 + +VirtualAlloc :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD): LPVOID +VirtualFree :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, dwFreeType: DWORD): BOOL \ No newline at end of file diff --git a/ast.cpp b/ast.cpp index ffa7729..00d154e 100644 --- a/ast.cpp +++ b/ast.cpp @@ -498,6 +498,7 @@ ast_module_namespace(Token *pos, Ast_Module *module, Intern_String name){ function Ast_Module * ast_module(Intern_String filename){ AST_NEW(Module, MODULE, 0, 0); + result->parent_scope = 0; result->kind = AST_MODULE; result->name = filename; result->module = result; diff --git a/base.kl b/base.kl index a6173da..8f8fc78 100644 --- a/base.kl +++ b/base.kl @@ -1,12 +1,47 @@ #import "Windows.kl" +SizeU :: #strict U64 +OS_PAGE_SIZE :: 4096 + OS_Memory :: struct - commit : U64 - reserve: U64 + commit : SizeU + reserve: SizeU data : *U8 Arena :: struct memory: OS_Memory alignment: U64 - len: U64 + len: SizeU + +get_align_offset :: (size: SizeU, align: SizeU): SizeU + mask := align - 1 + val := size & mask + if val != 0 + val = align - val + return val + +align_up :: (size: SizeU, align: SizeU): SizeU + result := size + get_align_offset(size, align) + return result + +reserve :: (size: SizeU): OS_Memory + result := OS_Memory{reserve=align_up(size, OS_PAGE_SIZE)} + result.data = VirtualAlloc(0, result.reserve, MEM_RESERVE, PAGE_READWRITE)->*U8 + return result + +clamp_top_sizeu :: (val: SizeU, max: SizeU): SizeU + if val > max + return max + return val + +commit :: (m: *OS_Memory, size: SizeU): Bool + commit_size := align_up(size, OS_PAGE_SIZE) + total_commit := m.commit + commit_size + clamped_commit := clamp_top_sizeu(total_commit, m.reserve) + adjusted_commit := clamped_commit - m.commit + if adjusted_commit != 0 + result := VirtualFree(m.data->*void, 0, MEM_RELEASE) + m.commit += adjusted_commit + return true + return false diff --git a/ccodegen.cpp b/ccodegen.cpp index f92c788..55eb59f 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -338,7 +338,7 @@ gen_ast(Ast *ast){ } CASE(LAMBDA, Decl){ - if(is_flag_set(node->flags, AST_FOREIGN)){ + if(is_flag_set(node->expr->flags, AST_FOREIGN)){ gen("/*foreign*/"); } gen_lambda(node->name, node->lambda); @@ -506,7 +506,8 @@ parse_files(Ast_Module *module){ function void parse_all_modules(){ parsing_time_begin = os_time(); - For(pctx->modules){ + for(S64 i = 0; i < pctx->modules.len; i++){ + Ast_Module *it = pctx->modules[i]; parse_files(it); it->implicit_imports.add(pctx->builtins); } diff --git a/main.cpp b/main.cpp index 958c2d3..9f1eecf 100644 --- a/main.cpp +++ b/main.cpp @@ -41,13 +41,14 @@ want to export all the symbols, we can namespace them optionally. ------------------------------------------------------------------------------- @todo +[ ] - Fix field access, cant cast, cant index +[ ] - Fix codegen renames +[ ] - Add parent_scope to Ast_Type [ ] - Should compound resolution use an algorithm to reorder compounds to initialize all fields in order [ ] - Switch [ ] - Add c string [ ] - Some way to take slice of data [ ] - slices should be properly displayed in debugger -[ ] - Rewrite where # happen, -[ ] - cast -> [ ] - elif [ ] - #assert that handles constants at compile time and vars at runtime @@ -74,6 +75,8 @@ want to export all the symbols, we can namespace them optionally. @donzo [x] - Scope +[x] - Rewrite where # happen, +[x] - cast -> [x] - Remodel compound from call to {} [x] - Field access rewrite [-] - Constants embeded in structs should be able to refer to other constants in that namespace without prefix diff --git a/parsing.cpp b/parsing.cpp index 2f6b90b..df0f588 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -402,9 +402,9 @@ binding_power(Binding binding, Token_Kind kind){ case TK_Mod: return {17,18}; case TK_Dot: - return {24,23}; - case TK_Arrow: return {31,30}; + case TK_Arrow: + return {29,28}; default: return {}; } Postfix: switch(kind){ @@ -605,6 +605,7 @@ register_ast_file(Intern_String filename, Ast_Module *module, B32 global_implici file = result; file->filename = filename; file->module = module; + file->parent_scope = 0; file->file = file; // @warning: self referential! file->decls = {pctx->heap}; file->implicit_imports = {pctx->heap}; @@ -657,6 +658,8 @@ parse_decl(B32 is_global){ Ast_Flag flags = 0; if(token_match_pound(intern_foreign)){ set_flag(flags, AST_FOREIGN); + } else if(token_match_pound(intern_strict)){ + set_flag(flags, AST_STRICT); } // @note parse struct binding @@ -685,7 +688,8 @@ parse_decl(B32 is_global){ if(expr->kind == AST_LAMBDA_EXPR){ auto a = (Ast_Lambda *)expr; if(a->scope || is_flag_set(flags, AST_FOREIGN)){ - set_flag(result->flags, flags); + if(is_flag_set(flags, AST_FOREIGN)) + set_flag(expr->flags, flags); result->kind = AST_LAMBDA; } }