Fixing bugs, prepending all names with context name

This commit is contained in:
Krzosa Karol
2022-06-13 18:07:17 +02:00
parent f9487a2c24
commit 955167ce18
8 changed files with 68 additions and 37 deletions

View File

@@ -1,12 +1,22 @@
#load "globals.kl"
#load "lambdas.kl"
DWORD :: U32 DWORD :: U32
LPCSTR :: *char LPCSTR :: *char
HWND :: *void HWND :: *void
HMENU :: *void HMENU :: *void
HINSTANCE :: *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 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
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

View File

@@ -107,10 +107,6 @@ struct Ast_Unary: Ast_Expr{
U64 padding[2]; // For folding constants into atoms U64 padding[2]; // For folding constants into atoms
}; };
struct Ast_Load: Ast{
String string;
};
struct Ast_Index: Ast_Expr{ struct Ast_Index: Ast_Expr{
Ast_Expr *expr; Ast_Expr *expr;
Ast_Expr *index; Ast_Expr *index;

12
base.kl Normal file
View File

@@ -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

View File

@@ -18,6 +18,12 @@ gen_line(Ast *node){
genln("#line %d", node->pos->line+1); 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 // @todo: Gen complicated decl
//array 10 ( pointer (pointer array 5 int a)) //array 10 ( pointer (pointer array 5 int a))
// int (*(*(a[5])))[10] // int (*(*(a[5])))[10]
@@ -41,7 +47,9 @@ gen_simple_decl_prefix(Ast_Type *ast){
auto name = constant->name; auto name = constant->name;
gen("%s ", name.str); gen("%s ", name.str);
}break; }break;
default: gen("%s ", name(ast)); default: {
gen("%s ", name(ast));
}
} }
} }
@@ -62,7 +70,7 @@ gen_simple_decl_postfix(Ast_Type *ast){
} }
function void 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) { if(ast->kind == TYPE_LAMBDA) {
gen_simple_decl_prefix(ast->func.ret); gen_simple_decl_prefix(ast->func.ret);
gen("(*%s)(", name.str); gen("(*%s)(", name.str);
@@ -75,6 +83,7 @@ gen_simple_decl(Ast_Type *ast, Intern_String name = {}){
else{ else{
gen_simple_decl_prefix(ast); gen_simple_decl_prefix(ast);
if(name.len) { if(name.len) {
if(scope) gen_scope_name(scope);
gen("%s", name.str); gen("%s", name.str);
} }
gen_simple_decl_postfix(ast); gen_simple_decl_postfix(ast);
@@ -124,18 +133,18 @@ enum {
}; };
function void function void
gen_var(Intern_String name, Ast_Type *type, Ast_Expr *expr, B32 emit_value){ gen_var(Ast_Decl *decl, B32 emit_value){
gen_simple_decl(type, name); gen_simple_decl(decl->type, decl->name, decl->parent_scope);
if(emit_value == DONT_EMIT_VALUE){ if(emit_value == DONT_EMIT_VALUE){
return; return;
} }
if(expr){ if(decl->expr){
gen(" = "); gen(" = ");
gen_expr(expr); gen_expr(decl->expr);
} else { // Default zero } else { // Default zero
if(is_numeric(type)){ if(is_numeric(decl->type)){
gen(" = 0"); gen(" = 0");
} else { } else {
gen(" = {}"); gen(" = {}");
@@ -145,10 +154,10 @@ gen_var(Intern_String name, Ast_Type *type, Ast_Expr *expr, B32 emit_value){
function void function void
gen_lambda(Intern_String name, Ast_Lambda *lambda){ 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("("); gen("(");
For(lambda->args){ 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(", "); if(&it != (lambda->args.end() - 1)) gen(", ");
} }
gen(")"); gen(")");
@@ -163,13 +172,17 @@ function void
gen_expr(Ast_Expr *ast){ gen_expr(Ast_Expr *ast){
switch(ast->kind){ switch(ast->kind){
CASE(IDENT, Atom){ CASE(IDENT, Atom){
gen_scope_name(node->parent_scope);
gen("%s", node->intern_val.str); gen("%s", node->intern_val.str);
BREAK(); BREAK();
} }
CASE(VALUE, Atom){ CASE(VALUE, Atom){
B32 written = gen_value(node->value); 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(); BREAK();
} }
@@ -273,7 +286,7 @@ gen_ast(Ast *ast){
} }
CASE(VAR, Decl){ 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(";"); if(!is_flag_set(ast->flags, AST_EXPR)) gen(";");
BREAK(); BREAK();
} }
@@ -333,7 +346,9 @@ gen_ast(Ast *ast){
} }
CASE(STRUCT, Decl){ 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++; global_indent++;
is_inside_struct++; is_inside_struct++;
For(node->scope->decls){ For(node->scope->decls){
@@ -343,7 +358,9 @@ gen_ast(Ast *ast){
is_inside_struct--; is_inside_struct--;
global_indent--; global_indent--;
genln("}%s;", node->name.str); genln("}");
gen_scope_name(node->parent_scope);
gen("%s;", node->name.str);
BREAK(); BREAK();
} }
@@ -359,10 +376,8 @@ gen_ast(Ast *ast){
global_indent++; global_indent++;
For(node->scope->decls){ For(node->scope->decls){
genln("%s", it->name.str); genln("%s", it->name.str);
if(it->expr){
gen(" = "); gen(" = ");
gen_expr(it->expr); gen_value(it->value);
}
gen(","); gen(",");
} }
global_indent--; global_indent--;
@@ -521,7 +536,7 @@ resolve_everything_in_module(Ast_Module *module){
For_Named(it->decls, jt){ For_Named(it->decls, jt){
resolve_name(it, jt->pos, jt->name); resolve_name(it, jt->pos, jt->name);
if(jt->kind == AST_STRUCT){ if(jt->kind == AST_STRUCT){
type_complete(jt->type); type_complete(jt->type_val);
} }
} }
} }

View File

@@ -1,7 +1,5 @@
Allocator_Kind :: enum #flag Allocator_Kind :: enum #flag
Null Null; Arena; Heap
Arena
Heap :: 4
kind := Allocator_Kind.Heap kind := Allocator_Kind.Heap

View File

@@ -174,9 +174,7 @@ int main(int argument_count, char **arguments){
#else #else
F64 total_time = os_time(); F64 total_time = os_time();
begin_compilation(); begin_compilation();
add_module(pctx->intern("order1.kl"_s)); Ast_Module *module = add_module(pctx->intern("base.kl"_s));
Ast_Module *module = add_module(pctx->intern("Windows.kl"_s));
parse_all_modules(); parse_all_modules();
assert(module); assert(module);
resolve_everything_in_module(module); resolve_everything_in_module(module);

View File

@@ -1,6 +1,5 @@
unary_test :: () unary_test :: ()
// int_val :: 125152553512512512512 // @todo bit ints
int_val :: 141 int_val :: 141
float_val :: 124.42 float_val :: 124.42
conversion: F64 = -+int_val conversion: F64 = -+int_val
@@ -57,8 +56,6 @@ binary_test :: (thing: S32 = 442)
if int_val > 1 if int_val > 1
pass pass
basic_type_assignment :: () basic_type_assignment :: ()
// custom_data1 := Custom_Data(thing = 23) // custom_data1 := Custom_Data(thing = 23)
// custom_data2: Custom_Data // custom_data2: Custom_Data

View File

@@ -967,7 +967,7 @@ resolve_decl(Ast_Decl *ast){
node->type = type_type; node->type = type_type;
node->type_val = type_enum(node, type_of_enum); node->type_val = type_enum(node, type_of_enum);
S64 value = 0; S64 value = 1;
For(node->scope->decls){ For(node->scope->decls){
Operand op = {}; Operand op = {};
if(it->expr){ if(it->expr){
@@ -976,7 +976,12 @@ resolve_decl(Ast_Decl *ast){
} else{ } else{
it->state = DECL_RESOLVED; it->state = DECL_RESOLVED;
op.type = untyped_int; 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; it->value = op.value;