Commit Graph

558 Commits

Author SHA1 Message Date
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
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
Lucas Leblow
4e02c452dc
test: add match given-away value error test (#1351) 2021-10-28 10:18:38 +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
Erik Svedäng
320bc67bac
feat: scan functions (#1339) 2021-10-22 06:59:40 +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
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
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
Veit Heller
39e8fc6d24
fix: escape quotes in String.prn (#1287) 2021-07-29 08:30:42 +02:00
Veit Heller
0adc1abd50
feat: allow for multibranches in case (#1276) 2021-07-08 20:43:48 +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
Erik Svedäng
f4eb4b5e93
test: REPL (#1239) 2021-06-15 08:02:44 +02:00
guberathome
6cd2a96486
clean up IO library (#1232) 2021-06-08 21:05:51 +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
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
a8b43fa403
test: Regression tests for recent improvements and bug fixes (#1218)
* test: Test for error when recursing using wrong nr of args

* test: Wrong type when recursing

* test: Defining function with def

* test: Using special symbol as binder

* test: Dynamic closures can refer to itself

* test: Avoid unification failure

* fix: Address feedback
2021-05-25 08:08:59 +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
e1943b29a9
Refactor: clean up Env module, store type environments in modules (#1207)
* refactor: major environment mgmt refactor

This big refactor primarily changes two things in terms of behavior:

1. Stores a SymPath on concretely named (non-generic) struct types;
   before we stored a string.
2. The SymPath mentioned in (1.) designates where the struct is stored
   in the current environment chain. Modules now carry a local type
   environment in addition to their local value environments. Any types
   defined in the module are added to this environment rather than the
   global type environment.

To resolve a type such as `Foo.Bar` we now do the following:

- Search the *global value environment* for the Foo module.
- Get the type environment stored in the Foo module.
- Search for Bar in the Foo module's type environment.

Additionally, this commit eliminates the Lookup module entirely and
refactors the Env module to handle all aspects of environment management
in hopefully a more reusable fashion.

I also took the opportunity to refactor primitiveDeftype in Primitives
and qualifySym in Qualify, both of which were hefty functions that I
found difficult to grok and needed refactoring anyway as a result of
lookup changes (lookups now return an Either instead of a Maybe).

Subsequent commits will clean up and clarify this work further.

This does include one minor regression. Namely, an implementation of
`hash` in core/Color that was maximally generic now needs type casting.

* refactor: clean up recent Env changes

This commit removes some redundant functions, unifies some logic, and
renames some routines across the Env module in efforts to make it
cleaner. Call sites have been updated accordingly.

* chore: format code with ormolu

* fix: update lookup tests

Changes references to renamed functions in the Env module.

* refactor: style + additional improvements from eriksvedang@

- Rename arrayTy -> arrayTyA in ArrayTemplates.hs to disambiguate.
- Add maybeId util function.
- Remove commented code.
- Refactor a few functions for readability.

* fix: fix type inference regression

Recent commits introduced one minor regression whereby an instance of
type inference in core/Color.carp no longer worked and required
explicit type annotation. The problem ultimately had to do with
qualification:

- Prior to the recent changes, type inference worked because the call in
  question was qualified to Color.Id.get-tag, fixing the type.
- Failing to copy over a local envs Use modules to function envs
  resulted in finding more than just Color.Id.get-tag for this instance.

We now copy use modules over to function envs generated during
qualification to ensure we resolve to Use'd definitions before more
general cases.

Similarly, I made a small change to primitiveUse to support contextual
use calls (e.g. the `(use Id)` in Color.carp, which really means `(use
Color.Id)`)

* chore: Update some clarificatory comments

* chore: fix inline comment
2021-05-19 19:20:48 +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
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
Veit Heller
1f8c8765d3
feat: treat keywords as symbols (#1190) 2021-04-06 11:37:29 +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
Veit Heller
64b0dc4922
feat: add String.to-list (#1185) 2021-03-15 16:22:10 +01:00
Scott Olsen
816eb65474
fix: allow dynamic closures to mutate the global env (#1184) 2021-03-09 23:30:49 +01:00
Tim Dévé
19c1a4c557
feat: Adds defn- and def- macros (#1174)
* refactor: Groups Dynamic together in Macros.carp

* fix: Fixes doc for `hidden` referring to the wrong symbol

* feat: Adds defn- & def- macros

Adding these macros as a shortand for declaring a def or defn and making
them `hidden` and `private`, useful to keep things internal to a module.

* test: Adds expected error output tests for def- & defn-

* refactor: Changes position of Module and Interface section in LanguageGuide

Trying to introduce concepts in the same order they are referred to in
the examples: structs > modules > interfaces.

* docs: Adds private & hidden section in the LanguageGuide
2021-03-03 08:57:36 +01:00
Veit Heller
dacc13560b
feat: add dynamic Map type (#1168)
* feat: add dynamic map prototype

* feat: feature parity for dynamic map

* docs: document dynamic map

* test: add dynamic map tests

* fix: defdynamics are handled in getBinderDescription

* test: i forgot to add dynamic tests, whoops
2021-02-11 09:12:58 +01:00
Veit Heller
e42922e96e
feat: add Introspect.arguments (#1163)
* feat: add Introspect.arguments

* fix: fix struct case of Introspect.arguments
2021-02-01 17:03:38 +01:00
Veit Heller
74da9c2277
refactor: reformat (sorry for forgetting) (#1162) 2021-01-31 14:54:13 +01:00
Veit Heller
2f03c4af6a
feat: make reseeding of Random at startup configurable (#1161)
* fix: respect quotes in macro expand

* feat: make reseeding of Random at startup configurable

* refactor: better reseed api
2021-01-29 17:38:39 +01:00
Veit Heller
f6c9c338bb
fix: check calls of address and ref (#1156)
* fix: check calls of address and ref

* fix: expand arg to ref

* fix: allow lists in call to address
2021-01-29 17:24:04 +01:00
Veit Heller
6057b03288
fix: respect quotes in macro expand (#1160) 2021-01-28 16:40:41 +01:00
Veit Heller
8e1e827e57
fix: register should only take unqualified symbols (#1154)
* fix: register should only take unqualified symbols

* test: add tests for unqualified register
2021-01-27 13:31:00 +01:00
Veit Heller
a6a52c7605
feat: a proper dynamic numeric tower (#1140)
* feat: a proper dynamic numeric tower

The following things were changed and/or added:
- `Dynamic.neg` was added
- `Dynamic.mod` was changed to work on float values
- `Dynamic.cxr` was changed to work with `0` instructions
- `Dynamic.=` was changed to ignore the type of the number
- `Dynamic.round` was added
- dynamic arithmetic was changed to respect the numeric type tower
- the instances of `Eq` and `Ord` for `Number` are no longer derived, so that they work across numeric types
- the instance of `Num` for `Number` was changed to work across numeric types
- `promoteNumber` was added as a type function to implement the numeric tower.

The numeric tower is as follows:
Byte -> Int -> Long -> Float -> Double

* test: add tests for cxr, neg, and =

* test: add tests for Dynamic.round
2021-01-26 06:19:00 +01:00
Veit Heller
07f6330bf2
fix: deal with literals in match (#1146) 2021-01-26 06:18:32 +01:00
Veit Heller
8c1999d656
feat: add fstr (#1142)
* feat: add fstr

* test: add fstr test

* fix: memory error in test

* fix: fix backslash parser

* feat: add octal escape literals
2021-01-26 06:18:16 +01:00
Veit Heller
63291c53af
refactor: use Set for used modules (#1150)
* refactor: use Set for used modules

* fix: fix lookup test
2021-01-25 21:18:01 +01:00
Scott Olsen
95881850a2
fix: make set! work with dynamic args (thanks @hellerve) (#1151)
* fix: make set! work with dynamic args (thanks @hellerve)

Like `let` before it, we used to bind function arguments to their values
only, which wasn't accounted for in `set!` such that one could not
`set!` `i` in `defndynamic [i] ...`. To fix this for let bindings we
introduced a `LetDef` form for consistency with Def forms. This commit
renames `LetDef` to `LocalDef` and uses it as a value for function
arguments in addition to let bindings, ensuring `set!` works on function
arguments too. Big thanks to @hellerve for the suggestion!

* test: add regression test for set! on dynamic function args
2021-01-25 21:16:53 +01:00
Veit Heller
2d34af6aa9
feat: make empty? an interface (#1139) 2021-01-20 09:54:08 +01:00
Veit Heller
e966f36a58
feat: add walk* (#1134) 2021-01-17 23:34:11 +01:00
Veit Heller
2023c93d62
feat: Quasiquotation (#1129)
* feat: quasiquotation

* test: add tests for quasiquotation

* fix: fix typo in call to doc

* fix: do not evaluate quasiquote too eagerly

* test: pull quasiquote test into macro

* docs: fix unquote example with better constant

* feat: add quasiquote literals

* refactor: simplify reader macros
2021-01-15 10:50:04 +01:00