This commit is contained in:
Nathan Braswell
2020-05-12 21:30:37 -04:00
parent bf53d3701f
commit 722a72579a
5 changed files with 183 additions and 328 deletions

20
bf.kp
View File

@@ -5,25 +5,25 @@
(def! make-atom (fn* (x) [x]))
(def! set-atom! (fn* (x y) (set-nth! x 0 y)))
(def! get-atom (fn* (x) (nth x 0)))
(add_grammer_rule 'form ["@" 'form] (fn* (_ x) `(get-atom ~x)))
(add_grammar_rule 'form ["@" 'form] (fn* (_ x) `(get-atom ~x)))
; Now begin by defining our BF syntax & semantics
; Define our tokens as BF atoms
(add_grammer_rule 'bfs_atom ["<"] (fn* (_) '(set-atom! cursor (- @cursor 1))))
(add_grammer_rule 'bfs_atom [">"] (fn* (_) '(set-atom! cursor (+ @cursor 1))))
(add_grammer_rule 'bfs_atom ["\\+"] (fn* (_) '(set-nth! tape @cursor (+ (nth tape @cursor) 1))))
(add_grammer_rule 'bfs_atom ["-"] (fn* (_) '(set-nth! tape @cursor (- (nth tape @cursor) 1))))
(add_grammer_rule 'bfs_atom [","] (fn* (_) '(let* (value (nth input @inptr))
(add_grammar_rule 'bfs_atom ["<"] (fn* (_) '(set-atom! cursor (- @cursor 1))))
(add_grammar_rule 'bfs_atom [">"] (fn* (_) '(set-atom! cursor (+ @cursor 1))))
(add_grammar_rule 'bfs_atom ["\\+"] (fn* (_) '(set-nth! tape @cursor (+ (nth tape @cursor) 1))))
(add_grammar_rule 'bfs_atom ["-"] (fn* (_) '(set-nth! tape @cursor (- (nth tape @cursor) 1))))
(add_grammar_rule 'bfs_atom [","] (fn* (_) '(let* (value (nth input @inptr))
(do (set-atom! inptr (+ 1 @inptr))
(set-nth! tape @cursor value)))))
(add_grammer_rule 'bfs_atom ["."] (fn* (_) '(set-atom! output (cons (nth tape @cursor) @output))))
(add_grammar_rule 'bfs_atom ["."] (fn* (_) '(set-atom! output (cons (nth tape @cursor) @output))))
; Define strings of BF atoms
(add_grammer_rule 'bfs ['bfs_atom *] (fn* (x) x))
(add_grammar_rule 'bfs ['bfs_atom *] (fn* (x) x))
; Add loop as an atom
; (note that closure cannot yet close over itself by value, so we pass it in)
(add_grammer_rule 'bfs_atom ["\\[" 'bfs "]"] (fn* (_ x _)
(add_grammar_rule 'bfs_atom ["\\[" 'bfs "]"] (fn* (_ x _)
`(let* (f (fn* (f)
(if (= 0 (nth tape @cursor))
nil
@@ -32,7 +32,7 @@
; For now, stick BFS rule inside an unambigious BFS block
; Also add setup code
(add_grammer_rule 'form ["bf" 'optional_WS "{" 'optional_WS 'bfs 'optional_WS "}"]
(add_grammar_rule 'form ["bf" 'optional_WS "{" 'optional_WS 'bfs 'optional_WS "}"]
(fn* (_ _ _ _ x _ _)
`(fn* (input)
(let* (