diff --git a/core/Core.carp b/core/Core.carp index 70c568dc..f1cea229 100644 --- a/core/Core.carp +++ b/core/Core.carp @@ -35,6 +35,7 @@ (load "System.carp") (load "Pattern.carp") (load "Debug.carp") +(load "Pointer.carp") (load "Format.carp") (load "Random.carp") (load "Map.carp") diff --git a/core/Pointer.carp b/core/Pointer.carp new file mode 100644 index 00000000..e175ba03 --- /dev/null +++ b/core/Pointer.carp @@ -0,0 +1,4 @@ +(defmodule Pointer + (defn inc [a] (Pointer.add a 1l)) + (defn dec [a] (Pointer.sub a 1l)) +) diff --git a/docs/core/Array.html b/docs/core/Array.html index fe46b903..99a68fe9 100644 --- a/docs/core/Array.html +++ b/docs/core/Array.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Bench.html b/docs/core/Bench.html index 2d24152a..4d07531b 100644 --- a/docs/core/Bench.html +++ b/docs/core/Bench.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Bool.html b/docs/core/Bool.html index fcc299a1..5fe762af 100644 --- a/docs/core/Bool.html +++ b/docs/core/Bool.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Char.html b/docs/core/Char.html index b7bec017..bbbcd091 100644 --- a/docs/core/Char.html +++ b/docs/core/Char.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Debug.html b/docs/core/Debug.html index 63660867..5fbbc657 100644 --- a/docs/core/Debug.html +++ b/docs/core/Debug.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Double.html b/docs/core/Double.html index 625cc8da..2883d672 100644 --- a/docs/core/Double.html +++ b/docs/core/Double.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Dynamic.html b/docs/core/Dynamic.html index ce5db781..149c8db3 100644 --- a/docs/core/Dynamic.html +++ b/docs/core/Dynamic.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Float.html b/docs/core/Float.html index ff8223f8..f492e660 100644 --- a/docs/core/Float.html +++ b/docs/core/Float.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Geometry.html b/docs/core/Geometry.html index 5dcb3087..7590d76e 100644 --- a/docs/core/Geometry.html +++ b/docs/core/Geometry.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/IO.html b/docs/core/IO.html index 47ee808c..bb4e3aa6 100644 --- a/docs/core/IO.html +++ b/docs/core/IO.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Int.html b/docs/core/Int.html index bae186b0..ddc3a0d8 100644 --- a/docs/core/Int.html +++ b/docs/core/Int.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Long.html b/docs/core/Long.html index 998d223d..e9e459a8 100644 --- a/docs/core/Long.html +++ b/docs/core/Long.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Map.html b/docs/core/Map.html index 453bc34b..0236bd6f 100644 --- a/docs/core/Map.html +++ b/docs/core/Map.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Maybe.html b/docs/core/Maybe.html index 64117c74..8bf8411b 100644 --- a/docs/core/Maybe.html +++ b/docs/core/Maybe.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Pattern.html b/docs/core/Pattern.html index e5cdd747..b0b0c44b 100644 --- a/docs/core/Pattern.html +++ b/docs/core/Pattern.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Pointer.html b/docs/core/Pointer.html new file mode 100644 index 00000000..24e3c12e --- /dev/null +++ b/docs/core/Pointer.html @@ -0,0 +1,349 @@ + + + + + + + + + +
    + +

    + Pointer +

    +
    + +
    +
    + +

    + add +

    +
    +
    + template +
    +

    + (λ [(Ptr p), Long] (Ptr p)) +

    + + + +

    +

    adds a long integer value to a pointer.

    + +

    +
    +
    + +

    + copy +

    +
    +
    + template +
    +

    + (λ [(Ref (Ptr p))] (Ptr p)) +

    + + + +

    +

    copies a pointer p.

    + +

    +
    +
    + +

    + dec +

    +
    +
    + defn +
    +

    + (λ [(Ptr a)] (Ptr a)) +

    +
    +                    (dec a)
    +                
    +

    + +

    +
    +
    + +

    + eq +

    +
    +
    + template +
    +

    + (λ [(Ptr p), (Ptr p)] Bool) +

    + + + +

    +

    checks two pointers for equality.

    + +

    +
    +
    + +

    + from-long +

    +
    +
    + template +
    +

    + (λ [Long] (Ptr p)) +

    + + + +

    +

    converts a long integer to a pointer.

    + +

    +
    +
    + +

    + inc +

    +
    +
    + defn +
    +

    + (λ [(Ptr a)] (Ptr a)) +

    +
    +                    (inc a)
    +                
    +

    + +

    +
    +
    + +

    + sub +

    +
    +
    + template +
    +

    + (λ [(Ptr p), Long] (Ptr p)) +

    + + + +

    +

    subtracts a long integer value from a pointer.

    + +

    +
    +
    + +

    + to-long +

    +
    +
    + template +
    +

    + (λ [(Ptr p)] Long) +

    + + + +

    +

    converts a pointer to a long integer.

    + +

    +
    +
    + +

    + to-ref +

    +
    +
    + template +
    +

    + (λ [(Ptr p)] &p) +

    + + + +

    +

    converts a pointer to a reference type. The user will have to ensure themselves that this is a safe operation.

    + +

    +
    +
    + +

    + width +

    +
    +
    + template +
    +

    + (λ [(Ptr p)] Long) +

    + + + +

    +

    gets the byte size of a pointer.

    + +

    +
    +
    + + diff --git a/docs/core/Result.html b/docs/core/Result.html index 5ce77649..6ad741f4 100644 --- a/docs/core/Result.html +++ b/docs/core/Result.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Statistics.html b/docs/core/Statistics.html index abff6b14..43dc1f77 100644 --- a/docs/core/Statistics.html +++ b/docs/core/Statistics.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/String.html b/docs/core/String.html index f358d196..f39a4e76 100644 --- a/docs/core/String.html +++ b/docs/core/String.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/System.html b/docs/core/System.html index 6f0759f6..ae90b9a5 100644 --- a/docs/core/System.html +++ b/docs/core/System.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Test.html b/docs/core/Test.html index aa7fd703..2a87a300 100644 --- a/docs/core/Test.html +++ b/docs/core/Test.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Vector2.html b/docs/core/Vector2.html index ec28dc5f..f5877015 100644 --- a/docs/core/Vector2.html +++ b/docs/core/Vector2.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/Vector3.html b/docs/core/Vector3.html index 92ee04ee..7b43d264 100644 --- a/docs/core/Vector3.html +++ b/docs/core/Vector3.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/VectorN.html b/docs/core/VectorN.html index 33649595..b6b1b9f3 100644 --- a/docs/core/VectorN.html +++ b/docs/core/VectorN.html @@ -132,6 +132,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/core_index.html b/docs/core/core_index.html index 586fd835..de1bd1e4 100644 --- a/docs/core/core_index.html +++ b/docs/core/core_index.html @@ -128,6 +128,11 @@ Result +
  • + + Pointer + +
  • diff --git a/docs/core/generate_core_docs.carp b/docs/core/generate_core_docs.carp index f4de354c..56c6d24c 100644 --- a/docs/core/generate_core_docs.carp +++ b/docs/core/generate_core_docs.carp @@ -34,6 +34,7 @@ Map Maybe Result + Pointer ) (quit) diff --git a/src/StartingEnv.hs b/src/StartingEnv.hs index 8038ad91..6f14abd9 100644 --- a/src/StartingEnv.hs +++ b/src/StartingEnv.hs @@ -52,7 +52,15 @@ pointerModule = Env { envBindings = bindings , envUseModules = [] , envMode = ExternalEnv , envFunctionNestingLevel = 0 } - where bindings = Map.fromList [ templatePointerCopy, templatePointerEqual, templatePointerToRef ] + where bindings = Map.fromList [ templatePointerCopy + , templatePointerEqual + , templatePointerToRef + , templatePointerAdd + , templatePointerSub + , templatePointerWidth + , templatePointerToLong + , templatePointerFromLong + ] -- | A template function for copying (= deref:ing) any pointer. templatePointerCopy :: (String, Binder) @@ -87,6 +95,56 @@ templatePointerToRef = defineTemplate ,"}"]) (const []) +templatePointerAdd = defineTemplate + (SymPath ["Pointer"] "add") + (FuncTy [PointerTy (VarTy "p"), LongTy] (PointerTy (VarTy "p"))) + "adds a long integer value to a pointer." + (toTemplate "$p* $NAME ($p *p, long x)") + (toTemplate $ unlines ["$DECL {" + ," return p + x;" + ,"}"]) + (const []) + +templatePointerSub = defineTemplate + (SymPath ["Pointer"] "sub") + (FuncTy [PointerTy (VarTy "p"), LongTy] (PointerTy (VarTy "p"))) + "subtracts a long integer value from a pointer." + (toTemplate "$p* $NAME ($p *p, long x)") + (toTemplate $ unlines ["$DECL {" + ," return p - x;" + ,"}"]) + (const []) + +templatePointerWidth = defineTemplate + (SymPath ["Pointer"] "width") + (FuncTy [PointerTy (VarTy "p")] LongTy) + "gets the byte size of a pointer." + (toTemplate "long $NAME ($p *p)") + (toTemplate $ unlines ["$DECL {" + ," return sizeof(*p);" + ,"}"]) + (const []) + +templatePointerToLong = defineTemplate + (SymPath ["Pointer"] "to-long") + (FuncTy [PointerTy (VarTy "p")] LongTy) + "converts a pointer to a long integer." + (toTemplate "long $NAME ($p *p)") + (toTemplate $ unlines ["$DECL {" + ," return (long)p;" + ,"}"]) + (const []) + +templatePointerFromLong = defineTemplate + (SymPath ["Pointer"] "from-long") + (FuncTy [LongTy] (PointerTy (VarTy "p"))) + "converts a long integer to a pointer." + (toTemplate "$p* $NAME (long p)") + (toTemplate $ unlines ["$DECL {" + ," return ($p*)p;" + ,"}"]) + (const []) + -- | The System module contains functions for various OS related things like timing and process control. systemModule :: Env systemModule = Env { envBindings = bindings @@ -97,6 +155,7 @@ systemModule = Env { envBindings = bindings , envFunctionNestingLevel = 0 } where bindings = Map.fromList [ templateExit ] + -- | A template function for exiting. templateExit :: (String, Binder) templateExit = defineTemplate diff --git a/test/pointer.carp b/test/pointer.carp new file mode 100644 index 00000000..e9df82f3 --- /dev/null +++ b/test/pointer.carp @@ -0,0 +1,38 @@ +(load "Test.carp") + +(use-all Test Pointer) + +; we go to the middle of a chunk of 10 safe elements +(def x (add (Array.raw (the (Array Int) (Array.allocate 10))) 5l)) +(def xa (to-long x)) +(def w (width x)) + +; these tests are sadly a little unsafe +(deftest test + (assert-equal test + 1l + ; we assume that the width of a char is 1 + (width (Array.raw (the (Array Char) []))) + "Pointer.width works as expected" + ) + (assert-equal test + (+ xa (* 3l w)) + (to-long (add x 3l)) + "Pointer.add works as expected" + ) + (assert-equal test + (- xa (* 3l w)) + (to-long (sub x 3l)) + "Pointer.sub works as expected" + ) + (assert-equal test + (+ xa w) + (to-long (inc x)) + "Pointer.inc works as expected" + ) + (assert-equal test + (- xa w) + (to-long (dec x)) + "Pointer.dec works as expected" + ) +)