From d68f0cc886aff410f5776e2d503fde21fd451c01 Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Sat, 17 Aug 2024 15:59:57 +0000 Subject: [PATCH] important changes in makefile, applyntimes, cosmetic cleanup elsewhere --- src/Makefile | 11 +---------- src/builtins.c | 7 +------ src/builtins/arithmetic.h | 2 +- src/builtins/combinators.c | 1 - src/builtins/combinators.h | 2 +- src/builtins/core.c | 8 +++----- src/dict.c | 8 -------- src/dict.h | 2 -- src/eval.c | 7 ------- src/eval.h | 6 ------ src/parser.c | 10 +--------- src/repl.c | 5 +---- src/sexpr.c | 16 ---------------- 13 files changed, 9 insertions(+), 76 deletions(-) diff --git a/src/Makefile b/src/Makefile index 4c65d96..ff4cd89 100644 --- a/src/Makefile +++ b/src/Makefile @@ -22,16 +22,6 @@ CFLAGS:= -g -Wall `pkg-config $(LIBRARIES) --cflags` LDFLAGS:= -g -Wall `pkg-config $(LIBRARIES) --libs` -#$(BUILD)/lex.yy.o: lisp.l -#lex -o $(BUILD)/lex.yy.c lisp.l - -#$(BUILD)/y.tab.o: lisp.y -# yacc -defines lisp.y - -#y.tab.h: lisp.y -# yacc -d lisp.y - - $(BUILD)/$(BIN): $(OBJS) $(CC) $(BIN_OBJS) $(LDFLAGS) -o $(BUILD)/$(BIN) @@ -39,6 +29,7 @@ $(BUILD)/$(BIN): $(OBJS) $(OBJS): $(BUILD)/%.o:%.c Makefile + mkdir -p $(BUILD)/builtins $(CC) -c $< -MMD -o $@ $(CFLAGS) diff --git a/src/builtins.c b/src/builtins.c index 8168887..ff676bc 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -4,7 +4,6 @@ #include "sexpr.h" #include "dict.h" - // this is where you include whatever extra sets of instructions #include "builtins/core.h" @@ -12,7 +11,6 @@ #include "builtins/combinators.h" #include -#include Sexpr* dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { @@ -29,7 +27,6 @@ Sexpr* dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { } } - Sexpr* dispatch_others(Sexpr* b, Sexpr* rest, Sexpr* env) { // this is for applying other types, // i.e. doing @@ -54,7 +51,6 @@ Sexpr* dispatch_others(Sexpr* b, Sexpr* rest, Sexpr* env) { return b; } else if(b->type == UINT) { - // the hard one uint64_t anx = (CORE_PREFIX << 8) | CORE_APPLYN; Sexpr* idk = from_opcode(anx); Sexpr* ret = cons(idk, cons(clone(b), rest)); @@ -63,7 +59,6 @@ Sexpr* dispatch_others(Sexpr* b, Sexpr* rest, Sexpr* env) { return from_nil(); } - uint64_t u64_get_num_args(Sexpr* b) { uint64_t curr_num = 0; Sexpr* args = b->value.b.args; @@ -99,6 +94,6 @@ Sexpr* load_env(Sexpr* env) { newenv = load_core_env(env); newenv = load_arith_env(newenv); newenv = load_comb_env(newenv); - //append_to_dict(env, from_sym("asdf"), from_uint(5455)); + return newenv; } diff --git a/src/builtins/arithmetic.h b/src/builtins/arithmetic.h index 0969286..5706674 100644 --- a/src/builtins/arithmetic.h +++ b/src/builtins/arithmetic.h @@ -3,7 +3,7 @@ #include "../types.h" -#define ARITH_PREFIX 0x01 +#define ARITH_PREFIX 0x02 #define ARITH_ADD 0x00 diff --git a/src/builtins/combinators.c b/src/builtins/combinators.c index ac1d352..ad4179a 100644 --- a/src/builtins/combinators.c +++ b/src/builtins/combinators.c @@ -43,7 +43,6 @@ Sexpr* c_k(Sexpr* b, Sexpr* rest, Sexpr* env) { } Sexpr* args = b->value.b.args; Sexpr* x = clone(car(cdr(args))); - //Sexpr* y = clone(car(args)); // not needed sexpr_free(b); return cons(x, rest); } diff --git a/src/builtins/combinators.h b/src/builtins/combinators.h index 9b8cb32..3a3e38a 100644 --- a/src/builtins/combinators.h +++ b/src/builtins/combinators.h @@ -3,7 +3,7 @@ #include "../types.h" -#define COMB_PREFIX 0x02 +#define COMB_PREFIX 0x01 #define COMB_I 0x00 #define COMB_I_ARGS 1 diff --git a/src/builtins/core.c b/src/builtins/core.c index 23bee2c..6827b40 100644 --- a/src/builtins/core.c +++ b/src/builtins/core.c @@ -35,7 +35,6 @@ Sexpr* c_cons(Sexpr* b, Sexpr* rest, Sexpr* env) { if(_car->type == QUOTE) _car = _car->value.q; Sexpr* ret = cons(from_quote(cons(clone(_car),clone(_cdr))), rest); - //return cons(from_quote(cons(_car, _cdr)), rest); sexpr_free(raw_car); sexpr_free(raw_cdr); sexpr_free(b); @@ -57,8 +56,6 @@ Sexpr* c_cdr(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* args = b->value.b.args; Sexpr* unqargev = eval(clone(car(args)), env); Sexpr* ret = cons(from_quote(clone(cdr(unquote(unqargev)))), rest); - //Sexpr* arg = unquote(eval(car(args), env)); - //return cons(from_quote(cdr(arg)), rest); sexpr_free(unqargev); sexpr_free(b); return ret; @@ -68,8 +65,6 @@ Sexpr* c_if(Sexpr* b, Sexpr* rest, Sexpr* env) { return cons(b, rest); Sexpr* args = b->value.b.args; - //Sexpr* falsy = car(args); - //Sexpr* truthy = car(cdr(args)); Sexpr* cond = eval(clone(car(cdr(cdr(args)))), env); if(unquote(cond)->type != NIL) { Sexpr* truthy = clone(car(cdr(args))); @@ -138,6 +133,8 @@ Sexpr* c_applyn(Sexpr* b, Sexpr* rest, Sexpr* env) { // possible commentary for the future: // i may wish to rewrite to eval (f x) // then return something like ((n-1) f x) + // OOPS I NEED to actually eval + // and not just macroexpand, for memory reasons if(CORE_APPLYN_ARGS != u64_get_num_args(b)) return cons(b, rest); Sexpr* args = b->value.b.args; @@ -151,6 +148,7 @@ Sexpr* c_applyn(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* ret = cons(arg, from_nil()); while(num > 0) { ret = cons(clone(fun), cons(ret, from_nil())); + ret = eval(ret, env); num--; } sexpr_free(b); diff --git a/src/dict.c b/src/dict.c index 8c3176f..5de9e49 100644 --- a/src/dict.c +++ b/src/dict.c @@ -3,17 +3,11 @@ #include "sexpr.h" #include "dict.h" -#include - - // IMMENSE WARNING: as of now this is the only place where any sort of variable is modified in place.. that may come to pass more later, but beware // okay, doing an incredibly /incredibly/ naive dict implementation for now, // proper hash tables come later -// wait, this naive method can totally just be done in sexprs that I already have - - // great redo: have it in a cons cell, so I can modify it without having to return anything Sexpr* init_dict() { @@ -24,10 +18,8 @@ Sexpr* append_to_dict(Sexpr* dict, Sexpr* key, Sexpr* value) { // assumes dict well-formed, returns new dict // puts new things on the front of the dict, makes it so looking up gets the newest stuff first Sexpr* new = cons(clone(key), clone(value)); - // not yet sure if the above 'clone' is needed, or where the call should take place Sexpr* head = cons(new, car(dict)); dict->value.c->car = head; - //return cons(new, dict); return dict; } diff --git a/src/dict.h b/src/dict.h index fe1aeea..600f731 100644 --- a/src/dict.h +++ b/src/dict.h @@ -7,8 +7,6 @@ // everything needed for the dictionary -// key type is string, value type is sexpr, hash range is uint64_t - Sexpr* init_dict(); Sexpr* append_to_dict(Sexpr* dict, Sexpr* key, Sexpr* value); Sexpr* lookup(Sexpr* dict, Sexpr* key); diff --git a/src/eval.c b/src/eval.c index f15251c..986be0b 100644 --- a/src/eval.c +++ b/src/eval.c @@ -21,10 +21,8 @@ Sexpr* eval(Sexpr* s, Sexpr* dict) { // non-null s // generally assumes that a sexpr passed to this is well-formed (ie no inapt NULLS) - // question: does a completed builtin get evaluated here? if(s == NULL) return NULL; // not needed if assumptions accurate, but yknow if(s->type == SYM) { - // look up the value in the lookup table Sexpr* lookedup = lookup(dict, s); if(lookedup->type == NIL) { printf("%s not defined\n", s->value.s); @@ -54,21 +52,16 @@ Sexpr* eval(Sexpr* s, Sexpr* dict) { // now i need to apply actually //Sexpr* arg = car(cdr(curr)); Sexpr* rest = clone(cdr(curr)); - // wait, where does rest get freed? Sexpr* func = eval(clone(car(curr)), dict); sexpr_free(curr); curr = apply_builtin(func, rest, dict); // is this ok? - //sexpr_free(rest); sexpr_free(func); } return curr; } - Sexpr* apply_builtin(Sexpr* func, Sexpr* rest, Sexpr* env) { if(func->type != BUILTIN) { - //printf("uh oh\n"); - //return from_nil(); // uhh this /should/ actually be impossible... return dispatch_others(func, rest, env); } Sexpr* ret = malloc(sizeof(Sexpr)); diff --git a/src/eval.h b/src/eval.h index 358df08..8045301 100644 --- a/src/eval.h +++ b/src/eval.h @@ -5,13 +5,7 @@ #include #include -// everything needed for the dictionary - -// key type is string, value type is sexpr, hash range is uint64_t - Sexpr* eval(Sexpr* s, Sexpr* env); Sexpr* apply_builtin(Sexpr* func, Sexpr* rest, Sexpr* env); - - #endif diff --git a/src/parser.c b/src/parser.c index 847c089..5e3124a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -72,7 +72,7 @@ bool is_u64(char* s) { Sexpr* vals_parse(Sexpr* tokens) { - // should only accept a singly-linked list of symbols + // should only accept a non-nested list of symbols // note: may also reverse Sexpr* out = from_nil(); Sexpr* next = NULL; @@ -107,14 +107,12 @@ bool balance_checker(Sexpr* tokens) { } curr = cdr(curr); } - //printf("b: %d\n", balance); return balance == 0; } Sexpr* cons_parse(Sexpr* tokens) { Sexpr* reversedptr = reverse(tokens); Sexpr* reversed = reversedptr; - // takes results from previous parsing ops, aka the forward-facing? Sexpr* heads_stack = from_nil(); Sexpr* curr_head = from_nil(); Sexpr* curr_car; @@ -147,21 +145,15 @@ Sexpr* cons_parse(Sexpr* tokens) { Sexpr* parse(char* s) { - //printf("s: %s\n", s); Sexpr* tokens = tokenize(s); - //printf("t: %s\n", sprint_sexpr(tokens)); Sexpr* vals = vals_parse(tokens); sexpr_free(tokens); - //printf("v: %s\n", sprint_sexpr(vals)); if(!balance_checker(vals)) { printf("unbalanced parenthesis\n"); sexpr_free(vals); return NULL; } - //printf("v1: %s\n", sprint_sexpr(vals)); Sexpr* done = cons_parse(vals); - //printf("v2: %s\n", sprint_sexpr(vals)); sexpr_free(vals); - //printf("c: %s\n", sprint_sexpr(*done)); return done; } diff --git a/src/repl.c b/src/repl.c index 019c602..d3adff4 100644 --- a/src/repl.c +++ b/src/repl.c @@ -2,7 +2,6 @@ #include #include -//#include #include @@ -13,14 +12,12 @@ #include "builtins.h" #include "sexpr.h" -// huh? - int main(int argc, char** argv) { Sexpr* env = init_dict(); load_env(env); - // now do the loop, right? + char* input = NULL; while(1) { input = readline("> "); diff --git a/src/sexpr.c b/src/sexpr.c index 8902555..803ea06 100644 --- a/src/sexpr.c +++ b/src/sexpr.c @@ -61,8 +61,6 @@ 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); @@ -82,7 +80,6 @@ Sexpr* unquote(Sexpr* s) { } Sexpr* equal(Sexpr* a, Sexpr* b) { - // need to change later with proper builtins and so forth if(a->type != b->type) return from_nil(); Sexpr_Type t = a->type; @@ -107,7 +104,6 @@ Sexpr* equal(Sexpr* a, Sexpr* b) { return equal(cdr(a), cdr(b)); } } - // leaving everything else off for... reasons return from_nil(); } @@ -197,18 +193,6 @@ char* sprint_sexpr(Sexpr* s) { strcpy(out, s->value.s); return out; } - /* - else if(s->type == FUN) { - out = malloc(6*sizeof(char)); - strcpy(out, ""); - return out; - } - else if(s->type == FEXP) { - out = malloc(7*sizeof(char)); - strcpy(out, ""); - return out; - } - */ else if(s->type == UINT) { nbytes = snprintf(NULL, 0, "%" PRIu64 "", s->value.u) + 1; out = malloc(nbytes*sizeof(char)); -- 2.39.2