This commit is contained in:
Luna 2025-03-23 20:42:31 +01:00
parent 0d16b96fc5
commit b61c1f9ef2
7 changed files with 54 additions and 42 deletions

View file

@ -7,20 +7,6 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#define MAX_ALLOCS 256
struct Alloc {
void *start;
uint size;
struct Alloc *next;
};
struct Mem {
void *mem;
size_t mem_size;
uint count;
struct Alloc *alloc;
struct Alloc allocs[MAX_ALLOCS];
};
struct Mem *make_mem(size_t size) { struct Mem *make_mem(size_t size) {
struct Mem *mem = malloc(sizeof(struct Mem) + size); struct Mem *mem = malloc(sizeof(struct Mem) + size);
mem->mem_size = size; mem->mem_size = size;

View file

@ -228,10 +228,10 @@ enum PawVal *make_pawval_point(struct Mem *mem, paw_p n) {
return val; return val;
} }
enum PawVal *make_pawval_spoint(struct Mem *mem, paw_sp n) { enum PawVal *make_pawval_spoint(struct Mem *mem, int n) {
enum PawVal *val; enum PawVal *val;
val_make(val, paw_sp, PAW_VAL_SPOINT); val_make(val, paw_p, PAW_VAL_SPOINT);
paw_sp *i = val_get_data(val); int *i = val_get_data(val);
*i = n; *i = n;
return val; return val;
} }

View file

@ -176,6 +176,7 @@ enum PawVal *make_pawval_float(struct Mem *mem, float n);
enum PawVal *make_pawval_int(struct Mem *mem, int n); enum PawVal *make_pawval_int(struct Mem *mem, int n);
enum PawVal *make_pawval_bool(struct Mem *mem, bool n); enum PawVal *make_pawval_bool(struct Mem *mem, bool n);
enum PawVal *make_pawval_point(struct Mem *mem, paw_p n); enum PawVal *make_pawval_point(struct Mem *mem, paw_p n);
enum PawVal *make_pawval_spoint(struct Mem *mem, int n);
enum PawVal *make_pawval_expr(struct Mem *mem, enum PawVal *lhs, enum PawVal *make_pawval_expr(struct Mem *mem, enum PawVal *lhs,
enum PawVal *rhs, uint32_t op); enum PawVal *rhs, uint32_t op);
void free_pawval(struct Mem *mem, enum PawVal *val); void free_pawval(struct Mem *mem, enum PawVal *val);
@ -215,6 +216,7 @@ void ast_exec(struct PawAST *ast);
uint32_t paw_get_ops_size(); uint32_t paw_get_ops_size();
const char *paw_get_op_sym(uint32_t index); const char *paw_get_op_sym(uint32_t index);
int paw_get_op_importance(uint32_t index);
uint32_t paw_get_op_index(char *sym); uint32_t paw_get_op_index(char *sym);
void *paw_get_op(uint32_t); void *paw_get_op(uint32_t);
uint32_t paw_get_cfunc_index(char *name); uint32_t paw_get_cfunc_index(char *name);

View file

@ -10,6 +10,7 @@ struct FuncDef {
struct OpDef { struct OpDef {
struct PawStackVal (*op)(struct PawStackVal, struct PawStackVal); struct PawStackVal (*op)(struct PawStackVal, struct PawStackVal);
const char *sym; const char *sym;
int importance;
// enum ArgType *args; // enum ArgType *args;
}; };
@ -19,12 +20,13 @@ struct FuncDef PAW_CFUNCS[] = {
// important to unimportant!!! // important to unimportant!!!
struct OpDef PAW_OPS[] = { struct OpDef PAW_OPS[] = {
{op_equals, "=="}, {op_unequals, "!="}, {NULL, "*"}, {op_equals, "==", 0}, {op_unequals, "!=", 0}, {NULL, "*", 2},
{NULL, "/"}, {op_add, "+"}, {NULL, "-"}, {NULL, "/", 2}, {op_add, "+", 1}, {NULL, "-", 1},
}; };
uint32_t paw_get_ops_size() { return sizeof(PAW_OPS); } uint32_t paw_get_ops_size() { return sizeof(PAW_OPS); }
const char *paw_get_op_sym(uint32_t index) { return PAW_OPS[index].sym; } const char *paw_get_op_sym(uint32_t index) { return PAW_OPS[index].sym; }
int paw_get_op_importance(uint32_t index) { return PAW_OPS[index].importance; }
uint32_t paw_get_op_index(char *sym) { uint32_t paw_get_op_index(char *sym) {
for (int i = 0; i < sizeof(PAW_OPS) / sizeof(PAW_OPS[0]); i++) { for (int i = 0; i < sizeof(PAW_OPS) / sizeof(PAW_OPS[0]); i++) {

View file

@ -41,10 +41,12 @@ enum PawVal *parse_val(int stack_offset, struct StringTable *vars,
// figure out the most important op // figure out the most important op
char *opstart = start; char *opstart = start;
int op_rating = INT_MAX; int op_rating = INT_MAX;
int op_index = -1;
char *op = NULL; char *op = NULL;
// as * before + in functable it will choose to split on + first, meaning // as * before + in functable it will choose to split on + first, meaning
// a+b*c => (a)+(b*c) // a+b*c => (a)+(b*c)
// also do a + b - c => (a + b) - c
while (opstart < end) { while (opstart < end) {
if (*opstart == '(') { if (*opstart == '(') {
opstart = skip_brackets(opstart, '(', ')'); opstart = skip_brackets(opstart, '(', ')');
@ -52,40 +54,48 @@ enum PawVal *parse_val(int stack_offset, struct StringTable *vars,
} }
for (int i = 0; i < paw_get_ops_size(); i++) { for (int i = 0; i < paw_get_ops_size(); i++) {
if (start_matches(opstart, (char *)paw_get_op_sym(i))) { if (start_matches(opstart, (char *)paw_get_op_sym(i))) {
if (i >= op_rating) { if (paw_get_op_importance(i) < op_rating) {
continue; continue;
} }
op_rating = i; op_rating = paw_get_op_importance(i);
op = opstart; op = opstart;
op_index = i;
} }
} }
opstart++; opstart++;
} }
if (op == 0) { if (op != NULL) {
} enum PawVal *lhs =
parse_val(stack_offset, vars, tmp, pem, start, opstart - 1);
enum PawVal *lhs; enum PawVal *rhs =
// expected: var, brackets or function parse_val(stack_offset, vars, tmp, pem,
if (start_matches(start, "(")) { opstart + strlen(paw_get_op_sym(op_index)), end);
char *in_end = skip_brackets(start, '(', ')'); return make_pawval_expr(pem, lhs, rhs, op_index);
lhs = parse_val(stack_offset, vars, tmp, pem, start + 1, in_end);
start = in_end;
} else { } else {
char *tmp = start; enum PawVal *res;
while (is_char(*tmp)) { // expected: var, brackets or function
tmp++; if (start_matches(start, "(")) {
} char *in_end = skip_brackets(start, '(', ')');
tmp = trim_start(tmp); res = parse_val(stack_offset, vars, tmp, pem, start + 1, in_end);
if (*tmp == '(') { start = in_end;
// TODO, call
} else { } else {
// TODO, var char *tmpc = start;
while (tmpc < end && is_char(*tmpc)) {
tmpc++;
}
tmpc = trim_start(tmpc);
if (*tmpc == '(') {
// TODO, call
} else {
char *name = mem_strdup_reg(start, end, tmp);
return make_pawval_spoint(pem, stack_offset + st_get_index(vars, name));
}
}
start = trim_start(start);
if (start == end) {
return res;
} }
}
start = trim_start(start);
if (start == end) {
return lhs;
} }
} }

View file

@ -67,6 +67,15 @@ void *st_get(struct StringTable *st, char *key) {
} }
return NULL; return NULL;
} }
int st_get_index(struct StringTable *st, char *key) {
for (int i = 0; i < st->keys.count; i++) {
if (strcmp(st->keys.items[i].key, key) == 0) {
return i;
}
}
return -1;
}
void st_remove(struct StringTable *st, char *key) { void st_remove(struct StringTable *st, char *key) {
for (int i = 0; i < st->keys.count; i++) { for (int i = 0; i < st->keys.count; i++) {
if (strcmp(st->keys.items[i].key, key) == 0) { if (strcmp(st->keys.items[i].key, key) == 0) {

View file

@ -3,6 +3,8 @@
#define UINT32_NULL 42424242 #define UINT32_NULL 42424242
#include "allocator.h"
float randf(); float randf();
#define SWAP($a, $b) \ #define SWAP($a, $b) \
@ -19,6 +21,7 @@ struct StringTable *st_dup(struct StringTable *st);
void st_insert(struct StringTable *st, char *key, void *value); void st_insert(struct StringTable *st, char *key, void *value);
bool st_has_key(struct StringTable *st, char *key); bool st_has_key(struct StringTable *st, char *key);
void *st_get(struct StringTable *st, char *key); void *st_get(struct StringTable *st, char *key);
int st_get_index(struct StringTable *st, char *key);
bool st_has_value(struct StringTable *st, void *value); bool st_has_value(struct StringTable *st, void *value);
void st_remove(struct StringTable *st, char *key); void st_remove(struct StringTable *st, char *key);
void st_unmake(struct StringTable *st); void st_unmake(struct StringTable *st);