From 0962601976f9def199ee4316e2ec5a677514219a Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Fri, 6 Nov 2020 16:10:46 -0500 Subject: [PATCH] Move library-like functions into examples/lib.pbl Remove `tip` demo function for now. May make a move toward a folder of .pbl files to test with, and it will be re-included then. --- src/env.c | 34 ---------------------------------- src/examples/lib.pbl | 20 ++++++++++++++++++++ src/pebblisp.c | 2 +- src/tests.sh | 1 - 4 files changed, 21 insertions(+), 36 deletions(-) create mode 100644 src/examples/lib.pbl diff --git a/src/env.c b/src/env.c index 3ea4909..75f7351 100644 --- a/src/env.c +++ b/src/env.c @@ -180,35 +180,6 @@ void deleteEnv(struct Environment *e) e->objects = NULL; } -const char *codes[] = { - // Exponentiate a^b - "(def exp (fn (a b) " - "(if (= b 0)" - "1" - "(* a (exp a (- b 1)))" - ")" - "))", - - // Square a - "(def sq (fn (a) (* a a)))", - - // Cube a - "(def cube (fn (a) (exp a 3)))", - - // Return the larger of the two - "(def max (fn (a b) (if (> a b) a b)))", - - // Return the smaller of the two - "(def min (fn (a b) (if (< a b) a b)))", - - // A demo tip calculator - "(def spent (fn (a) \ - (cat \"Tip: $\" (/ a 5) \".\" \ - (/ (* 100 (% a 5)) 5) \ - ) \ - ))", -}; - struct symFunc { const char *sym; Object (*func)(Object, Object, struct Environment*); @@ -241,10 +212,5 @@ struct Environment defaultEnv() addFunc(symFuncs[i].sym, symFuncs[i].func, &e); } - for(unsigned i = 0; i < sizeof(codes)/sizeof(codes[0]); i++) { - Object toClean = parseEval(codes[i], &e); - cleanObject(&toClean); - } - return e; } diff --git a/src/examples/lib.pbl b/src/examples/lib.pbl new file mode 100644 index 0000000..91c7782 --- /dev/null +++ b/src/examples/lib.pbl @@ -0,0 +1,20 @@ +#!/usr/bin/pl +; Exponentiate a^b +(def exp (fn (a b) + (if (= b 0) + 1 + (* a (exp a (- b 1))) + ) +)) + +; Square a +(def sq (fn (a) (* a a))) + +; Cube a +(def cube (fn (a) (exp a 3))) + +; Return the larger of the two +(def max (fn (a b) (if (> a b) a b))) + +; Return the smaller of the two +(def min (fn (a b) (if (< a b) a b))) diff --git a/src/pebblisp.c b/src/pebblisp.c index 2a7c02f..52b8350 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -766,7 +766,6 @@ int readFile(const char *filename, struct Environment *env) { r = parseEval(page, env); cleanObject(&r); - printf("\n"); fclose(input); return 0; @@ -780,6 +779,7 @@ void repl(struct Environment *env) int main(int argc, const char* argv[]) { struct Environment env = defaultEnv(); + readFile("/usr/share/pebblisp/lib.pbl", &env); if(argc >= 2) { if(readFile(argv[1], &env) != 0) { Object r = numberObject(0); diff --git a/src/tests.sh b/src/tests.sh index d05cbca..d6ba1f8 100755 --- a/src/tests.sh +++ b/src/tests.sh @@ -120,7 +120,6 @@ check "VWidwSpc" " ( + 1093 102852 ) " "103945" endBlock title "DemoFunctions" -check "DemoFunc" "(spent 68)" "Tip: \$13.60" check "Squaring" "(sq 9876)" "97535376" check "Cubing" "(cube 81)" "531441" check "Exponent" "(exp 9 9)" "387420489"