-
Notifications
You must be signed in to change notification settings - Fork 93
Integer and Float Instructions
Because Clojure is dynamic typed, instructions of integers and floats are defined together. Both of them are defined in instructions/numbers.clj
.
Most of them are defined by making a higher level function which returns a function. And then define-registered
is used to register them for both integers and floats.
A few other instructions are defined separately.
The instructions integer_add
and float_add
share a higher level function adder
in their definition. adder
is defined in line 8 of instructions/number.clj
.
(defn adder
"Returns a function that pushes the sum of the top two items."
[type]
(fn [state]
(if (not (empty? (rest (type state))))
(let [first (stack-ref type 0 state)
second (stack-ref type 1 state)]
(->> (pop-item type state)
(pop-item type)
(push-item (keep-number-reasonable (+' first second)) type)))
state)))
It takes a type, which is an atom for a stack, and returns a function which is the adding instruction for the type.
Following the adder
definition, these 2 instructions are define-registered.
(define-registered integer_add (adder :integer))
(define-registered float_add (adder :float))
All of them are defined in instructions/numbers.clj
.
higher level function | usage | integer | float |
---|---|---|---|
adder |
add two numbers |
|
|
subtracter |
subtract two numbers |
|
|
multiplier |
multiply two numbers |
|
|
divider |
divide two numbers |
|
|
modder |
return the modulus of two number |
|
|
lessthaner |
test <? and push to |
|
|
greaterthaner |
test >? and push to |
|
|
minner |
return the minimum of the top 2 |
|
|
maxer |
return the maximum of the top 2 |
|
|
Note
|
divider and modder would do nothing if the denominator is 0. |
instruction | usage | input stack | output stack |
---|---|---|---|
|
true to 1, false to 0 |
|
|
|
convert float to integer |
|
|