modulo-1.7.4: Modular C code generator

PortabilityGHC
Stabilityexperimental
Maintainerhans@hanshoglund.se
Safe HaskellNone

Language.Modulo

Contents

Description

This Haskell module defines the Modulo description language. Typically, modules are created by writing .module files and using the parser defined in Language.Modulo.Parser, or the modulo command-line tool. The abstract syntax tree is given below.

The module language is very simple. Each module consists of a name followed by eventual import declarations, followed by all other declarations. Here is an example module:

 module Scl.List
 {
   opaque Elem;
   opaque List;
 
   nil     : () -> List;
   cons    : (Elem, List) -> List;
   head    : (List) -> Elem;
   tail    : (List) -> List;
 
   empty   : (List) -> Bool;
   lenght  : (List) -> Int;
 
   reverse : (List) -> List;
   sort    : (List) -> List;
 }

Like C, the module language uses structural typing for pointers and functions, but nominal typing for structures and unions. Thus in the following example values of type A and B are interchangeable, but values of type C and D are not.

 type A = Ptr Int
 type B = Ptr Int
 type C = struct { x : Int, y : Int }
 type D = struct { x : Int, y : Int }

The pointer type constructor can be used with any type:

 type IntPtr = Int*
 type FloatPtr = Float*

The array type constructor need an explicit length:

 type IntVec = [Int x 10]

Functions are written as in Haskell, except for the parentheses enclosing the arguments. Thus, higher-arity functions are easily distinguished.

 type NoCurry = (A, B) -> C
 type Curry   = (A) -> (B) -> C

Compound types are written in a uniform manner:

 type IntCounter = struct { a : Int, b : Int -> Int }
 type NumValue   = union { left : Int, right : Float }
 type Color      = enum { Red, Green, Blue }

The following primitive types are provided:

 Void Size Ptrdiff Intptr UIntptr 
 Char Short Int Long LongLong
 UChar UShort UInt ULong ULongLong
 Float Double LongDouble
 Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64

Synopsis

Modules

data Module Source

A module is a named container of imports and declarations.

Each module can depend on a set of other modules (translated as include directives). Recursive dependencies are not allowed for now.

Constructors

Module 

Fields

modName :: ModuleName

Name of module

modOptions :: ModuleOptions

Module options.

modDoc :: Doc

Module documentation.

modImports :: [(ModuleName, Maybe String)]

Imports with optional import conventions

modDecls :: [(Doc, Decl)]

List of declarations

Instances

data ModuleOptions Source

Constructors

ModuleOptions 

Fields

optTransient :: Bool

If true, this module does not incur any C prefix.

newtype ModuleName Source

A module name is a non-empty list of strings.

Constructors

ModuleName 

Documentation

newtype Doc Source

Constructors

Doc 

Fields

getDoc :: String
 

Instances

Names

data Name Source

Name of a type, function or constant value.

Note that any Unicode string may be used as a name.

Instances

Declarations

data Decl Source

An declaration maps a name to type and optionally a value.

Constructors

TypeDecl Name (Maybe Type)

Declares a type or opaque.

FunctionDecl Name FunType

Declares a function.

TagDecl Type

Declares a struct or enum tag.

ConstDecl Name (Maybe Value) Type

Declares a constant value.

GlobalDecl Name (Maybe Value) Type

Declares a global variable.

Instances

data Value Source

A value is anything that can be declared as a C constant.

Instances

Types

data Type Source

A type is either an alias, a primitive or a compound type.

Constructors

AliasType Name

An alias, introduced by a former type declaration.

PrimType PrimType 
RefType RefType 
FunType FunType 
CompType CompType 

Instances

data RefType Source

Constructors

Pointer Type

The C pointer type t*.

Array Type Natural

The C array type t[n].

Instances

data FunType Source

A function type.

Constructors

Function [(Maybe Name, Type)] Type

The C function type Tn(T1, ... Tn-1).

Instances

data CompType Source

Constructors

Enum (NonEmpty Name)

A C enum type.

Struct (NonEmpty (Name, Type))

A C struct type.

Union (NonEmpty (Name, Type))

A C union type.

BitField (NonEmpty (Name, Type, Natural))

A C bitfield type.

Instances