Skip to content

Integer and Float Instructions

Haoxi Zhan edited this page Nov 29, 2013 · 7 revisions

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.

Instructions shared by integers and floats

An example of the definition

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))

List of all shared instructions

All of them are defined in instructions/numbers.clj.

higher level function usage integer float

adder

add two numbers

integer_add

float_add

subtracter

subtract two numbers

integer_sub

float_sub

multiplier

multiply two numbers

integer_mult

float_mult

divider

divide two numbers

integer_div

float_div

modder

return the modulus of two number

integer_mod

float_mod

lessthaner

test <? and push to :boolean stack

integer_lt

float_lt

greaterthaner

test >? and push to :boolean stack

integer_gt

float_gt

minner

return the minimum of the top 2

integer_min

float_min

maxer

return the maximum of the top 2

integer_max

float_max

Note
divider and modder would do nothing if the denominator is 0.

Separately defined instructions

Integer instructions

instruction usage input stack output stack

integer_fromboolean

true to 1, false to 0

:boolean

:integer

integer_fromfloat

convert float to integer

:float

:integer

Float instructions

instruction usage input stack output stack

float_fromboolean

true to 1.0, false to 0.0

:boolean

:float

float_frominteger

convert integer to float

:integer

:float

float_sin

sine function

:float

:float

float_cos

cosine function

:float

:float

float_tan

tangent function

:float

:float