Carp/core/Long.carp
scottolsen 040e9e4391 Add an implements primitive, update core
This change adds a new primitive Implements which changes interface
implementations from being implicit to being explicit. Going forward,
users will have to declare (implements <interface> <implementation>) to
explicitly add a function to an interface. This provides two benefits:

- Prevents unwitting name clashes. Previously, if one defined a function
  that happened to have the same name as an interface, it was
  automatically assumed the function implemented that interface when this
  is not always the intention, especially in large programs.
- Name flexibility. One can now implement an interface with a function
  that has a different name than the interface, which allows for greater
  flexibility.

I've updated core to make the necessary calls to the new primitive.

Since str and copy are derived automatically for types, we treat these
functions as a special case and auto-implement the interfaces.
2020-05-09 12:59:47 -04:00

72 lines
1.9 KiB
Plaintext

(system-include "carp_long.h")
(defmodule Long
(register MAX Long "LONG_MAX")
(register MIN Long "LONG_MIN")
(register + (λ [Long Long] Long))
(register - (λ [Long Long] Long))
(register * (λ [Long Long] Long))
(register / (λ [Long Long] Long))
(register < (λ [Long Long] Bool))
(register > (λ [Long Long] Bool))
(register = (λ [Long Long] Bool))
(register neg (λ [Long] Long))
(register mod (λ [Long Long] Long))
(register seed (λ [Long] ()))
(register bit-shift-left (λ [Long Long] Long))
(register bit-shift-right (λ [Long Long] Long))
(register bit-and (λ [Long Long] Long))
(register bit-or (λ [Long Long] Long))
(register bit-xor (λ [Long Long] Long))
(register bit-not (λ [Long] Long))
(register inc (λ [Long] Long))
(register dec (λ [Long] Long))
(register to-int (λ [Long] Int))
(register from-int (λ [Int] Long))
(todo copy "Should not be needed when refs to value types are auto-converted
to non-refs.")
(register copy (λ [&Long] Long))
(implements + Long.+)
(implements - Long.-)
(implements * Long.*)
(implements / Long./)
(implements < Long.<)
(implements > Long.>)
(implements = Long.=)
(implements copy Long.copy)
(implements inc Long.inc)
(implements dec Long.dec)
(implements neg Long.neg)
(implements mod Long.mod)
(implements to-int Long.to-int)
(implements from-int Long.from-int)
(not-on-windows ; this seems to generate invalid code on some windows machines
(register safe-add (λ [Long Long (Ref Long)] Bool))
(register safe-sub (λ [Long Long (Ref Long)] Bool))
(register safe-mul (λ [Long Long (Ref Long)] Bool))
)
(register abs (λ [Long] Long))
(defn even? [a] (= (mod a 2l) 0l))
(defn odd? [a] (not (even? a)))
(defn zero [] 0l)
(implements abs Long.abs)
(implements zero Long.zero)
)
(defmodule LongRef
(defn = [a b]
(Long.= @a @b))
(defn < [a b]
(Long.< @a @b))
(defn > [a b]
(Long.> @a @b))
)