Add Forbble comments.
End Forbble definitions with : not $ (feval) one big string, instead of doing a for-each.
This commit is contained in:
parent
1094a85227
commit
cb8d966a05
|
@ -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) (
|
||||
(if (= "" text) (prn nl) (
|
||||
(feval text)
|
||||
(prn nl)
|
||||
"" ; Have the underlying REPL do nothing
|
||||
))
|
||||
))
|
||||
"" ; Have the underlying REPL do nothing
|
||||
)))
|
||||
)))
|
||||
|
||||
(plf-repl)
|
||||
""
|
||||
|
|
|
@ -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 <sagev9000@tutanota.com>'")
|
||||
(Alias "plf" "((loadfile forthFile) (plf-tests))")
|
||||
(Alias "plf" "(loadForth)")
|
||||
(Alias "sudo" "echo -e '\e[1;31m' && sudo")
|
||||
))
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
Loading…
Reference in New Issue