Commit Graph

1845 Commits

Author SHA1 Message Date
Erik Svedäng
11239f1c8b chore: apply code formatting 2021-12-20 15:54:49 +01:00
Scott Olsen
d82e8a5a3f
refactor: add type candidates and template generators (#1361)
* 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
2021-12-20 15:41:14 +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
1175bb795b
fix: permit registering types in modules (#1362)
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.
2021-12-12 14:56:41 +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
Tim Dévé
da25a255e9
feat: Adds flag to always output C id with headerparse (#1353)
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`.
2021-11-03 09:09:26 +01:00
Veit Heller
6f4e09f71f
docs: add documentation to core expressions (#1350) (#1352) 2021-10-28 14:30:00 +02:00
Scott Olsen
c471fcce89
fix: don't emit Unit type the casts (#1349)
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.
2021-10-25 09:53:08 +02:00
Lucas Leblow
0682f1a61e
Bug fix for #1064 and #843 (#1321)
* 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
2021-10-25 09:50:10 +02:00
Scott Olsen
499a03e63e
fix: don't hang on module expansions (#1340) 2021-10-22 06:59:51 +02:00
Erik Svedäng
643efd5ad6
fix: Don't pass 'If' to InvalidObj when Obj actually is 'Mod' (#1327)
* 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
2021-10-18 16:54:36 +02:00
Scott Olsen
f4bcc28fc0
feat: register-type improvements (#1332)
* 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
2021-10-18 16:48:02 +02:00
Veit Heller
5f01d64406
fix: categorize static calls correctly (#1322) 2021-10-12 21:23:11 +02:00
Veit Heller
0188264463
feat: add compiler error on maximum sumtype constructors (#1319) 2021-09-18 08:49:19 +02:00
Veit Heller
e307654521
feat: add expand-compiled (#1310) 2021-09-17 05:57:50 +02:00
Veit Heller
a4f8041288
fix: run ormolu and clang-format (#1312) 2021-09-15 19:49:20 +02:00
Scott Olsen
4630e0e6a5
feat: allow C types to satisfy all type constraints (#1306)
This commit builds on the emit-c feature by permitting C typed values to
be used anywhere in Carp code.

For example, if one wants to use the literal C macro `EDOM`:

```clojure
(register EDOM C "EDOM")

(Int.+ 1 EDOM)
=> 34
```

when compiled, this will produce the call:

```c
Int__PLUS(1, EDOM)
```

So it provides a quite flexible means of using C macros directly. It is,
of course, also radically unsafe. Anyone registering and using values of
the C type better be cautious.

One can get pretty crazy with this feature:

```clojure
(register comment-it C "// commented out;")

(Int.+ 1 comment-it)
=> int _11 = Int__PLUS_(1, // commented out;)
   int* _12 = &_11; // ref
   String _13 = IntRef_str(_12);
```
2021-09-04 15:08:51 +02:00
Erik Svedäng
d0a98a5065
fix: #1261 (#1272)
* Revert "refactor: Remove deps functions in ArrayTemplates (#1093)"

This reverts commit 861ba26f52.

* test: Regression test for issue #1261
2021-09-04 15:07:52 +02:00
Veit Heller
74d7d5767b
fix: elide void definitions (#1305) 2021-09-04 14:29:44 +02:00
Scott Olsen
ae2186f4b7
fix: Allow types defined in modules to be members (#1303)
This commit fixes issue #1302, whereby types defined in modules were not
recognized as valid member types by our validation routines.

We simply need to account for types defined in modules in the global
env, threading the global env along through validation (previously we
only passed the top-level type env, which contains no modules).
2021-09-04 14:27:48 +02:00
Scott Olsen
03b453cb36
fix: ensure interface values aren't discarded (#1308)
A call to sequence in our handling of polymorphics would result in
discarding legitimate interface implementation results in rare cases.
Calling rights instead fixes the issue. We also need to take the union
of the results to ensure we account for cases such as polymorphically
realized interfaces (e.g. the `prn` function for a (Maybe String), other
wise, we don't resolve to concrete polymorphics for types that have
ad hoc polymorphic members.

Fixes #1307
2021-09-04 14:27:07 +02:00
Scott Olsen
0c9c475e6c
fix: fix nested polymorphic types (#1294)
* refactor: refactor concretize module

This commit primarily refactors the concretize module, breaking out the
local definitions for visit functions into top level functions that are
hopefully easier to change. I've also modified some monadic code in the
interest of terseness.

This commit adds some additional patterns for XObj forms as well.

* refactor: Only export called functions in Concretize

Adds an export list to Concretize so that the module encapsulates those
functions that are only used internally within the module.

* refactor: better names in concretize functions

Clarify the names of variables in visitor type functions.

* refactor: ensure findType returns a type

Adds an additional check to findType that ensures the retrieved binder
is in fact a type and not another object. This is necessary for certain
contexts like type concretization since modules may also be designated
by symbols that refer to types.

* fix: ensure nested polymorphic types are emitted

This commit fixes an issue whereby nested polymorphic types would not be
emitted by the compiler, even though their member functions were
emitted.

In order to support this, we need to update a couple of functions to
take the global environment (to find nested types, which live in
modules) in addition to the top level type environment. Additionally, we
had to update scoring to account for nested names.

fixes #1293

* test: add tests for nested polymorphic types

Adds regression tests to ensure nested polymorphic types are concretized
and emitted correctly.
2021-08-10 08:41:20 +02:00
Scott Olsen
b74e674bb1
refactor: Cleanup Concretize module (#1283)
* refactor: refactor concretize module

This commit primarily refactors the concretize module, breaking out the
local definitions for visit functions into top level functions that are
hopefully easier to change. I've also modified some monadic code in the
interest of terseness.

This commit adds some additional patterns for XObj forms as well.

* refactor: Only export called functions in Concretize

Adds an export list to Concretize so that the module encapsulates those
functions that are only used internally within the module.

* refactor: better names in concretize functions

Clarify the names of variables in visitor type functions.
2021-08-05 07:36:29 +02:00
Scott Olsen
50680e921a
fix: Fix scoring of type and function declarations (#1290)
Previously, there was an edge case whereby types that depended on other
types that were not scored yet and functions that in turn depended on
these types could be assigned the same score. When this happened, we'd
sometimes incorrectly emit functions before the struct types they
depended on.

Since functions are always at the bottom of a type hierarchy (they
always depend on their members), there's a simple fix: we just add 1 to
ensure functions are emitted after type declarations in all possible
cases.
2021-07-30 09:27:10 +02:00
Scott Olsen
98acdb53e9
fix: make Symbol.prefix work on qualified symbols (#1286)
This commit makes it so that `Symbol.prefix` can take qualified symbols
as prefix arguments, e.g.

`(Symbol.prefix 'Foo.Bar 'baz) => 'Foo.Bar.baz`
2021-07-29 08:26:31 +02:00
Veit Heller
cbdf425247
fix: respect line number in repl (#1282)
* fix: respect line number in repl

* refactor: simplify Parse.parse

* fix: multiline line counter in repl
2021-07-23 21:24:59 +02:00
Veit Heller
760726e001
fix: fix dynamic let bindings recursion and binder leaks (#1281) 2021-07-15 22:45:05 +02:00
Veit Heller
fb1ee66ecc
feat: add machine-info primitive draft (#1269)
* feat: add machine-info primitive draft

* feat: rename machine-info to structured-info

* fix: use the right primitive name in structured-info error

* refactor: remove unnecessary code
2021-07-09 20:45:23 +02:00
Veit Heller
ca0f9f7d4f
docs: set infoFile on primitives and commands (#1273) 2021-07-06 11:33:12 +02:00
Veit Heller
00c85cac42
fix: whitespace in context errors (#1274) 2021-07-06 11:32:31 +02:00
Veit Heller
aac92fe9dd
fix: set correct info on defmodule (#1270) 2021-07-03 13:00:14 +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
Veit Heller
c592485783
fix: Dynamic.String.slice index handling (#1258) 2021-06-20 21:45:21 +02:00
Erik Svedäng
3b429541a3
refactor: Clean up memory management functions (#1240)
* refactor: Mid-refactor save point.

* feat: Code compiles

* refactor: Remove unused imports

* refactor: Move functions out of massive `manageMemory` block

* refactor: Move out even more functions from `manageMemory`

* refactor: Made most patterns match on "head form" of each s-expression

e.g. (if a b c) matches on 'if', 'a', 'b' and 'c'

* refactor: Use the pattern synonyms in Memory

* refactor: Remove a little cruft

* refactor: whenOK function

* refactor: Use 'whenRight' functions to avoid directly matching on Either

* docs: Comment the 'getConcretizedPath' function

* refactor: Move functionFinding-functions into Polymorphism module
2021-06-16 21:41:58 +02:00
Veit Heller
38951efc7d
fix: check for generic main at build time (#1247)
* fix: check for generic main at build time

* docs: add comment about main error handling in concretization
2021-06-16 09:40:28 +02:00
Veit Heller
c149bfa7af
docs: document managed? primitive (#1244)
* docs: document managed? primitive

* docs: rewrite managed? docs
2021-06-14 09:50:04 +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
Scott Olsen
62dff785ab
fix: fix repl function application evaluation (#1238)
The previous pattern matching changes introduced a subtle error in the
evaluator's handling of function applications, we used the same Resolver
for both lists and symbols, where previously we hadn't. Restoring the
old resolution selection and adding a new clause to capture forms like
((defn f [] 2)) restores the correct functioning in the repl.

Importantly, forms such as ((defn foo [x] x) 3), should only be resolved
when the defn form is the result of expanding a symbol.

Eventually, resolving defns to anonymous functions might simplify this
case.

Fixes #1237
2021-06-08 23:17:59 +02:00
Scott Olsen
4f7905d85b
refactor: move form validation into a separate module (#1233) 2021-06-08 07:39:06 +02:00
Scott Olsen
bda32d6104
fix: support recursive let bindings in static code (Fix 402) (#1230) 2021-06-05 17:45:50 +02:00
Erik Svedäng
8f055f287a
fix: Don't crash on invalid member in struct / sumtype (#1228) 2021-06-01 21:15:41 +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
Veit Heller
f82dbc9d6f
feat: add Dynamic.get-env and Dynamic.set-env (#1227) 2021-05-27 07:56:39 +02:00
Scott Olsen
bff7f9803e
fix: fix repl autocomplete (#1225)
The recent Env refactor commits unwittingly broke the REPL's
autocompletion. This change restores the previous behavior.
2021-05-26 15:07:26 +02:00
Erik Svedäng
861ba26f52
refactor: Remove deps functions in ArrayTemplates (#1093) 2021-05-26 11:17:31 +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
Erik Svedäng
0bb32ab0e6
chore: Re-format Haskell code (#1219)
* chore: Re-format Haskell code

* fix: Re-add comment
2021-05-25 08:08:30 +02:00
Scott Olsen
085089e293
fix: allow function arguments to shadow commands (#1217)
This fix is a close cousin of the one that allowed let bindings to
shadow global commands. We now allow function arguments to shadow
commands as well by using a local lookup preference for argument names,
making functions such as:

```
(defndynamic foo [car]
  (Symbol.prefix car 'foo)
```

work as anticipated. I've also removed unused code from `apply`.

Fixes #1057
2021-05-24 21:07:30 +02:00
Scott Olsen
2701517753
fix: don't expand inner module macros on first pass; privacy (#1216)
* fix: don't expand inner module macros on first pass; privacy

This commit changes the behavior of expansions to avoid expanding module
expressions until we're actually processing the module in question.

Previously, the following form would be entirely expanded at the time of evaluating A:

```clojure
(defmodule A <- current environment

  (some-macro) <- expand

  (defmodule B
    (some-macro f) <- expand, current env is A, *NOT* B.
    so if this expands to
    (private f)
    (defn f ....)
    the f of the expansion is added to *A*, and we have a duplicate
    ghost binder.
  )

  (defn foo [] B.f) <- expand, B.f does not exist yet, any meta on the
  binding will be ignored, permitting privacy errors since expansion
  ignores undefined bindings, instead, we'll look this up at eval time,
  and not check privacy as doing so would cause problems for legitimate
  cases.
)
```

This meant that if the macro happened to have side-effects, e.g. calling
`meta-set!` we'd produce side-effects in the wrong environment, A,
resulting in duplicate bindings, missing bindings at evaluation time,
and other problems.

Now, we instead process the form as follows:

```clojure
(defmodule A <- current environment

  (some-macro) <- expand

  (defmodule B
    (some-macro f) <- wait
  )

  (defn foo [] B.f)
)

;; step 2
(defmodule A

  (foo-bar ) <- previously expanded macro

  (defmodule B <- current environment
    (some-macro f) <- expand
  )
  ....
)
```

In general, this prevents the generation of a bunch of unintentional and
incorrectly added bindings when calling `meta-set!` from various macros.

Additionally, privacy constraints are now carried across nested modules:

```
(defmodule A
  (defmodule B
    (private f)
    (defn f [] 0)
  )
  (defn g [] (B.f)) ;; Privacy error!
)
```

This change also fixed an issue whereby recursive functions with `sig`
annotations could trick the compiler. Again, this had to do with the
unintentionally added bindings stemming from expansion of nested module
expressions via meta-set.

Fixes #1213, Fixes #467

* fix: ensure we check privacy against the path of found binders
2021-05-24 21:04:10 +02:00
Scott Olsen
263caee564
Fix: Allow shadows of global commands, allow recursion in let bindings. (#1214)
* fix: don't shadow local bindings with dynamics

This commit adds a new lookup preference to the evaluator, LookupLocal,
and uses it to lookup bindings in the scope of let forms. This fixes an
issue whereby our original bias toward dynamic bindings would prevent
users from shadowing dynamic bindings with local bindings of the same
name. Before the following code returned `command c`:

```
(defdynamic test-val (let [c (car (list 1 2 3))]
                        c))
```

It now returns `1`.

I also fixed a small issue with top-level (as in, without a
corresponding function environment) let forms (they'd cause a crash for
lack of an environment parent).

Fixes #659

* refactor: only prefer local lookups for shadows

The prior commit introduced a local lookup preference into the evaluator
in order allow for shadowing of global names in local scopes (like let
bindings). However, this introduced prohibitive performance costs,
especially for dynamic functions.

To mitigate this, we only perform local-biased lookups for a set of
known-shadows. Since we know the names of local bindings at form
evaluation time, we can restrict our local lookup bias to these paths
only. This greatly reduces the performance penalties initially incurred
by the change.

I've also refactored some of the lookup code for clarity.

* fix: support recursive let bindings

Previously, the bodies of anonymous functions bound to a let name did
not have access to their names, making recursion such as:

```
(let [f (fn [x] (if (= x 1) x (f (dec x))))] (f 10))
```

impossible. We now equip evaluation of let bindings with an additional
recursion environment making this possible. The example above will now
resolve to `1`.

Fixes #1133
2021-05-24 08:58:16 +02:00