Return history from interpret-program if start-state includes truthy value for :keep-history
This commit is contained in:
parent
890c2de152
commit
d3589b9aee
@ -5,9 +5,9 @@
|
|||||||
<output-test url="file://$MODULE_DIR$/target/classes" />
|
<output-test url="file://$MODULE_DIR$/target/classes" />
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<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$/resources" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" 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" />
|
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
</content>
|
</content>
|
||||||
|
@ -37,10 +37,17 @@
|
|||||||
(name instruction))))))))
|
(name instruction))))))))
|
||||||
|
|
||||||
(defn interpret-program
|
(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]
|
[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))
|
(if (or (empty? (:exec state))
|
||||||
(> (:step state) step-limit))
|
(> (:step state) step-limit))
|
||||||
state
|
(if (:keep-history state)
|
||||||
(recur (update (interpret-one-step state) :step inc)))))
|
(assoc state :history history)
|
||||||
|
state)
|
||||||
|
(recur (update (interpret-one-step state) :step inc)
|
||||||
|
(when (:keep-history state) (conj history state))))))
|
||||||
|
@ -10,9 +10,12 @@
|
|||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]))
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]))
|
||||||
|
|
||||||
;#_(interpreter/interpret-program
|
#_(interpreter/interpret-program
|
||||||
; '(1 2 :integer_add) state/empty-state 1000)
|
'(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
|
;#_(interpreter/interpret-program
|
||||||
; '(3 3 :integer_eq :exec_if (1 "yes") (2 "no"))
|
; '(3 3 :integer_eq :exec_if (1 "yes") (2 "no"))
|
||||||
; state/empty-state
|
; state/empty-state
|
||||||
|
Loading…
x
Reference in New Issue
Block a user