]> git.eli173.com Git - klapaucius/commitdiff
added clone, sexpr_free, other minor amendments
authorElijah Cohen <eli@eli173.com>
Sun, 11 Aug 2024 03:31:24 +0000 (22:31 -0500)
committerElijah Cohen <eli@eli173.com>
Sun, 11 Aug 2024 03:31:24 +0000 (22:31 -0500)
ideas.org
src/builtins/core.c
src/eval.c
src/sexpr.c
src/sexpr.h

index 09d6a8739d28ea7ee7dff071c90c912a2520539c..c8b9514738129bbcde038072f4c4d39c9a4f235a 100644 (file)
--- a/ideas.org
+++ b/ideas.org
@@ -1,10 +1,16 @@
+GARBAGE COLLECTION
+my grand design is that I will allocate and deallocate all the time, that is, if I 'def' something I keep it, but dereferencing something clones the thing, and then any call using it frees it again.
+is that too slow? yeah probably
 
+---
 
 okay current problem is figuring out the right way to do 'quote'... I think I just need a special thing in my sexpr typedef for a quoted form.. should be easy enough
 
 ALL THE PLACES WHERE I NEED TO GO THROUGH ALL THE TYPES:
 sprint_sexpr
 eval
+clone
+free
 
 is that it?
 
index 98deab4cc9010a72507a3b08d06ceb617cce7246..244ba494804e2005c0e3bf284bfb9a7a6165b42e 100644 (file)
@@ -24,8 +24,8 @@ Sexpr* c_cons(Sexpr* b, Sexpr* env) {
        if(CORE_CONS_ARGS != u64_get_num_args(b))
                return b;
        Sexpr* args = b->value.b.args;
-       Sexpr* _cdr = car(args);
-       Sexpr* _car = car(cdr(args));
+       Sexpr* _cdr = eval(car(args), env);
+       Sexpr* _car = eval(car(cdr(args)), env);
        return cons(_car, _cdr);
 }
 Sexpr* c_car(Sexpr* b, Sexpr* env) {
index ba3e7ca4f893e24d4c2a2bf536fa66535a3cd0ed..031fb2522140aa140f67e9bd56190aa636f8d130 100644 (file)
@@ -30,8 +30,7 @@ Sexpr* eval(Sexpr* s, Sexpr* dict) {
        Sexpr* curr = s;
        while(curr->type == CONS) {
                if(cdr(curr)->type == NIL)
-                       //return eval(car(curr), dict);
-                       return car(curr);
+                       return eval(car(curr), dict);
                if(cdr(curr)->type != CONS)
                        // need to redo, how do i eval "(f . x)" ?
                        // ^ easy, just return (f . x)
index e68c2643fbfc8880ad56f6aa3e263fca0189b260..8a9c9ac5fdccf1f50497742999b1d97346ee44ee 100644 (file)
@@ -94,6 +94,53 @@ Sexpr* equal(Sexpr* a, Sexpr* b) {
        return from_nil();
 }
 
+void sexpr_free(Sexpr* s) {
+       if(s->type == CONS) {
+               sexpr_free(car(s));
+               sexpr_free(cdr(s));
+       }
+       if(s->type ==QUOTE) {
+               sexpr_free(s->value.q);
+       }
+       if(s->type == BUILTIN) {
+               sexpr_free(s->value.b.args);
+       }
+       if(s->type == SYM) {
+               free(s->value.s);
+       }
+       free(s);
+}
+
+Sexpr* clone(Sexpr* s) {
+       Sexpr* ret;
+       switch(s->type) {
+       case UINT:
+               ret = from_uint(s->value.u);
+               break;
+       case SYM:
+               ret = from_sym(strdup(s->value.s));
+               break;
+       case BUILTIN:
+               ret = from_opcode(s->value.b.opcode);
+               sexpr_free(ret->value.b.args);
+               ret->value.b.args = clone(s->value.b.args);
+               break;
+       case T:
+               ret = from_t();
+               break;
+       case CONS:
+               ret = cons(clone(car(s)), clone(cdr(s)));
+               break;
+       case QUOTE:
+               ret = from_quote(clone(s->value.q));
+               break;
+       case NIL:
+       default:
+               ret = from_nil();
+               break;
+       }
+       return ret;
+}
 
 
 Sexpr* reverse(Sexpr* s) {
index 274dd3e9a261d511335b8ef5b76dbd84b7409b47..b2282b39e75782fbc1ae98909cc07ea102e968b9 100644 (file)
@@ -9,7 +9,10 @@ Sexpr* from_sym(char* s);
 Sexpr* from_uint(uint64_t u);
 Sexpr* from_opcode(uint64_t u);
 Sexpr* from_quote(Sexpr* s);
-       
+
+void sexpr_free(Sexpr* s);
+Sexpr* clone(Sexpr* s);
+
 Sexpr* cons(Sexpr* car, Sexpr* cdr);
 Sexpr* car(Sexpr* s);
 Sexpr* cdr(Sexpr* s);