From d825ca3fe2fed84d1f8fadf305f5687308309d57 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 19 Apr 2022 20:43:53 -0400 Subject: [PATCH] Add fib to forbble. Add git branch to pl prompt. Fix readFileToString() for empty files. --- src/examples/forbble.pbl | 55 ++++++++++++++++++++++++++++++++------- src/examples/pebblisp.pbl | 17 ++++++++++-- src/pebblisp.c | 8 +++--- src/tests.sh | 6 ++--- 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/examples/forbble.pbl b/src/examples/forbble.pbl index ea5e228..ce353bb 100644 --- a/src/examples/forbble.pbl +++ b/src/examples/forbble.pbl @@ -33,7 +33,28 @@ (stkadd bottom) ))) +(def dup (fn () ( + (def top (pop)) + (stkadd top) + (stkadd top) +))) + +(def over (fn () ( + (def second (at 1 stack)) + (stkadd second) +))) + +(def rot (fn () ( + (def a (pop)) + (def b (pop)) + (def c (pop)) + (stkadd b) + (stkadd a) + (stkadd c) +))) + (def get-words (fn (text) ( + (def words ()) (split text " ") ))) @@ -67,14 +88,21 @@ )) (def operations (table)) -(h-insert operations "cls" '(sys "clear")) -(h-insert operations "swap" '(swap)) -(h-insert operations "??" '(pstack)) -(h-insert operations "+" '(twop +)) -(h-insert operations "-" '(twop -)) -(h-insert operations "/" '(twop /)) -(h-insert operations "*" '(twop *)) -(h-insert operations "." '(loud-pop)) +(def add-op (fn (name op) ( + (h-insert operations name op) +))) +(add-op "cls" '(sys "clear")) +(add-op "over" '(over)) +(add-op "rot" '(rot)) +(add-op "dup" '(dup)) +(add-op "swap" '(swap)) +(add-op "??" '(pstack)) +(add-op "." '(loud-pop)) +(add-op "+" '(twop +)) +(add-op "-" '(twop -)) +(add-op "/" '(twop /)) +(add-op "*" '(twop *)) +(add-op "=" '(twop =)) (def noterr (fn (e) (not (iserr e)))) @@ -99,11 +127,20 @@ )) ))) -(def feval (fn (text) ( +(def fline-eval (fn (text) ( (def words (get-words text)) (fmap words) ))) +(def feval (fn (text) ( + (for-each fline-eval (split text nl)) +))) + +(for-each feval ( + ": fib swap over + $" + ": peek dup . $" +)) + (def esc (ch 27)) (def reset (cat esc "[0m")) (def fprompt (cat esc "[33;1mplf:> " reset)) diff --git a/src/examples/pebblisp.pbl b/src/examples/pebblisp.pbl index ea6f841..d815965 100644 --- a/src/examples/pebblisp.pbl +++ b/src/examples/pebblisp.pbl @@ -22,7 +22,7 @@ (reloadConfig) ))) -(def forthFile "/home/sagevaillancourt/projects/pebblisp/src/examples/forbble.pbl") +(def forthFile (cat ~ "/projects/pebblisp/src/examples/forbble.pbl")) (struct Alias (name value)) (def aliases ( (Alias "ls" "ls --color") @@ -85,11 +85,24 @@ ) ))) +(def current-commit (fn () ( + (stdout "git rev-parse --short HEAD") +))) + +(def current-branch (fn () ( + (stdout "git branch --show-current") +))) + (def prompt (fn (a) ( (def d (cleanDir)) + (def branch (current-branch)) + (set branch + (if (> (slen branch) 2) + (cat bold yellow " [" (substr 0 (- (slen branch) 1) branch) "]") + branch)) (cat nl esc "]0; " d (ch 7) - bold red "[sage] " blue (clock (time)) " " reset cyan d nl + bold red "[sage] " blue (clock (time)) " " reset cyan d branch nl bold green "pebblisp ~> " reset) ))) diff --git a/src/pebblisp.c b/src/pebblisp.c index 8b44f26..ffe0964 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -422,7 +422,7 @@ int hexChar(char c) if (c >= 'a' && c <= 'f') { return c - 'a' + 10; } - if (c >= 'A' && c <= 'F'){ + if (c >= 'A' && c <= 'F') { return c - 'A' + 10; } return -1; @@ -444,7 +444,7 @@ int decChar(char c) return -1; } -Object parseNum(int base, const char* text, int length, int (*func)(char)) +Object parseNum(int base, const char* text, int length, int (* func)(char)) { int num = 0; for (int i = 0; i < length; i++) { @@ -461,7 +461,7 @@ Object parseNum(int base, const char* text, int length, int (*func)(char)) Object optimize(Object (* func)(Object*, int, struct Environment*), int argCount, Object args[]) { - struct StaticFunction *f = malloc(sizeof(struct StaticFunction) + (sizeof(Object) * argCount)); + struct StaticFunction* f = malloc(sizeof(struct StaticFunction) + (sizeof(Object) * argCount)); f->refs = 1; f->func = func; f->arguments = (void*) f + sizeof(struct StaticFunction); @@ -617,7 +617,7 @@ char* readFileToString(FILE* input) c = fgetc(input); } while (c != '\n' && c != EOF); } else { - string[i++] = c; + string[i++] = c == EOF ? '\0' : c; } while ((c = fgetc(input)) != EOF) { diff --git a/src/tests.sh b/src/tests.sh index bc6e0f4..08421e2 100755 --- a/src/tests.sh +++ b/src/tests.sh @@ -242,9 +242,9 @@ check "Map Filter" \ "( 64 81 100 121 144 )" title "Forbble" -check "BasicForbbleOp" '(loadfile "examples/forbble.pbl") (feval "10 10 * .") ""' "100" -check "Basic Forbble Definition" '(loadfile "examples/forbble.pbl") (feval ": ft 12 * $") (feval "10 ft .") ""' "120" -#check "FibForbble" '(loadfile "examples/forbble.pbl") (feval (1 1 _f _f _f _f _f _f _f _f _f _f _f _f _f _f _f _f _f _f _f _f _f .)) ""' "28657" +check "BasicForbbleOp" '(loadfile "examples/forbble.pbl") (feval "10 10 * .")' "100" +check "Basic Forbble Definition" '(loadfile "examples/forbble.pbl") (feval ": ft 12 * $") (feval "10 ft .")' "120" +check "FibForbble" '(loadfile "examples/forbble.pbl") (feval "1 1 fib fib fib fib fib fib fib fib fib fib fib fib fib fib fib fib fib fib fib fib fib .")' "28657" #check "ForbbleDefine" '(loadfile "examples/forbble.pbl") (feval ( : "cubed" dup dup * * $ )) (feval (4 cubed .)) ""' "64" title "Environment"