From 8b470fbf34dc01f02dcf39844af4d7e858b2d58c Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 15 Jun 2022 12:11:34 +0200 Subject: [PATCH] Squashing file write bugs --- ccodegen.cpp | 40 ++++++++++++++++++++-------------------- compiler.h | 3 +-- main.cpp | 18 +++++++++++++----- parsing.cpp | 2 +- programs/Windows.kl | 3 ++- programs/main.kl | 3 ++- programs/os.kl | 4 ++-- 7 files changed, 41 insertions(+), 32 deletions(-) diff --git a/ccodegen.cpp b/ccodegen.cpp index 50a675d..a8f7433 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -20,7 +20,7 @@ gen_line(Ast *node){ genln("#line %d", node->pos->line+1); if(node->pos->file != last_filename){ last_filename = node->pos->file; - gen(" \"%s\"", last_filename.str); + gen(" \"%Q\"", last_filename); } } } @@ -30,7 +30,7 @@ string_scope_name(Allocator *a, Ast_Scope *scope){ String string = {}; if(!should_gen_scope_name) return string; if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope); - if(scope->name.str) string_fmt(a, "%Q%s_", string, scope->name.str); + if(scope->name.str) string_fmt(a, "%Q%Q_", string, scope->name); return string; } @@ -157,9 +157,9 @@ gen_value(Value a){ CASE_STRING: if(is_pointer(a.type)){ assert(a.type == type_pointer_to_char); - gen("\"%s\"", a.intern_val.str); + gen("\"%Q\"", a.intern_val); } else{ - gen("LIT(\"%s\")", a.intern_val.str); + gen("LIT(\"%Q\")", a.intern_val); } break; CASE_BOOL: a.bool_val ? gen("true"):gen("false"); break; @@ -210,7 +210,7 @@ gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names){ function void gen_lambda(Intern_String name, Ast_Lambda *lambda, B32 generate_block = true){ bool is_foreign = is_flag_set(lambda->flags, AST_FOREIGN); - if(name == pctx->intern("main"_s)){ + if(name == pctx->intern("main"_s) || name == pctx->intern("WinMain"_s)){ is_foreign = true; } gen_simple_decl(lambda->resolved_type->func.ret, name, lambda->parent_scope, !is_foreign); @@ -242,10 +242,10 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ if(type_of_var && is_slice(type_of_var) && is_array(node->resolved_decl->type)){ gen("{%d, ", (int)node->resolved_decl->type->arr.size); if(print_scope) gen_scope_name(node->resolved_decl->parent_scope); - gen("%s}", node->intern_val.str); + gen("%Q}", node->intern_val); } else { if(print_scope) gen_scope_name(node->resolved_decl->parent_scope); - gen("%s", node->intern_val.str); + gen("%Q", node->intern_val); } BREAK(); } @@ -254,7 +254,7 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ B32 written = gen_value(node->value); if(!written) { gen_scope_name(node->parent_scope); - gen("%s", node->value.intern_val.str); + gen("%Q", node->value.intern_val); } BREAK(); } @@ -346,7 +346,7 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ For(node->exprs){ if(is_struct(node->resolved_type)) - gen(".%s = ", it->resolved_name.str); + gen(".%Q = ", it->resolved_name); else if(is_array(node->resolved_type)) gen("[%d] = ", (int)it->resolved_index); gen_expr(it->item); @@ -371,7 +371,7 @@ gen_ast(Ast *ast){ gen("("); gen_expr(node->expr); if(node->assert_message.len){ - gen(", \"%s\"", node->assert_message.str); + gen(", \"%Q\"", node->assert_message); } gen(");"); BREAK(); @@ -460,7 +460,7 @@ gen_ast(Ast *ast){ CASE(STRUCT, Decl){ gen("struct "); gen_scope_name(node->parent_scope); - gen("%s{", node->name.str); + gen("%Q{", node->name); global_indent++; is_inside_struct++; For(node->scope->decls){ @@ -475,17 +475,17 @@ gen_ast(Ast *ast){ } CASE(TYPE, Decl){ - gen("// Type %s = ", node->name.str); + gen("// Type %Q = ", node->name); gen_simple_decl(node->type_val); BREAK(); } CASE(ENUM, Decl){ - gen("/*enum %s{", node->name.str); + gen("/*enum %Q{", node->name); // @todo add typespec global_indent++; For(node->scope->decls){ - genln("%s", it->name.str); + genln("%Q", it->name); gen(" = "); gen_value(it->value); gen(","); @@ -498,20 +498,20 @@ gen_ast(Ast *ast){ CASE(CONST, Decl){ switch(node->type->kind){ CASE_FLOAT:{ - gen("// F64 %s = ", node->name.str); + gen("// F64 %Q = ", node->name); gen_value(node->value); } break; CASE_INT:{ - gen("// constant int %s = ", node->name.str); + gen("// constant int %Q = ", node->name); gen_value(node->value); }break; CASE_STRING:{ assert(is_pointer(node->type) ? node->type == type_pointer_to_char : 1); - gen("// const String %s = ", node->name.str); + gen("// const String %Q = ", node->name); gen_value(node->value); }break; CASE_BOOL:{ - gen("// const Bool %s = ", node->name.str); + gen("// const Bool %Q = ", node->name); gen_value(node->value); }break; case TYPE_LAMBDA:{ @@ -710,7 +710,7 @@ typedef struct String{ #endif For(pctx->ordered_decls){ if(it->kind == AST_STRUCT){ - genln("typedef struct %s %s;", it->name.str, it->name.str); + genln("typedef struct %Q %Q;", it->name, it->name); } } @@ -719,8 +719,8 @@ typedef struct String{ gen_ast(it); } - exp_destroy(pctx->heap); String string_result = string_flatten(pctx->perm, &pctx->gen); + exp_destroy(pctx->heap); generating_time_end = os_time(); return string_result; } diff --git a/compiler.h b/compiler.h index afc8a04..d291650 100644 --- a/compiler.h +++ b/compiler.h @@ -235,14 +235,13 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator) pctx = ctx; ctx->perm = perm_allocator; ctx->heap = heap_allocator; - ctx->gen = {ctx->perm}; + ctx->gen = {ctx->heap}; ctx->ordered_decls = {ctx->heap}; ctx->type_map = {ctx->heap}; ctx->modules = {ctx->heap}; bigint_allocator = ctx->perm; lex_init(ctx->perm, ctx->heap, ctx); - ctx->builtins = ast_module(ctx->intern("builtins"_s)); insert_builtin_types_into_scope((Ast_Scope *)ctx->builtins); diff --git a/main.cpp b/main.cpp index 5ac75b3..8f278e6 100644 --- a/main.cpp +++ b/main.cpp @@ -160,7 +160,7 @@ int main(int argument_count, char **arguments){ Scratch scratch; String name = string_fmt(scratch, "%s.kl", arguments[1]); String c_filename = string_fmt(scratch, "%s.c", arguments[1]); - String compiler_call = string_fmt(scratch, "clang.exe %s.c -g -o %s.exe", arguments[1], arguments[1]); + String run_program = string_fmt(scratch, "%s.exe", arguments[1]); Array files = {scratch}; @@ -186,10 +186,18 @@ int main(int argument_count, char **arguments){ resolve_everything_in_module(module); String result = end_compilation(); assert(os_write_file("program.c"_s, result)); - printf("\nTotal time = %f", os_time() - total_time); - printf("\nTotal parsing = %f", parsing_time_end - parsing_time_begin); - printf("\nTotal resolving = %f", resolving_time_end - resolving_time_begin); - printf("\nTotal generatin = %f", generating_time_end - generating_time_begin); + { + Scratch scratch; + F64 begin = os_time(); + String compiler_call = string_fmt(scratch, "clang.exe program.c -Wall -Wno-parentheses-equality -g -o a.exe -lgdi32 -luser32"); + system((const char *)compiler_call.str); + printf("\nclang = %f", os_time() - begin); + } + + printf("\ntotal = %f", os_time() - total_time); + printf("\nparsing = %f", parsing_time_end - parsing_time_begin); + printf("\nresolving = %f", resolving_time_end - resolving_time_begin); + printf("\ngeneratin = %f", generating_time_end - generating_time_begin); #endif __debugbreak(); } diff --git a/parsing.cpp b/parsing.cpp index c4ec26e..b5caee9 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -435,7 +435,7 @@ binding_power(Binding binding, Token_Kind kind){ case TK_Dot: return {31,30}; case TK_Arrow: - return {36,35}; + return {29,28}; default: return {}; } Postfix: switch(kind){ diff --git a/programs/Windows.kl b/programs/Windows.kl index 0ea267f..31de501 100644 --- a/programs/Windows.kl +++ b/programs/Windows.kl @@ -47,4 +47,5 @@ STD_INPUT_HANDLE :: 4294967286//(-10)->DWORD STD_OUTPUT_HANDLE :: 4294967285//(-11)->DWORD //STD_ERROR_HANDLE :: (-12)->DWORD GetStdHandle :: #foreign (nStdHandle: DWORD): HANDLE -WriteConsoleA :: #foreign (hConsoleOutput: HANDLE,lpBuffer: *VOID,nNumberOfCharsToWrite: DWORD,lpNumberOfCharsWritten: LPDWORD,lpReserve: LPVOID): BOOL \ No newline at end of file +WriteConsoleA :: #foreign (hConsoleOutput: HANDLE,lpBuffer: *VOID,nNumberOfCharsToWrite: DWORD,lpNumberOfCharsWritten: LPDWORD,lpReserve: LPVOID): BOOL +WriteConsoleW :: #foreign (hConsoleOutput: HANDLE,lpBuffer: *VOID,nNumberOfCharsToWrite: DWORD,lpNumberOfCharsWritten: LPDWORD,lpReserve: LPVOID): BOOL \ No newline at end of file diff --git a/programs/main.kl b/programs/main.kl index 02d1548..0450895 100644 --- a/programs/main.kl +++ b/programs/main.kl @@ -1,6 +1,7 @@ // #import "base.kl" #load "gdi32.kl" #load "user32.kl" +#load "os.kl" String16 :: struct;; str: *U16; len: S64 String32 :: struct;; str: *U32; len: S64 @@ -130,7 +131,6 @@ create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap result.hdc = CreateCompatibleDC(hdc) return result - window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT if msg == WM_DESTROY PostQuitMessage(0) @@ -142,6 +142,7 @@ main :: (argc: int, argv: **char): int string := " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..." string_result := string_to_string16(string) + print(string_result) result := utf8_to_utf32(&"A"[0], 1) assert(result.out_str == 'A, "Invalid decode") diff --git a/programs/os.kl b/programs/os.kl index 14f0f62..43c6e37 100644 --- a/programs/os.kl +++ b/programs/os.kl @@ -39,6 +39,6 @@ release :: (m: *Memory) m.commit = 0 m.reserve = 0 -print :: (string: String) +print :: (string: String16) handle := GetStdHandle(STD_OUTPUT_HANDLE) - WriteConsoleA(handle, &string[0]->*void, length_of(string)->DWORD, 0, 0) + WriteConsoleW(handle, (string.str)->*void, string.len->DWORD, 0, 0)