Commit Graph

4926 Commits

Author SHA1 Message Date
Veit Heller
0adc1abd50
feat: allow for multibranches in case (#1276) 2021-07-08 20:43:48 +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
d3a6ca562f
docs: document core modules (#1271) 2021-07-05 14:48:35 +02:00
Veit Heller
aac92fe9dd
fix: set correct info on defmodule (#1270) 2021-07-03 13:00:14 +02:00
Veit Heller
39b1e8e5fa
docs: add pattern limitations (#1268)
* docs: add pattern limitations

* docs: incorporate feedback by @guberathome into pattern limitations
2021-07-02 10:41:49 +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
Veit Heller
c592485783
fix: Dynamic.String.slice index handling (#1258) 2021-06-20 21:45:21 +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
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
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
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
Tim Dévé
bfe30e9dd6
docs: Updates memory docs to discourage overriding delete (#1245) 2021-06-15 10:42:13 +02:00
Erik Svedäng
f4eb4b5e93
test: REPL (#1239) 2021-06-15 08:02:44 +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
Erik Svedäng
38757dea03
Update README.md 2021-06-08 22:05:25 +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
Erik Svedäng
7c821543bc fix: Clarification 2021-06-06 20:59:08 +02:00
Erik Svedäng
36b7c071f4 fix: Make changelog into a markdown list 2021-06-06 20:57:51 +02:00
Erik Svedäng
99c4284f27 build: Release 0.5.1 2021-06-06 20:55:54 +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
guberathome
e9537a8ba9
PR: additional local documentation as html (#1229)
* docs: implemented python script to convert local documentation from .md files to .html files

* docs: filled index.md; reviewed all references in .md files

* docs: updated Embedded.md and resized carp_on_arduboy.jpg to sensible width

* docs: copy sub folders (./docs/core and ./docs/sdl) to ./docs-html/ and refer docu to it

* docs: phrased requirements more clearly

* docs: generate docs for Standard libraries before converting .md docs to .html

* docs: change index to markdown lists

* docs: index.md worked around limitation in md converter

* docs: removed modules count from Libraries.md

Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
2021-05-31 10:15:09 +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
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
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
Scott Olsen
20302c9f79
fix: don't crash validation on invalid types (#1208) 2021-05-22 23:44:40 +02:00
Scott Olsen
4b4db25984
fix: don't type check untyped forms in set! (#1209) 2021-05-22 23:44:04 +02:00
Scott Olsen
764a61151d
fix: concretize def forms (#1211)
Most def forms bind values, so their types are wholly determined at
initial assignment time. However, defs that bind to lambdas may have
polymorphic types by virtue of their use of interfaces:

```
(defn duplicate-arg [f]
  (fn [x] (f x x)))

(def double (duplicate-arg +))

(defn main []
  (println* (double 3)))
```

Previously, the above program would yield a concretization error. This
commit adds concretization handling for def forms, and makes it possible
to call lambdas bound to defs as is done in the program above.

(Note: the example will emit an error when run as it results in main
returning an error code, 6, but the program works as intended.)

Fixes #364
2021-05-22 15:15:39 +02:00
Scott Olsen
86e409fbd1
fix: properly assign types to recursive calls (#1210)
This commit fixes an issue whereby all recursive calls were given var
types, this resulted in strange behavior such as the following code:

```clojure
(defn recurse [a b]
  (let [c (+ b 1)
        out (recurse c)]
    (+ out 1)))

(defn main []
  (println* (recurse 1 2)))
```

compiling just fine and yielding runtime segfaults. Now, if a recursive
instance of a symbol was previously typed, we use that type--if not, we
assign a fresh type variable (the old behavior). This fixes code like
that above, throwing an error at compile time about an incorrect number
of arguments for `recurse`.

Fixes #348
2021-05-22 15:15:22 +02:00
Scott Olsen
e7746c3d1e
fix: rename type variables during concretization to prevent collisions (#1212)
Our use of type variables in deftype forms to support parametric
polymorphism relied on precise matching between variables in the head
and the body of the form. However, this could lead to unification
failure when the concretizer encountered a type that mixed two
polymorphic types that use the same type var name:

```
(deftype (HitRecord a) [ t a ])
(deftype (CurrentHit a) [ hr (Maybe (HitRecord a)) ])
```

Concretizing this would previously attempt to bind both Maybe(HitRecord
) and just (HitRecord) to `a`.

To fix this, we temporarily rename all type variables in type
definitions during concretization. Names are preserved in place, so
there's no danger of losing a variable. The code above will now work:

```
=> (CurrentHit.init (Just (HitRecord.init 5)))
(CurrentHit (Just (HitRecord 5)))
=> (HitRecord.init 3)
(HitRecord 3)
```

Fixes #521
2021-05-22 15:05:29 +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
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