]> git.eli173.com Git - klapaucius/commitdiff
reference counting
authorElijah Cohen <eli@eli173.com>
Thu, 18 Sep 2025 23:37:47 +0000 (18:37 -0500)
committerElijah Cohen <eli@eli173.com>
Thu, 18 Sep 2025 23:37:47 +0000 (18:37 -0500)
klsrc/combinators.kl
klsrc/util.kl
src/eval.c
src/sexpr.c
src/types.h

index 431ef579e54e95a0cd4ac186b55773d951dc0f5a..0bee6654a5d53827170ecad2feb87f8433cb417f 100644 (file)
@@ -27,3 +27,5 @@
 (def C3 (C* (B C2)))
 
 (def Cn (Z (B (S (C (eq 0) C)) (B (B (B C)) (B D (C B (C - 1)))))))
+
+(def C?n (Z (B (S (C (eq 0) C)) (B (S (R B (B C))) (C B (C - 1))))))
\ No newline at end of file
index 8e8a40cfb0b2299174e83a4b91390bec0758475f..6cb8483389533005ff67b0f54ad245097fc412d5 100644 (file)
@@ -1,4 +1,6 @@
 
+(def printthen (D (D evalarg (K I) print)))
+
 (def range (abstract-rec (eq 0) (C cons) (C - 1) nil))
 
 (def x-times-n (C (C (D abstract-rec (eq 0) (D B K cons)) (C - 1)) nil))
index d07236d23c14c3c7e73e71eabf29506b85c838ca..f48e1795f00b7420fa921346692a89626c47abe6 100644 (file)
@@ -65,10 +65,12 @@ Sexpr* apply_builtin(Sexpr* func, Sexpr* rest, Sexpr* env) {
        if(func->type != BUILTIN) {
                return dispatch_others(func, rest, env);
        }
+       // should i find a nice way to move these next few lines to its own function in sexpr.c?
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = BUILTIN;
        ret->value.b.opcode = func->value.b.opcode;
        ret->value.b.args = cons(clone(car(rest)), clone(func->value.b.args));
+       ret->refcount = 1;
        Sexpr* cdrrest = clone(cdr(rest));
        sexpr_free(rest);
        return dispatch(ret, cdrrest, env);
index f57edc8ca380e18a7d5d17fa94d7a1779924e593..bac1d11fa6cc95e7d919f5f8cdd37f26a050217a 100644 (file)
@@ -14,6 +14,7 @@ Sexpr* from_nil() {
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = NIL;
        ret->value.n = NULL;
+       ret->refcount = 1;
        return ret;
 }
 
@@ -21,6 +22,7 @@ Sexpr* from_t() {
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = T;
        ret->value.t = NULL;
+       ret->refcount = 1;
        return ret;
 }
 
@@ -29,6 +31,7 @@ Sexpr* from_sym(char* s) {
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = SYM;
        ret->value.s = strdup(s);
+       ret->refcount = 1;
        return ret;
 }
 
@@ -36,6 +39,7 @@ Sexpr* from_string(char* s) {
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = STR;
        ret->value.str = strdup(s);
+       ret->refcount = 1;
        return ret;
 }
 
@@ -43,6 +47,7 @@ Sexpr* from_uint(K_UINT_TYPE u) {
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = UINT;
        ret->value.u = u;
+       ret->refcount = 1;
        return ret;
 }
 
@@ -51,6 +56,7 @@ Sexpr* from_opcode(uint64_t u) {
        ret->type = BUILTIN;
        ret->value.b.opcode = u;
        ret->value.b.args = from_nil();
+       ret->refcount = 1;
        return ret;
 }
 
@@ -58,6 +64,7 @@ Sexpr* from_quote(Sexpr* s) {
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = QUOTE;
        ret->value.q = s;
+       ret->refcount = 1;
        return ret;
 }
 
@@ -65,6 +72,7 @@ Sexpr* from_pointer(void* p) {
        Sexpr* ret = malloc(sizeof(Sexpr));
        ret->type = PTR;
        ret->value.p = p;
+       ret->refcount = 1;
        return ret;
 }
 
@@ -75,6 +83,7 @@ Sexpr* cons(Sexpr* car, Sexpr* cdr) {
        c->cdr = cdr;
        s->type = CONS;
        s->value.c = c;
+       s->refcount = 1;
        return s;
 }
 
@@ -129,6 +138,10 @@ Sexpr* equal(Sexpr* a, Sexpr* b) {
 }
 
 void sexpr_free(Sexpr* s) {
+       if(s->refcount > 1) {
+               s->refcount--;
+               return;
+       }
        if(s->type == CONS) {
                sexpr_free(car(s));
                sexpr_free(cdr(s));
@@ -150,6 +163,9 @@ void sexpr_free(Sexpr* s) {
 }
 
 Sexpr* clone(Sexpr* s) {
+       s->refcount++;
+       return s;
+       /*
        Sexpr* ret;
        switch(s->type) {
        case UINT:
@@ -184,6 +200,7 @@ Sexpr* clone(Sexpr* s) {
                break;
        }
        return ret;
+       */
 }
 
 
index eaff3ab8a510f527b2e972bb10ca2607a14014ab..583aa07c797b282a23fef96b6e682c5dbd8c7649 100644 (file)
@@ -32,6 +32,7 @@ typedef struct Builtin {
 
 typedef struct Sexpr {
        Sexpr_Type type;
+       size_t refcount;
        union {
                K_UINT_TYPE u;
                char* str;