I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Execute Brain****/Janet

From Rosetta Code
Execute Brain****/Janet is an implementation of Brainf***. Other implementations of Brainf***.
Execute Brain****/Janet is part of RCBF. You may find other members of RCBF at Category:RCBF.
(defn clean [src]
(def valid (zipcoll "[],.+-<>" (range 8)))
(filter valid src))
 
(defn make-jumps [insns]
(var stack @[])
(var jumps @{})
(eachp [i insn] insns
(case insn
(chr "[") (array/push stack i)
(chr "]") (put jumps i (array/pop stack))))
(merge jumps (invert jumps)))
 
(defn interpret [insns jumps]
(def tape @{})
(var dp 0)
(var ip 0)
(loop [insn :iterate (get insns ip)]
(def cur (get tape dp 0))
(case insn
(chr "<") (-- dp)
(chr ">") (++ dp)
(chr "+") (set (tape dp) (inc cur))
(chr "-") (set (tape dp) (dec cur))
(chr ",") (set (tape dp) (if-let [c (:read stdin 1)] (c 0) -1))
(chr ".") (prin (string/from-bytes (tape dp)))
(chr "[") (if (= 0 cur) (set ip (jumps ip)))
(chr "]") (if (not= 0 cur) (set ip (jumps ip))))
(++ ip)))
 
(let [insns (clean (slurp ((dyn :args) 1)))
jumps (make-jumps insns)]
(interpret insns jumps))
Output:
$ echo Wnarg | janet bf.janet rot13.bf
Janet