* 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
* 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
* 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
* 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.
* 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.
* 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>
* 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.
* refactor: project configuration get/set parity
This commit refactors our handling of project configuration
getters/setters in hopes of making it easier to ensure fields that have
getters have setters and vice versa, and to ensure field keys are
consistent across the Project.config and get-config commands.
This commit also makes these commands return XObjs for all calls,
instead of coercing certain things into strings and also adds getters
for fields that previously lacked them, e.g.:
(Project.get-config "libflags")
=> ("-lm")
I've maintained parity with existing behaviors to avoid breaking
changes, thus, this works as it did before this commit:
(Project.config "libflags" "-lfoo")
(Project.get-config "libflags")
=> ("-lm" "-lfoo")
though it could now arguably take a list as an argument to permit
complete one-liner replacements; one could still add a single flag and
the interface would be more functional:
(Project.config "libflags"
(cons "-lfoo" (Project.get-config "libflags")))
=> ("-lm" "-lfoo")
(Project.config "libflags" ("-lbar" "-qux"))
=> ("-lbar" "-lqux")
But, again, I've retained the old behavior to avoid breaking existing
builds.
This also fixes a small issue where by setting the file path print
length to "full" still resulted in the "short" printing scheme.
* fix: small typo fix in config field error messages
* fix: restore old names for cflag and libflag config fields
I accidentally pluralized these field keys, which broke some existing
tests. This commit fixes that issue by restoring the original names.
* fix: backwards compatibility for configuration fields
Another small fix to ensure we retain backwards compatibility with
existing code after recent configuration handling changes: pkg-config
and load-stack fields should return array values, not lists.
* refactor: move project configuration get/setters into a new module
Moves the project configuration frontend code into its own module to
avoid cyclic deps between Project and Obj and keep the Obj module
clean.
I've also removed some noisy comments about "internal only"
project configuration fields.
* ci: Fixes scoop install as admin user
* ci: Upgrades Win CI runners to Windows Server 2019
* ci: Disables Debug.sanitize-addresses
We're encountering the following error and I can't resolve it:
https://github.com/actions/virtual-environments/issues/4978
* 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>
* 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
* fix: mangle field names in setter templates
This fixes a regression whereby the names of struct fields weren't
mangled in calls to setters/mutators, resulting in invalid C code if the
user happened to use reserved words in their struct field names such as
"short" or a disallowed character like a dash.
* test: check struct field name mangle regression
This test ensures struct field names are mangled correctly in C output.
* fix: Fixes nix install by using correct pkg-configDepends config key
As reported by Hrafn Blóðbók, nix updated references to pkgconfig in its
codebase to use the `pkg-config` spelling, this seems to break the
installation of Carp using nix.
See this PR: 9bb3fccb5b
* ci: Uses 21.05 release of nix to be compatible with pkg-config changes
* refactor: add type candidates for validation
This commit adds a new module and type, the TypeCandidate, which
represents a potentially valid or invalid type. We use it as the
input for both type validation routines and type binding generation. The
type also allows us to unify the structure of sum types and product
types in an xobj agnostic way, paving the way for future simplification
of binding generation for type definitions.
This commit also removes SumtypeCase.hs, since it's no longer needed.
* refactor: add template generators; update type templates
This commit builds on the TypeCandidate data structure further by
providing "template generators" that work on candidates. Using
generators, templates for type functions ("methods") can be written
almost completely declaratively. Generators also remove some of the
typical boilerplate involved in creating templates from lists of tokens
and enable us to unify several of the generic and concrete templates for
types.
Generators can act on type candidates or their fields (for
field-specific functions). In general, this approach makes the
generation of type templates more structured. A type candidate now
contains all the information a generator needs to create appropriate
templates, thus it is a single and well-defined input for validation and
generation of user defined types.
This commit also updates the Deftype templates to use template
generators.
* refactor: use template generators for sumtype templates
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.
Historically, we did not support calling (register-type) within a
module--while there were instances of such calls (even in our core
library!) they were not properly handled in two ways:
1. They weren't added to the right place in the type environment.
2. Their corresponding emitted code wasn't correct.
This commit enables registering types in modules by making a few fixes:
1. Fix the logic in environment mutations -- before, adding a type to an
environment would result in traversing the environment chain
incorrectly. This is now fixed (we traverse modules and retrieve a
type environment only at the end of the traversal).
2. Fix the typedef emission for registered types--it previously emitted
the C code for the type path, not for the type definition (it called
pathToC not tyToC).
This will now allow authors of wrapper libraries to add more structure
to their Carp wrapper APIs.
This commit also adds a test to check the behavior.
* 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>
Adds -c|--emitcname flag to headerparse to always emit the C identifier
in `register` definitions (useful when the register will be in a
module).
Fixes a bug where the kebab case flag would not output C identifiers
making the emitted C identifiers not match with the ones in the headers.
Adds docs entry about headerparse in CInterop doc.
Makes headerparse emit `CChar` instead of `Char` when encountering a
signature containing `char`.
Previously, the forms cast to the type Unit would still result in
variable assignment emissions, which produces invalid C.
Consider the case:
```clojure
;; type of System.exit is (Int -> a)
(defn main []
(the () (System.exit 0)))
```
This previously produced bad variable assignments. It now works as
expected and emits only the function call.
* Bug fix for #1064 and #843
Removes broken fix for #843 in Emit.hs, thus fixing #1064. And then
this commit focuses on fixing the memory management side of things,
so that we don't add deleters for symbols in the left-hand-side of
match case expressions if we are matching on a ref (e.g. using
match-ref).
* Add sumtype memory tests
* fix: Don't pass 'If' to InvalidObj when Obj actually is 'Mod'
* fix: Better error message
* fix: Better error
* fix: Show the name of the module in the error message
* fix: Use `root` to get correct location for InvalidObj error
* fix: keep old xobj for now
* fix: don't instantiate dummy fields for external types
For ANSI C compatibility reasons, we add a dummy field for memberless
types defined in Carp (see commit 59ef5bbf2b). When registering a type
with no fields, `(register-type A [])`, we'd also attempt to set our
dummy field in the Carp generated initializer for the type. However, the
registered type is totally opaque from the perspective of Carp, and we
can't assume it has a field corresponding to our dummy field.
This commit changes our handling of __dummy in initializers to avoid
setting it for registered types.
* feat: automatically implement str and prn for registered types
This commit makes the auto-generated str and prn functions for
registered types implement the str and prn interfaces, removing the need
for users to call implements on these functions explicitly.
It alters the signature of `autoDerive` in Primitives.hs slightly to
make it more flexible (since registered types have no delete or copy
functions that we can add to the implementation lists of these
interfaces).
* docs: add docs on register-type to CInterop.md
The new documentation clarifies the usage of `register-type` and accounts
for the changes in the prior two commits.
* fix: fix function signatures for generic memberless initers
Filter out dummy field arguments.
* docs: Add details about type name overrides to CInterop.md
* docs: clarify that users can implement delete for registered types