Add Forbble comments.

End Forbble definitions with : not $
(feval) one big string, instead of doing a for-each.
This commit is contained in:
Sage Vaillancourt 2022-04-22 14:56:15 -04:00 committed by Sage Vaillancourt
parent 1094a85227
commit cb8d966a05
5 changed files with 58 additions and 31 deletions

View File

@ -28,10 +28,10 @@
))) )))
(def drop (fn () (def drop (fn ()
"Remove the top of the stack" "Remove the top of the stack. a b -- a"
(if (> (len stack) 0) ( (if (> (len stack) 0)
(set stack (rest stack)) (set stack (rest stack))
) (prnl "pop: STACK EMPTY")) (prnl "pop: STACK EMPTY"))
)) ))
(def twop (fn (op) (def twop (fn (op)
@ -43,24 +43,28 @@
)) ))
))) )))
(def swap (fn () ( (def swap (fn ()
"a b -- b a" (
(def '(top bottom) (popN 2)) (def '(top bottom) (popN 2))
(push-down top) (push-down top)
(push-down bottom) (push-down bottom)
))) )))
(def dup (fn () ( (def dup (fn ()
"a -- a a" (
(def top (pop)) (def top (pop))
(push-down top) (push-down top)
(push-down top) (push-down top)
))) )))
(def over (fn () ( (def over (fn ()
"a b -- b a b" (
(def second (at 1 stack)) (def second (at 1 stack))
(push-down second) (push-down second)
))) )))
(def rot (fn () ( (def rot (fn ()
"a b c -- c a b" (
(def '(a b c) (popN 3)) (def '(a b c) (popN 3))
(push-down b) (push-down b)
(push-down a) (push-down a)
@ -87,7 +91,7 @@
(def get-code (fn (words) ( (def get-code (fn (words) (
(def next (first words)) (def next (first words))
(if (iserr next) () (if (iserr next) ()
(if (= "$" next) () (if (= ";" next) ()
(pre (get-code (rest words)) next))) (pre (get-code (rest words)) next)))
))) )))
@ -125,6 +129,7 @@
(def-op "=" '(twop =)) (def-op "=" '(twop =))
(def-op ">" '(twop >)) (def-op ">" '(twop >))
(def-op "<" '(twop <)) (def-op "<" '(twop <))
;(def-op "if" '(set words (plf-if words))) ?
(def noterr (fn (e) (not (iserr e)))) (def noterr (fn (e) (not (iserr e))))
@ -149,7 +154,13 @@
)) ))
))) )))
(def comment-char "#")
(def fline-eval (fn (text) ( (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)) (def words (get-words text))
(fmap words) (fmap words)
))) )))
@ -158,25 +169,31 @@
(for-each fline-eval (split text nl)) (for-each fline-eval (split text nl))
))) )))
(for-each feval ( (feval "
": sq dup * $" : sq dup * ;
": fib swap over + $"
; Peek at the top of the stack # Fibonnacci sequence
": peek dup . $" : fibinit 0 1 ;
: fib swap over + ;
; Basic imperial distances (with inches as the base unit) # Peek at the top of the stack
": ft 12 * $" : peek dup . ;
": yd 3 ft * $"
": yds 3 ft * $"
": in $"
": >ft 1 ft / $"
": >yds 1 yd / $"
; Convert between Farenheit and Celcius # Basic imperial distances (with inches as the base unit)
": f>c 32 - 5 * 9 / $" : ft 12 * ;
": c>f 9 * 5 / 32 + $" : 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) ( (def plf-assert (fn (test expected) (
(set stack ()) (set stack ())
@ -192,7 +209,7 @@
(plf-assert "12 sq" 144) (plf-assert "12 sq" 144)
(plf-assert "100 c>f" 212) (plf-assert "100 c>f" 212)
(plf-assert "10 yds >ft" 30) (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 ()) (set stack ())
))) )))
@ -215,11 +232,14 @@
(set prompt fprompt) (set prompt fprompt)
(set preprocess (fn (text) ( (set preprocess (fn (text) (
(if (= "q" text) (restore-repl) ( (if (= "q" text) (restore-repl) (
(feval text) (if (= "" text) (prn nl) (
(prn nl) (feval text)
"" ; Have the underlying REPL do nothing (prn nl)
))
)) ))
"" ; Have the underlying REPL do nothing
))) )))
))) )))
(plf-repl) (plf-repl)
""

View File

@ -23,6 +23,12 @@
))) )))
(def forthFile (cat ~ "/projects/pebblisp/src/examples/forbble.pbl")) (def forthFile (cat ~ "/projects/pebblisp/src/examples/forbble.pbl"))
(def loadForth (fn () (
(loadfile forthFile)
(plf-tests)
""
)))
(struct Alias (name value)) (struct Alias (name value))
(def aliases ( (def aliases (
(Alias "ls" "ls --color") (Alias "ls" "ls --color")
@ -33,7 +39,7 @@
(Alias "r" "(reloadConfig)") (Alias "r" "(reloadConfig)")
(Alias "cd ~" (cat "cd " ~)) (Alias "cd ~" (cat "cd " ~))
(Alias "rename" "git commit --amend --author 'Sage Vaillancourt <sagev9000@tutanota.com>'") (Alias "rename" "git commit --amend --author 'Sage Vaillancourt <sagev9000@tutanota.com>'")
(Alias "plf" "((loadfile forthFile) (plf-tests))") (Alias "plf" "(loadForth)")
(Alias "sudo" "echo -e '\e[1;31m' && sudo") (Alias "sudo" "echo -e '\e[1;31m' && sudo")
)) ))

View File

@ -671,6 +671,7 @@ inline Object nullTerminated(const char* string)
return stringFromSlice(string, strlen(string)); return stringFromSlice(string, strlen(string));
} }
// TODO: Unescaped version
inline Object stringFromSlice(const char* string, int len) inline Object stringFromSlice(const char* string, int len)
{ {
#ifdef STANDALONE #ifdef STANDALONE

View File

@ -32,7 +32,7 @@ Object takeInput(Object* params, int length, unused struct Environment* env)
} }
char input[256] = ""; char input[256] = "";
if (fgets(input, 256, stdin)) { if (fgets(input, 256, stdin)) {
return stringFromSlice(input, strlen(input) - 1); return nullTerminated(input);
} }
return errorWithContext(NULL_PARSE, "fgets() error"); return errorWithContext(NULL_PARSE, "fgets() error");
} }

View File

@ -246,7 +246,7 @@ check "Map Filter" \
title "Forbble" title "Forbble"
check "BasicForbbleOp" '(loadfile "examples/forbble.pbl") (feval "10 10 * .")' "100" 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 "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" #check "ForbbleDefine" '(loadfile "examples/forbble.pbl") (feval ( : "cubed" dup dup * * $ )) (feval (4 cubed .)) ""' "64"