Commit Graph

27 Commits

Author SHA1 Message Date
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
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
Erik Svedäng
a152a0d6e1
fix: Bumped Ormolu version to 0.1.4.1 (#1050) 2020-12-03 12:02:58 +01:00
Erik Svedäng
7920a751bf
refactor: Apply Ormolu auto-formatting (#1045) 2020-12-02 16:33:37 +01:00
jacereda
9520caf658
Replace return by pure. (#1009)
* Replace return by pure.

* Remove commandHelp again.
2020-11-24 06:09:15 +01:00
scottolsen
25839de02d Enhance type reflection; get types of values, get kinds
Extends Carp's support for type reflection by returning types for
values as well as bindings.

`type` now also returns a valid Carp expression/s-expression and so its
output can be used as input to dynamic functions and macros (prior to
this commit, `type` printed the type to the REPL but did not return a
meaningful expression in Carp).

Here are a few illustrations of the behavior:

```
(def x 1)
;; type now returns an s-expression/symbol
(type x)
=> Int
;; It also works on values
(type 1)
=> Int
(type 2b)
=> Byte
(type "foo")
=> (Ref String <StaticLifetime>)
;; It works on more complex values as well
(type Maybe)
=> Module
(type Maybe.Just)
(Fn [a] (Maybe a) <StaticLifetime>)
;; reports honestly about polymorphism
(type (Maybe.Nothing))
=> (Maybe a)
(type (Pair.init 1 2))
=> (Pair Int Int)
;; What about the type of types?
(type (type 2))
=> Type
;; Or the type of types of types?
(type (type (type 2)))
=> ()
;; One more time!
(type (type (type (type 2))))
=> ()
;; so, () is the fixpoint of type, and is reached after two applications
(type zero)
;; the type of an interface is all of its implementations
=> (((Fn [] (Array a) <StaticLifetime>) (Fn [] Bool <StaticLifetime>) (Fn
[] Byte <StaticLifetime>) (Fn [] Char <StaticLifetime>) (Fn [] Double
<StaticLifetime>) (Fn [] Float <StaticLifetime>) (Fn [] Int
<StaticLifetime>) (Fn [] Int16 <StaticLifetime>) (Fn [] Int32
<StaticLifetime>) (Fn [] Int64 <StaticLifetime>) (Fn [] Int8
<StaticLifetime>) (Fn [] Long <StaticLifetime>) (Fn [] (Maybe a)
<StaticLifetime>) (Fn [] (Pair a b) <StaticLifetime>) (Fn [] (Quadruple
a b c d) <StaticLifetime>) (Fn [] String <StaticLifetime>) (Fn []
(Triple a b c) <StaticLifetime>) (Fn [] Uint16 <StaticLifetime>) (Fn []
Uint32 <StaticLifetime>) (Fn [] Uint64 <StaticLifetime>) (Fn [] Uint8
<StaticLifetime>)))
```

As shown in the example above, this change also includes a cosmetic
update to the representation of lifetime variables, which are surrounded
in <> to distinguish them from type variables.

This commit also adds a new `kind` primitive that reports on the kind of
a binding or value:

```
(def x 3)
(kind x)
=> Base
(kind 2)
=> Base
(kind Maybe.Just)
=> Higher
(kind (Maybe.Just 2))
=> Higher
```

`kind` and `type` both support interactive development in the repl, for
example, a user can rely on `kind` to check the kind of a type they plan
on using in an interface that demands a higher-kinded argument.

Likewise, they both also support developing macros based on type
information.
2020-10-02 17:48:58 -04:00
Jorge Acereda
dba9ef53c3 Switch option parsing to optparse-applicative, remove tcc dependency. 2020-05-21 20:06:07 +02:00
scottolsen
3a1a39f496 Update headerparse to use ConcreteNameTys 2020-05-04 12:53:52 -04:00
Jorge Acereda
28104b0fb2 Path module. 2019-10-09 12:18:30 +02:00
Jorge Acereda
9bba5d6b7e Fix multiline macros 2019-09-17 19:31:36 +02:00
hellerve
f823618002 headerparse: fix for comments after decl 2019-05-29 18:41:17 +02:00
hellerve
75b6fe7e8a headerparse: remove unnecessary import of Debug.Trace 2018-08-17 15:26:51 +02:00
hellerve
c44e018b73 header parser: add multiline macros 2018-08-07 17:35:00 +02:00
hellerve
d0057d3338 headerparse: generate bindings for function-like macros 2018-08-07 16:07:43 +02:00
Erik Svedäng
7f3eeb392d Don't kebab case macros. 2018-03-11 20:17:26 +01:00
Erik Svedäng
4fc899b0df carp-header-parse can handle #defines 2018-03-06 13:47:53 +01:00
Erik Svedäng
633be61c50 GLFW bindings (automatically generated). 2018-02-27 16:48:31 +01:00
Erik Svedäng
9857dd64db Remove deps. 2018-02-27 16:00:55 +01:00
Erik Svedäng
20d63ff7dc Removed 'sed' support, allow --kebabcase flag, and -p which can remove
a prefix from each function.
2018-02-27 15:57:00 +01:00
Erik Svedäng
c4a0f51e09 Handle const args. 2018-02-27 14:28:48 +01:00
Erik Svedäng
10b5ec1ef4 Can (optionally) batch rename all functions parsed from a .h file
using 'sed'.

Example:
$ carp-header-parse ~/Desktop/glfw.h -s "s/glfw/GLFW/g"
2018-02-27 13:40:40 +01:00
Erik Svedäng
4f86ae7907 Handle function prototypes prefixed with a single macro token. 2018-02-27 13:09:21 +01:00
Erik Svedäng
ebbdf95679 Cleanup. 2018-02-27 11:21:43 +01:00
Erik Svedäng
a7fa295074 Can parse C pointers now. 2018-02-27 11:00:39 +01:00
Erik Svedäng
cfd918c8c6 Parse more types. 2018-02-27 10:47:03 +01:00
Erik Svedäng
ef334048b2 The tool "carp-header-parse" works, at least for simple inputs. 2018-02-27 10:32:25 +01:00
Erik Svedäng
01d77688ea Started working on utility for parsing C headers. 2018-02-27 09:50:20 +01:00