progress
This commit is contained in:
148
attempt_1/main.c
148
attempt_1/main.c
@@ -12,7 +12,8 @@ typedef Enum_(U4, STag) {
|
|||||||
X(Call, "Call", 0x0033FF33, "~") /* GREEN */ \
|
X(Call, "Call", 0x0033FF33, "~") /* GREEN */ \
|
||||||
X(Data, "Data", 0x00FFFF33, "$") /* CYAN */ \
|
X(Data, "Data", 0x00FFFF33, "$") /* CYAN */ \
|
||||||
X(Imm, "Imm", 0x0033FFFF, "^") /* YELLOW */ \
|
X(Imm, "Imm", 0x0033FFFF, "^") /* YELLOW */ \
|
||||||
X(Comment, "Comment", 0x00888888, ".") /* DIM */
|
X(Comment, "Comment", 0x00888888, ".") /* DIM */ \
|
||||||
|
X(Format, "Format", 0x00444444, " ") /* INVISIBLE/FORMAT */
|
||||||
|
|
||||||
#define X(n, s, c, p) tmpl(STag, n),
|
#define X(n, s, c, p) tmpl(STag, n),
|
||||||
Tag_Entries()
|
Tag_Entries()
|
||||||
@@ -34,6 +35,13 @@ global const char* tag_prefixes[] = {
|
|||||||
#undef X
|
#undef X
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Helper array to fetch the full name of the STag
|
||||||
|
global const char* tag_names[] = {
|
||||||
|
#define X(n, s, c, p) s,
|
||||||
|
Tag_Entries()
|
||||||
|
#undef X
|
||||||
|
};
|
||||||
|
|
||||||
// Token Packing: 28 bits payload | 4 bits tag
|
// Token Packing: 28 bits payload | 4 bits tag
|
||||||
#define PACK_TOKEN(tag, val) (((U4)(tag) << 28) | ((U4)(val) & 0x0FFFFFFF))
|
#define PACK_TOKEN(tag, val) (((U4)(tag) << 28) | ((U4)(val) & 0x0FFFFFFF))
|
||||||
#define UNPACK_TAG(token) (((token) >> 28) & 0x0F)
|
#define UNPACK_TAG(token) (((token) >> 28) & 0x0F)
|
||||||
@@ -72,8 +80,11 @@ void* memcpy(void* dest, const void* src, U8 count) {
|
|||||||
#pragma clang optimize on
|
#pragma clang optimize on
|
||||||
|
|
||||||
IA_ void scatter(U4 token) {
|
IA_ void scatter(U4 token) {
|
||||||
U4*r ptr = farena_push_type(&tape_arena, U4);
|
if (tape_arena.used + sizeof(U4) <= tape_arena.capacity) {
|
||||||
if (ptr) { ptr[0] = token; }
|
U4*r ptr = C_(U4*r, tape_arena.start + tape_arena.used);
|
||||||
|
ptr[0] = token;
|
||||||
|
tape_arena.used += sizeof(U4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IA_ void u64_to_hex(U8 val, char* buf, S4 chars) {
|
IA_ void u64_to_hex(U8 val, char* buf, S4 chars) {
|
||||||
@@ -218,8 +229,33 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) {
|
|||||||
case MS_WM_KEYDOWN: {
|
case MS_WM_KEYDOWN: {
|
||||||
if (wparam == MS_VK_RIGHT && cursor_idx < tape_count - 1) cursor_idx++;
|
if (wparam == MS_VK_RIGHT && cursor_idx < tape_count - 1) cursor_idx++;
|
||||||
if (wparam == MS_VK_LEFT && cursor_idx > 0) cursor_idx--;
|
if (wparam == MS_VK_LEFT && cursor_idx > 0) cursor_idx--;
|
||||||
if (wparam == MS_VK_DOWN && cursor_idx + TOKENS_PER_ROW < tape_count) cursor_idx += TOKENS_PER_ROW;
|
|
||||||
if (wparam == MS_VK_UP && cursor_idx >= TOKENS_PER_ROW) cursor_idx -= TOKENS_PER_ROW;
|
if (wparam == MS_VK_UP) {
|
||||||
|
U8 line_start = cursor_idx;
|
||||||
|
while (line_start > 0 && UNPACK_TAG(tape_ptr[line_start - 1]) != tmpl(STag, Format)) line_start--;
|
||||||
|
if (line_start > 0) {
|
||||||
|
U8 col = cursor_idx - line_start;
|
||||||
|
U8 prev_line_start = line_start - 1;
|
||||||
|
while (prev_line_start > 0 && UNPACK_TAG(tape_ptr[prev_line_start - 1]) != tmpl(STag, Format)) 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (wparam == MS_VK_DOWN) {
|
||||||
|
U8 line_start = cursor_idx;
|
||||||
|
while (line_start > 0 && UNPACK_TAG(tape_ptr[line_start - 1]) != tmpl(STag, Format)) line_start--;
|
||||||
|
U8 col = cursor_idx - line_start;
|
||||||
|
|
||||||
|
U8 next_line_start = cursor_idx;
|
||||||
|
while (next_line_start < tape_count && UNPACK_TAG(tape_ptr[next_line_start]) != tmpl(STag, Format)) next_line_start++;
|
||||||
|
if (next_line_start < tape_count) {
|
||||||
|
next_line_start++; // Skip the newline
|
||||||
|
U8 next_line_end = next_line_start;
|
||||||
|
while (next_line_end < tape_count && UNPACK_TAG(tape_ptr[next_line_end]) != tmpl(STag, Format)) next_line_end++;
|
||||||
|
U8 next_line_len = next_line_end - next_line_start;
|
||||||
|
cursor_idx = next_line_start + (col < next_line_len ? col : next_line_len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (wparam == MS_VK_TAB) {
|
if (wparam == MS_VK_TAB) {
|
||||||
// Cycle Color Tag
|
// Cycle Color Tag
|
||||||
@@ -227,22 +263,27 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) {
|
|||||||
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) {
|
||||||
// Delete Token
|
// Delete Token and move cursor left
|
||||||
if (tape_count > 1) {
|
if (tape_count > 0) {
|
||||||
for (U8 i = cursor_idx; i < tape_count - 1; i++) {
|
for (U8 i = cursor_idx; i < tape_count - 1; i++) {
|
||||||
tape_ptr[i] = tape_ptr[i+1];
|
tape_ptr[i] = tape_ptr[i+1];
|
||||||
}
|
}
|
||||||
tape_arena.used -= sizeof(U4);
|
tape_arena.used -= sizeof(U4);
|
||||||
if (cursor_idx >= tape_count - 1 && cursor_idx > 0) cursor_idx--;
|
if (cursor_idx > 0) cursor_idx--;
|
||||||
}
|
}
|
||||||
} else if (wparam == MS_VK_SPACE || wparam == MS_VK_RETURN) {
|
} else if (wparam == MS_VK_SPACE || wparam == MS_VK_RETURN) {
|
||||||
// Insert New Token (Default to Comment)
|
// Insert New Token (Pre-append at cursor)
|
||||||
if (tape_arena.used + sizeof(U4) <= tape_arena.capacity) {
|
if (tape_arena.used + sizeof(U4) <= tape_arena.capacity) {
|
||||||
for (U8 i = tape_count; i > cursor_idx + 1; i--) {
|
for (U8 i = tape_count; i > cursor_idx; i--) {
|
||||||
tape_ptr[i] = tape_ptr[i-1];
|
tape_ptr[i] = tape_ptr[i-1];
|
||||||
}
|
}
|
||||||
cursor_idx++;
|
if (wparam == MS_VK_RETURN) {
|
||||||
tape_ptr[cursor_idx] = PACK_TOKEN(tmpl(STag, Comment), ID2(' ',' '));
|
tape_ptr[cursor_idx] = PACK_TOKEN(tmpl(STag, Format), 0xA); // Newline
|
||||||
|
cursor_idx++; // Move past newline
|
||||||
|
} else {
|
||||||
|
tape_ptr[cursor_idx] = PACK_TOKEN(tmpl(STag, Comment), ID2(' ',' '));
|
||||||
|
cursor_idx++; // Move past space
|
||||||
|
}
|
||||||
tape_arena.used += sizeof(U4);
|
tape_arena.used += sizeof(U4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -264,15 +305,15 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) {
|
|||||||
|
|
||||||
ms_set_bk_color(hdc, 0x001E1E1E);
|
ms_set_bk_color(hdc, 0x001E1E1E);
|
||||||
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;
|
||||||
|
|
||||||
U4*r tape_ptr = C_(U4*r, tape_arena.start);
|
U4*r tape_ptr = C_(U4*r, tape_arena.start);
|
||||||
|
|
||||||
// Render Tokens
|
// Render Tokens
|
||||||
for (U8 i = 0; i < tape_count; i++) {
|
for (U8 i = 0; i < tape_count; i++) {
|
||||||
S4 col = (S4)(i % TOKENS_PER_ROW);
|
if (x >= start_x + (TOKENS_PER_ROW * spacing_x)) {
|
||||||
S4 row = (S4)(i / TOKENS_PER_ROW);
|
x = start_x; y += spacing_y;
|
||||||
S4 x = start_x + (col * spacing_x);
|
}
|
||||||
S4 y = start_y + (row * spacing_y);
|
|
||||||
|
|
||||||
if (i == cursor_idx) {
|
if (i == cursor_idx) {
|
||||||
void* hBrush = ms_get_stock_object(2); // GRAY_BRUSH
|
void* hBrush = ms_get_stock_object(2); // GRAY_BRUSH
|
||||||
@@ -284,38 +325,61 @@ S8 win_proc(void* hwnd, U4 msg, U8 wparam, S8 lparam) {
|
|||||||
U4 tag = UNPACK_TAG(t);
|
U4 tag = UNPACK_TAG(t);
|
||||||
U4 val = UNPACK_VAL(t);
|
U4 val = UNPACK_VAL(t);
|
||||||
|
|
||||||
U4 color = tag_colors[tag];
|
if (tag == tmpl(STag, Format) && val == 0xA) {
|
||||||
const char* prefix = tag_prefixes[tag];
|
ms_set_text_color(hdc, 0x00444444);
|
||||||
|
ms_text_out_a(hdc, x, y, " \\n ", 6);
|
||||||
ms_set_text_color(hdc, color);
|
x = start_x;
|
||||||
|
y += spacing_y;
|
||||||
char val_str[9];
|
|
||||||
if (tag == tmpl(STag, Data)) {
|
|
||||||
u64_to_hex(val, val_str, 6);
|
|
||||||
val_str[6] = '\0';
|
|
||||||
} else {
|
} else {
|
||||||
// Decode 2-character dictionary ID
|
U4 color = tag_colors[tag];
|
||||||
val_str[0] = (char)((val >> 8) & 0xFF);
|
const char* prefix = tag_prefixes[tag];
|
||||||
val_str[1] = (char)(val & 0xFF);
|
|
||||||
val_str[2] = ' ';
|
|
||||||
val_str[3] = ' ';
|
|
||||||
val_str[4] = ' ';
|
|
||||||
val_str[5] = ' ';
|
|
||||||
val_str[6] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
char out_buf[10];
|
ms_set_text_color(hdc, color);
|
||||||
out_buf[0] = prefix[0];
|
|
||||||
out_buf[1] = ' ';
|
char val_str[9];
|
||||||
mem_copy(u8_(out_buf + 2), u8_(val_str), 6);
|
if (tag == tmpl(STag, Data)) {
|
||||||
out_buf[8] = '\0';
|
u64_to_hex(val, val_str, 6);
|
||||||
|
val_str[6] = '\0';
|
||||||
ms_text_out_a(hdc, x, y, out_buf, 8);
|
} else {
|
||||||
|
// Decode 2-character dictionary ID
|
||||||
|
val_str[0] = (char)((val >> 8) & 0xFF);
|
||||||
|
val_str[1] = (char)(val & 0xFF);
|
||||||
|
val_str[2] = ' '; val_str[3] = ' '; val_str[4] = ' '; val_str[5] = ' ';
|
||||||
|
val_str[6] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
char out_buf[10];
|
||||||
|
out_buf[0] = prefix[0];
|
||||||
|
out_buf[1] = ' ';
|
||||||
|
mem_copy(u8_(out_buf + 2), u8_(val_str), 6);
|
||||||
|
out_buf[8] = '\0';
|
||||||
|
|
||||||
|
ms_text_out_a(hdc, x, y, out_buf, 8);
|
||||||
|
x += spacing_x;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ms_set_text_color(hdc, 0x00AAAAAA);
|
ms_set_text_color(hdc, 0x00AAAAAA);
|
||||||
ms_text_out_a(hdc, 40, 20, "x86-64 Machine Code Emitter | 2-Reg Stack + Global Tape | Factorial", 68);
|
ms_text_out_a(hdc, 40, 20, "x86-64 Machine Code Emitter | 2-Reg Stack + Global Tape | Factorial", 68);
|
||||||
|
|
||||||
|
// HUD: Display Current Token Meaning
|
||||||
|
if (tape_count > 0 && cursor_idx < tape_count) {
|
||||||
|
U4 cur_tag = UNPACK_TAG(tape_ptr[cursor_idx]);
|
||||||
|
const char* tag_name = tag_names[cur_tag];
|
||||||
|
U4 cur_color = tag_colors[cur_tag];
|
||||||
|
|
||||||
|
char semantics_str[64] = "Current Tag: ";
|
||||||
|
U4 name_len = 0;
|
||||||
|
while (tag_name[name_len]) {
|
||||||
|
semantics_str[13 + name_len] = tag_name[name_len];
|
||||||
|
name_len++;
|
||||||
|
}
|
||||||
|
semantics_str[13 + name_len] = '\0';
|
||||||
|
|
||||||
|
ms_set_text_color(hdc, cur_color);
|
||||||
|
ms_text_out_a(hdc, 400, 20, semantics_str, 13 + name_len);
|
||||||
|
}
|
||||||
|
|
||||||
// Render VM State
|
// Render VM State
|
||||||
ms_set_text_color(hdc, 0x00FFFFFF);
|
ms_set_text_color(hdc, 0x00FFFFFF);
|
||||||
char jit_str[32] = "JIT Size: 0x000 bytes";
|
char jit_str[32] = "JIT Size: 0x000 bytes";
|
||||||
@@ -366,7 +430,7 @@ int main(void) {
|
|||||||
scatter(PACK_TOKEN(tmpl(STag, Data), 1)); // $1 (Addr)
|
scatter(PACK_TOKEN(tmpl(STag, Data), 1)); // $1 (Addr)
|
||||||
scatter(PACK_TOKEN(tmpl(STag, Imm), ID2('!',' '))); // ^!
|
scatter(PACK_TOKEN(tmpl(STag, Imm), ID2('!',' '))); // ^!
|
||||||
|
|
||||||
scatter(PACK_TOKEN(tmpl(STag, Comment), ID2('-','-'))); // .--
|
scatter(PACK_TOKEN(tmpl(STag, Format), 0xA)); // Newline
|
||||||
|
|
||||||
scatter(PACK_TOKEN(tmpl(STag, Imm), ID2('F','S'))); // ^FS
|
scatter(PACK_TOKEN(tmpl(STag, Imm), ID2('F','S'))); // ^FS
|
||||||
scatter(PACK_TOKEN(tmpl(STag, Imm), ID2('F','S')));
|
scatter(PACK_TOKEN(tmpl(STag, Imm), ID2('F','S')));
|
||||||
|
|||||||
Reference in New Issue
Block a user