Return history from interpret-program if start-state includes truthy value for :keep-history

This commit is contained in:
Lee Spector 2021-07-20 13:25:23 -04:00
parent 890c2de152
commit d3589b9aee
3 changed files with 18 additions and 8 deletions

View File

@ -5,9 +5,9 @@
<output-test url="file://$MODULE_DIR$/target/classes" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/dev-resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/dev-resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>

View File

@ -37,10 +37,17 @@
(name instruction))))))))
(defn interpret-program
"Runs the given problem starting with the stacks in start-state."
"Runs the given problem starting with the stacks in start-state. If the
start-state includes the key :keep-history with a truthy value, then
the returned state will include the key :history with a value that is a
vector containing all states prior to the final state."
[program start-state step-limit]
(loop [state (assoc start-state :exec program :step 1)]
(loop [state (assoc start-state :exec program :step 1)
history []]
(if (or (empty? (:exec state))
(> (:step state) step-limit))
state
(recur (update (interpret-one-step state) :step inc)))))
(if (:keep-history state)
(assoc state :history history)
state)
(recur (update (interpret-one-step state) :step inc)
(when (:keep-history state) (conj history state))))))

View File

@ -10,9 +10,12 @@
[propeller.push.state :as state]
[propeller.push.utils.helpers :refer [get-stack-instructions]]))
;#_(interpreter/interpret-program
; '(1 2 :integer_add) state/empty-state 1000)
;
#_(interpreter/interpret-program
'(1 2 :integer_add) state/empty-state 1000)
#_(interpreter/interpret-program
'(1 2 :integer_add) (assoc state/empty-state :keep-history true) 1000)
;#_(interpreter/interpret-program
; '(3 3 :integer_eq :exec_if (1 "yes") (2 "no"))
; state/empty-state