From 955167ce1833f253fb9c9ee338fe4f1b3392e15e Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Mon, 13 Jun 2022 18:07:17 +0200 Subject: [PATCH] Fixing bugs, prepending all names with context name --- Windows.kl | 18 +++++++++++++---- ast.cpp | 4 ---- base.kl | 12 ++++++++++++ ccodegen.cpp | 51 +++++++++++++++++++++++++++++++----------------- enums.kl | 4 +--- main.cpp | 4 +--- new_types.kl | 3 --- typechecking.cpp | 9 +++++++-- 8 files changed, 68 insertions(+), 37 deletions(-) create mode 100644 base.kl diff --git a/Windows.kl b/Windows.kl index 9058e37..103adbf 100644 --- a/Windows.kl +++ b/Windows.kl @@ -1,12 +1,22 @@ -#load "globals.kl" -#load "lambdas.kl" - DWORD :: U32 LPCSTR :: *char HWND :: *void HMENU :: *void HINSTANCE :: *void +LPVOID :: *void +SIZE_T :: U64 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 -// @todo: enum #flag \ No newline at end of file + +MEM_COMMIT :: 1//0x00001000 +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 + +VirtualAlloc :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD): LPVOID \ No newline at end of file diff --git a/ast.cpp b/ast.cpp index b25a290..ffa7729 100644 --- a/ast.cpp +++ b/ast.cpp @@ -107,10 +107,6 @@ struct Ast_Unary: Ast_Expr{ U64 padding[2]; // For folding constants into atoms }; -struct Ast_Load: Ast{ - String string; -}; - struct Ast_Index: Ast_Expr{ Ast_Expr *expr; Ast_Expr *index; diff --git a/base.kl b/base.kl new file mode 100644 index 0000000..a6173da --- /dev/null +++ b/base.kl @@ -0,0 +1,12 @@ +#import "Windows.kl" + +OS_Memory :: struct + commit : U64 + reserve: U64 + data : *U8 + +Arena :: struct + memory: OS_Memory + alignment: U64 + len: U64 + diff --git a/ccodegen.cpp b/ccodegen.cpp index 334a333..f92c788 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -18,6 +18,12 @@ gen_line(Ast *node){ genln("#line %d", node->pos->line+1); } +function void +gen_scope_name(Ast_Scope *scope){ + if(scope->parent_scope) gen_scope_name(scope->parent_scope); + if(scope->name.str) gen("%s_", scope->name.str); +} + // @todo: Gen complicated decl //array 10 ( pointer (pointer array 5 int a)) // int (*(*(a[5])))[10] @@ -41,7 +47,9 @@ gen_simple_decl_prefix(Ast_Type *ast){ auto name = constant->name; gen("%s ", name.str); }break; - default: gen("%s ", name(ast)); + default: { + gen("%s ", name(ast)); + } } } @@ -62,7 +70,7 @@ gen_simple_decl_postfix(Ast_Type *ast){ } function void -gen_simple_decl(Ast_Type *ast, Intern_String name = {}){ +gen_simple_decl(Ast_Type *ast, Intern_String name = {}, Ast_Scope *scope = 0){ if(ast->kind == TYPE_LAMBDA) { gen_simple_decl_prefix(ast->func.ret); gen("(*%s)(", name.str); @@ -75,6 +83,7 @@ gen_simple_decl(Ast_Type *ast, Intern_String name = {}){ else{ gen_simple_decl_prefix(ast); if(name.len) { + if(scope) gen_scope_name(scope); gen("%s", name.str); } gen_simple_decl_postfix(ast); @@ -124,18 +133,18 @@ enum { }; function void -gen_var(Intern_String name, Ast_Type *type, Ast_Expr *expr, B32 emit_value){ - gen_simple_decl(type, name); +gen_var(Ast_Decl *decl, B32 emit_value){ + gen_simple_decl(decl->type, decl->name, decl->parent_scope); if(emit_value == DONT_EMIT_VALUE){ return; } - if(expr){ + if(decl->expr){ gen(" = "); - gen_expr(expr); + gen_expr(decl->expr); } else { // Default zero - if(is_numeric(type)){ + if(is_numeric(decl->type)){ gen(" = 0"); } else { gen(" = {}"); @@ -145,10 +154,10 @@ gen_var(Intern_String name, Ast_Type *type, Ast_Expr *expr, B32 emit_value){ function void gen_lambda(Intern_String name, Ast_Lambda *lambda){ - gen_simple_decl(lambda->type->func.ret, name); + gen_simple_decl(lambda->type->func.ret, name, lambda->parent_scope); gen("("); For(lambda->args){ - gen_var(it->name, it->type, 0, DONT_EMIT_VALUE); + gen_var(it, DONT_EMIT_VALUE); if(&it != (lambda->args.end() - 1)) gen(", "); } gen(")"); @@ -163,13 +172,17 @@ function void gen_expr(Ast_Expr *ast){ switch(ast->kind){ CASE(IDENT, Atom){ + gen_scope_name(node->parent_scope); gen("%s", node->intern_val.str); BREAK(); } CASE(VALUE, Atom){ B32 written = gen_value(node->value); - if(!written) gen("%s", node->value.intern_val.str); + if(!written) { + gen_scope_name(node->parent_scope); + gen("%s", node->value.intern_val.str); + } BREAK(); } @@ -273,7 +286,7 @@ gen_ast(Ast *ast){ } CASE(VAR, Decl){ - gen_var(node->name, node->type, node->expr, is_inside_struct ? DONT_EMIT_VALUE : ALWAYS_EMIT_VALUE); + gen_var(node, is_inside_struct ? DONT_EMIT_VALUE : ALWAYS_EMIT_VALUE); if(!is_flag_set(ast->flags, AST_EXPR)) gen(";"); BREAK(); } @@ -333,7 +346,9 @@ gen_ast(Ast *ast){ } CASE(STRUCT, Decl){ - gen("typedef struct %s{", node->name.str); + gen("typedef struct "); + gen_scope_name(node->parent_scope); + gen("%s{", node->name.str); global_indent++; is_inside_struct++; For(node->scope->decls){ @@ -343,7 +358,9 @@ gen_ast(Ast *ast){ is_inside_struct--; global_indent--; - genln("}%s;", node->name.str); + genln("}"); + gen_scope_name(node->parent_scope); + gen("%s;", node->name.str); BREAK(); } @@ -359,10 +376,8 @@ gen_ast(Ast *ast){ global_indent++; For(node->scope->decls){ genln("%s", it->name.str); - if(it->expr){ - gen(" = "); - gen_expr(it->expr); - } + gen(" = "); + gen_value(it->value); gen(","); } global_indent--; @@ -521,7 +536,7 @@ resolve_everything_in_module(Ast_Module *module){ For_Named(it->decls, jt){ resolve_name(it, jt->pos, jt->name); if(jt->kind == AST_STRUCT){ - type_complete(jt->type); + type_complete(jt->type_val); } } } diff --git a/enums.kl b/enums.kl index aed8599..82c8293 100644 --- a/enums.kl +++ b/enums.kl @@ -1,7 +1,5 @@ Allocator_Kind :: enum #flag - Null - Arena - Heap :: 4 + Null; Arena; Heap kind := Allocator_Kind.Heap diff --git a/main.cpp b/main.cpp index 704e7d3..958c2d3 100644 --- a/main.cpp +++ b/main.cpp @@ -174,9 +174,7 @@ int main(int argument_count, char **arguments){ #else F64 total_time = os_time(); begin_compilation(); - add_module(pctx->intern("order1.kl"_s)); - Ast_Module *module = add_module(pctx->intern("Windows.kl"_s)); - + Ast_Module *module = add_module(pctx->intern("base.kl"_s)); parse_all_modules(); assert(module); resolve_everything_in_module(module); diff --git a/new_types.kl b/new_types.kl index 04170de..6d8042e 100644 --- a/new_types.kl +++ b/new_types.kl @@ -1,6 +1,5 @@ unary_test :: () - // int_val :: 125152553512512512512 // @todo bit ints int_val :: 141 float_val :: 124.42 conversion: F64 = -+int_val @@ -57,8 +56,6 @@ binary_test :: (thing: S32 = 442) if int_val > 1 pass - - basic_type_assignment :: () // custom_data1 := Custom_Data(thing = 23) // custom_data2: Custom_Data diff --git a/typechecking.cpp b/typechecking.cpp index 3baf66f..5474c1c 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -967,7 +967,7 @@ resolve_decl(Ast_Decl *ast){ node->type = type_type; node->type_val = type_enum(node, type_of_enum); - S64 value = 0; + S64 value = 1; For(node->scope->decls){ Operand op = {}; if(it->expr){ @@ -976,7 +976,12 @@ resolve_decl(Ast_Decl *ast){ } else{ it->state = DECL_RESOLVED; op.type = untyped_int; - bigint_init_signed(&op.big_int_val, value++); + bigint_init_signed(&op.big_int_val, value); + if(is_flag_set(node->flags, AST_FLAG)){ + value = value << 1; + } else { + value += 1; + } } it->value = op.value;