Commit Graph

1089 Commits

Author SHA1 Message Date
Veit Heller
ca5774b1ae
chore: remove usage of sprintf (#1453) 2023-01-31 20:47:15 +01:00
Veit Heller
7ab466edeb
docs: add clarification to SDL comment (#1454) 2023-01-31 20:46:11 +01:00
Scott Olsen
8c5845ea65
docs: cleanup core IO docs (#1447) 2022-12-28 08:16:04 +01:00
Veit Heller
0a7a83543d
fix: quasiquoting breaks List.pairs (#1431) 2022-08-30 15:40:13 +02:00
Veit Heller
e4adef0c97
feat: add bag data structure (#1429)
* feat: add bag data structure

* refactor: incorporate feedback by scolsen
2022-08-29 09:56:58 +02:00
Scott Olsen
9d9f982ce8
feat: add fputc wrapper to IO (#1417)
* feat: add fputc wrapper to IO

fputc is a low-level function that writes a single C character to a
file. We can use this as a basis for more elegant APIs.

The signature matches that of the C standard library function for
compatibility, which takes an Int instead of a char. However, the
documentation notes that the int argument is converted to an unsigned
char.

* doc: fix typos for fgetc and fputc docs
2022-04-19 16:29:26 +02:00
Scott Olsen
5d530b7491
feat: register MAX and MIN macros for stdint types (#1412)
* 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
2022-04-13 09:32:46 +02:00
Tim Dévé
d7ad0b2629
feat: Adds Dynamic.sort & improves output of failing dynamic tests (#1411)
* feat: Adds Dynamic.sort

* test: Adds tests for Dynamic.sort

* refactor: Makes dynamic test handler display diff

of expected vs actual instead of displaying "true" : "true"

* test: Removes complex implementation of list-equal-unordered

Replaces it with sort + equal. While this is less "correct", having
complex untested functions within test files is undesirable. This
implementation is "good enough" for lists of integers.
2022-04-09 07:04:16 +02:00
Scott Olsen
a9e806fee7
feat: implement blit on ByteOrder (#1410)
* 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>

* feat: implement blit on ByteOrder

ByteOrder is effectively just an enum that indicates desired endianness.
We can freely copy it around without penalty.

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>
2022-04-06 09:49:00 +02:00
Scott Olsen
bf6df6405c
feat: add IO.fgetc (#1405)
* feat: add feof and ferror

These are C stdio functions that enable programmers to determine if a
file read resulted in an error or EOF encounter.

We can use these to power a definition of IO.fgetc, which is currently
not defined.

* feat: implement missing IO.fgetc

IO defined a function, fgetc (distinct from IO.Raw.fgetc) which actually
produced invalid code, since the name was not overridden and C does not
define IO_fgetc.

There was also a TODO to handle EOF conditions; so, I've implemented the
function, checking for EOF and error conditions using the Raw stdio
wrappers. IO.fgetc returns a Char in Success on success and an error
string on failure.

* refactor: distinguish EOF from errors in IO.fgetc

We now report whether or not the error encountered in fgetc was EOF.
Note that we don't yet report on the contents of other errors.
2022-03-28 10:16:51 +02:00
Scott Olsen
35edce70cd
feat: add c-name meta field (#1398)
* 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>

* feat: add c-name meta key for code emission overrides

This commit adds a new special compiler meta key, c-name, that enables
users to explicitly c the C identifier Carp should emit for a given
symbol. For now, it is only explicitly supported for Def and Defn forms.

For example:

```clojure
(defn foo-bar [] 2)
(c-name foo-bar "foo_bar")
```

Will cause foo-bar in emitted C code to be emitted as `foo_bar` instead
of `foo_MINUS_bar`.

I've also refactored some of the meta code to be a bit more principled
about keys that are understood by the compiler.

* docs: update CInterop docs

Adds a section on using the c-name meta field to override identifiers
exclusively defined in Carp. Also performs some minor editorial.

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>
2022-03-18 09:34:45 +01:00
Scott Olsen
3148703a22
feat: add Dynamic.String.to-array (#1382)
* feat: add Dynamic.String.to-array

Adds a new dynamic function that converts a string to an array of
strings, one for each character in the string. In other words, it's the
same as Dynamic.String.to-list but the result is an array structure
instead of a list.

It's also significantly faster than to-list on large inputs. The current
implementation of Dynamic.String.to-list takes long to process large
inputs, presumably because of its recursive behavior and multiple calls
to cons (I'm currently investigating this) (e.g. use read-file then try
calling to-list, you may need to wait a while). Contrarily, to-array is
nearly instantaneous even when the input string is large. This is
because it leverages Haskell's `splitOn` directly, which, when given an
empty delimiter, (which is the case for the empty string), splits the
entire input sequence. Because "" is the empty list for a string in
Haskell's representation, calling split-on with "" results in the
splitting behavior. The output also includes the delimiting case, so we
drop it using cdr.

* test: add test for Dynamic.String.to-array
2022-02-14 09:31:13 +01:00
Veit Heller
bd653ad6e6
fix: fix leading % format in fmt (#1380) 2022-01-24 13:05:27 +01:00
Scott Olsen
b3ae93bfc4
fix: ensure registered types with fields emit path (#1364)
This fixes an issue where by types with fields registered in modules
weren't emitted with their module paths. This makes the behavior between
RegisterTypeWithoutFields and RegisterTypeWithFields the same. They both
account for the current module path in which the type is registered and
output the emitted typedef appropriately.

This fix also eliminates the need for a workaround in core/Pattern.carp.
We previously had to register MatchResult with an override because of
the old behavior, but now the override is no longer needed (since
MatchResult is defined as PatternMatchResult in its source header).  The
call (register MatchResult) within (defmodule Pattern) emits
"PatternMatchResult" by default since we now account for module paths
for registered types.
2021-12-16 21:31:16 +01:00
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
Scott Olsen
b62a05f91f
feat: add bytes->hex-string (#1354) 2021-11-06 11:24:17 +01:00
Erik Svedäng
320bc67bac
feat: scan functions (#1339) 2021-10-22 06:59:40 +02:00
rgkirch
102181d244
Update ControlMacros.carp (#1336) 2021-10-14 10:24:59 +02:00
Scott Olsen
bd553fb78e
feat: add assignment operator macros (#1320)
These macros apply an operation to the current value of a variable and
then set the variable to the result of the application. They are
effectively sugar for writing `(set! <var> (<op> <var> <val>))` and
should be familiar to those who have programmed in imperative languages
like C.

In Carp, all the underlying operations these macros use are interfaces,
so one can flexibly use them for more than just numeric types.

Example usage:

```clojure
(let-do [dial 0]
  ;; crank it up to 11!
  (while-do (dial < 12)
    (++ dial))
  dial)

;; expanded
(let-do [dial 0]
  ;; crank it up to 11!
  (while-do (dial < 12)
    (set! dial (inc dial)))
  dial)
```
2021-09-27 10:15:54 +02:00
Veit Heller
0efb9825f0
feat: add Dynamic.List.find-index (#1316) 2021-09-17 05:58:39 +02:00
Veit Heller
5a449b0919
fix: use non-deprecated unlink function on windows (#1311) 2021-09-16 07:56:32 +02:00
Veit Heller
a4f8041288
fix: run ormolu and clang-format (#1312) 2021-09-15 19:49:20 +02:00
Scott Olsen
d2dd875935
feat: Add Float.round (#1301)
Just a wrapper around the c std function.
2021-08-23 20:31:29 +02:00
Scott Olsen
c9967ddf6e
feat: Add additional ignore macros (#1300)
* feat: Add additional ignore macros

This commit adds two new ignore macros, ignore*, which wraps an
arbitrary number of forms in calls to `ignore` and ignore-do, which
wraps an arbitrary number of forms in ignore, then bundles the whole in
a do call, effectively executing each form only for side effects and
ignoring all results.

* docs: Update ignore* docs

Link to `ignore` doc

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

* fix: Call ignore* in ignore-do

ignore-all was an old name that no longer exists!

* test: Add test for ignore-do

Co-authored-by: Veit Heller <veit@veitheller.de>
2021-08-23 20:31:10 +02:00
Veit Heller
39e8fc6d24
fix: escape quotes in String.prn (#1287) 2021-07-29 08:30:42 +02:00
Veit Heller
f3944ce73d
feat: add Dynamic.Debug.trace (#1279) 2021-07-14 08:14:07 +02:00
Veit Heller
74d64a85bb
feat: add Dynamic.proc? (#1278) 2021-07-13 18:04:58 +02:00
Veit Heller
092b249ac7
feat: add List.remove-nth (#1277) 2021-07-13 17:54:15 +02:00
Veit Heller
0adc1abd50
feat: allow for multibranches in case (#1276) 2021-07-08 20:43:48 +02:00
Veit Heller
d3a6ca562f
docs: document core modules (#1271) 2021-07-05 14:48:35 +02:00
guberathome
45b70852dc
fix: added missing definitions so Carp can be used with TCC 32bit on Windows (#1267)
Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
2021-07-01 10:20:42 +02:00
Veit Heller
97c0b5a61b
docs: make doc work on top level and document macros (#1265) 2021-07-01 09:42:37 +02:00
Erik Svedäng
09c91c7f90
feat: Emit docs for top level bindings (#1253)
* refactor: Tiny cleanup before we begin

* refactor: Moved module finding to its own local function

* feat: save-docs-internal is now a binary command

* feat: This seems to work

* fix: Cleaned up the code, save-docs now emit one module per file listed
2021-06-28 19:56:59 +02:00
guberathome
e412559380
reorganize Pattern library (#1257)
* feat!: implemented Pattern.match which returns start and end indizes of the first match

* fix: Pattern.match now returns correct end index

* feat: moved Pattern.match-str and Pattern.find from C to Carp code

* chore: fix build after merges

* chore: fix build after merges

* feat: moved Pattern.find-all from C to Carp code

* feat: Pattern.global-match-str no longer relies on Pattern.global-match

* docs: updated for Pattern.global-match

* fix: moved str/prn functions into sub module

* fix: removed unused functions from carp_pattern.h (using cflow)

* feat!: renamed (Pattern.global-match) to (Pattern.match-all-groups)

* fix: unit test

* fix: some functions renamed to match Carp style

Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
2021-06-23 22:07:56 +02:00
guberathome
0d15a57d0e
(and) and (or) now handle any number of parameters (#1251)
* feat: generalized (and) and (or) to handle any number of parameters

* feat!: removed (and*) and (or*) macros

* chore: worked around compiler issue for unit test

* fix: unit test in ./test/macro.carp

Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
2021-06-20 21:44:04 +02:00
guberathome
0c038365d7
IO.Raw (#1243)
* feat!: move C library wrapper funtions to IO.Raw

* docs: fixed doc string for (String.allocated?)

* docs: removed superfluous parameter names for defns

* fix: replaces IO.exit by System.exit

* fix: ./test/regression.carp for (IO.read->EOF)

* fix: ./test/system.carp

* feat: implemented IO.fwrite!

* feat!: removed IO.exit

* feat!: raw character input now returns Int instead of Char

* fix: removed irritating space chars

* fix: repaired ./example/carp_demo.carp

* fix: reverted System.exit to return an Int for SDL examples

* fix: removed System.exit!

Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
2021-06-17 17:33:10 +02:00
guberathome
5e5cf8d4c2
Up down case (#1248)
* feat!: implemented String.ascii-to-lower and String.ascii-to-upper

* fix: corrected docstrings

* fix: added unit test for ascii-to-lower and ascii-to-upper

* fix: moved tolower- and toupper from String to Byte module

Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
2021-06-16 09:45:32 +02:00
Veit Heller
f7785ad93d
fix: render submodules in html docs (#1242)
* fix: render submodules in html docs

* fix: also render deeply nested modules

* feat: no prefixes in nested submodule doc rendering

* fix:  fix text alignment of module index for sdl

* feat: make submodules expandable
2021-06-11 13:02:52 +02:00
guberathome
6cd2a96486
clean up IO library (#1232) 2021-06-08 21:05:51 +02:00
Erik Svedäng
889f55fe8f
feat: Remove address (#1223)
* chore: Abuse deftemplate to get rid of Address

* chore: Avoid semicolon at end of define

* fix: address should be useable as an argument to a HOF

* chore: adapt examples to new address signature

* fix: Remove Address from typeOf

* fix: Remove more uses of Address

* fix: Remove more mentions of address in the Haskell code

* fix: Remove test that ensure you can't take the address of non-symbols

* refactor: Moved `address` to Pointer.carp

* refactor: Move address into Pointer module

Co-authored-by: Jorge Acereda <jacereda@gmail.com>
2021-05-27 22:04:46 +02:00
Erik Svedäng
470f0f827d
fix: Unify aupdate and aupdate! with other update functions (#1220)
* chore: Re-format Haskell code

* fix: Unify aupdate and aupdate! with other update functions

* fix: Re-add comment

* fix: Also make StaticArray.aupdate! adhere to the normal update signature

* fix: Failing test
2021-05-25 12:11:31 +02:00
Veit Heller
f90d677993
feat: add Unsafe.C.asm (#1206) 2021-05-03 15:14:26 +02:00
Veit Heller
77020df042
fix: fix String.words for multiple spaces (#1205) 2021-04-23 09:35:34 +02:00
Veit Heller
2afe09a94e
docs: fix order of args in map-reduce docs (#1203) 2021-04-20 09:52:24 +02:00
Veit Heller
00146c70b9
feat: add Array.map-reduce (#1201)
* feat: add Array.map-reduce

* test: add map-reduce to memory tests
2021-04-19 16:45:15 +02:00
Tim Dévé
35465b9ffa
refactor: Uses def- & defn- macro in core (#1200) 2021-04-10 09:26:22 +02:00
Veit Heller
d6f40b8570
fix: remove broken file (#1194) 2021-04-01 09:44:03 +02:00
Veit Heller
fd9ceef1ae
docs: add docstrings to some dynamic functions (#1191) 2021-03-30 10:22:18 +02:00
Veit Heller
459a62e61f
feat: add Char.to-byte and Char.from-byte (#1187) 2021-03-16 11:14:16 +01:00
Veit Heller
007d020e05
refactor: use assert-dynamic-equal in test (#1186) 2021-03-16 11:14:01 +01:00