:333
This commit is contained in:
parent
0d16b96fc5
commit
b61c1f9ef2
7 changed files with 54 additions and 42 deletions
|
@ -7,20 +7,6 @@
|
|||
#include <string.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 *mem = malloc(sizeof(struct Mem) + size);
|
||||
mem->mem_size = size;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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++) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue