Carp/core/Int.carp

115 lines
2.8 KiB
Plaintext
Raw Normal View History

2018-01-24 18:08:18 +03:00
(system-include "carp_int.h")
2018-01-24 17:53:18 +03:00
2021-07-05 15:48:35 +03:00
(doc Int "is the default integral data type.")
2017-06-26 12:15:03 +03:00
(defmodule Int
(register + (λ [Int Int] Int))
(register - (λ [Int Int] Int))
(register * (λ [Int Int] Int))
(register / (λ [Int Int] Int))
(register < (λ [Int Int] Bool))
(register > (λ [Int Int] Bool))
(register = (λ [Int Int] Bool))
(register copy (λ [&Int] Int))
(register inc (λ [Int] Int))
(register dec (λ [Int] Int))
(register neg (λ [Int] Int))
(register mod (λ [Int Int] Int))
(doc to-int "acts as the identity function to implement the interface.")
(sig to-int (Fn [Int] Int))
(defn to-int [a] a)
(doc from-int "acts as the identity function to implement the interface.")
(sig from-int (Fn [Int] Int))
(defn from-int [a] a)
(implements + Int.+)
(implements - Int.-)
(implements * Int.*)
(implements / Int./)
(implements < Int.<)
(implements > Int.>)
(implements = Int.=)
(implements copy Int.copy)
(implements inc Int.inc)
(implements dec Int.dec)
(implements neg Int.neg)
(implements mod Int.mod)
(implements to-int Int.to-int)
(implements from-int Int.from-int)
)
(defmodule Int
(register MAX Int "CARP_INT_MAX")
(register MIN Int "CARP_INT_MIN")
2017-11-29 15:10:35 +03:00
(register bit-shift-left (λ [Int Int] Int))
(register bit-shift-right (λ [Int Int] Int))
(register bit-and (λ [Int Int] Int))
(register bit-or (λ [Int Int] Int))
(register bit-xor (λ [Int Int] Int))
(register bit-not (λ [Int] Int))
2020-07-08 22:11:13 +03:00
(implements bit-shift-left Int.bit-shift-left)
(implements bit-shift-right Int.bit-shift-right)
(implements bit-and Int.bit-and)
(implements bit-or Int.bit-or)
(implements bit-xor Int.bit-xor)
(implements bit-not Int.bit-not)
2017-11-17 14:26:01 +03:00
2018-03-27 10:43:39 +03:00
(doc abs "The absolute value (removes the negative sign) of an Int.")
2017-11-17 14:26:01 +03:00
(register abs (λ [Int] Int))
(implements abs Int.abs)
2017-11-17 14:26:01 +03:00
2017-11-29 17:28:21 +03:00
(defn even? [a] (= (mod a 2) 0))
(defn odd? [a] (not (even? a)))
(defn zero []
0)
(implements zero Int.zero)
(defn add-ref [x y]
(Int.+ @x @y))
2018-06-01 17:40:14 +03:00
;; Move to generic math module?
(defn clamp [min, max, val]
(if (> val max)
max
(if (< val min)
min
val)))
(doc pow "Raise x to the power of y.")
(defn pow [x y]
(let-do [r 1]
(while (/= y 0)
(do
(when (/= (bit-and y 1) 0)
(set! r (* r x)))
(set! y (/ y 2))
(set! x (* x x))))
r))
(doc positive-mod "Like mod but always returns a positive answer.")
(defn positive-mod [k n]
(let [r (Int.mod k n)]
(if (> 0 r)
(+ r n)
r)))
(implements add-ref Int.add-ref)
(implements pow Int.pow)
2018-01-15 18:27:05 +03:00
)
(defmodule IntRef
(defn = [a b]
(Int.= @a @b))
(implements = IntRef.=)
2018-01-15 18:27:05 +03:00
(defn < [a b]
(Int.< @a @b))
(implements < IntRef.<)
2018-01-15 18:27:05 +03:00
(defn > [a b]
(Int.> @a @b))
(implements > IntRef.>)
2017-11-29 17:28:21 +03:00
)