Squashing file write bugs
This commit is contained in:
40
ccodegen.cpp
40
ccodegen.cpp
@@ -20,7 +20,7 @@ gen_line(Ast *node){
|
|||||||
genln("#line %d", node->pos->line+1);
|
genln("#line %d", node->pos->line+1);
|
||||||
if(node->pos->file != last_filename){
|
if(node->pos->file != last_filename){
|
||||||
last_filename = node->pos->file;
|
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 = {};
|
String string = {};
|
||||||
if(!should_gen_scope_name) return string;
|
if(!should_gen_scope_name) return string;
|
||||||
if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope);
|
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;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,9 +157,9 @@ gen_value(Value a){
|
|||||||
CASE_STRING:
|
CASE_STRING:
|
||||||
if(is_pointer(a.type)){
|
if(is_pointer(a.type)){
|
||||||
assert(a.type == type_pointer_to_char);
|
assert(a.type == type_pointer_to_char);
|
||||||
gen("\"%s\"", a.intern_val.str);
|
gen("\"%Q\"", a.intern_val);
|
||||||
} else{
|
} else{
|
||||||
gen("LIT(\"%s\")", a.intern_val.str);
|
gen("LIT(\"%Q\")", a.intern_val);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
CASE_BOOL: a.bool_val ? gen("true"):gen("false"); 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
|
function void
|
||||||
gen_lambda(Intern_String name, Ast_Lambda *lambda, B32 generate_block = true){
|
gen_lambda(Intern_String name, Ast_Lambda *lambda, B32 generate_block = true){
|
||||||
bool is_foreign = is_flag_set(lambda->flags, AST_FOREIGN);
|
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;
|
is_foreign = true;
|
||||||
}
|
}
|
||||||
gen_simple_decl(lambda->resolved_type->func.ret, name, lambda->parent_scope, !is_foreign);
|
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)){
|
if(type_of_var && is_slice(type_of_var) && is_array(node->resolved_decl->type)){
|
||||||
gen("{%d, ", (int)node->resolved_decl->type->arr.size);
|
gen("{%d, ", (int)node->resolved_decl->type->arr.size);
|
||||||
if(print_scope) gen_scope_name(node->resolved_decl->parent_scope);
|
if(print_scope) gen_scope_name(node->resolved_decl->parent_scope);
|
||||||
gen("%s}", node->intern_val.str);
|
gen("%Q}", node->intern_val);
|
||||||
} else {
|
} else {
|
||||||
if(print_scope) gen_scope_name(node->resolved_decl->parent_scope);
|
if(print_scope) gen_scope_name(node->resolved_decl->parent_scope);
|
||||||
gen("%s", node->intern_val.str);
|
gen("%Q", node->intern_val);
|
||||||
}
|
}
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
@@ -254,7 +254,7 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
|
|||||||
B32 written = gen_value(node->value);
|
B32 written = gen_value(node->value);
|
||||||
if(!written) {
|
if(!written) {
|
||||||
gen_scope_name(node->parent_scope);
|
gen_scope_name(node->parent_scope);
|
||||||
gen("%s", node->value.intern_val.str);
|
gen("%Q", node->value.intern_val);
|
||||||
}
|
}
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
@@ -346,7 +346,7 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
|
|||||||
|
|
||||||
For(node->exprs){
|
For(node->exprs){
|
||||||
if(is_struct(node->resolved_type))
|
if(is_struct(node->resolved_type))
|
||||||
gen(".%s = ", it->resolved_name.str);
|
gen(".%Q = ", it->resolved_name);
|
||||||
else if(is_array(node->resolved_type))
|
else if(is_array(node->resolved_type))
|
||||||
gen("[%d] = ", (int)it->resolved_index);
|
gen("[%d] = ", (int)it->resolved_index);
|
||||||
gen_expr(it->item);
|
gen_expr(it->item);
|
||||||
@@ -371,7 +371,7 @@ gen_ast(Ast *ast){
|
|||||||
gen("(");
|
gen("(");
|
||||||
gen_expr(node->expr);
|
gen_expr(node->expr);
|
||||||
if(node->assert_message.len){
|
if(node->assert_message.len){
|
||||||
gen(", \"%s\"", node->assert_message.str);
|
gen(", \"%Q\"", node->assert_message);
|
||||||
}
|
}
|
||||||
gen(");");
|
gen(");");
|
||||||
BREAK();
|
BREAK();
|
||||||
@@ -460,7 +460,7 @@ gen_ast(Ast *ast){
|
|||||||
CASE(STRUCT, Decl){
|
CASE(STRUCT, Decl){
|
||||||
gen("struct ");
|
gen("struct ");
|
||||||
gen_scope_name(node->parent_scope);
|
gen_scope_name(node->parent_scope);
|
||||||
gen("%s{", node->name.str);
|
gen("%Q{", node->name);
|
||||||
global_indent++;
|
global_indent++;
|
||||||
is_inside_struct++;
|
is_inside_struct++;
|
||||||
For(node->scope->decls){
|
For(node->scope->decls){
|
||||||
@@ -475,17 +475,17 @@ gen_ast(Ast *ast){
|
|||||||
}
|
}
|
||||||
|
|
||||||
CASE(TYPE, Decl){
|
CASE(TYPE, Decl){
|
||||||
gen("// Type %s = ", node->name.str);
|
gen("// Type %Q = ", node->name);
|
||||||
gen_simple_decl(node->type_val);
|
gen_simple_decl(node->type_val);
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
|
|
||||||
CASE(ENUM, Decl){
|
CASE(ENUM, Decl){
|
||||||
gen("/*enum %s{", node->name.str);
|
gen("/*enum %Q{", node->name);
|
||||||
// @todo add typespec
|
// @todo add typespec
|
||||||
global_indent++;
|
global_indent++;
|
||||||
For(node->scope->decls){
|
For(node->scope->decls){
|
||||||
genln("%s", it->name.str);
|
genln("%Q", it->name);
|
||||||
gen(" = ");
|
gen(" = ");
|
||||||
gen_value(it->value);
|
gen_value(it->value);
|
||||||
gen(",");
|
gen(",");
|
||||||
@@ -498,20 +498,20 @@ gen_ast(Ast *ast){
|
|||||||
CASE(CONST, Decl){
|
CASE(CONST, Decl){
|
||||||
switch(node->type->kind){
|
switch(node->type->kind){
|
||||||
CASE_FLOAT:{
|
CASE_FLOAT:{
|
||||||
gen("// F64 %s = ", node->name.str);
|
gen("// F64 %Q = ", node->name);
|
||||||
gen_value(node->value);
|
gen_value(node->value);
|
||||||
} break;
|
} break;
|
||||||
CASE_INT:{
|
CASE_INT:{
|
||||||
gen("// constant int %s = ", node->name.str);
|
gen("// constant int %Q = ", node->name);
|
||||||
gen_value(node->value);
|
gen_value(node->value);
|
||||||
}break;
|
}break;
|
||||||
CASE_STRING:{
|
CASE_STRING:{
|
||||||
assert(is_pointer(node->type) ? node->type == type_pointer_to_char : 1);
|
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);
|
gen_value(node->value);
|
||||||
}break;
|
}break;
|
||||||
CASE_BOOL:{
|
CASE_BOOL:{
|
||||||
gen("// const Bool %s = ", node->name.str);
|
gen("// const Bool %Q = ", node->name);
|
||||||
gen_value(node->value);
|
gen_value(node->value);
|
||||||
}break;
|
}break;
|
||||||
case TYPE_LAMBDA:{
|
case TYPE_LAMBDA:{
|
||||||
@@ -710,7 +710,7 @@ typedef struct String{
|
|||||||
#endif
|
#endif
|
||||||
For(pctx->ordered_decls){
|
For(pctx->ordered_decls){
|
||||||
if(it->kind == AST_STRUCT){
|
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);
|
gen_ast(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
exp_destroy(pctx->heap);
|
|
||||||
String string_result = string_flatten(pctx->perm, &pctx->gen);
|
String string_result = string_flatten(pctx->perm, &pctx->gen);
|
||||||
|
exp_destroy(pctx->heap);
|
||||||
generating_time_end = os_time();
|
generating_time_end = os_time();
|
||||||
return string_result;
|
return string_result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -235,14 +235,13 @@ parse_init(Parse_Ctx *ctx, Allocator *perm_allocator, Allocator *heap_allocator)
|
|||||||
pctx = ctx;
|
pctx = ctx;
|
||||||
ctx->perm = perm_allocator;
|
ctx->perm = perm_allocator;
|
||||||
ctx->heap = heap_allocator;
|
ctx->heap = heap_allocator;
|
||||||
ctx->gen = {ctx->perm};
|
ctx->gen = {ctx->heap};
|
||||||
ctx->ordered_decls = {ctx->heap};
|
ctx->ordered_decls = {ctx->heap};
|
||||||
ctx->type_map = {ctx->heap};
|
ctx->type_map = {ctx->heap};
|
||||||
ctx->modules = {ctx->heap};
|
ctx->modules = {ctx->heap};
|
||||||
bigint_allocator = ctx->perm;
|
bigint_allocator = ctx->perm;
|
||||||
|
|
||||||
lex_init(ctx->perm, ctx->heap, ctx);
|
lex_init(ctx->perm, ctx->heap, ctx);
|
||||||
|
|
||||||
ctx->builtins = ast_module(ctx->intern("builtins"_s));
|
ctx->builtins = ast_module(ctx->intern("builtins"_s));
|
||||||
insert_builtin_types_into_scope((Ast_Scope *)ctx->builtins);
|
insert_builtin_types_into_scope((Ast_Scope *)ctx->builtins);
|
||||||
|
|
||||||
|
|||||||
18
main.cpp
18
main.cpp
@@ -160,7 +160,7 @@ int main(int argument_count, char **arguments){
|
|||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
String name = string_fmt(scratch, "%s.kl", arguments[1]);
|
String name = string_fmt(scratch, "%s.kl", arguments[1]);
|
||||||
String c_filename = string_fmt(scratch, "%s.c", 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]);
|
String run_program = string_fmt(scratch, "%s.exe", arguments[1]);
|
||||||
|
|
||||||
Array<String> files = {scratch};
|
Array<String> files = {scratch};
|
||||||
@@ -186,10 +186,18 @@ int main(int argument_count, char **arguments){
|
|||||||
resolve_everything_in_module(module);
|
resolve_everything_in_module(module);
|
||||||
String result = end_compilation();
|
String result = end_compilation();
|
||||||
assert(os_write_file("program.c"_s, result));
|
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);
|
Scratch scratch;
|
||||||
printf("\nTotal resolving = %f", resolving_time_end - resolving_time_begin);
|
F64 begin = os_time();
|
||||||
printf("\nTotal generatin = %f", generating_time_end - generating_time_begin);
|
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
|
#endif
|
||||||
__debugbreak();
|
__debugbreak();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -435,7 +435,7 @@ binding_power(Binding binding, Token_Kind kind){
|
|||||||
case TK_Dot:
|
case TK_Dot:
|
||||||
return {31,30};
|
return {31,30};
|
||||||
case TK_Arrow:
|
case TK_Arrow:
|
||||||
return {36,35};
|
return {29,28};
|
||||||
default: return {};
|
default: return {};
|
||||||
}
|
}
|
||||||
Postfix: switch(kind){
|
Postfix: switch(kind){
|
||||||
|
|||||||
@@ -48,3 +48,4 @@ STD_OUTPUT_HANDLE :: 4294967285//(-11)->DWORD
|
|||||||
//STD_ERROR_HANDLE :: (-12)->DWORD
|
//STD_ERROR_HANDLE :: (-12)->DWORD
|
||||||
GetStdHandle :: #foreign (nStdHandle: DWORD): HANDLE
|
GetStdHandle :: #foreign (nStdHandle: DWORD): HANDLE
|
||||||
WriteConsoleA :: #foreign (hConsoleOutput: HANDLE,lpBuffer: *VOID,nNumberOfCharsToWrite: DWORD,lpNumberOfCharsWritten: LPDWORD,lpReserve: LPVOID): BOOL
|
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
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
// #import "base.kl"
|
// #import "base.kl"
|
||||||
#load "gdi32.kl"
|
#load "gdi32.kl"
|
||||||
#load "user32.kl"
|
#load "user32.kl"
|
||||||
|
#load "os.kl"
|
||||||
|
|
||||||
String16 :: struct;; str: *U16; len: S64
|
String16 :: struct;; str: *U16; len: S64
|
||||||
String32 :: struct;; str: *U32; 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)
|
result.hdc = CreateCompatibleDC(hdc)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT
|
window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRESULT
|
||||||
if msg == WM_DESTROY
|
if msg == WM_DESTROY
|
||||||
PostQuitMessage(0)
|
PostQuitMessage(0)
|
||||||
@@ -142,6 +142,7 @@ main :: (argc: int, argv: **char): int
|
|||||||
|
|
||||||
string := " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..."
|
string := " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..."
|
||||||
string_result := string_to_string16(string)
|
string_result := string_to_string16(string)
|
||||||
|
print(string_result)
|
||||||
|
|
||||||
result := utf8_to_utf32(&"A"[0], 1)
|
result := utf8_to_utf32(&"A"[0], 1)
|
||||||
assert(result.out_str == 'A, "Invalid decode")
|
assert(result.out_str == 'A, "Invalid decode")
|
||||||
|
|||||||
@@ -39,6 +39,6 @@ release :: (m: *Memory)
|
|||||||
m.commit = 0
|
m.commit = 0
|
||||||
m.reserve = 0
|
m.reserve = 0
|
||||||
|
|
||||||
print :: (string: String)
|
print :: (string: String16)
|
||||||
handle := GetStdHandle(STD_OUTPUT_HANDLE)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user