diff --git a/src/examples/forbble.pbl b/src/examples/forbble.pbl index 4b3a710..5e297d7 100644 --- a/src/examples/forbble.pbl +++ b/src/examples/forbble.pbl @@ -28,10 +28,10 @@ ))) (def drop (fn () - "Remove the top of the stack" - (if (> (len stack) 0) ( + "Remove the top of the stack. a b -- a" + (if (> (len stack) 0) (set stack (rest stack)) - ) (prnl "pop: STACK EMPTY")) + (prnl "pop: STACK EMPTY")) )) (def twop (fn (op) @@ -43,24 +43,28 @@ )) ))) -(def swap (fn () ( +(def swap (fn () + "a b -- b a" ( (def '(top bottom) (popN 2)) (push-down top) (push-down bottom) ))) -(def dup (fn () ( +(def dup (fn () + "a -- a a" ( (def top (pop)) (push-down top) (push-down top) ))) -(def over (fn () ( +(def over (fn () + "a b -- b a b" ( (def second (at 1 stack)) (push-down second) ))) -(def rot (fn () ( +(def rot (fn () + "a b c -- c a b" ( (def '(a b c) (popN 3)) (push-down b) (push-down a) @@ -87,7 +91,7 @@ (def get-code (fn (words) ( (def next (first words)) (if (iserr next) () - (if (= "$" next) () + (if (= ";" next) () (pre (get-code (rest words)) next))) ))) @@ -125,6 +129,7 @@ (def-op "=" '(twop =)) (def-op ">" '(twop >)) (def-op "<" '(twop <)) +;(def-op "if" '(set words (plf-if words))) ? (def noterr (fn (e) (not (iserr e)))) @@ -149,7 +154,13 @@ )) ))) +(def comment-char "#") (def fline-eval (fn (text) ( + (def slash-index (indexOf text comment-char)) + (set text + (if (> slash-index 0) + (substr 0 slash-index text) + text)) (def words (get-words text)) (fmap words) ))) @@ -158,25 +169,31 @@ (for-each fline-eval (split text nl)) ))) -(for-each feval ( - ": sq dup * $" - ": fib swap over + $" +(feval " + : sq dup * ; - ; Peek at the top of the stack - ": peek dup . $" + # Fibonnacci sequence + : fibinit 0 1 ; + : fib swap over + ; - ; Basic imperial distances (with inches as the base unit) - ": ft 12 * $" - ": yd 3 ft * $" - ": yds 3 ft * $" - ": in $" - ": >ft 1 ft / $" - ": >yds 1 yd / $" + # Peek at the top of the stack + : peek dup . ; - ; Convert between Farenheit and Celcius - ": f>c 32 - 5 * 9 / $" - ": c>f 9 * 5 / 32 + $" -)) + # Basic imperial distances (with inches as the base unit) + : ft 12 * ; + : yd 3 ft * ; + : yds 3 ft * ; + : in ; + : >ft 1 ft / ; + : >yds 1 yd / ; + + # Convert between Farenheit and Celcius + : f>c 32 - 5 * 9 / ; + : c>f 9 * 5 / 32 + ; + + # TODO + # : checkTen 10 = if 1 else 0 then ; +") (def plf-assert (fn (test expected) ( (set stack ()) @@ -192,7 +209,7 @@ (plf-assert "12 sq" 144) (plf-assert "100 c>f" 212) (plf-assert "10 yds >ft" 30) - (plf-assert "1 1 fib fib fib fib fib fib fib" 34) + (plf-assert "0 1 fib fib fib fib fib fib fib fib" 34) (set stack ()) ))) @@ -215,11 +232,14 @@ (set prompt fprompt) (set preprocess (fn (text) ( (if (= "q" text) (restore-repl) ( - (feval text) - (prn nl) - "" ; Have the underlying REPL do nothing + (if (= "" text) (prn nl) ( + (feval text) + (prn nl) + )) )) + "" ; Have the underlying REPL do nothing ))) ))) (plf-repl) +"" diff --git a/src/examples/pebblisp.pbl b/src/examples/pebblisp.pbl index 46036f6..7a85d34 100644 --- a/src/examples/pebblisp.pbl +++ b/src/examples/pebblisp.pbl @@ -23,6 +23,12 @@ ))) (def forthFile (cat ~ "/projects/pebblisp/src/examples/forbble.pbl")) +(def loadForth (fn () ( + (loadfile forthFile) + (plf-tests) + "" +))) + (struct Alias (name value)) (def aliases ( (Alias "ls" "ls --color") @@ -33,7 +39,7 @@ (Alias "r" "(reloadConfig)") (Alias "cd ~" (cat "cd " ~)) (Alias "rename" "git commit --amend --author 'Sage Vaillancourt '") - (Alias "plf" "((loadfile forthFile) (plf-tests))") + (Alias "plf" "(loadForth)") (Alias "sudo" "echo -e '\e[1;31m' && sudo") )) diff --git a/src/object.c b/src/object.c index d81ce63..b7d512f 100644 --- a/src/object.c +++ b/src/object.c @@ -671,6 +671,7 @@ inline Object nullTerminated(const char* string) return stringFromSlice(string, strlen(string)); } +// TODO: Unescaped version inline Object stringFromSlice(const char* string, int len) { #ifdef STANDALONE diff --git a/src/plfunc/pc.c b/src/plfunc/pc.c index eb8f35b..3953b68 100644 --- a/src/plfunc/pc.c +++ b/src/plfunc/pc.c @@ -32,7 +32,7 @@ Object takeInput(Object* params, int length, unused struct Environment* env) } char input[256] = ""; if (fgets(input, 256, stdin)) { - return stringFromSlice(input, strlen(input) - 1); + return nullTerminated(input); } return errorWithContext(NULL_PARSE, "fgets() error"); } diff --git a/src/tests.sh b/src/tests.sh index 9a3ad37..c56f979 100755 --- a/src/tests.sh +++ b/src/tests.sh @@ -246,7 +246,7 @@ check "Map Filter" \ 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 "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"