Address the possibility of vectors being empty in vector/_first

The original version just used the built-in `first`, which returns `nil` if you give it an empty collection, which is almost certainly not a useful behavior.

This changes it to return `:ignore-instruction` if the vector is empty, thereby leaving all the stacks unchanged.
This commit is contained in:
Nic McPhee 2020-12-17 18:45:06 -06:00
parent f2eecc774f
commit 66ec0a1ebd

View File

@ -52,12 +52,16 @@
(make-instruction state empty? [stack] :boolean)))
;; Pushes the first item of the top element of the vector stack onto the
;; approrpiately-typed literal stack
;; approrpiately-typed literal stack. If the vector is empty, return
;; :ignore-instruction so that nothing is changed on the stacks.
(def _first
^{:stacks #{:elem}}
(fn [stack state]
(let [lit-stack (get-vector-literal-type stack)]
(make-instruction state first [stack] lit-stack))))
(make-instruction state
#(if (empty? %) :ignore-instruction (first %))
[stack]
lit-stack))))
;; Pushes onto the INTEGER stack the index of the top element of the
;; appropriately-typed literal stack within the top element of the vector stack