diff --git a/core/Byte.carp b/core/Byte.carp new file mode 100644 index 00000000..0bef1083 --- /dev/null +++ b/core/Byte.carp @@ -0,0 +1,61 @@ +(system-include "carp_byte.h") + +(defmodule 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 = (λ [Byte Byte] Bool)) + (register mod (λ [Byte Byte] 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)) + (register inc (λ [Byte] Byte)) + (register dec (λ [Byte] Byte)) + (register copy (λ [&Byte] Byte)) + (register to-int (λ [Byte] Int)) + (register from-int (λ [Int] Byte)) + + (defn even? [a] (= (mod a 2b) 0b)) + (defn odd? [a] (not (even? a))) + + (defn zero [] 0b) + + (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)) +) + +(defmodule ByteRef + (defn = [a b] + (Byte.= @a @b)) + + (defn < [a b] + (Byte.< @a @b)) + + (defn > [a b] + (Byte.> @a @b)) +) diff --git a/core/Core.carp b/core/Core.carp index 70c568dc..3999b72f 100644 --- a/core/Core.carp +++ b/core/Core.carp @@ -23,6 +23,7 @@ (load "Result.carp") (load "Dynamic.carp") (load "Format.carp") +(load "Byte.carp") (load "Int.carp") (load "Long.carp") (load "Double.carp") diff --git a/core/String.carp b/core/String.carp index 99586106..28a8721e 100644 --- a/core/String.carp +++ b/core/String.carp @@ -179,6 +179,12 @@ (register from-string (λ [&String] Int)) ) +(defmodule Byte + (register str (Fn [Byte] String)) + (register format (Fn [&String Byte] String)) + (register from-string (λ [&String] Byte)) +) + (defmodule Float (register str (Fn [Float] String)) (register format (Fn [&String Float] String)) diff --git a/core/carp_byte.h b/core/carp_byte.h new file mode 100644 index 00000000..2983e155 --- /dev/null +++ b/core/carp_byte.h @@ -0,0 +1,36 @@ +typedef uint8_t byte; + +uint8_t Byte__PLUS_(uint8_t x, uint8_t y) { return x + y; } +uint8_t Byte__MINUS_(uint8_t x, uint8_t y) { return x - y; } +uint8_t Byte__MUL_(uint8_t x, uint8_t y) { return x * y; } +uint8_t Byte__DIV_(uint8_t x, uint8_t y) { return x / y; } +bool Byte__EQ_(uint8_t x, uint8_t y) { return x == y; } +bool Byte__LT_(uint8_t x, uint8_t y) { return x < y; } +bool Byte__GT_(uint8_t x, uint8_t y) { return x > y; } + +uint8_t Byte_inc(uint8_t x) { return x + 1; } +uint8_t Byte_dec(uint8_t x) { return x - 1; } +uint8_t Byte_bit_MINUS_shift_MINUS_left(uint8_t x, uint8_t y) { return x << y; } +uint8_t Byte_bit_MINUS_shift_MINUS_right(uint8_t x, uint8_t y) { return x >> y; } +uint8_t Byte_bit_MINUS_and(uint8_t x, uint8_t y) { return x & y; } +uint8_t Byte_bit_MINUS_or(uint8_t x, uint8_t y) { return x | y; } +uint8_t Byte_bit_MINUS_xor(uint8_t x, uint8_t y) { return x ^ y; } +uint8_t Byte_bit_MINUS_not(uint8_t x) { return ~x; } + +uint8_t Byte_copy(const uint8_t *x) { return *x; } + +uint8_t Byte_mod(uint8_t x, uint8_t divider) { + return x % divider; +} + +bool Byte_mask(uint8_t a, uint8_t b) { + return a & b; +} + +int Byte_to_MINUS_int(uint8_t a) { + return a; +} + +uint8_t Byte_from_MINUS_int(int a) { + return a; +} diff --git a/core/carp_string.h b/core/carp_string.h index 8dacd503..c179e70b 100644 --- a/core/carp_string.h +++ b/core/carp_string.h @@ -243,6 +243,24 @@ long Long_from_MINUS_string(const String *s) { return atol(*s); } +String Byte_str(uint8_t x) { + int size = snprintf(NULL, 0, "%ub", x)+1; + String buffer = CARP_MALLOC(size); + snprintf(buffer, size, "%ub", x); + return buffer; +} + +String Byte_format(const String* str, uint8_t x) { + int size = snprintf(NULL, 0, *str, x)+1; + String buffer = CARP_MALLOC(size); + snprintf(buffer, size, *str, x); + return buffer; +} + +uint8_t Byte_from_MINUS_string(const String *s) { + return atoi(*s); +} + int String_index_MINUS_of_MINUS_from(const String *s, char c, int i) { /* Return index of first occurrence of `c` in `s` AFTER index i * Returns -1 if not found diff --git a/docs/core/Array.html b/docs/core/Array.html index fe46b903..a48b49d3 100644 --- a/docs/core/Array.html +++ b/docs/core/Array.html @@ -27,6 +27,11 @@ Int +
  • + + Byte + +
  • Long diff --git a/docs/core/Bench.html b/docs/core/Bench.html index 2d24152a..7d5b55ed 100644 --- a/docs/core/Bench.html +++ b/docs/core/Bench.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Bool.html b/docs/core/Bool.html index fcc299a1..87c01278 100644 --- a/docs/core/Bool.html +++ b/docs/core/Bool.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Byte.html b/docs/core/Byte.html new file mode 100644 index 00000000..4aa584bc --- /dev/null +++ b/docs/core/Byte.html @@ -0,0 +1,684 @@ + + + + + + + + + +
    + +

    + Byte +

    +
    + +
    +
    + +

    + * +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + + +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + - +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + / +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + < +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Bool) +

    + + + +

    + +

    +
    +
    + +

    + = +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Bool) +

    + + + +

    + +

    +
    +
    + +

    + > +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Bool) +

    + + + +

    + +

    +
    +
    + +

    + add-ref +

    +
    +
    + defn +
    +

    + (λ [&Byte, &Byte] Byte) +

    +
    +                    (add-ref x y)
    +                
    +

    + +

    +
    +
    + +

    + bit-and +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + bit-not +

    +
    +
    + external +
    +

    + (λ [Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + bit-or +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + bit-shift-left +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + bit-shift-right +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + bit-xor +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + clamp +

    +
    +
    + defn +
    +

    + (λ [a, a, a] a) +

    +
    +                    (clamp min max val)
    +                
    +

    + +

    +
    +
    + +

    + copy +

    +
    +
    + external +
    +

    + (λ [&Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + dec +

    +
    +
    + external +
    +

    + (λ [Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + even? +

    +
    +
    + defn +
    +

    + (λ [Byte] Bool) +

    +
    +                    (even? a)
    +                
    +

    + +

    +
    +
    + +

    + format +

    +
    +
    + external +
    +

    + (λ [&String, Byte] String) +

    + + + +

    + +

    +
    +
    + +

    + from-int +

    +
    +
    + external +
    +

    + (λ [Int] Byte) +

    + + + +

    + +

    +
    +
    + +

    + from-string +

    +
    +
    + external +
    +

    + (λ [&String] Byte) +

    + + + +

    + +

    +
    +
    + +

    + inc +

    +
    +
    + external +
    +

    + (λ [Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + mod +

    +
    +
    + external +
    +

    + (λ [Byte, Byte] Byte) +

    + + + +

    + +

    +
    +
    + +

    + odd? +

    +
    +
    + defn +
    +

    + (λ [Byte] Bool) +

    +
    +                    (odd? a)
    +                
    +

    + +

    +
    +
    + +

    + pow +

    +
    +
    + defn +
    +

    + (λ [Byte, Byte] Byte) +

    +
    +                    (pow x y)
    +                
    +

    +

    Raise x to the power of y.

    + +

    +
    +
    + +

    + str +

    +
    +
    + external +
    +

    + (λ [Byte] String) +

    + + + +

    + +

    +
    +
    + +

    + to-int +

    +
    +
    + external +
    +

    + (λ [Byte] Int) +

    + + + +

    + +

    +
    +
    + +

    + zero +

    +
    +
    + defn +
    +

    + (λ [] Byte) +

    +
    +                    (zero)
    +                
    +

    + +

    +
    +
    + + diff --git a/docs/core/Char.html b/docs/core/Char.html index b7bec017..ef4edefa 100644 --- a/docs/core/Char.html +++ b/docs/core/Char.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Debug.html b/docs/core/Debug.html index 63660867..fed53b62 100644 --- a/docs/core/Debug.html +++ b/docs/core/Debug.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Double.html b/docs/core/Double.html index 625cc8da..d7bfb31c 100644 --- a/docs/core/Double.html +++ b/docs/core/Double.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Dynamic.html b/docs/core/Dynamic.html index ce5db781..3b872e63 100644 --- a/docs/core/Dynamic.html +++ b/docs/core/Dynamic.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Float.html b/docs/core/Float.html index ff8223f8..75ed5333 100644 --- a/docs/core/Float.html +++ b/docs/core/Float.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Geometry.html b/docs/core/Geometry.html index 5dcb3087..b9b0dc2c 100644 --- a/docs/core/Geometry.html +++ b/docs/core/Geometry.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/IO.html b/docs/core/IO.html index 47ee808c..25e6bc5c 100644 --- a/docs/core/IO.html +++ b/docs/core/IO.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Int.html b/docs/core/Int.html index bae186b0..22fdff15 100644 --- a/docs/core/Int.html +++ b/docs/core/Int.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Long.html b/docs/core/Long.html index 998d223d..c3045f49 100644 --- a/docs/core/Long.html +++ b/docs/core/Long.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Map.html b/docs/core/Map.html index 453bc34b..be4c57d8 100644 --- a/docs/core/Map.html +++ b/docs/core/Map.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Maybe.html b/docs/core/Maybe.html index 64117c74..2b7b75bc 100644 --- a/docs/core/Maybe.html +++ b/docs/core/Maybe.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Pattern.html b/docs/core/Pattern.html index e5cdd747..db7fbcbf 100644 --- a/docs/core/Pattern.html +++ b/docs/core/Pattern.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Result.html b/docs/core/Result.html index 5ce77649..64d8eaf1 100644 --- a/docs/core/Result.html +++ b/docs/core/Result.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Statistics.html b/docs/core/Statistics.html index abff6b14..e01db517 100644 --- a/docs/core/Statistics.html +++ b/docs/core/Statistics.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/String.html b/docs/core/String.html index f358d196..4283d33e 100644 --- a/docs/core/String.html +++ b/docs/core/String.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/System.html b/docs/core/System.html index 6f0759f6..3003afd4 100644 --- a/docs/core/System.html +++ b/docs/core/System.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Test.html b/docs/core/Test.html index aa7fd703..981c6f25 100644 --- a/docs/core/Test.html +++ b/docs/core/Test.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Vector2.html b/docs/core/Vector2.html index ec28dc5f..0025f060 100644 --- a/docs/core/Vector2.html +++ b/docs/core/Vector2.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/Vector3.html b/docs/core/Vector3.html index 92ee04ee..98df1c6b 100644 --- a/docs/core/Vector3.html +++ b/docs/core/Vector3.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/VectorN.html b/docs/core/VectorN.html index 33649595..04b9bfd2 100644 --- a/docs/core/VectorN.html +++ b/docs/core/VectorN.html @@ -27,6 +27,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/core_index.html b/docs/core/core_index.html index 586fd835..a3996742 100644 --- a/docs/core/core_index.html +++ b/docs/core/core_index.html @@ -23,6 +23,11 @@ Int
  • +
  • + + Byte + +
  • Long diff --git a/docs/core/generate_core_docs.carp b/docs/core/generate_core_docs.carp index f4de354c..31a372e9 100644 --- a/docs/core/generate_core_docs.carp +++ b/docs/core/generate_core_docs.carp @@ -13,6 +13,7 @@ (save-docs Dynamic Int + Byte Long Bool Float diff --git a/src/Emit.hs b/src/Emit.hs index 1c93ea12..3ac1c8f5 100644 --- a/src/Emit.hs +++ b/src/Emit.hs @@ -98,6 +98,7 @@ toC toCMode (Binder meta root) = emitterSrc (execState (visit startingIndent roo Arr _ -> visitArray indent xobj Num IntTy num -> return (show (round num :: Int)) Num LongTy num -> return (show (round num :: Int) ++ "l") + Num ByteTy num -> return (show (round num :: Int)) Num FloatTy num -> return (show num ++ "f") Num DoubleTy num -> return (show num) Num _ _ -> error "Can't emit invalid number type." diff --git a/src/Obj.hs b/src/Obj.hs index f751a00e..12ffa02b 100644 --- a/src/Obj.hs +++ b/src/Obj.hs @@ -257,6 +257,7 @@ pretty = visit 0 Dict dict -> "{" ++ joinWithSpace (map (visit indent) (concatMap (\(a, b) -> [a, b]) (Map.toList dict))) ++ "}" Num IntTy num -> show (round num :: Int) Num LongTy num -> show num ++ "l" + Num ByteTy num -> show num Num FloatTy num -> show num ++ "f" Num DoubleTy num -> show num Num _ _ -> error "Invalid number type." @@ -600,6 +601,7 @@ xobjToTy (XObj (Sym (SymPath _ "Int") _) _ _) = Just IntTy xobjToTy (XObj (Sym (SymPath _ "Float") _) _ _) = Just FloatTy xobjToTy (XObj (Sym (SymPath _ "Double") _) _ _) = Just DoubleTy xobjToTy (XObj (Sym (SymPath _ "Long") _) _ _) = Just LongTy +xobjToTy (XObj (Sym (SymPath _ "Byte") _) _ _) = Just ByteTy xobjToTy (XObj (Sym (SymPath _ "String") _) _ _) = Just StringTy xobjToTy (XObj (Sym (SymPath _ "Pattern") _) _ _) = Just PatternTy xobjToTy (XObj (Sym (SymPath _ "Char") _) _ _) = Just CharTy diff --git a/src/Parsing.hs b/src/Parsing.hs index 3a289c58..0268bac6 100644 --- a/src/Parsing.hs +++ b/src/Parsing.hs @@ -58,6 +58,12 @@ integer :: Parsec.Parsec String ParseState XObj integer = do (i, num) <- maybeSigned return (XObj (Num IntTy (read num)) i Nothing) +byte :: Parsec.Parsec String ParseState XObj +byte = do (i, num) <- maybeSigned + _ <- Parsec.char 'b' + incColumn 1 + return (XObj (Num ByteTy (read num)) i Nothing) + long :: Parsec.Parsec String ParseState XObj long = do (i, num) <- maybeSigned _ <- Parsec.char 'l' @@ -67,6 +73,7 @@ long = do (i, num) <- maybeSigned number :: Parsec.Parsec String ParseState XObj number = Parsec.try float <|> Parsec.try floatNoPeriod <|> + Parsec.try byte <|> Parsec.try double <|> Parsec.try long <|> Parsec.try integer diff --git a/src/Types.hs b/src/Types.hs index 245b845d..e7a56010 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -27,6 +27,7 @@ import Util -- | Carp types. data Ty = IntTy | LongTy + | ByteTy | BoolTy | FloatTy | DoubleTy @@ -60,6 +61,7 @@ instance Show Ty where show FloatTy = "Float" show DoubleTy = "Double" show LongTy = "Long" + show ByteTy = "Byte" show BoolTy = "Bool" show StringTy = "String" show PatternTy = "Pattern" @@ -107,6 +109,7 @@ tyToCManglePtr _ BoolTy = "bool" tyToCManglePtr _ FloatTy = "float" tyToCManglePtr _ DoubleTy = "double" tyToCManglePtr _ LongTy = "long" +tyToCManglePtr _ ByteTy = "uint8_t" tyToCManglePtr _ StringTy = "String" tyToCManglePtr _ PatternTy = "Pattern" tyToCManglePtr _ CharTy = "char" diff --git a/test/byte_math.carp b/test/byte_math.carp new file mode 100644 index 00000000..b32a54b2 --- /dev/null +++ b/test/byte_math.carp @@ -0,0 +1,50 @@ +(load "Test.carp") + +(use-all Byte Test) + +(deftest test + (assert-equal test + 1b + (min 1b 2b) + "min works as expected") + (assert-equal test + 2b + (max 1b 2b) + "max works as expected") + (assert-equal test + false + (even? 3b) + "even? works as expected") + (assert-equal test + true + (odd? 3b) + "odd? works as expected") + (assert-equal test + 1b + (bit-and 3b 5b) + "bit-and works as expected") + (assert-equal test + 5b + (bit-or 1b 4b) + "bit-or works as expected") + (assert-equal test + 4b + (bit-xor 1b 5b) + "bit-xor works as expected") + (assert-equal test + 1b + (bit-not 254b) + "bit-not works as expected") + (assert-equal test + 8b + (bit-shift-left 2b 2b) + "bit-shift-left works as expected") + (assert-equal test + 2b + (bit-shift-right 16b 3b) + "bit-shift-right works as expected") + (assert-equal test + 1 + (/ 3 2) + "integer division truncates as expected") +)