Carp/core/Byte.carp
2021-07-05 14:48:35 +02:00

96 lines
2.3 KiB
Plaintext

(system-include "carp_byte.h")
(doc Byte "is the data type for single bytes. Literals of type `Byte` are
suffixed with `b`.")
(defmodule Byte
(register = (λ [Byte Byte] Bool))
(register copy (λ [&Byte] Byte))
(register + (λ [Byte Byte] Byte))
(register - (λ [Byte Byte] Byte))
(register * (λ [Byte Byte] Byte))
(register / (λ [Byte Byte] Byte))
(register < (λ [Byte Byte] Bool))
(register > (λ [Byte Byte] Bool))
(register mod (λ [Byte Byte] Byte))
(register inc (λ [Byte] Byte))
(register dec (λ [Byte] Byte))
(register to-int (λ [Byte] Int))
(register from-int (λ [Int] Byte))
(defn zero [] 0b)
(implements = Byte.=)
(implements copy Byte.copy)
(implements + Byte.+)
(implements - Byte.-)
(implements * Byte.*)
(implements / Byte./)
(implements < Byte.<)
(implements > Byte.>)
(implements mod Byte.mod)
(implements inc Byte.inc)
(implements dec Byte.dec)
(implements zero Byte.zero)
(implements to-int Byte.to-int)
(implements from-int Byte.from-int)
)
(defmodule Byte
(register bit-shift-left (λ [Byte Byte] Byte))
(register bit-shift-right (λ [Byte Byte] Byte))
(register bit-and (λ [Byte Byte] Byte))
(register bit-or (λ [Byte Byte] Byte))
(register bit-xor (λ [Byte Byte] Byte))
(register bit-not (λ [Byte] Byte))
(implements bit-shift-left Byte.bit-shift-left)
(implements bit-shift-right Byte.bit-shift-right)
(implements bit-and Byte.bit-and)
(implements bit-or Byte.bit-or)
(implements bit-xor Byte.bit-xor)
(implements bit-not Byte.bit-not)
(defn even? [a] (= (mod a 2b) 0b))
(defn odd? [a] (not (even? a)))
(defn add-ref [x y]
(Byte.+ @x @y))
;; 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 1b]
(while (/= y 0b)
(do
(when (/= (bit-and y 1b) 0b)
(set! r (* r x)))
(set! y (/ y 2b))
(set! x (* x x))))
r))
(implements pow Byte.pow)
(implements add-ref Byte.add-ref)
)
(defmodule ByteRef
(defn = [a b]
(Byte.= @a @b))
(defn < [a b]
(Byte.< @a @b))
(defn > [a b]
(Byte.> @a @b))
(implements = ByteRef.=)
(implements < ByteRef.<)
(implements > ByteRef.>)
)