From b9fa669fd11e63d026c6f298fc18f2fac5ffb6fe Mon Sep 17 00:00:00 2001 From: Timerix Date: Wed, 12 Mar 2025 12:57:16 +0500 Subject: [PATCH] fixed bugs --- src/compiler/Binary.c | 2 + src/compiler/Binary.h | 1 + src/compiler/Compiler.c | 44 +++++++++++++++------- src/compiler/Lexer.c | 2 +- src/compiler/Parser.c | 9 ++++- src/instructions/impl/logical_operators.c | 6 +-- src/instructions/impl/math_operators.c | 2 +- src/main.c | 46 ----------------------- 8 files changed, 47 insertions(+), 65 deletions(-) diff --git a/src/compiler/Binary.c b/src/compiler/Binary.c index f382724..ac7b82e 100644 --- a/src/compiler/Binary.c +++ b/src/compiler/Binary.c @@ -27,6 +27,8 @@ void BinaryObject_construct(BinaryObject* ptr){ ptr->section_list = List_CompiledSection_alloc(64); HashMap_CompiledSectionPtr_alloc(&ptr->section_map); HashMap_ConstDataProps_alloc(&ptr->const_data_map); + ptr->main_section = NULL; + ptr->total_size = 0; } void BinaryObject_free(BinaryObject* ptr){ diff --git a/src/compiler/Binary.h b/src/compiler/Binary.h index 5a28436..bbd6e43 100644 --- a/src/compiler/Binary.h +++ b/src/compiler/Binary.h @@ -57,6 +57,7 @@ HashMap_declare(CompiledSectionPtr); typedef struct BinaryObject { List_CompiledSection section_list; HashMap_CompiledSectionPtr section_map; + NULLABLE(CompiledSection*) main_section; HashMap_ConstDataProps const_data_map; u32 total_size; } BinaryObject; diff --git a/src/compiler/Compiler.c b/src/compiler/Compiler.c index a61742e..7a1ee32 100644 --- a/src/compiler/Compiler.c +++ b/src/compiler/Compiler.c @@ -124,6 +124,9 @@ static bool compileSection(Compiler* cmp, Section* sec){ } static bool compileBinary(Compiler* cmp){ + returnErrorIf_auto(cmp->state != CompilerState_Parsing); + cmp->state = CompilerState_Compiling; + for(u32 i = 0; i < cmp->ast.sections.len; i++){ SectionPtr sec = &cmp->ast.sections.data[i]; if(!compileSection(cmp, sec)){ @@ -137,6 +140,7 @@ static bool compileBinary(Compiler* cmp){ if(main_sec_ptrptr == NULL){ returnError("no 'main' section was defined"); } + cmp->binary.main_section = *main_sec_ptrptr; // create linked list of CompiledSection where main is the first CompiledSection* prev_sec = *main_sec_ptrptr; @@ -160,6 +164,8 @@ static bool compileBinary(Compiler* cmp){ returnError("duplicate named data '%s'", str_copy(cd.name).data); } } + + prev_sec = sec; } // insert calculated offsets into sections @@ -194,26 +200,16 @@ static bool compileBinary(Compiler* cmp){ } static bool writeBinaryFile(Compiler* cmp, FILE* f){ - returnErrorIf_auto(cmp->state != CompilerState_Parsing); - cmp->state = CompilerState_Compiling; + returnErrorIf_auto(cmp->state != CompilerState_Compiling); - if(!compileBinary(cmp)){ - return false; - } - - - CompiledSection** main_sec_ptrptr = HashMap_CompiledSectionPtr_tryGetPtr(&cmp->binary.section_map, STR("main")); - if(main_sec_ptrptr == NULL){ - returnError("no 'main' section was defined"); - } - CompiledSection* sec = *main_sec_ptrptr; + CompiledSection* sec = cmp->binary.main_section; while(sec){ fwrite(sec->bytes.data, 1, sec->bytes.len, f); + fflush(f); sec = sec->next; } //TODO: print warnings for unused sections - return true; } @@ -289,6 +285,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, if(debug_log) printf("===================================[parsing]===================================\n"); success = Compiler_parse(cmp); + if (debug_log){ printf("-------------------------------------[AST]-------------------------------------\n"); for(u32 i = 0; i < cmp->ast.sections.len; i++){ @@ -309,6 +306,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, Operation* op = &sec->code.data[j]; const Instruction* instr = Instruction_getByOpcode(op->opcode); if(instr == NULL){ + fclose(f); returnError("unknown opcode: %i", op->opcode) } @@ -352,6 +350,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, } } } + if(!success){ fclose(f); return false; @@ -359,6 +358,25 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, if(debug_log) printf("==================================[compiling]==================================\n"); + success = compileBinary(cmp); + + if(debug_log){ + for(u32 i = 0; i < cmp->binary.section_list.len; i++){ + CompiledSection* sec = &cmp->binary.section_list.data[i]; + str tmpstr = str_copy(sec->name); + printf("compiled section '%s' to %u bytes with offset 0x%x\n", tmpstr.data, sec->bytes.len, sec->offset); + free(tmpstr.data); + } + } + + if(!success){ + fclose(f); + return false; + } + + + if(debug_log) + printf("----------------------------[writing output to file]---------------------------\n"); success = writeBinaryFile(cmp, f); fclose(f); if(success){ diff --git a/src/compiler/Lexer.c b/src/compiler/Lexer.c index b0043bc..665de3a 100644 --- a/src/compiler/Lexer.c +++ b/src/compiler/Lexer.c @@ -130,7 +130,7 @@ static void readArguments(Compiler* cmp){ // string argument reading if(quot != '\0'){ - if(c == quot && cmp->code.data[cmp->pos - 1] != '\\'){ + if(c == quot && (cmp->code.data[cmp->pos - 1] != '\\' || cmp->code.data[cmp->pos - 2] == '\\')){ quot = '\0'; } else if(c == '\r' || c == '\n'){ diff --git a/src/compiler/Parser.c b/src/compiler/Parser.c index d0c1573..2214e8f 100644 --- a/src/compiler/Parser.c +++ b/src/compiler/Parser.c @@ -44,7 +44,8 @@ static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){ c = src.data[i]; if(c == '\\'){ escaped = !escaped; - continue; + if(escaped) + continue; } if(!escaped){ @@ -69,11 +70,17 @@ static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){ case 'e': StringBuilder_append_char(&sb, '\e'); break; + case '"': + case '\'': + StringBuilder_append_char(&sb, c); + break; default: setError_unexpectedTokenChar(cmp->tokens.data[cmp->tok_i], i); StringBuilder_free(&sb); return str_null; } + + escaped = false; } return StringBuilder_getStr(&sb); diff --git a/src/instructions/impl/logical_operators.c b/src/instructions/impl/logical_operators.c index 21d3f84..c831dbc 100644 --- a/src/instructions/impl/logical_operators.c +++ b/src/instructions/impl/logical_operators.c @@ -22,7 +22,7 @@ i32 NAME##_impl (VM* vm) {\ vm->registers.array[src_register_i].u64v = OPERATOR vm->registers.array[src_register_i].u64v;\ break;\ }\ - return sizeof(src_register_i) + sizeof(value_size);\ + return sizeof(src_register_i) /*+ sizeof(value_size)*/;\ } #define logicalOperator2Impl(NAME, OPERATOR)\ @@ -48,7 +48,7 @@ i32 NAME##_impl (VM* vm) {\ vm->registers.array[dst_register_i].u64v OPERATOR##= vm->registers.array[src_register_i].u64v;\ break;\ }\ - return sizeof(dst_register_i) + sizeof(src_register_i) + sizeof(value_size);\ + return sizeof(dst_register_i) + sizeof(src_register_i) /*+ sizeof(value_size)*/;\ } #define logicalOperator3Impl(NAME, OPERATOR)\ @@ -75,7 +75,7 @@ i32 NAME##_impl (VM* vm) {\ vm->registers.array[dst_register_i].u64v = vm->registers.array[src0_register_i].u64v OPERATOR vm->registers.array[src1_register_i].u64v;\ break;\ }\ - return sizeof(dst_register_i) + sizeof(src0_register_i) + sizeof(src1_register_i) + sizeof(value_size);\ + return sizeof(dst_register_i) + sizeof(src0_register_i) + sizeof(src1_register_i) /*+ sizeof(value_size)*/;\ } logicalOperator3Impl(EQ, ==) diff --git a/src/instructions/impl/math_operators.c b/src/instructions/impl/math_operators.c index 83a6455..0446600 100644 --- a/src/instructions/impl/math_operators.c +++ b/src/instructions/impl/math_operators.c @@ -23,7 +23,7 @@ i32 NAME##_impl (VM* vm) {\ vm->registers.array[dst_register_i].u64v OPERATOR##= vm->registers.array[src_register_i].u64v;\ break;\ }\ - return sizeof(dst_register_i) + sizeof(src_register_i) + sizeof(value_size);\ + return sizeof(dst_register_i) + sizeof(src_register_i) /*+ sizeof(value_size)*/;\ } /// ADD [dst_register] [src_register] diff --git a/src/main.c b/src/main.c index 6d4d484..648ab03 100644 --- a/src/main.c +++ b/src/main.c @@ -10,53 +10,7 @@ i32 compileSources(cstr source_file, cstr out_file, bool debug_log); i32 bootFromImage(cstr image_file); -#define assert_sdl(EXPR) if(!(EXPR)) { printf("assert failed: %s\nSDL_Error: %s\n", #EXPR, SDL_GetError()); return false; } - -bool test_display(){ - Display* display = Display_create(STR("le display"), 1600, 900, DisplayFlags_Default); - assert_sdl(display != NULL); - - i64 fps = 60; - i64 nsec_per_frame = 1e9 / fps; - for(u32 i = 0; i < 600; i++){ - static i64 last_frame_time = 0; - i64 current_frame_time = SDL_GetTicksNS(); - i64 delta_time = current_frame_time - last_frame_time; - last_frame_time = current_frame_time; - - SDL_Event event; - while(SDL_PollEvent(&event)) { - if(event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED){ - printfe("event WINDOW_CLOSE"); - return 0; - } - if(event.type == SDL_EVENT_QUIT){ - printfe("event QUIT"); - return 0; - } - } - - assert_sdl(Display_setDrawingColor(display, ColorRGBA_create(200, 200, 200, 255))); - assert_sdl(Display_clear(display)); - assert_sdl(Display_setDrawingColor(display, ColorRGBA_create(240, 40, 40, 255))); - assert_sdl(Display_fillRect(display, Rect_create(i, i, 64, 64))); - assert_sdl(Display_swapBuffers(display)); - - i64 time_ellapsed = SDL_GetTicksNS() - last_frame_time; - i64 delay = nsec_per_frame - time_ellapsed; - if (delay > 0) { - SDL_DelayNS(delay); - } - } - - Display_destroy(display); - return true; -} - i32 main(const i32 argc, cstr* argv){ - test_display(); - return 0; - if(argc < 2){ printfe("ERROR: no arguments provided. Use --help to know more.\n"); return 1;