From 7ca83267ba5c4f1b2e768201866f37bd9be7d0aa Mon Sep 17 00:00:00 2001 From: Ed_ Date: Fri, 20 Feb 2026 22:56:19 -0500 Subject: [PATCH] fixes --- attempt_1/main.c | 90 ++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 33 deletions(-) diff --git a/attempt_1/main.c b/attempt_1/main.c index 9351d6e..3ee8f1f 100644 --- a/attempt_1/main.c +++ b/attempt_1/main.c @@ -44,7 +44,7 @@ global FArena tape_arena; global FArena anno_arena; global U8 cursor_idx = 0; global U4 editor_mode = MODE_NAV; -global U4 mode_switch_now = false; +global B4 mode_switch_now = false; global FArena code_arena; @@ -87,10 +87,19 @@ global const char* prim_names[] = { "PRINT " }; +internal U4 resolve_name_to_index(const char* ref_name); + IA_ void scatter(U4 token, const char* anno_str) { if (tape_arena.used + sizeof(U4) <= tape_arena.capacity && anno_arena.used + sizeof(U8) <= anno_arena.capacity) { + U4 tag = unpack_tag(token); + U4 val = unpack_val(token); + + if (anno_str && (tag == STag_Call || tag == STag_Imm)) { + val = resolve_name_to_index(anno_str); + } + U4*r ptr = u4_r(tape_arena.start + tape_arena.used); - ptr[0] = token; + ptr[0] = pack_token(tag, val); tape_arena.used += sizeof(U4); U8*r aptr = u8_r(anno_arena.start + anno_arena.used); aptr[0] = 0; @@ -119,6 +128,36 @@ internal void pad32(void) { while ((code_arena.used % 4) != 0) emit8(0x90); } +internal U4 resolve_name_to_index(const char* ref_name) { + U8 tape_count = tape_arena.used / sizeof(U4); + U4*r tape_ptr = u4_r(tape_arena.start); + U8*r anno_ptr = u8_r(anno_arena.start); + + for (int p = 1; p <= 9; p++) { + int match = 1; + for (int c = 0; c < 8; c++) { + char c1 = ref_name[c] ? ref_name[c] : ' '; + char c2 = prim_names[p][c] ? prim_names[p][c] : ' '; + if (c1 != c2) { match = 0; break; } + } + if (match) return p; + } + + for (U8 j = 0; j < tape_count; j++) { + if (unpack_tag(tape_ptr[j]) == STag_Define) { + char* def_name = (char*)&anno_ptr[j]; + int match = 1; + for (int c = 0; c < 8; c++) { + char c1 = ref_name[c] ? ref_name[c] : ' '; + char c2 = def_name[c] ? def_name[c] : ' '; + if (c1 != c2) { match = 0; break; } + } + if (match) return j; + } + } + return 0; +} + internal void relink_tape(void) { U8 tape_count = tape_arena.used / sizeof(U4); U4*r tape_ptr = u4_r(tape_arena.start); @@ -129,32 +168,7 @@ internal void relink_tape(void) { U4 tag = unpack_tag(t); if (tag == STag_Call || tag == STag_Imm) { char* ref_name = (char*)&anno_ptr[i]; - - U4 new_val = 0; - for (int p = 1; p <= 9; p++) { - int match = 1; - for (int c = 0; c < 8; c++) { - char c1 = ref_name[c] ? ref_name[c] : ' '; - char c2 = prim_names[p][c] ? prim_names[p][c] : ' '; - if (c1 != c2) { match = 0; break; } - } - if (match) { new_val = p; break; } - } - - if (new_val == 0) { - for (U8 j = 0; j < tape_count; j++) { - if (unpack_tag(tape_ptr[j]) == STag_Define) { - char* def_name = (char*)&anno_ptr[j]; - int match = 1; - for (int c = 0; c < 8; c++) { - char c1 = ref_name[c] ? ref_name[c] : ' '; - char c2 = def_name[c] ? def_name[c] : ' '; - if (c1 != c2) { match = 0; break; } - } - if (match) { new_val = j; break; } - } - } - } + U4 new_val = resolve_name_to_index(ref_name); tape_ptr[i] = pack_token(tag, new_val); } } @@ -300,7 +314,7 @@ IA_ void compile_and_run_tape(void) S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) { U8 tape_count = tape_arena.used / sizeof(U4); - U4*r tape_ptr = C_(U4*r, tape_arena.start); + U4*r tape_ptr = u4_r(tape_arena.start); switch (msg) { case MS_WM_CHAR: { if (editor_mode != MODE_EDIT) return 0; @@ -310,7 +324,7 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) U4 val = unpack_val(t); U1 c = u1_(wparam); - bool should_skip = c < 32 || (c == 'e' && mode_switch_now); + B4 should_skip = c < 32 || (c == 'e' && mode_switch_now); if (should_skip) { mode_switch_now = false; return 0; } if (tag == STag_Data) { @@ -331,10 +345,15 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) if (len < 8) { anno_str[len] = (char)c; for (int i = len + 1; i < 8; i++) anno_str[i] = '\0'; + + if (tag == STag_Call || tag == STag_Imm || tag == STag_Define) { + U4 new_val = resolve_name_to_index(anno_str); + tape_ptr[cursor_idx] = pack_token(tag, new_val); + if (tag == STag_Define) relink_tape(); + } } } vm_rax = 0; vm_rdx = 0; mem_zero(u8_(vm_globals), sizeof(vm_globals)); - relink_tape(); compile_and_run_tape(); ms_invalidate_rect(hwnd, nullptr, true); return 0; @@ -348,6 +367,7 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) } if (wparam == 0x1B && editor_mode == MODE_EDIT) { editor_mode = MODE_NAV; + relink_tape(); ms_invalidate_rect(hwnd, nullptr, true); return 0; } @@ -367,10 +387,14 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) while (len < 8 && anno_str[len] != '\0' && anno_str[len] != ' ') len ++; if (len > 0) { anno_str[len - 1] = '\0'; + if (tag == STag_Call || tag == STag_Imm || tag == STag_Define) { + U4 new_val = resolve_name_to_index(anno_str); + tape_ptr[cursor_idx] = pack_token(tag, new_val); + if (tag == STag_Define) relink_tape(); + } } } vm_rax = 0; vm_rdx = 0; mem_zero(u8_(vm_globals), sizeof(vm_globals)); - relink_tape(); compile_and_run_tape(); ms_invalidate_rect(hwnd, nullptr, true); } @@ -435,6 +459,7 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) tape_arena.used -= sizeof(U4); anno_arena.used -= sizeof(U8); } + relink_tape(); } else if (wparam == MS_VK_SPACE || wparam == MS_VK_RETURN) { B4 is_shift = (ms_get_async_key_state(MS_VK_SHIFT) & 0x8000) != 0; @@ -463,7 +488,6 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) vm_rax = 0; vm_rdx = 0; mem_zero(u8_(vm_globals), sizeof(vm_globals)); - relink_tape(); compile_and_run_tape(); ms_invalidate_rect(hwnd, nullptr, true);