Files
kraken/bf.kp

71 lines
3.4 KiB
Plaintext
Raw Normal View History

2020-04-10 22:46:53 -04:00
; Now we have native BF support
2020-04-10 23:08:31 -04:00
(def! with_update (fn* [arr idx val]
(if (= idx 0)
(cons val (rest arr))
(cons (first arr) (with_update (rest arr) (- idx 1) val)))))
2020-04-10 23:08:31 -04:00
(add_grammer_rule 'bfs_atom ["<"] (fn* [xs] (list 'left)))
(add_grammer_rule 'bfs_atom [">"] (fn* [xs] (list 'right)))
(add_grammer_rule 'bfs_atom ["\\+"] (fn* [xs] (list 'plus)))
(add_grammer_rule 'bfs_atom ["-"] (fn* [xs] (list 'minus)))
(add_grammer_rule 'bfs_atom [","] (fn* [xs] (list 'in)))
(add_grammer_rule 'bfs_atom ["."] (fn* [xs] (list 'out)))
2020-04-10 23:08:31 -04:00
(add_grammer_rule 'non_empty_bfs_list ['bfs_atom] (fn* [xs] (list (nth xs 0))))
(add_grammer_rule 'non_empty_bfs_list ['bfs_atom 'optional_WS 'non_empty_bfs_list] (fn* [xs] (cons (nth xs 0) (nth xs 2))))
2020-04-10 23:08:31 -04:00
(add_grammer_rule 'bfs_list [] (fn* [xs] xs))
(add_grammer_rule 'bfs_list ['non_empty_bfs_list] (fn* [xs] (nth xs 0)))
2020-04-10 23:08:31 -04:00
(add_grammer_rule 'bfs_atom ["\\[" 'bfs_list "]"] (fn* [xs]
2020-04-10 23:05:05 -04:00
`(let* (f (fn* []
2020-04-10 22:46:53 -04:00
(if (= 0 (nth (deref arr) (deref ptr)))
nil
2020-04-10 23:05:05 -04:00
(do ,(nth xs 1) (f)))))
(f))))
2020-04-10 23:08:31 -04:00
(add_grammer_rule 'bfs ['bfs_list] (fn* [xs] (nth xs 0)))
2020-04-10 23:08:31 -04:00
(add_grammer_rule 'form ["bf" 'optional_WS "{" 'optional_WS 'bfs 'optional_WS "}"]
2020-04-10 22:46:53 -04:00
(fn* [xs]
`(fn* [input]
(let* (
;arr (atom [0 0 0 0 0])
arr (atom (vector 0 0 0 0 0))
output (atom [])
ptr (atom 0)
inptr (atom 0)
left (fn* [] (swap! ptr (fn* [old] (- old 1))))
right (fn* [] (swap! ptr (fn* [old] (+ old 1))))
plus (fn* [] (swap! arr (fn* [old] (with_update old (deref ptr) (+ (nth (deref arr) (deref ptr)) 1)))))
minus (fn* [] (swap! arr (fn* [old] (with_update old (deref ptr) (- (nth (deref arr) (deref ptr)) 1)))))
2020-04-10 23:08:31 -04:00
in (fn* [] (let* ( h (nth input (deref inptr))
_ (swap! inptr (fn* [old] (+ old 1))))
(swap! arr (fn* [old] (with_update old (deref ptr) h)))))
out (fn* [] (swap! output (fn* [old] (cons (nth (deref arr) (deref ptr)) old)))))
2020-04-10 23:11:17 -04:00
(do ,(nth xs 4) (deref output))))))
2020-04-10 23:08:31 -04:00
2020-04-10 23:11:17 -04:00
(println (bf { ,>+++[<.>-] } [1337]))
2020-04-13 22:45:40 -04:00
;(def! main (fn* [argv] 2))
2020-04-13 22:55:25 -04:00
;(def! main (fn* [] (let* (a 13 b 12 c 11) b)))
;(def! main (fn* [] (do 13 12 11)))
;(def! main (fn* [] (if false 1 2)))
;(def! main (fn* [] (+ 13 1)))
;(def! main (fn* [] (- 13 -1)))
2020-04-14 23:34:23 -04:00
;(def! main (fn* [] (- 13 -1)))
;(def! main (fn* [] (+ 13 -)))
;(def! main (fn* [] (+ 13 1 2)))
2020-04-14 23:34:23 -04:00
;(def! main (fn* [] (cond false 1 false 2 true 3 true 4 false 5)))
;(def! main (fn* [] ((fn* [] (+ (+ 1 2) 3)) 13 1 2)))
2020-04-14 23:53:12 -04:00
;(def! main (fn* [] (((fn* [] (fn* [] 1))))))
2020-04-14 23:59:52 -04:00
;(def! main (fn* [] ((fn* [a b c] (- (+ a b) c)) 13 1 4)))
;(def! main (fn* [] (fn* [] 1)))
2020-04-14 23:59:52 -04:00
;(def! other (fn* [a b c] (- (+ a b) c)))
;(def! main (fn* [] (other 13 1 4)))
(def! other 12)
(def! main (fn* [] (+ other 4)))