general cleanup

This commit is contained in:
zongor 2026-02-11 16:57:19 -08:00
parent f4c5577153
commit 3ee4442b3d
5 changed files with 44 additions and 34 deletions

View File

@ -4,23 +4,6 @@
#include <stdio.h>
#include <stdlib.h>
/*
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.

View File

@ -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;
};

View File

@ -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);

View File

@ -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: {

View File

@ -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) \