This commit is contained in:
2026-02-20 20:46:25 -05:00
parent 9db1748249
commit 784f3b9945

View File

@@ -314,14 +314,14 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam)
} }
case MS_WM_KEYDOWN: { case MS_WM_KEYDOWN: {
if (wparam == 0x45 && editor_mode == MODE_NAV) { // 'E' if (wparam == 0x45 && editor_mode == MODE_NAV) { // 'E'
editor_mode = MODE_EDIT; editor_mode = MODE_EDIT;
ms_invalidate_rect(hwnd, nullptr, true); ms_invalidate_rect(hwnd, nullptr, true);
return 0; // Consume the keypress so it doesn't trigger WM_CHAR return 0; // Consume the keypress so it doesn't trigger WM_CHAR
} }
if (wparam == 0x1B && editor_mode == MODE_EDIT) { // ESC if (wparam == 0x1B && editor_mode == MODE_EDIT) { // ESC
editor_mode = MODE_NAV; editor_mode = MODE_NAV;
ms_invalidate_rect(hwnd, nullptr, true); ms_invalidate_rect(hwnd, nullptr, true);
return 0; return 0;
} }
if (editor_mode == MODE_EDIT) { if (editor_mode == MODE_EDIT) {
if (wparam == MS_VK_BACK) { if (wparam == MS_VK_BACK) {
@@ -333,10 +333,10 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam)
tape_ptr[cursor_idx] = pack_token(tag, val); tape_ptr[cursor_idx] = pack_token(tag, val);
} }
else if (tag != STag_Format) { else if (tag != STag_Format) {
U8*r anno_ptr = C_(U8*r, anno_arena.start); U8*r anno_ptr = u8_r(anno_arena.start);
char* anno_str = (char*)&anno_ptr[cursor_idx]; char* anno_str = (char*) & anno_ptr[cursor_idx];
int len = 0; int len = 0;
while (len < 8 && anno_str[len] != '\0' && anno_str[len] != ' ') len++; while (len < 8 && anno_str[len] != '\0' && anno_str[len] != ' ') len ++;
if (len > 0) { if (len > 0) {
anno_str[len - 1] = '\0'; anno_str[len - 1] = '\0';
char c1 = anno_str[0] ? anno_str[0] : ' '; char c1 = anno_str[0] ? anno_str[0] : ' ';
@@ -355,30 +355,29 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam)
if (wparam == MS_VK_LEFT && cursor_idx > 0) cursor_idx --; if (wparam == MS_VK_LEFT && cursor_idx > 0) cursor_idx --;
if (wparam == MS_VK_UP) { if (wparam == MS_VK_UP) {
U8 line_start = cursor_idx; U8 line_start = cursor_idx;
while (line_start > 0 && unpack_tag(tape_ptr[line_start - 1]) != STag_Format) line_start--; while (line_start > 0 && unpack_tag(tape_ptr[line_start - 1]) != STag_Format) line_start--;
if (line_start > 0) { if (line_start > 0) {
U8 col = cursor_idx - line_start; U8 col = cursor_idx - line_start;
U8 prev_line_start = line_start - 1; U8 prev_line_start = line_start - 1;
while (prev_line_start > 0 && unpack_tag(tape_ptr[prev_line_start - 1]) != STag_Format) prev_line_start--; while (prev_line_start > 0 && unpack_tag(tape_ptr[prev_line_start - 1]) != STag_Format) prev_line_start--;
U8 prev_line_len = (line_start - 1) - prev_line_start; U8 prev_line_len = (line_start - 1) - prev_line_start;
cursor_idx = prev_line_start + (col < prev_line_len ? col : prev_line_len); cursor_idx = prev_line_start + (col < prev_line_len ? col : prev_line_len);
} }
} }
if (wparam == MS_VK_DOWN) { if (wparam == MS_VK_DOWN) {
U8 line_start = cursor_idx; U8 line_start = cursor_idx;
while (line_start > 0 && unpack_tag(tape_ptr[line_start - 1]) != STag_Format) line_start--; while (line_start > 0 && unpack_tag(tape_ptr[line_start - 1]) != STag_Format) line_start --;
U8 col = cursor_idx - line_start; U8 col = cursor_idx - line_start;
U8 next_line_start = cursor_idx;
U8 next_line_start = cursor_idx; while (next_line_start < tape_count && unpack_tag(tape_ptr[next_line_start]) != STag_Format) next_line_start ++;
while (next_line_start < tape_count && unpack_tag(tape_ptr[next_line_start]) != STag_Format) next_line_start++; if (next_line_start < tape_count) {
if (next_line_start < tape_count) { next_line_start ++; // Skip the newline
next_line_start++; // Skip the newline U8 next_line_end = next_line_start;
U8 next_line_end = next_line_start; while (next_line_end < tape_count && unpack_tag(tape_ptr[next_line_end]) != STag_Format) next_line_end ++;
while (next_line_end < tape_count && unpack_tag(tape_ptr[next_line_end]) != STag_Format) next_line_end++; U8 next_line_len = next_line_end - next_line_start;
U8 next_line_len = next_line_end - next_line_start; cursor_idx = next_line_start + (col < next_line_len ? col : next_line_len);
cursor_idx = next_line_start + (col < next_line_len ? col : next_line_len); }
}
} }
if (wparam == MS_VK_PRIOR) { scroll_y_offset -= 100; if (scroll_y_offset < 0) scroll_y_offset = 0; } if (wparam == MS_VK_PRIOR) { scroll_y_offset -= 100; if (scroll_y_offset < 0) scroll_y_offset = 0; }
@@ -386,10 +385,10 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam)
if (wparam == MS_VK_F5) { run_full = !run_full; } if (wparam == MS_VK_F5) { run_full = !run_full; }
if (wparam == MS_VK_TAB) { if (wparam == MS_VK_TAB) {
// Cycle Color Tag // Cycle Color Tag
U4 t = tape_ptr[cursor_idx]; U4 t = tape_ptr[cursor_idx];
U4 tag = (unpack_tag(t) + 1) % STag_Count; U4 tag = (unpack_tag(t) + 1) % STag_Count;
tape_ptr[cursor_idx] = pack_token(tag, unpack_val(t)); tape_ptr[cursor_idx] = pack_token(tag, unpack_val(t));
} }
else if (wparam == MS_VK_BACK) else if (wparam == MS_VK_BACK)
{ {
@@ -422,21 +421,21 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam)
if (is_shift) insert_idx ++; if (is_shift) insert_idx ++;
if (tape_arena.used + sizeof(U4) <= tape_arena.capacity && anno_arena.used + sizeof(U8) <= anno_arena.capacity) { if (tape_arena.used + sizeof(U4) <= tape_arena.capacity && anno_arena.used + sizeof(U8) <= anno_arena.capacity) {
U8*r anno_ptr = C_(U8*r, anno_arena.start); U8*r anno_ptr = u8_r(anno_arena.start);
for (U8 i = tape_count; i > insert_idx; i--) { for (U8 i = tape_count; i > insert_idx; i--) {
tape_ptr[i] = tape_ptr[i-1]; tape_ptr[i] = tape_ptr[i-1];
anno_ptr[i] = anno_ptr[i-1]; anno_ptr[i] = anno_ptr[i-1];
} }
if (wparam == MS_VK_RETURN) { if (wparam == MS_VK_RETURN) {
tape_ptr[insert_idx] = pack_token(STag_Format, 0xA); tape_ptr[insert_idx] = pack_token(STag_Format, 0xA);
anno_ptr[insert_idx] = 0; anno_ptr[insert_idx] = 0;
} else { } else {
tape_ptr[insert_idx] = pack_token(STag_Comment, id2(' ',' ')); tape_ptr[insert_idx] = pack_token(STag_Comment, id2(' ',' '));
anno_ptr[insert_idx] = 0; anno_ptr[insert_idx] = 0;
} }
if (is_shift) cursor_idx++; if (is_shift) cursor_idx++;
tape_arena.used += sizeof(U4); tape_arena.used += sizeof(U4);
anno_arena.used += sizeof(U8); anno_arena.used += sizeof(U8);
} }
} }
@@ -451,8 +450,8 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam)
} }
case MS_WM_PAINT: { case MS_WM_PAINT: {
MS_PAINTSTRUCT ps; MS_PAINTSTRUCT ps;
void* hdc = ms_begin_paint(hwnd, &ps); void* hdc = ms_begin_paint(hwnd, & ps);
void* hFont = ms_create_font_a(20, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 0, "Consolas"); void* hFont = ms_create_font_a(20, 0, 0, 0, 400, 0, 0, 0, 0, 0, 0, 0, 0, "Consolas");
void* hOldFont = ms_select_object(hdc, hFont); void* hOldFont = ms_select_object(hdc, hFont);
ms_set_bk_mode(hdc, 1); // TRANSPARENT text background ms_set_bk_mode(hdc, 1); // TRANSPARENT text background
@@ -462,81 +461,86 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam)
ms_rectangle(hdc, -1, -1, 3000, 3000); ms_rectangle(hdc, -1, -1, 3000, 3000);
void* hBrushEdit = ms_create_solid_brush(0x008E563B); void* hBrushEdit = ms_create_solid_brush(0x008E563B);
void* hBrushNav = ms_create_solid_brush(0x00262F3B); void* hBrushNav = ms_create_solid_brush(0x00262F3B);
S4 start_x = 40, start_y = 60, spacing_x = 110, spacing_y = 35; S4 start_x = 40, start_y = 60, spacing_x = 110, spacing_y = 35;
S4 x = start_x, y = start_y; S4 x = start_x, y = start_y;
U4*r tape_ptr = C_(U4*r, tape_arena.start); U4*r tape_ptr = u4_r(tape_arena.start);
U8*r anno_ptr = C_(U8*r, anno_arena.start); U8*r anno_ptr = u8_r(anno_arena.start);
// Render Tokens // Render Tokens
for (U8 i = 0; i < tape_count; i++) { for (U8 i = 0; i < tape_count; i++)
if (x >= start_x + (TOKENS_PER_ROW * spacing_x)) { {
x = start_x; y += spacing_y; if (x >= start_x + (TOKENS_PER_ROW * spacing_x)) {
} x = start_x; y += spacing_y;
}
S4 render_y = y - scroll_y_offset; S4 render_y = y - scroll_y_offset;
if (i == cursor_idx && render_y >= 30 && render_y < 500) {
if (i == cursor_idx && render_y >= 30 && render_y < 500) { ms_select_object(hdc, editor_mode == MODE_EDIT ? hBrushEdit : hBrushNav);
ms_select_object(hdc, editor_mode == MODE_EDIT ? hBrushEdit : hBrushNav); ms_rectangle(hdc, x - 5, render_y - 2, x + 95, render_y + 22);
ms_rectangle(hdc, x - 5, render_y - 2, x + 95, render_y + 22); }
} if (render_y >= 30 && render_y < 500)
{
if (render_y >= 30 && render_y < 500) { U4 t = tape_ptr[i];
U4 t = tape_ptr[i]; U4 tag = unpack_tag(t);
U4 tag = unpack_tag(t); U4 val = unpack_val(t);
U4 val = unpack_val(t); U8 anno = anno_ptr[i];
U8 anno = anno_ptr[i]; if (tag == STag_Format && val == 0xA) {
ms_set_text_color(hdc, 0x00444444);
if (tag == STag_Format && val == 0xA) { ms_text_out_a(hdc, x, render_y, " \\n ", 6);
ms_set_text_color(hdc, 0x00444444); x = start_x;
ms_text_out_a(hdc, x, render_y, " \\n ", 6); y += spacing_y;
x = start_x; }
y += spacing_y; else
} else { {
U4 color = tag_colors[tag]; U4 color = tag_colors[tag];
const char* prefix = tag_prefixes[tag]; const char* prefix = tag_prefixes[tag];
ms_set_text_color(hdc, color); ms_set_text_color(hdc, color);
if (editor_mode == MODE_EDIT && i == cursor_idx) { if (editor_mode == MODE_EDIT && i == cursor_idx) {
// Better visibility in Edit Mode: White text on White-ish cursor // Better visibility in Edit Mode: White text on White-ish cursor
ms_set_text_color(hdc, 0x001E1E1E); ms_set_text_color(hdc, 0x001E1E1E);
} }
char val_str[9]; if (tag == STag_Data) { char val_str[9];
u64_to_hex(val, val_str, 6); if (tag == STag_Data) {
val_str[6] = '\0'; u64_to_hex(val, val_str, 6);
} else { val_str[6] = '\0';
// Extract annotation string }
char* a_str = (char*)&anno; else
if (a_str[0] == '\0') { {
// Fallback to 2-character ID // Extract annotation string
val_str[0] = (char)((val >> 8) & 0xFF); char* a_str = (char*) & anno;
val_str[1] = (char)(val & 0xFF); if (a_str[0] == '\0') {
val_str[2] = ' '; val_str[3] = ' '; val_str[4] = ' '; val_str[5] = ' '; // Fallback to 2-character ID
val_str[6] = '\0'; val_str[0] = (char)((val >> 8) & 0xFF);
} else { val_str[1] = (char)(val & 0xFF);
mem_copy(u8_(val_str), u8_(a_str), 8); val_str[2] = ' '; val_str[3] = ' '; val_str[4] = ' '; val_str[5] = ' ';
val_str[8] = '\0'; val_str[6] = '\0';
}
}
char out_buf[12];
out_buf[0] = prefix[0];
out_buf[1] = ' ';
mem_copy(u8_(out_buf + 2), u8_(val_str), 8);
out_buf[10] = '\0';
ms_text_out_a(hdc, x, render_y, out_buf, 10);
x += spacing_x;
} }
} else if (unpack_tag(tape_ptr[i]) == STag_Format && unpack_val(tape_ptr[i]) == 0xA) { else {
x = start_x; mem_copy(u8_(val_str), u8_(a_str), 8);
y += spacing_y; val_str[8] = '\0';
} else { }
x += spacing_x; }
char out_buf[12];
out_buf[0] = prefix[0];
out_buf[1] = ' ';
mem_copy(u8_(out_buf + 2), u8_(val_str), 8);
out_buf[10] = '\0';
ms_text_out_a(hdc, x, render_y, out_buf, 10);
x += spacing_x;
} }
}
else if (unpack_tag(tape_ptr[i]) == STag_Format && unpack_val(tape_ptr[i]) == 0xA) {
x = start_x;
y += spacing_y;
}
else {
x += spacing_x;
}
} }
// Draw a solid background behind the HUD to cover scrolling text // Draw a solid background behind the HUD to cover scrolling text
@@ -563,47 +567,45 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam)
// HUD: Display Current Token Meaning // HUD: Display Current Token Meaning
if (tape_count > 0 && cursor_idx < tape_count) { if (tape_count > 0 && cursor_idx < tape_count) {
U4 cur_tag = unpack_tag(tape_ptr[cursor_idx]); U4 cur_tag = unpack_tag(tape_ptr[cursor_idx]);
const char* tag_name = tag_names[cur_tag]; const char* tag_name = tag_names [cur_tag];
U4 cur_color = tag_colors[cur_tag]; U4 cur_color = tag_colors[cur_tag];
char semantics_str[64] = "Current Tag: ";
char semantics_str[64] = "Current Tag: "; U4 name_len = 0;
U4 name_len = 0; while (tag_name[name_len]) {
while (tag_name[name_len]) { semantics_str[13 + name_len] = tag_name[name_len];
semantics_str[13 + name_len] = tag_name[name_len]; name_len ++;
name_len++; }
} semantics_str[13 + name_len] = '\0';
semantics_str[13 + name_len] = '\0'; ms_set_text_color(hdc, cur_color);
ms_text_out_a(hdc, 40, 580, semantics_str, 13 + name_len);
ms_set_text_color(hdc, cur_color);
ms_text_out_a(hdc, 40, 580, semantics_str, 13 + name_len);
} }
ms_set_text_color(hdc, 0x00C8C8C8); ms_set_text_color(hdc, 0x00C8C8C8);
ms_text_out_a(hdc, 400, 520, "Global Memory (Contiguous Array):", 33); ms_text_out_a(hdc, 400, 520, "Global Memory (Contiguous Array):", 33);
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
char glob_str[32] = "[0]: 00000000"; char glob_str[32] = "[0]: 00000000";
glob_str[1] = '0' + i; glob_str[1] = '0' + i;
u64_to_hex(vm_globals[i], glob_str + 5, 8); u64_to_hex(vm_globals[i], glob_str + 5, 8);
ms_set_text_color(hdc, 0x00D6A454); // Soft blue ms_set_text_color(hdc, 0x00D6A454); // Soft blue
ms_text_out_a(hdc, 400, 550 + (i * 25), glob_str, 13); ms_text_out_a(hdc, 400, 550 + (i * 25), glob_str, 13);
} }
// Print Log // Print Log
ms_set_text_color(hdc, 0x00C8C8C8); ms_set_text_color(hdc, 0x00C8C8C8);
ms_text_out_a(hdc, 750, 520, "Print Log:", 10); ms_text_out_a(hdc, 750, 520, "Print Log:", 10);
for (int i=0; i<log_count && i<4; i++) { for (int i=0; i<log_count && i<4; i++) {
char log_str[32] = "00000000"; char log_str[32] = "00000000";
u64_to_hex(log_buffer[i], log_str, 8); u64_to_hex(log_buffer[i], log_str, 8);
ms_set_text_color(hdc, 0x0094BAA1); ms_set_text_color(hdc, 0x0094BAA1);
ms_text_out_a(hdc, 750, 550 + (i * 25), log_str, 8); ms_text_out_a(hdc, 750, 550 + (i * 25), log_str, 8);
} }
ms_select_object(hdc, hOldFont); ms_select_object(hdc, hOldFont);
ms_delete_object(hBgBrush); ms_delete_object(hBgBrush);
ms_delete_object(hBrushEdit); ms_delete_object(hBrushEdit);
ms_delete_object(hBrushNav); ms_delete_object(hBrushNav);
ms_delete_object(hHudBrush); ms_delete_object(hHudBrush);
ms_end_paint(hwnd, &ps); ms_end_paint(hwnd, & ps);
return 0; return 0;
} }
case MS_WM_DESTROY: { ms_post_quit_message(0); return 0; } case MS_WM_DESTROY: { ms_post_quit_message(0); return 0; }
@@ -616,11 +618,11 @@ int main(void) {
Slice tape_mem = slice_ut_(u8_(ms_virtual_alloc(nullptr, 64 * 1024, MS_MEM_COMMIT | MS_MEM_RESERVE, MS_PAGE_READWRITE)), 64 * 1024); Slice tape_mem = slice_ut_(u8_(ms_virtual_alloc(nullptr, 64 * 1024, MS_MEM_COMMIT | MS_MEM_RESERVE, MS_PAGE_READWRITE)), 64 * 1024);
Slice anno_mem = slice_ut_(u8_(ms_virtual_alloc(nullptr, 64 * 1024, MS_MEM_COMMIT | MS_MEM_RESERVE, MS_PAGE_READWRITE)), 64 * 1024); Slice anno_mem = slice_ut_(u8_(ms_virtual_alloc(nullptr, 64 * 1024, MS_MEM_COMMIT | MS_MEM_RESERVE, MS_PAGE_READWRITE)), 64 * 1024);
Slice code_mem = slice_ut_(u8_(ms_virtual_alloc(nullptr, 64 * 1024, MS_MEM_COMMIT | MS_MEM_RESERVE, MS_PAGE_EXECUTE_READWRITE)), 64 * 1024); Slice code_mem = slice_ut_(u8_(ms_virtual_alloc(nullptr, 64 * 1024, MS_MEM_COMMIT | MS_MEM_RESERVE, MS_PAGE_EXECUTE_READWRITE)), 64 * 1024);
if (!tape_mem.ptr || !anno_mem.ptr || !code_mem.ptr) ms_exit_process(1); if (! tape_mem.ptr || ! anno_mem.ptr || ! code_mem.ptr) ms_exit_process(1);
farena_init(&tape_arena, tape_mem); farena_init(& tape_arena, tape_mem);
farena_init(&anno_arena, anno_mem); farena_init(& anno_arena, anno_mem);
farena_init(&code_arena, code_mem); farena_init(& code_arena, code_mem);
// Bootstrap Robust Sample: Factorial State Machine // Bootstrap Robust Sample: Factorial State Machine
scatter(pack_token(STag_Comment, id2('I','N')), "INIT "); // .IN scatter(pack_token(STag_Comment, id2('I','N')), "INIT "); // .IN
@@ -677,11 +679,11 @@ int main(void) {
wc.hInstance = ms_get_stock_object(0); wc.hInstance = ms_get_stock_object(0);
wc.lpszClassName = "ColorForthWindow"; wc.lpszClassName = "ColorForthWindow";
wc.hbrBackground = ms_get_stock_object(4); wc.hbrBackground = ms_get_stock_object(4);
ms_register_class_a(&wc); ms_register_class_a(& wc);
void* hwnd = ms_create_window_ex_a(0, wc.lpszClassName, "Sourceless Global Memory Explorer", MS_WS_OVERLAPPEDWINDOW | MS_WS_VISIBLE, 100, 100, 1100, 750, nullptr, nullptr, wc.hInstance, nullptr); void* hwnd = ms_create_window_ex_a(0, wc.lpszClassName, "Sourceless Global Memory Explorer", MS_WS_OVERLAPPEDWINDOW | MS_WS_VISIBLE, 100, 100, 1100, 750, nullptr, nullptr, wc.hInstance, nullptr);
MS_MSG msg; MS_MSG msg;
while (ms_get_message_a(&msg, nullptr, 0, 0)) { ms_translate_message(&msg); ms_dispatch_message_a(&msg); } while (ms_get_message_a(& msg, nullptr, 0, 0)) { ms_translate_message(& msg); ms_dispatch_message_a(& msg); }
ms_exit_process(0); ms_exit_process(0);
return 0; return 0;
} }