mirror of
https://github.com/carp-lang/Carp.git
synced 2024-10-05 17:47:30 +03:00
5d530b7491
* feat: register MAX and MIN macros for stdint types Adds MAX and MIN for each INT<N> type and MAX for each UINT<N> type. These are macros defined by stdint.h and are sometimes useful for bounds determinations and conversions and such. * feat: make MAX and MIN interfaces Since several numeric types define maximum and minimum values, it makes sense for these to be defined as interfaces. This commit also makes existing definitions of MAX and MIN for Carp's numeric types implement the interfaces. * fix: respect let binding shadowing in memory management (#1413) * fix: respect let binding shadowing in memory management Previously, we didn't account for shadowing in let bindings in our memory management routines. This led to rare situations in which multiple deleters might be added for a single variable name, for example: ```clojure (defn n [xs] (let [xs [1 2 3] n &xs] n)) ``` The borrow checker would fail on this code since it would assign `xs` two deleters, one for the untyped argument and another for the let binding. Instead, we now perform *exclusive* ownership transfer for the duration of the let scope--when a shadow is introduced, the previous deleters for that variable name are dropped until the end of the let scope, since we evaluate all instances of the shadowed name to the more local binding. At the end of the let scope, the original deleter is restored. Fixes issue #597 * refactor: improved dead reference error for let Since let scopes resolve to their bodies, we can report the body of the let as the xobj producing an error when a dead reference is returned. * test: update error message for dead refs in let * test: add regression test for issue #597 Ensure we don't regress and fail to manage memory when let bindings shadow function argument names. * fix: respect symbol modes on interface concretization (#1415) * fix: respect symbol modes on interface concretization When concretizing interfaces (finding the appropriate implementation at a call site) we previously set the lookup mode of all such resolved symbols to CarpLand AFunction. This incorrectly overwrites the lookup mode of Externally registered types, causing them to emit incorrect C when the user specifies an override. We now preserve whatever lookup mode is assigned to the implementation the concretization resolves the interface to. This not only fixes the external override emission issue, but should be more correct in general. fixes #1414 * test: add regression test for issue #1414
117 lines
2.8 KiB
Plaintext
117 lines
2.8 KiB
Plaintext
(system-include "carp_int.h")
|
|
|
|
(doc Int "is the default integral data type.")
|
|
(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")
|
|
(implements MAX MAX)
|
|
(register MIN Int "CARP_INT_MIN")
|
|
(implements MIN MIN)
|
|
(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))
|
|
(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)
|
|
|
|
(doc abs "The absolute value (removes the negative sign) of an Int.")
|
|
(register abs (λ [Int] Int))
|
|
(implements abs Int.abs)
|
|
|
|
(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))
|
|
|
|
;; 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)
|
|
)
|
|
|
|
(defmodule IntRef
|
|
(defn = [a b]
|
|
(Int.= @a @b))
|
|
(implements = IntRef.=)
|
|
|
|
(defn < [a b]
|
|
(Int.< @a @b))
|
|
(implements < IntRef.<)
|
|
|
|
(defn > [a b]
|
|
(Int.> @a @b))
|
|
(implements > IntRef.>)
|
|
)
|