From 99541748723dd50f5f75bd0de9c766b867bd2939 Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Thu, 26 Sep 2024 14:55:12 -0500 Subject: [PATCH] boutta reorganize a bunch of stuff i think --- ideas.org | 4 ++++ src/builtins.c | 4 ++++ src/builtins/combinators.c | 41 ++++++++++++++++++++++++++++++++++++++ src/builtins/combinators.h | 4 ++++ src/builtins/core.c | 1 + src/repl.c | 2 +- src/sexpr.c | 16 +++++++++++++++ src/types.h | 3 ++- 8 files changed, 73 insertions(+), 2 deletions(-) diff --git a/ideas.org b/ideas.org index 3991bab..111e80c 100644 --- a/ideas.org +++ b/ideas.org @@ -1,3 +1,7 @@ +I have something working (to some extent), now I want to see what I can actually do with it + + + MEMORY MANAGEMENT IMPORTANT: all builtins need to free themselves when executed diff --git a/src/builtins.c b/src/builtins.c index ff676bc..fa344f9 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -46,6 +46,10 @@ Sexpr* dispatch_others(Sexpr* b, Sexpr* rest, Sexpr* env) { // what even is this? return b; } + else if(b->type == PTR) { + // what even? + return b; + } else if(b->type == QUOTE) { // what even is this? return b; diff --git a/src/builtins/combinators.c b/src/builtins/combinators.c index ad4179a..bd97a45 100644 --- a/src/builtins/combinators.c +++ b/src/builtins/combinators.c @@ -88,6 +88,41 @@ Sexpr* c_w(Sexpr* b, Sexpr* rest, Sexpr* env) { return ret; } +Sexpr* c_phi(Sexpr* b, Sexpr* rest, Sexpr* env) { + if(COMB_PHI_ARGS != u64_get_num_args(b)) { + return cons(b, rest); + } + Sexpr* args = b->value.b.args; + Sexpr* d = clone(car(args)); + Sexpr* c = clone(car(cdr(args))); + Sexpr* bee = clone(car(cdr(cdr(args)))); + Sexpr* a = clone(car(cdr(cdr(cdr(args))))); + Sexpr* d2 = clone(d); + Sexpr* bd = cons(bee, cons(d, from_nil())); + Sexpr* cd = cons(c, cons(d2, from_nil())); + Sexpr* ret = cons(a, cons(bd, cons(cd, from_nil()))); + sexpr_free(b); + return ret; +} + +Sexpr* c_psi(Sexpr* b, Sexpr* rest, Sexpr* env) { + if(COMB_PHI_ARGS != u64_get_num_args(b)) { + return cons(b, rest); + } + Sexpr* args = b->value.b.args; + Sexpr* d = clone(car(args)); + Sexpr* c = clone(car(cdr(args))); + Sexpr* bee = clone(car(cdr(cdr(args)))); + Sexpr* a = clone(car(cdr(cdr(cdr(args))))); + Sexpr* bee2 = clone(bee); + Sexpr* bd = cons(bee, cons(d, from_nil())); + Sexpr* bc = cons(bee2, cons(c, from_nil())); + Sexpr* ret = cons(a, cons(bc, cons(bd, from_nil()))); + sexpr_free(b); + return ret; +} + + Sexpr* x_comb_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { uint64_t code = b->value.b.opcode & 0xff; @@ -105,6 +140,10 @@ Sexpr* x_comb_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { return c_c(b, rest, env); case COMB_W: return c_w(b, rest, env); + case COMB_PHI: + return c_phi(b, rest, env); + case COMB_PSI: + return c_psi(b, rest, env); default: return from_nil(); } @@ -118,6 +157,8 @@ Sexpr* load_comb_env(Sexpr* env) { load_builtin("B", (COMB_PREFIX << 8) | COMB_B, env); load_builtin("C", (COMB_PREFIX << 8) | COMB_C, env); load_builtin("W", (COMB_PREFIX << 8) | COMB_W, env); + load_builtin("Phi", (COMB_PREFIX << 8) | COMB_PHI, env); + load_builtin("Psi", (COMB_PREFIX << 8) | COMB_PSI, env); return env; } diff --git a/src/builtins/combinators.h b/src/builtins/combinators.h index 3a3e38a..07b4464 100644 --- a/src/builtins/combinators.h +++ b/src/builtins/combinators.h @@ -17,6 +17,10 @@ #define COMB_C_ARGS 3 #define COMB_W 0x05 #define COMB_W_ARGS 2 +#define COMB_PHI 0x06 +#define COMB_PHI_ARGS 4 +#define COMB_PSI 0x07 +#define COMB_PSI_ARGS 4 Sexpr* x_comb_dispatch(Sexpr* s, Sexpr* rest, Sexpr* env); Sexpr* load_comb_env(Sexpr* env); diff --git a/src/builtins/core.c b/src/builtins/core.c index 6827b40..504d127 100644 --- a/src/builtins/core.c +++ b/src/builtins/core.c @@ -178,6 +178,7 @@ Sexpr* c_exit(Sexpr* b, Sexpr* rest, Sexpr* env) { return NULL; } + Sexpr* x_core_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { uint64_t code = b->value.b.opcode & 0xff; switch(code) { diff --git a/src/repl.c b/src/repl.c index d3adff4..b076f16 100644 --- a/src/repl.c +++ b/src/repl.c @@ -30,7 +30,7 @@ int main(int argc, char** argv) { } else { //printf("- -%s\n", sprint_sexpr(in)); - Sexpr* out = eval(clone(car(in)), env); + Sexpr* out = eval(clone(in), env); char* outstr = sprint_sexpr(out); printf(" - %s\n", outstr); sexpr_free(in); diff --git a/src/sexpr.c b/src/sexpr.c index 803ea06..07bdecf 100644 --- a/src/sexpr.c +++ b/src/sexpr.c @@ -51,6 +51,13 @@ Sexpr* from_quote(Sexpr* s) { return ret; } +Sexpr* from_pointer(void* p) { + Sexpr* ret = malloc(sizeof(Sexpr)); + ret->type = PTR; + ret->value.p = p; + return ret; +} + Sexpr* cons(Sexpr* car, Sexpr* cdr) { Cons_t* c = malloc(sizeof(Cons_t)); Sexpr* s = malloc(sizeof(Sexpr)); @@ -89,6 +96,8 @@ Sexpr* equal(Sexpr* a, Sexpr* b) { return from_t(); if(t == SYM) return strcmp(a->value.s, b->value.s) == 0 ? from_t() : from_nil(); + if(t == PTR) + return (a->value.p == b->value.p) ? from_t() : from_nil(); if(t == UINT) return (a->value.u == b->value.u) ? from_t() : from_nil(); if(t == BUILTIN) @@ -134,6 +143,9 @@ Sexpr* clone(Sexpr* s) { case SYM: ret = from_sym(s->value.s); break; + case PTR: + ret = from_pointer(s->value.p); + break; case BUILTIN: ret = from_opcode(s->value.b.opcode); sexpr_free(ret->value.b.args); @@ -193,6 +205,10 @@ char* sprint_sexpr(Sexpr* s) { strcpy(out, s->value.s); return out; } + else if(s->type == PTR) { + out = strdup("<*>"); + return out; + } else if(s->type == UINT) { nbytes = snprintf(NULL, 0, "%" PRIu64 "", s->value.u) + 1; out = malloc(nbytes*sizeof(char)); diff --git a/src/types.h b/src/types.h index 4d2e6c1..f5c377a 100644 --- a/src/types.h +++ b/src/types.h @@ -12,7 +12,7 @@ typedef void* Nil_t; typedef void* Truth_t; typedef enum Sexpr_Type { - UINT, SYM, BUILTIN, NIL, T, CONS, QUOTE + UINT, SYM, BUILTIN, NIL, T, CONS, QUOTE, PTR } Sexpr_Type; typedef struct Cons { @@ -36,6 +36,7 @@ typedef struct Sexpr { Nil_t n; Truth_t t; struct Sexpr* q; + void* p; } value; } Sexpr; -- 2.39.2