From 3319fdf2c3ebbe3eeb199260ee4e9ad91eed725b Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 2 Nov 2020 12:40:42 -0500 Subject: [PATCH] Lambdas return only last item. EOL file comments Added (penv) to print the current environment Changed "Duplicate" test to work with new Lambda structure --- src/env.c | 2 +- src/pebblisp.c | 31 ++++++++++++++++++++++++++----- src/pebblisp.h | 1 + src/tests.sh | 2 +- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/env.c b/src/env.c index d68fff4..ed0dcf8 100644 --- a/src/env.c +++ b/src/env.c @@ -230,7 +230,7 @@ struct Environment defaultEnv() {"ap", &append}, {"pre", &prepend}, {"at", &at}, {"rest", &rest}, {"rev", &reverse}, {"isnum", &isNum}, - {"prn", &print}, + {"prn", &print}, {"penv", &printEnvO}, }; for(unsigned i = 0; i < sizeof(symFuncs)/sizeof(symFuncs[0]); i++) { diff --git a/src/pebblisp.c b/src/pebblisp.c index 12cccea..075b78e 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -154,6 +154,11 @@ Object evalList(const Object *obj, struct Environment *env) Object ret = eval(&first_eval.lambda->body, &newEnv); deleteEnv(&newEnv); cleanObject(&first_eval); + Object *t = tail(&ret); + if(t) { + // cleanObject(&ret); + return cloneObject(*t); + } return ret; } else { @@ -387,6 +392,15 @@ Object print(Object p, Object ignore, struct Environment *env) return p; } +Object printEnvO(Object i1, Object i2, struct Environment *env) { + while(env->outer) { + env = env-> outer; + } + + printEnv(env); + return numberObject(0); +} + void copySlice(char * dest, struct Slice *src) { if(!dest || !src) @@ -573,20 +587,27 @@ int readFile(const char *filename, struct Environment *env) { } Object r = numberObject(0); char page[4096] = ""; - char line[256]; - if(fgets(line, 256, input)){ + const unsigned LINE_MAX = 256; + char line[LINE_MAX]; + if(fgets(line, LINE_MAX, input)){ if(line[0] != '#' || line[1] != '!') { strncat(page, line, strlen(line) - 1); } } - while(fgets(line, 256, input)) { + while(fgets(line, LINE_MAX, input)) { int i; - for(i = 0; i < 256; i++) { + for(i = 0; i < LINE_MAX; i++) { if(line[i] != ' ') { if(line[i] == ';') { break; } else { - strncat(page, line, strlen(line) - 1); + int j = 0; + for(j = i; j < LINE_MAX; j++) { + if(line[j] == ';' || line[j] == '\0') + break; + } + strncat(page, line, j); + strcat(page, " "); break; } } diff --git a/src/pebblisp.h b/src/pebblisp.h index 7fc33b5..e128ac7 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -58,5 +58,6 @@ Object reverse(Object _list, Object ignore, struct Environment *ignore2); Object isNum(Object test, Object ignore, struct Environment *ignore2); Object print(Object p, Object ignore, struct Environment *ignore2); +Object printEnvO(Object i1, Object i2, struct Environment *env); #endif diff --git a/src/tests.sh b/src/tests.sh index 967bb89..da22e14 100755 --- a/src/tests.sh +++ b/src/tests.sh @@ -146,7 +146,7 @@ check "Factorial" "(def fac (fn (a) \ )));\ (fac 11)" "39916800" check "LambdaClone" "(def y (fn (a) (* 10 a))) (def b y) (def y 12345) ((b 5) y)" "( 50 12345 )" -check "Duplicate" "(def dupe (fn (a) (a a a)));(dupe (*10 10))" "( 100 100 100 )" +check "Duplicate" "(def dupe (fn (a) (() (a a a))));(dupe (*10 10))" "( 100 100 100 )" endBlock title "Cat"