From: Elijah Cohen Date: Wed, 14 Aug 2024 05:11:43 +0000 (-0500) Subject: wrote some membranes around quoting X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=638c3b7e79fb2687c9e9ca7d2508065d5e709fc3;p=klapaucius wrote some membranes around quoting --- diff --git a/ideas.org b/ideas.org index bbf6e65..fdf7d0d 100644 --- 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)) diff --git a/src/builtins/arithmetic.c b/src/builtins/arithmetic.c index 8a04be5..fb2b16b 100644 --- a/src/builtins/arithmetic.c +++ b/src/builtins/arithmetic.c @@ -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); } diff --git a/src/builtins/core.c b/src/builtins/core.c index 03791a6..ee739e4 100644 --- a/src/builtins/core.c +++ b/src/builtins/core.c @@ -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); } diff --git a/src/sexpr.c b/src/sexpr.c index 8a9c9ac..593283b 100644 --- a/src/sexpr.c +++ b/src/sexpr.c @@ -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) { diff --git a/src/sexpr.h b/src/sexpr.h index b2282b3..79c5e18 100644 --- a/src/sexpr.h +++ b/src/sexpr.h @@ -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);