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.
This commit is contained in:
Sage Vaillancourt 2020-11-06 16:10:46 -05:00
parent 775be84f5f
commit 0962601976
4 changed files with 21 additions and 36 deletions

View File

@ -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;
}

20
src/examples/lib.pbl Normal file
View File

@ -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)))

View File

@ -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);

View File

@ -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"