Skip to content

well-typed/c-expr

Repository files navigation

c-expr

License: BSD-3-Clause Build Status

In order to translate C macros to Haskell code, hs-bindgen needs to imbue C macros with a semantics. This project c-expr provides one such semantics.

  • The semantics of expressions is based on C arithmetic expressions, implementing the integral-promotion and arithmetic-conversion rules of the C standard.

  • The semantics of types is based on typedefs.

This project c-expr is the main macro language used by hs-bindgen, but can be used independently.

Packages in this repository

See Section 5.2 c-expr: a DSL for C Expressions in Cardwell, Derbyshire & de Vries, and Schrempf (2025) Automatic C Bindings Generation for Haskell (DOI, PDF).

  • c-expr-dsl: a library that provides a DSL for the expression and type language implemented in c-expr: a Parsec-based parser turning libclang macro tokens into a syntax tree, and a typechecker.

  • c-expr-runtime: a library that provides runtime support for this DSL. It is the semantic core: a type-level universe of C types and a class-per-operator hierarchy whose associated type families encode C's conversion rules. For example, addition is defined with the following type class, where the result type AddRes is computed according to the C standard's arithmetic-conversion rules:

    infixl 2 +
    type Add :: Type -> Type -> Constraint
    class Add a b where
      type family AddRes a b :: Type
      (+) :: a -> b -> AddRes a b

    Platform-specific instances (e.g., AddRes CInt CFloat = CDouble) are generated via Template Haskell.

Contribution

Our thanks go to those who have contributed to this project with development, bug reports, feature requests, blog posts, etc. We list contributors in the hs-bindgen README.

About

Macro language DSL and runtime intended for integration with hs-bindgen

Resources

License

Stars

3 stars

Watchers

2 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors