]> git.eli173.com Git - klapaucius/commitdiff
important changes in makefile, applyntimes, cosmetic cleanup elsewhere main
authorElijah Cohen <cohen@eli173.com>
Sat, 17 Aug 2024 15:59:57 +0000 (15:59 +0000)
committerElijah Cohen <cohen@eli173.com>
Sat, 17 Aug 2024 15:59:57 +0000 (15:59 +0000)
13 files changed:
src/Makefile
src/builtins.c
src/builtins/arithmetic.h
src/builtins/combinators.c
src/builtins/combinators.h
src/builtins/core.c
src/dict.c
src/dict.h
src/eval.c
src/eval.h
src/parser.c
src/repl.c
src/sexpr.c

index 4c65d969fc8fd42d6e93d20e886d890efc5bd5d4..ff4cd89edb19053ac018da04886c5a0f369f5bfc 100644 (file)
@@ -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)
 
 
index 81688874186a6b507cb5fa72d0fa48992322f28c..ff676bc29eb69a321ad5e7ccdcda8c2f427a32b7 100644 (file)
@@ -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 <inttypes.h>
-#include <stdio.h>
 
 
 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;
 }
index 0969286d1d80db3fd4be6845143b552ac1399659..570667426f097381e61c2a4118c63989e50a60a7 100644 (file)
@@ -3,7 +3,7 @@
 
 #include "../types.h"
 
-#define ARITH_PREFIX 0x01
+#define ARITH_PREFIX 0x02
 
 
 #define ARITH_ADD 0x00
index ac1d352b2e4ec8a3079daf34a13bbbcccbf23a7f..ad4179aedb40a615e0cd9394eb6231a81f21f53f 100644 (file)
@@ -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);
 }
index 9b8cb324d12b7bfcdec625cf0a40d93f618dae86..3a3e38a439e70c7abf62e8027ad3c9dfaa7fe7de 100644 (file)
@@ -3,7 +3,7 @@
 
 #include "../types.h"
 
-#define COMB_PREFIX 0x02
+#define COMB_PREFIX 0x01
 
 #define COMB_I 0x00
 #define COMB_I_ARGS 1
index 23bee2c8b25ceac193dbd3b8f222f8117e3fd04a..6827b404334d4a348428e430605acc9c3eeee36e 100644 (file)
@@ -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);
index 8c3176f788f0e447d99fbd57132ecdf81df4144a..5de9e491cb3fc0dc3bf9292267f8af6e359116d4 100644 (file)
@@ -3,17 +3,11 @@
 #include "sexpr.h"
 #include "dict.h"
 
-#include <stdio.h>
-
-
 // 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;
 }
 
index fe1aeeac19d8cebbb66331243dfe5dc7119dfbef..600f7319c86cc52f86d28b099b3f9d382d2d0b0f 100644 (file)
@@ -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);
index f15251c63271eaeaf68b75dc96279f72ab312569..986be0bc34c437eece2135f060ba16a4e49255f6 100644 (file)
@@ -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));
index 358df0832ad56cae9a3d37e818ebb56780f3758b..8045301316f5174c44401ed92600c830defafb2c 100644 (file)
@@ -5,13 +5,7 @@
 #include <inttypes.h>
 #include <string.h>
 
-// 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
index 847c0895f78903bf2924e2a0d31c3bd69e4f23d0..5e3124a19ec0829fc5e92c926414cf2b7b9cab80 100644 (file)
@@ -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;
 }
index 019c602269ef38b44b859e3ec81187dc36964924..d3adff48075c120ebbb895c4ccbe5ca3fb9b7aeb 100644 (file)
@@ -2,7 +2,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-//#include <histedit.h>
 
 #include <editline/readline.h>
 
 #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("> ");
index 890255559b139775f9e61aef69de71c80cee7420..803ea066162fa87ef3e5133d7370c0c48917ebc3 100644 (file)
@@ -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, "<fun>");
-               return out;
-       }
-       else if(s->type == FEXP) {
-               out = malloc(7*sizeof(char));
-               strcpy(out, "<fexp>");
-               return out;
-       }
-       */
        else if(s->type == UINT) {
                nbytes = snprintf(NULL, 0, "%" PRIu64 "", s->value.u) + 1;
                out = malloc(nbytes*sizeof(char));