From b61c1f9ef26ae690d791a0bef02638335f2d54a3 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 23 Mar 2025 20:42:31 +0100 Subject: [PATCH] :333 --- src/allocator.c | 14 ----------- src/lang/ast.c | 6 ++--- src/lang/ast.h | 2 ++ src/lang/functable.c | 6 +++-- src/lang/parser.c | 56 ++++++++++++++++++++++++++------------------ src/util.c | 9 +++++++ src/util.h | 3 +++ 7 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/allocator.c b/src/allocator.c index 6f911f1..bbc8676 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -7,20 +7,6 @@ #include #include -#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 *mem = malloc(sizeof(struct Mem) + size); mem->mem_size = size; diff --git a/src/lang/ast.c b/src/lang/ast.c index 90bcad7..71ca961 100644 --- a/src/lang/ast.c +++ b/src/lang/ast.c @@ -228,10 +228,10 @@ enum PawVal *make_pawval_point(struct Mem *mem, paw_p n) { 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; - val_make(val, paw_sp, PAW_VAL_SPOINT); - paw_sp *i = val_get_data(val); + val_make(val, paw_p, PAW_VAL_SPOINT); + int *i = val_get_data(val); *i = n; return val; } diff --git a/src/lang/ast.h b/src/lang/ast.h index d4c8ff4..69f1abc 100644 --- a/src/lang/ast.h +++ b/src/lang/ast.h @@ -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_bool(struct Mem *mem, bool 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 *rhs, uint32_t op); 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(); 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); void *paw_get_op(uint32_t); uint32_t paw_get_cfunc_index(char *name); diff --git a/src/lang/functable.c b/src/lang/functable.c index 8f0ca5d..82873c9 100644 --- a/src/lang/functable.c +++ b/src/lang/functable.c @@ -10,6 +10,7 @@ struct FuncDef { struct OpDef { struct PawStackVal (*op)(struct PawStackVal, struct PawStackVal); const char *sym; + int importance; // enum ArgType *args; }; @@ -19,12 +20,13 @@ struct FuncDef PAW_CFUNCS[] = { // important to unimportant!!! struct OpDef PAW_OPS[] = { - {op_equals, "=="}, {op_unequals, "!="}, {NULL, "*"}, - {NULL, "/"}, {op_add, "+"}, {NULL, "-"}, + {op_equals, "==", 0}, {op_unequals, "!=", 0}, {NULL, "*", 2}, + {NULL, "/", 2}, {op_add, "+", 1}, {NULL, "-", 1}, }; uint32_t paw_get_ops_size() { return sizeof(PAW_OPS); } 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) { for (int i = 0; i < sizeof(PAW_OPS) / sizeof(PAW_OPS[0]); i++) { diff --git a/src/lang/parser.c b/src/lang/parser.c index e7a2e2c..992f251 100644 --- a/src/lang/parser.c +++ b/src/lang/parser.c @@ -41,10 +41,12 @@ enum PawVal *parse_val(int stack_offset, struct StringTable *vars, // figure out the most important op char *opstart = start; int op_rating = INT_MAX; + int op_index = -1; char *op = NULL; // as * before + in functable it will choose to split on + first, meaning // a+b*c => (a)+(b*c) + // also do a + b - c => (a + b) - c while (opstart < end) { if (*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++) { if (start_matches(opstart, (char *)paw_get_op_sym(i))) { - if (i >= op_rating) { + if (paw_get_op_importance(i) < op_rating) { continue; } - op_rating = i; + op_rating = paw_get_op_importance(i); op = opstart; + op_index = i; } } opstart++; } - if (op == 0) { - } - - enum PawVal *lhs; - // expected: var, brackets or function - if (start_matches(start, "(")) { - char *in_end = skip_brackets(start, '(', ')'); - lhs = parse_val(stack_offset, vars, tmp, pem, start + 1, in_end); - start = in_end; + if (op != NULL) { + enum PawVal *lhs = + parse_val(stack_offset, vars, tmp, pem, start, opstart - 1); + enum PawVal *rhs = + parse_val(stack_offset, vars, tmp, pem, + opstart + strlen(paw_get_op_sym(op_index)), end); + return make_pawval_expr(pem, lhs, rhs, op_index); } else { - char *tmp = start; - while (is_char(*tmp)) { - tmp++; - } - tmp = trim_start(tmp); - if (*tmp == '(') { - // TODO, call + enum PawVal *res; + // expected: var, brackets or function + if (start_matches(start, "(")) { + char *in_end = skip_brackets(start, '(', ')'); + res = parse_val(stack_offset, vars, tmp, pem, start + 1, in_end); + start = in_end; } 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; } } diff --git a/src/util.c b/src/util.c index 55fd8fb..b141d11 100644 --- a/src/util.c +++ b/src/util.c @@ -67,6 +67,15 @@ void *st_get(struct StringTable *st, char *key) { } 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) { for (int i = 0; i < st->keys.count; i++) { if (strcmp(st->keys.items[i].key, key) == 0) { diff --git a/src/util.h b/src/util.h index def4017..0d9df66 100644 --- a/src/util.h +++ b/src/util.h @@ -3,6 +3,8 @@ #define UINT32_NULL 42424242 +#include "allocator.h" + float randf(); #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); bool st_has_key(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); void st_remove(struct StringTable *st, char *key); void st_unmake(struct StringTable *st);