 
 
 (defn remember-stack
-  [[interpreter locals] stackname & {:keys [as at]}]
-  (if (nil? as)
-    (vector interpreter locals)
+  [[interpreter scratch] stackname & {:keys [as at]}]
+  (if (nil? as)
+    (vector interpreter scratch)
     (let [stack (get-stack interpreter stackname)]
-      (into [] (list interpreter (assoc locals as stack))))))
+      (vector interpreter (assoc scratch as stack)))))
 
 (fact "remember-stack stores the stack specified in the :as variable"
   (let [integer-munged (-> [six-ints {}]



(defn remember-nth
  [[interpreter locals] stackname & {:keys [as at]}]
  (if (nil? as)
    (vector interpreter locals)
    (let [stack (get-stack interpreter stackname) 
          which (mod (or at 0) (count stack))
          result (nth stack which)]
      (into [] (list interpreter (assoc locals as result))))))


(fact "remember-nth stores the nth item of a named stack"

(def six-ints (make-interpreter :stacks {:integer '(6 5 4 3 2 1)}))



(defn store-scratch
  "stores a scratch kv pair; convenience function for DSL"
  [[interpreter scratch] k v]
  (vector interpreter (assoc scratch k v)))


(fact "store-scratch saves thing to the scratch hashmap"
  (second (store-scratch [(make-interpreter) {}] :foo 8)) => {:foo 8}
  (let [foo (store-scratch [(make-interpreter) {}] :foo 8)]
    (second (store-scratch foo :bar 9)) => {:foo 8, :bar 9}))


(fact "store-scratch can overwrite an existing :scratch value"
  (let [foo (store-scratch [(make-interpreter) {}] :foo 8)]
    (second (store-scratch foo :foo 999)) => {:foo 999}))


(defn consume
  [[interpreter scratch] stack & {:keys [as]}]
  (let [old-stack (get-stack interpreter stack)]
    (cond (empty? old-stack)
            (throw (Exception. (str "Push DSL Error: " stack " is empty")))
          (nil? as)
            [(set-stack interpreter stack (pop old-stack)) scratch]
          :else
            (store-scratch
              [(set-stack interpreter stack (pop old-stack)) scratch]
              as
              (first old-stack)))))


(defn get-stack-from-dslblob ;; convenience for testing only
  [[interpreter scratch] stackname]
  (get-stack interpreter stackname))


