Carp/core/Core.carp
Scott Olsen 380945bf32
feat: add box type (#1358)
* feat: add box templates and box type

This commit adds an implementation of Boxes, memory manged heap
allocated values.

Boxes are implemented as C pointers, with no additional structure but
are treated as structs in Carp. To facilitate this, we need to add them
as a clause to our special type emissions (TypesToC) as they'd otherwise
be emitted like other struct types.

Co-authored-by: Veit Heller <veit@veitheller.de>

* fix: slight memory management fix for Box

Make sure we free the box!

* test: add tests for box (including memory checks)

* Revert "fix: Ignore clang nitpick"

This reverts commit 70ec6d46d4.

* fix: update example/functor.carp

Now that a builtin type named Box exists, the definitions in this file
cause a conflict. I've renamed the "Box" type in the functor example to
remove the conflict.

* feat: add Box.peek

Box.peek allows users to transform a reference to a box into a a
reference to the box's contained value. The returned reference will have
the same lifetime as the box. This function allows callers to manipulate
the value in a box without re-allocation, for example:

```clojure
(deftype Num [val Int])

(let-do [box (Box.init (Num.init 0))]
  (Num.set-val! (Box.peek &box) 1)
  @(Num.val (Box.peek &box)))
```

This commit also includes tests for Box.peek.

Co-authored-by: TimDeve <TimDeve@users.noreply.github.com>

Co-authored-by: Veit Heller <veit@veitheller.de>
Co-authored-by: Erik Svedäng <erik@coherence.io>
Co-authored-by: TimDeve <TimDeve@users.noreply.github.com>
2021-11-30 10:35:22 +01:00

69 lines
1.7 KiB
Plaintext

(system-include "assert.h")
(system-include "stddef.h")
(system-include "stdlib.h")
(system-include "stdint.h")
(system-include "inttypes.h")
(system-include "float.h")
(system-include "limits.h")
(system-include "string.h")
(system-include "math.h")
(system-include "ctype.h")
(system-include "stdarg.h")
(system-include "stdio.h")
(system-include "time.h")
(system-include "carp_stdbool.h")
(system-include "core.h")
(system-include "carp_memory.h")
(load-once "Interfaces.carp")
(load-once "Blitable.carp")
(load-once "Bool.carp")
(load-once "Box.carp")
(load-once "Macros.carp")
(load-once "BoolExtras.carp")
(load-once "List.carp")
(load-once "Derive.carp")
(load-once "Gensym.carp")
(load-once "ControlMacros.carp")
(load-once "Project.carp")
(load-once "Platform.carp")
(load-once "Introspect.carp")
(load-once "Quasiquote.carp")
(load-once "Pointer.carp")
(load-once "Unsafe.carp")
(load-once "Function.carp")
(load-once "Generics.carp")
(load-once "Maybe.carp")
(load-once "Result.carp")
(load-once "Dynamic.carp")
(load-once "Format.carp")
(load-once "Byte.carp")
(load-once "Int.carp")
(load-once "Long.carp")
(load-once "Double.carp")
(load-once "Float.carp")
(load-once "Tuples.carp")
(load-once "Array.carp")
(load-once "StaticArray.carp")
(load-once "StdInt.carp")
(load-once "Char.carp")
(load-once "String.carp")
(load-once "ArrayExt.carp")
(load-once "System.carp")
(load-once "IO.carp")
(load-once "Pattern.carp")
(load-once "Debug.carp")
(load-once "Random.carp")
(load-once "Map.carp")
(load-once "Heap.carp")
(load-once "Sort.carp")
(load-once "Binary.carp")
(load-once "Control.carp")
(load-once "Opaque.carp")
(load-once "Unit.carp")
(posix-only
(system-include "sys/wait.h")
(system-include "unistd.h")
(system-include "signal.h"))