]> git.eli173.com Git - klapaucius/commitdiff
wrote some membranes around quoting
authorElijah Cohen <eli@eli173.com>
Wed, 14 Aug 2024 05:11:43 +0000 (00:11 -0500)
committerElijah Cohen <eli@eli173.com>
Wed, 14 Aug 2024 05:11:43 +0000 (00:11 -0500)
ideas.org
src/builtins/arithmetic.c
src/builtins/core.c
src/sexpr.c
src/sexpr.h

index bbf6e651b37caa17e99d081f229ee86dde3fc90e..fdf7d0d60fc95e91406fcbbcbb5246ec7deb5872 100644 (file)
--- a/ideas.org
+++ b/ideas.org
@@ -1,3 +1,16 @@
+rewrite eq for quote? perhaps
+
+how do i deal with when/where things get unquoted? really hard to say...
+like do I really want to throw something in to each and every function to decide how to handle quotes?
+
+wait when do i do equality with quotes?
+
+things to test:
+(cons 1 2)
+(car (cons 1 2))
+(cons 2 (cons 1 nil))
+
+
 okay the problem:
 (cons 1 (cons 2 nil))
 goes to (cons 1 (2))
index 8a04be59c4c2a280d6a9f06e8235f402631001f0..fb2b16b0ce489f5e4b6fb900b555956d0a828524 100644 (file)
@@ -15,8 +15,8 @@ Sexpr* a_plus(Sexpr* b, Sexpr* rest, Sexpr* env) {
                return cons(b, rest);
        }
        Sexpr* args = b->value.b.args;
-       uint64_t m = eval(car(args), env)->value.u;
-       uint64_t n = eval(car(cdr(args)), env)->value.u;
+       uint64_t m = unquote(eval(car(args), env))->value.u;
+       uint64_t n = unquote(eval(car(cdr(args)), env))->value.u;
        return cons(from_uint(m + n), rest);
 }
 
index 03791a61e9b98312bb8985cded5bf7072b5c408f..ee739e4fa4be5a6c083cec2314c2c5a8a9a199db 100644 (file)
@@ -37,15 +37,15 @@ Sexpr* c_car(Sexpr* b, Sexpr* rest, Sexpr* env) {
        if(CORE_CAR_ARGS != u64_get_num_args(b))
                return cons(b, rest);
        Sexpr* args = b->value.b.args;
-       // huh okay, can i ditch the eval now that i'm doing the 'rest' thing?
-       return cons(car(eval(car(args), env)), rest);
+       Sexpr* arg = unquote(eval(car(args), env));
+       return cons(from_quote(car(arg)), rest);
 }
 Sexpr* c_cdr(Sexpr* b, Sexpr* rest, Sexpr* env) {
        if(CORE_CDR_ARGS != u64_get_num_args(b))
                return cons(b, rest);
        Sexpr* args = b->value.b.args;
-       // see above
-       return cons(cdr(eval(car(args), env)), rest);
+       Sexpr* arg = unquote(eval(car(args), env));
+       return cons(from_quote(cdr(arg)), rest);
 }
 Sexpr* c_if(Sexpr* b, Sexpr* rest, Sexpr* env) {
        if(CORE_IF_ARGS != u64_get_num_args(b))
@@ -55,7 +55,7 @@ Sexpr* c_if(Sexpr* b, Sexpr* rest, Sexpr* env) {
        Sexpr* falsy = car(args);
        Sexpr* truthy = car(cdr(args));
        Sexpr* cond = car(cdr(cdr(args)));
-       if(eval(cond, env)->type != NIL)
+       if(unquote(eval(cond, env))->type != NIL)
                return cons(truthy, rest);
        else
                return cons(falsy, rest);
@@ -65,8 +65,9 @@ Sexpr* c_eq(Sexpr* b, Sexpr* rest, Sexpr* env) {
                return cons(b, rest);
        Sexpr* args = b->value.b.args;
        // yeah eval is kinda necessary for this one I'd say
-       Sexpr* lh = eval(car(args), env);
-       Sexpr* rh = eval(car(cdr(args)), env);
+       Sexpr* lh = unquote(eval(car(args), env));
+       Sexpr* rh = unquote(eval(car(cdr(args)), env));
+
        if(equal(lh, rh)->type == T)
                return cons(from_t(), rest);
        return cons(from_nil(), rest);
@@ -76,7 +77,7 @@ Sexpr* c_not(Sexpr* b, Sexpr* rest, Sexpr* env) {
                return cons(b, rest);
        Sexpr* args = b->value.b.args;
        // I guess I would need the eval to check if eventually is nil
-       if(eval(car(args), env)->type == NIL)
+       if(unquote(eval(car(args), env))->type == NIL)
                return cons(from_t(), rest);
        return cons(from_nil(), rest);
 }
index 8a9c9ac5fdccf1f50497742999b1d97346ee44ee..593283b08602f0e7978966f3bb95d6867391cc98 100644 (file)
@@ -62,13 +62,26 @@ Sexpr* cons(Sexpr* car, Sexpr* cdr) {
        return s;
 }
 
+
+// wait uh oh, do I do the quote-unquote thing here or in builtins/core?
 Sexpr* car(Sexpr* s) {
+       if(s->type == QUOTE)
+               return from_quote(s->value.q->value.c->car);
        return s->value.c->car;
 }
 Sexpr* cdr(Sexpr* s) {
+       if(s->type == QUOTE)
+               return from_quote(s->value.q->value.c->cdr);
        return s->value.c->cdr;
 }
 
+Sexpr* unquote(Sexpr* s) {
+       if(s->type ==QUOTE)
+               return s->value.q;
+       return s;
+
+}
+
 Sexpr* equal(Sexpr* a, Sexpr* b) {
        // need to change later with proper builtins and so forth
        if(a->type != b->type)
@@ -198,10 +211,10 @@ char* sprint_sexpr(Sexpr* s) {
                return out;
        }
        else if(s->type == BUILTIN) {
-               nbytes = snprintf(NULL, 0, "%" PRIu64 "", s->value.u) + 2;
+               nbytes = snprintf(NULL, 0, "%" PRIu64 "", s->value.b.opcode) + 2;
                out = malloc(nbytes*sizeof(char));
                out[0] = '_';
-               snprintf(out + 1, nbytes, "%" PRIu64 "", s->value.u);
+               snprintf(out + 1, nbytes, "%" PRIu64 "", s->value.b.opcode);
                return out;             
        }
        else if(s->type == QUOTE) {
index b2282b39e75782fbc1ae98909cc07ea102e968b9..79c5e188ac46ec5bf1eb88e6ea2ec57eccc88c34 100644 (file)
@@ -10,6 +10,8 @@ Sexpr* from_uint(uint64_t u);
 Sexpr* from_opcode(uint64_t u);
 Sexpr* from_quote(Sexpr* s);
 
+Sexpr* unquote(Sexpr* s);
+
 void sexpr_free(Sexpr* s);
 Sexpr* clone(Sexpr* s);