Pass inputs to grammer callbacks as individual parameters
This commit is contained in:
41
bf.kp
41
bf.kp
@@ -1,40 +1,37 @@
|
||||
; Use the power of GLL reader macros to implement
|
||||
; BF support
|
||||
|
||||
; Add atoms as length 1 vectors with nice syntax for deref
|
||||
; We don't have atoms built in, mutable vectors
|
||||
; are our base building block. In order to make the
|
||||
; following BF implementation nice, let's add atoms!
|
||||
; They will be implmented as length 1 vectors with nice syntax for deref
|
||||
(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* (xs) `(get-atom ~(nth xs 1))))
|
||||
(add_grammer_rule 'form ["@" 'form] (fn* (_ x) `(get-atom ~x)))
|
||||
|
||||
; Now begin by defining our BF syntax & semantics
|
||||
; Define our tokens as BF atoms
|
||||
; Ugly b/c using 1-length vectors as atoms
|
||||
(add_grammer_rule 'bfs_atom ["<"] (fn* (xs) '(set-atom! cursor (- @cursor 1))))
|
||||
(add_grammer_rule 'bfs_atom [">"] (fn* (xs) '(set-atom! cursor (+ @cursor 1))))
|
||||
(add_grammer_rule 'bfs_atom ["\\+"] (fn* (xs) '(set-nth! tape @cursor (+ (nth tape @cursor) 1))))
|
||||
(add_grammer_rule 'bfs_atom ["-"] (fn* (xs) '(set-nth! tape @cursor (- (nth tape @cursor) 1))))
|
||||
(add_grammer_rule 'bfs_atom [","] (fn* (xs) '(let* (value (nth input @inptr)) (do (set-atom! inptr (+ 1 @inptr)) (do (set-nth! tape @cursor value))))))
|
||||
(add_grammer_rule 'bfs_atom ["."] (fn* (xs) '(set-atom! output (cons (nth tape @cursor) @output))))
|
||||
(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)) (do (set-atom! inptr (+ 1 @inptr)) (do (set-nth! tape @cursor value))))))
|
||||
(add_grammer_rule 'bfs_atom ["."] (fn* (_) '(set-atom! output (cons (nth tape @cursor) @output))))
|
||||
|
||||
; Define strings of BF atoms
|
||||
(add_grammer_rule 'bfs ['bfs_atom *] (fn* (xs) (nth xs 0)))
|
||||
(add_grammer_rule 'bfs ['bfs_atom *] (fn* (x) x))
|
||||
|
||||
; Add loop as an atom
|
||||
(add_grammer_rule 'bfs_atom ["\\[" 'bfs "]"] (fn* (xs)
|
||||
; (note that closure cannot yet close over itself by value, so we pass it in)
|
||||
(add_grammer_rule 'bfs_atom ["\\[" 'bfs "]"] (fn* (_ x _)
|
||||
`(let* (f (fn* (f)
|
||||
(if (= 0 (nth tape @cursor))
|
||||
nil
|
||||
(do ,(nth xs 1) (f f)))))
|
||||
(do ,x (f f)))))
|
||||
(f f))))
|
||||
|
||||
; For now, stick BFS rule inside an unambigious BFS block
|
||||
; and add compilation/implementation
|
||||
; Note that this compilation into the underlying Lisp
|
||||
; happens at macro evaluation time. If this code were
|
||||
; to be compiled to C, it would be compiled all the way
|
||||
; to C code with no trace of the original BF code.
|
||||
; Also add setup code
|
||||
(add_grammer_rule 'form ["bf" 'optional_WS "{" 'optional_WS 'bfs 'optional_WS "}"]
|
||||
(fn* (xs)
|
||||
(fn* (_ _ _ _ x _ _)
|
||||
`(fn* (input)
|
||||
(let* (
|
||||
tape (vector 0 0 0 0 0)
|
||||
@@ -42,7 +39,7 @@
|
||||
inptr (make-atom 0)
|
||||
output (make-atom (vector))
|
||||
)
|
||||
(do (println "beginning bfs") ,(nth xs 4) (nth output 0))))))
|
||||
(do (println "beginning bfs") ,x (nth output 0))))))
|
||||
|
||||
; Let's try it out! This BF program prints the input 3 times
|
||||
(println (bf { ,>+++[<.>-] } [1337]))
|
||||
|
||||
Reference in New Issue
Block a user