Skip to content

Instantly share code, notes, and snippets.

@durka
Created May 29, 2009 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durka/119805 to your computer and use it in GitHub Desktop.
Save durka/119805 to your computer and use it in GitHub Desktop.
(defn subleq
([in out mem regs] ; in this case data and program memory are the same
(apply subleq in out mem regs (subvec mem (:ip regs) (+ 3 (:ip regs)))))
([in out mem regs a b c]
(let [a-ind (if (neg? a) (nth mem (- a)) a)
b-ind (if (neg? b) (nth mem (- b)) b)
c-ind (if (neg? c) (nth mem (- c)) c)
mem (assoc mem b-ind
(- (mem b-ind) (mem a-ind)))]
[mem (assoc regs
:ip (if (pos? (mem b-ind))
(+ 3 (:ip regs))
c-ind)
:in (= b-ind in)
:out (= b-ind out))])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment