From 3ee4442b3d48fc604bfaa1d6dd72c94f181e3ff9 Mon Sep 17 00:00:00 2001 From: zongor Date: Wed, 11 Feb 2026 16:57:19 -0800 Subject: [PATCH] general cleanup --- tools/compiler.c | 42 ++++++++++++++++++++++++------------------ tools/compiler.h | 3 ++- vm/libc.h | 18 ++++++++++-------- vm/vm.c | 6 +----- vm/vm.h | 9 +++++++-- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/tools/compiler.c b/tools/compiler.c index 47367b1..58fc1bc 100644 --- a/tools/compiler.c +++ b/tools/compiler.c @@ -4,23 +4,6 @@ #include #include -/* - -TODO: - -For expressions we should use the shunting yard algorithem. -This will be useful because it will make it trivial to track types. -If the type is a literal, we just read it, if it is a variable we read the variable type from the info. - -During the first pass we count the number of variables. We assign a local to each variable. - -When parsing a expression, we assign any function call or literal to a temp variable slot, -(3 maybe?) First one goes in 0, then 2nd in 1, then do operation. Store the operation in 0. -If it is a function call, use 1 to load and push the args, then use 1 for the return variable. -If both happen to be function calls use 0, 1, 2. Where 1 & 2 are the function calls, and the result operation gets stored in 0. - -*/ - #define DEBUG_COMPILER void emit_byte(u8 byte) { @@ -519,7 +502,30 @@ void define_branch(ScopeTable *st) { /** * Define a loop */ -void define_loop(ScopeTable *st) {} +void define_loop(ScopeTable *st) { + +} + +/** + * Parses an expression using the shunting yard algorithem. + * This will be useful because it will make it trivial to track types. + * If the type is a literal, we just read it, if it is a variable we read the variable type from the info. + * + * During the first pass we count the number of variables. We assign a local to each variable. + * + * When parsing a expression, we assign any function call or literal to a temp variable slot, + * (2 maybe?) First one goes in 0, then 2nd in 1, then do operation. Store the operation in 0. + * If it is a function call, use 1 to load and push the args, then use 1 for the return variable. + * Then do the operation on 1 and 0 and store in 0. + */ +Symbol value_stack[MAX_SYMBOLS]; +u8 vsp; +Symbol operator_stack[MAX_SYMBOLS]; +u8 osp; + +void parse_expression(ScopeTable *st) { + +} /** * Build the symbol table and calculate the types/size/offsets of all values. diff --git a/tools/compiler.h b/tools/compiler.h index 3fa7d04..b9e354f 100644 --- a/tools/compiler.h +++ b/tools/compiler.h @@ -69,8 +69,9 @@ struct symbol_s { u32 size; // size of symbol }; +#define MAX_SYMBOLS 256 struct symbol_tab_s { - Symbol symbols[256]; + Symbol symbols[MAX_SYMBOLS]; u8 count; i32 parent; }; diff --git a/vm/libc.h b/vm/libc.h index 2361cf2..224c3f0 100644 --- a/vm/libc.h +++ b/vm/libc.h @@ -18,14 +18,6 @@ typedef float f32; #define nil NULL -#define USED(x) ((void)(x)) - -#define AS_INT(v) ((i32)(v)) -#define AS_NAT(v) ((u32)(v)) -#define AS_REAL(v) ((i32)(v)) -#define FLOAT_TO_REAL(v) (((i32)(v)) * 65536.0f) -#define REAL_TO_FLOAT(v) (((f32)(v)) / 65536.0f) - #define I8_MIN -128 #define I8_MAX 127 #define U8_MAX 255 @@ -38,6 +30,16 @@ typedef float f32; #define I32_MAX 2147483647 #define U32_MAX 4294967295 +#define FIXED_CONST 65536.0f + +#define AS_INT(v) ((i32)(v)) +#define AS_NAT(v) ((u32)(v)) +#define AS_REAL(v) ((i32)(v)) +#define FLOAT_TO_REAL(v) (((i32)(v)) * FIXED_CONST) +#define REAL_TO_FLOAT(v) (((f32)(v)) / FIXED_CONST) + +#define USED(x) ((void)(x)) + void mcpy(void *dest, void *src, u32 n); i32 scpy(char* to, const char *from, u32 length); bool seq(const char *s1, const char *s2); diff --git a/vm/vm.c b/vm/vm.c index 56bee24..b6f46da 100644 --- a/vm/vm.c +++ b/vm/vm.c @@ -12,8 +12,6 @@ u32 *code; /* code */ u8 *mem; /* memory */ #define MAX_LEN_INT32 11 -#define MAX_INT32 2147483647 -#define MIN_INT32 -2147483648 const char radix_set[11] = "0123456789"; u32 str_alloc(char *str, u32 length) { @@ -102,10 +100,8 @@ bool step_vm() { } case OP_SYSCALL: { DECODE_A(instruction) - u32 id = dest; /* syscall id */ - u32 size = src1; /* size of heap at that pointer */ u32 rd = locals[src2]; /* the pointer */ - status = syscall(id, size, rd); + status = syscall(dest, src1, rd); return true; } case OP_PARG: { diff --git a/vm/vm.h b/vm/vm.h index da0275b..d7d797e 100644 --- a/vm/vm.h +++ b/vm/vm.h @@ -137,7 +137,9 @@ extern u8 *mem; /* memory */ #define READ_U16(addr) \ (((u16)mem[(addr) + 1] << 8) | ((u16)mem[(addr)])) -#define READ_U32(addr) (globals[(addr / 4)]) +#define READ_U32(addr) (((u32)mem[(addr) + 3] << 24) | \ + ((u32)mem[(addr) + 2] << 16) | \ + ((u32)mem[(addr) + 1] << 8) | ((u32)mem[(addr)])) #define WRITE_U8(addr, value) \ do { \ @@ -152,7 +154,10 @@ extern u8 *mem; /* memory */ #define WRITE_U32(addr, value) \ do { \ - globals[(addr / 4)] = value; \ + mem[(addr)] = (value) & 0xFF; \ + mem[(addr) + 1] = ((value) >> 8) & 0xFF; \ + mem[(addr) + 2] = ((value) >> 16) & 0xFF; \ + mem[(addr) + 3] = ((value) >> 24) & 0xFF; \ } while (0) #define MATH_OP(type, op) \