summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-05-04 11:46:56 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-05-04 11:46:56 -0400
commite0ce255661e892086d585f8148d30b01dc3bbc95 (patch)
tree17b45eaca55b4a485d71321ef7a54c70a7d71bb5
parentcd2199b424fa0c7f724e2a7b771ed3e0f9b90e07 (diff)
fixed f(f(x)) bug but it's not very nice
-rw-r--r--eval.c10
-rw-r--r--types.h2
2 files changed, 8 insertions, 4 deletions
diff --git a/eval.c b/eval.c
index 4b7dc40..e14e333 100644
--- a/eval.c
+++ b/eval.c
@@ -1459,12 +1459,13 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) {
/* set parameter values */
Declaration *params = fn->params;
Expression *arg = e->call.arg_exprs;
+ /* @OPTIM: figure out how much memory parameters use, then allocate that much space (possibly with alloca)? */
+ Value **to_free = NULL;
arr_foreach(params, Declaration, p) {
int idx = 0;
- Value *pval = decl_add_val(p);
- --arr_hdr(p->val_stack)->len;
bool multiple_idents = arr_len(p->idents) > 1;
bool is_tuple = p->type.kind == TYPE_TUPLE;
+ Value *pval = err_malloc(sizeof *pval);
if (type_is_builtin(&p->type, BUILTIN_VARARGS)) {
Expression *args_end = e->call.arg_exprs + nargs;
/* set varargs */
@@ -1487,7 +1488,8 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) {
++idx;
}
}
- ++arr_hdr(p->val_stack)->len;
+ arr_add(p->val_stack, pval);
+ arr_add(to_free, pval);
}
arr_foreach(fn->ret_decls, Declaration, d) {
@@ -1550,6 +1552,8 @@ static Status eval_expr(Evaluator *ev, Expression *e, Value *v) {
}
arr_foreach(fn->params, Declaration, p)
decl_remove_val(p);
+ arr_foreach(to_free, ValuePtr, p)
+ free(*p);
arr_foreach(fn->ret_decls, Declaration, d)
decl_remove_val(d);
} break;
diff --git a/types.h b/types.h
index 448727a..45b5752 100644
--- a/types.h
+++ b/types.h
@@ -176,6 +176,7 @@ typedef union Value {
struct Namespace *nms;
struct VarArg *varargs; /* dynamic array */
} Value;
+typedef Value *ValuePtr;
typedef struct VarArg {
struct Type *type;
@@ -880,7 +881,6 @@ typedef struct Declaration {
/* for eval, for non-constant local decls: */
/* the pointers to values need to be fixed, which is why this isn't just Value *. */
- /* @OPTIM: some block array of values somewhere which we can just use a pointer to, which is freed when the block is exited? */
Value **val_stack;
} Declaration;
typedef Declaration *DeclarationPtr;