Files
forth_bootslop/attempt_1/main.c
2026-02-19 16:50:22 -05:00

72 lines
2.5 KiB
C

#include "duffle.amd64.win32.h"
#include <stdio.h>
// Semantic Tags (The "Colors" of ColorForth)
#define TAG_DEFINE 0x0 // RED: New word definition
#define TAG_CALL 0x1 // GREEN: Call/Compile word
#define TAG_DATA 0x2 // CYAN: Variable or Literal Address
#define TAG_IMM 0x3 // YELLOW: Immediate value/Execute
#define TAG_COMMENT 0x4 // WHITE: Ignored by compiler
// Token Packing: 28 bits payload | 4 bits tag
#define PACK_TOKEN(tag, val) (((U4)(tag) << 28) | ((U4)(val) & 0x0FFFFFFF))
#define UNPACK_TAG(token) (((token) >> 28) & 0x0F)
#define UNPACK_VAL(token) ((token) & 0x0FFFFFFF)
// The Tape Drive (Memory Arena)
global U4* tape;
global U8 tape_pos = 0;
internal void scatter(U4 token) {
tape[tape_pos++] = token;
}
int main() {
// 1. Initialize the Sourceless Memory Arena (Win32 VirtualAlloc)
// We'll allocate 64KB for our initial "tape drive".
tape = (U4*)VirtualAlloc(NULL, 64 * 1024, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!tape) return 1;
// 2. "Bootstrap" the content (Preemptive Scatter)
// Here we manually define a word: :SQUARE ( x -- x*x )
// In a real editor, these are written directly by the UI to memory.
scatter(PACK_TOKEN(TAG_DEFINE, 0x51415245)); // ":SQUARE" (Encoded name or index)
scatter(PACK_TOKEN(TAG_CALL, 0x00000001)); // DUP
scatter(PACK_TOKEN(TAG_CALL, 0x00000002)); // *
scatter(PACK_TOKEN(TAG_CALL, 0x00000003)); // ; (Return)
// Now a comment
scatter(PACK_TOKEN(TAG_COMMENT, 0x4E4F5445)); // "NOTE"
// Now an execution block: 5 SQUARE
scatter(PACK_TOKEN(TAG_DATA, 5)); // The number 5
scatter(PACK_TOKEN(TAG_IMM, 0x51415245)); // EXECUTE SQUARE
// 3. The Visual "Interpreter" Loop
// This simulates the editor/decompiler reading the raw memory.
printf("--- Sourceless Tape Drive Visualization ---
");
for (U8 i = 0; i < tape_pos; i++) {
U4 t = tape[i];
U4 tag = UNPACK_TAG(t);
U4 val = UNPACK_VAL(t);
switch (tag) {
case TAG_DEFINE: printf("[RED] Define : 0x%07X
", val); break;
case TAG_CALL: printf("[GREEN] Call : 0x%07X
", val); break;
case TAG_DATA: printf("[CYAN] Data : %u
", val); break;
case TAG_IMM: printf("[YELLOW] Execute: 0x%07X
", val); break;
case TAG_COMMENT: printf("[WHITE] Note : 0x%07X
", val); break;
default: printf("[ERROR] Unknown: 0x%08X
", t); break;
}
}
return 0;
}