Fix memory corruption in shash, add lambda
This commit is contained in:
+22
-1
@@ -174,7 +174,28 @@ void emit(struct function *fn, struct context *ctx, struct node *expr) {
|
||||
}
|
||||
|
||||
// Check for macros/builtin operators
|
||||
if (!strcmp(n0->n_ident, "begin")) {
|
||||
if (!strcmp(n0->n_ident, "lambda")) {
|
||||
// (lambda list body...)
|
||||
n0 = cadr(expr);
|
||||
n1 = cddr(expr);
|
||||
assert(cons_q(n0));
|
||||
assert(!null_q(n1));
|
||||
|
||||
struct function *new_fn = unit_lambda(ctx->root);
|
||||
struct context new_ctx;
|
||||
new_fn->args = n0;
|
||||
new_fn->body = n1;
|
||||
new_fn->local_count = 0;
|
||||
assert(!ctx->parent);
|
||||
ctx_init(&new_ctx, ctx);
|
||||
new_ctx.var_counter = 0;
|
||||
new_ctx.owner = new_fn;
|
||||
|
||||
emit_function(&new_ctx, new_fn);
|
||||
emit_insn(fn, OP(OP_LDF, new_fn->index));
|
||||
|
||||
return;
|
||||
} else if (!strcmp(n0->n_ident, "begin")) {
|
||||
n1 = cdr(expr);
|
||||
assert(!null_q(n1));
|
||||
|
||||
|
||||
+2
-1
@@ -25,7 +25,7 @@ static struct hash_pair *shash_pair_new(void *key, void *value) {
|
||||
assert(strlen(key) < 128 - sizeof(struct hash_pair));
|
||||
struct hash_pair *res;
|
||||
|
||||
res = calloc(sizeof(struct hash_pair), 1);
|
||||
res = calloc(128, 1);
|
||||
if (!res) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -49,6 +49,7 @@ int shash_init(struct hash *h, size_t cap) {
|
||||
h->keycmp = (int (*) (const void *, const void *)) strcmp;
|
||||
|
||||
h->bucket_count = cap;
|
||||
assert(cap);
|
||||
h->buckets = malloc(sizeof(struct list_head) * cap);
|
||||
|
||||
if (!h->buckets) {
|
||||
|
||||
Reference in New Issue
Block a user