Add atom syntax

This commit is contained in:
Nathan Braswell
2020-05-12 09:19:01 -04:00
parent 45ea718bc8
commit 8e296d57c8

17
bf.kp
View File

@@ -1,19 +1,20 @@
; Use the power of GLL reader macros to implement ; Use the power of GLL reader macros to implement
; BF support ; BF support
; Add atoms as length 1 vectors ; Add atoms as length 1 vectors with nice syntax for deref
(def! make-atom (fn* (x) [x])) (def! make-atom (fn* (x) [x]))
(def! set-atom! (fn* (x y) (set-nth! x 0 y))) (def! set-atom! (fn* (x y) (set-nth! x 0 y)))
(def! get-atom (fn* (x) (nth x 0))) (def! get-atom (fn* (x) (nth x 0)))
(add_grammer_rule 'form ["@" 'form] (fn* (xs) `(get-atom ~(nth xs 1))))
; Define our tokens as BF atoms ; Define our tokens as BF atoms
; Ugly b/c using 1-length vectors as atoms ; Ugly b/c using 1-length vectors as atoms
(add_grammer_rule 'bfs_atom ["<"] (fn* (xs) '(set-atom! cursor (- (get-atom cursor) 1)))) (add_grammer_rule 'bfs_atom ["<"] (fn* (xs) '(set-atom! cursor (- @cursor 1))))
(add_grammer_rule 'bfs_atom [">"] (fn* (xs) '(set-atom! cursor (+ (get-atom cursor) 1)))) (add_grammer_rule 'bfs_atom [">"] (fn* (xs) '(set-atom! cursor (+ @cursor 1))))
(add_grammer_rule 'bfs_atom ["\\+"] (fn* (xs) '(set-nth! tape (get-atom cursor) (+ (nth tape (get-atom 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 (get-atom cursor) (- (nth tape (get-atom 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 (get-atom inptr))) (do (set-atom! inptr (+ 1 (get-atom inptr))) (do (set-nth! tape (get-atom cursor) value)))))) (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 (get-atom cursor)) (get-atom output))))) (add_grammer_rule 'bfs_atom ["."] (fn* (xs) '(set-atom! output (cons (nth tape @cursor) @output))))
; Define strings of BF atoms ; Define strings of BF atoms
(add_grammer_rule 'bfs ['bfs_atom *] (fn* (xs) (nth xs 0))) (add_grammer_rule 'bfs ['bfs_atom *] (fn* (xs) (nth xs 0)))
@@ -21,7 +22,7 @@
; Add loop as an atom ; Add loop as an atom
(add_grammer_rule 'bfs_atom ["\\[" 'bfs "]"] (fn* (xs) (add_grammer_rule 'bfs_atom ["\\[" 'bfs "]"] (fn* (xs)
`(let* (f (fn* (f) `(let* (f (fn* (f)
(if (= 0 (nth tape (get-atom cursor))) (if (= 0 (nth tape @cursor))
nil nil
(do ,(nth xs 1) (f f))))) (do ,(nth xs 1) (f f)))))
(f f)))) (f f))))