op (vec (drop 3 str-code))
apply-flag (fn [flag arg]
(case flag
- \0 (fn ([] arg) ([S] (get-in S [:memory arg])))
+ \0 (fn ([S] (get-in S [:memory arg]))
+ ([_ _] arg))
\1 (constantly arg)
- \2 #(get-in % [:memory (+' arg (:relctr %))])))
+ \2 (fn ([S] (get-in S [:memory (+ arg (:relctr S))]))
+ ([S _] (+ arg (:relctr S))))))
with-flags (fn [f]
(fn [S & args]
(apply f S (map apply-flag flags args))))]
(case op
[\0 \1] (fn [S a b c] ; ADD
(-> S
- (assoc-in [:memory (c)] (+' (a S) (b S)))
+ (assoc-in [:memory (c S 0)] (+' (a S) (b S)))
(update :ctr + 4)))
[\0 \2] (fn [S a b c] ; MULT
(-> S
- (assoc-in [:memory (c)] (*' (a S) (b S)))
+ (assoc-in [:memory (c S 0)] (*' (a S) (b S)))
(update :ctr + 4)))
[\0 \3] (fn [S a _ _] ; IN
(-> S
- (assoc-in [:memory (a)] (first (:input S)))
+ (assoc-in [:memory (a S 0)] (first (:input S)))
(update :input subvec 1)
(update :ctr + 2)))
[\0 \4] (fn [S a _ _] ; OUT
(-> S
- (update :output conj (get-in S [:memory (a)]))
+ (update :output conj (get-in S [:memory (a S 0)]))
(update :ctr + 2)))
[\0 \5] (fn [S a b _] ; BNEQ
(update S :ctr (if (not= (a S) 0) (constantly (b S)) #(+ % 3))))
(update S :ctr (if (= (a S) 0) (constantly (b S)) #(+ % 3))))
[\0 \7] (fn [S a b c] ; SLT
(-> S
- (assoc-in [:memory (c)] (if (< (a S) (b S)) 1 0))
+ (assoc-in [:memory (c S 0)] (if (< (a S) (b S)) 1 0))
(update :ctr + 4)))
[\0 \8] (fn [S a b c] ; SEQ
(-> S
- (assoc-in [:memory (c)] (if (= (a S) (b S)) 1 0))
+ (assoc-in [:memory (c S 0)] (if (= (a S) (b S)) 1 0))
(update :ctr + 4)))
[\0 \9] (fn [S a _ _] ; SREL
(-> S