epic-0.9.2: Compiler for a simple functional language

Portability | non-portable
Stability | experimental
Maintainer | eb@cs.st-andrews.ac.uk

Epic.Epic

Contents

- Expressions
- Types
- Operators
- Declarations and programs
- Compiling and execution
- Some basic definitions

Description

Combinators for builing Epic programs

Synopsis

- class EpicExpr e where
  - term :: e -> State Int Expr
- class EpicFn e
- class Alternative e
- data Expr
- type Term = State Int Expr
- data Name
- name :: String -> Name
- (@@) :: (EpicExpr f, EpicExpr a) => f -> a -> Term
- case_ :: EpicExpr e => e -> [State Int CaseAlt] -> Term
- con_ :: Int -> Term
- tuple_ :: Term
- con :: Alternative e => Int -> e -> State Int CaseAlt
- tuple :: Alternative e => e -> State Int CaseAlt
- constcase :: EpicExpr a => Int -> a -> State Int CaseAlt
- defaultcase :: EpicExpr a => a -> State Int CaseAlt
- if_ :: (EpicExpr a, EpicExpr t, EpicExpr e) => a -> t -> e -> Term
- while_ :: (EpicExpr t, EpicExpr b) => t -> b -> Term
- whileAcc_ :: (EpicExpr t, EpicExpr a, EpicExpr b) => t -> a -> b -> Term
- error_ :: String -> Term
- lazy_ :: EpicExpr a => a -> Term
- effect_ :: EpicExpr a => a -> Term
- foreign_ :: EpicExpr e => Type -> String -> [(e, Type)] -> Term
- foreignL_ :: EpicExpr e => Type -> String -> [(e, Type)] -> Term
- foreignConst_ :: Type -> String -> Term
- foreignConstL_ :: Type -> String -> Term
- let_ :: (LetExpr e, EpicExpr val) => val -> e -> State Int Expr
- letN_ :: (EpicExpr val, EpicExpr scope) => Name -> val -> scope -> Term
- update_ :: (EpicExpr val, EpicExpr scope) => Expr -> val -> scope -> Term
- op_ :: (EpicExpr a, EpicExpr b) => Op -> a -> b -> Term
- str :: String -> Term
- int :: Int -> Term
- bigint :: Integer -> Term
- float :: Double -> Term
- char :: Char -> Term
- bool :: Bool -> Term
- unit_ :: Term
- (!.) :: EpicExpr t => t -> Int -> Term
- fn :: String -> Term
- ref :: Name -> Term
- (+>) :: EpicExpr c => c -> Term -> Term
- malloc_ :: (EpicExpr a, EpicExpr b) => a -> b -> Term
- data Type
- tyInt :: Type
- tyBigInt :: Type
- tyChar :: Type
- tyBool :: Type
- tyFloat :: Type
- tyString :: Type
- tyPtr :: Type
- tyUnit :: Type
- tyAny :: Type
- tyC :: String -> Type
- data Op
- plus_ :: Op
- minus_ :: Op
- times_ :: Op
- divide_ :: Op
- plusF_ :: Op
- minusF_ :: Op
- timesF_ :: Op
- divideF_ :: Op
- eq_ :: Op
- lt_ :: Op
- lte_ :: Op
- gt_ :: Op
- gte_ :: Op
- eqF_ :: Op
- ltF_ :: Op
- lteF_ :: Op
- gtF_ :: Op
- gteF_ :: Op
- shiftl_ :: Op
- shiftr_ :: Op
- data EpicDecl
  - = forall e . EpicFn e => EpicFn Name e
  - | Include String
  - | Link String
  - | CType String
- data Program
- mkProgram :: [EpicDecl] -> Program
- compile :: Program -> FilePath -> IO ()
- compileObj :: Program -> FilePath -> IO ()
- link :: [FilePath] -> FilePath -> IO ()
- compileWith :: [CompileOptions] -> Program -> FilePath -> IO ()
- compileObjWith :: [CompileOptions] -> Program -> FilePath -> IO ()
- linkWith :: [CompileOptions] -> [FilePath] -> FilePath -> IO ()
- run :: Program -> IO ()
- evaluate :: EpicExpr e => Program -> e -> Expr
- data CompileOptions
  - = KeepC
  - | Trace
  - | ShowBytecode
  - | ShowParseTree
  - | MakeHeader FilePath
  - | GCCOpt String
  - | Debug
  - | Checking Int
  - | ExternalMain
  - | MainInc FilePath
  - | LinkObj FilePath
- basic_defs :: [EpicDecl]

# Expressions

class EpicExpr e where

Build expressions, with a name supply

Methods

term :: e -> State Int Expr

Instances

EpicExpr String |
EpicExpr Expr |
EpicExpr Term |
EpicExpr e => EpicExpr (Expr -> e) |
EpicExpr e => EpicExpr ([Name], e) |

class EpicFn e

Build a function definition, with a name supply

Instances

EpicFn Expr |
EpicFn Term |
EpicFn e => EpicFn (Expr -> e) |
EpicFn e => EpicFn ([Name], e) |

class Alternative e

Build a case alternative, with a name supply

Instances

Alternative Expr |
Alternative Term |
Alternative e => Alternative (Expr -> e) |
Alternative e => Alternative ([Name], e) |

data Expr

Instances

Eq Expr |
Show Expr |
Show Term |
SubstV Expr |
Quote Expr |
RtoV Expr |
Alternative Expr |
Alternative Term |
EpicFn Expr |
EpicFn Term |
EpicExpr Expr |
EpicExpr Term |
HOAS Func Expr |
HOAS Expr Expr |
Eq (Expr -> HRHS) |
Eq (Expr -> Expr) |
SubstV (Expr, Type) |
LetExpr (Expr -> Term) |
Alternative e => Alternative (Expr -> e) |
EpicFn e => EpicFn (Expr -> e) |
EpicExpr e => EpicExpr (Expr -> e) |

type Term = State Int Expr

A sub-term, with a name supply

data Name

Instances

Eq Name |
Show Name |
EpicExpr sc => LetExpr (Name, sc) |
Alternative e => Alternative ([Name], e) |
EpicFn e => EpicFn ([Name], e) |
EpicExpr e => EpicExpr ([Name], e) |

name :: String -> Name

(@@)

Arguments

:: (EpicExpr f, EpicExpr a) |
=> f | function
-> a | argument
-> Term |

Application

case_ :: EpicExpr e => e -> [State Int CaseAlt] -> Term

Build a case expression with a list of alternatives

con_

Arguments

:: Int | Tag
-> Term |

Build a constructor with the given tag

tuple_ :: Term

Build a tuple

con

Arguments

:: Alternative e |
=> Int | the tag
-> e | RHS of alternative
-> State Int CaseAlt |

Case alternative for constructor with the given tag

tuple

Arguments

:: Alternative e |
=> e | RHS of alternative
-> State Int CaseAlt |

Case alternative for a tuple with the given tag

constcase

Arguments

:: EpicExpr a |
=> Int | the constant
-> a |
-> State Int CaseAlt |

Case alternative for a constant

defaultcase :: EpicExpr a => a -> State Int CaseAlt

Default case if no other branches apply

if_ :: (EpicExpr a, EpicExpr t, EpicExpr e) => a -> t -> e -> Term

while_

Arguments

:: (EpicExpr t, EpicExpr b) |
=> t | Boolean test (most likely effectful)
-> b | Body
-> Term |

While loops (primitive, for efficiency).

whileAcc_

Arguments

:: (EpicExpr t, EpicExpr a, EpicExpr b) |
=> t | Boolean test (most likely effectful)
-> a | Accumulator (of type a)
-> b | Body (of type a -> a)
-> Term |

While loop, with an accumulator

error_ :: String -> Term

lazy_ :: EpicExpr a => a -> Term

Evaluate an expression lazily

effect_ :: EpicExpr a => a -> Term

Evaluate an expression but don't update the closure with the result. | Use this if the expression has a side effect.

foreign_ :: EpicExpr e => Type -> String -> [(e, Type)] -> Term

foreignL_ :: EpicExpr e => Type -> String -> [(e, Type)] -> Term

foreignConst_ :: Type -> String -> Term

foreignConstL_ :: Type -> String -> Term

let_ :: (LetExpr e, EpicExpr val) => val -> e -> State Int Expr

letN_ :: (EpicExpr val, EpicExpr scope) => Name -> val -> scope -> Term

Let bindings with an explicit name

update_ :: (EpicExpr val, EpicExpr scope) => Expr -> val -> scope -> Term

Update a local variable (could be an explicit name or bound with a lambda, so we let it be an `Expr`.

op_ :: (EpicExpr a, EpicExpr b) => Op -> a -> b -> Term

str :: String -> Term

Constant string

int :: Int -> Term

Constant integer

bigint :: Integer -> Term

Constant big integer

float :: Double -> Term

Constant float

char :: Char -> Term

Constant character

bool :: Bool -> Term

Constant bool

unit_ :: Term

(!.)

Arguments

:: EpicExpr t |
=> t | Expression in constructor form
-> Int | Argument number
-> Term |

Project an argument from an expression which evaluates to constructor form.

fn :: String -> Term

Reference to a function name

ref :: Name -> Term

Reference to a function name

(+>) :: EpicExpr c => c -> Term -> Term

Constructor for the unit type

Sequence terms --- evaluate the first then second

malloc_

Arguments

:: (EpicExpr a, EpicExpr b) |
=> a | Size of block to allocate
-> b | Expression to evaluate
-> Term |

Evaluate an expression under manually allocated memory. Creates a pool of memory. All allocation is from this pool, and there is no garbage collection. The pool is freed after evaluation.

# Types

data Type

Instances

Eq Type |
Show Type |
SubstV (Expr, Type) |
Quote a => Quote (a, Type) |
RtoV a => RtoV (a, Type) |
HOAS a a => HOAS (a, Type) (a, Type) |

tyInt :: Type

tyBigInt :: Type

tyChar :: Type

tyBool :: Type

tyFloat :: Type

tyString :: Type

tyPtr :: Type

tyUnit :: Type

tyAny :: Type

tyC :: String -> Type

# Operators

data Op

Instances

Eq Op |
Show Op |

plus_ :: Op

minus_ :: Op

times_ :: Op

divide_ :: Op

plusF_ :: Op

minusF_ :: Op

timesF_ :: Op

divideF_ :: Op

eq_ :: Op

lt_ :: Op

lte_ :: Op

gt_ :: Op

gte_ :: Op

eqF_ :: Op

ltF_ :: Op

lteF_ :: Op

gtF_ :: Op

gteF_ :: Op

shiftl_ :: Op

shiftr_ :: Op

# Declarations and programs

data EpicDecl

Top level declarations

Constructors

forall e . EpicFn e => EpicFn Name e | Normal function
Include String | Include a C header
Link String | Link to a C library
CType String | Export a C type

Instances

Show EpicDecl |

data Program

mkProgram :: [EpicDecl] -> Program

# Compiling and execution

compile :: Program -> FilePath -> IO ()

Compile a program to an executable

compileObj :: Program -> FilePath -> IO ()

Compile a program to a .o

link :: [FilePath] -> FilePath -> IO ()

Link a collection of object files. By convention, the entry point is the function called `main`.

compileWith :: [CompileOptions] -> Program -> FilePath -> IO ()

Compile a program to an executable, with options

compileObjWith :: [CompileOptions] -> Program -> FilePath -> IO ()

Compile a program to a .o, with options

linkWith :: [CompileOptions] -> [FilePath] -> FilePath -> IO ()

Link a collection of object files, with options. By convention, the entry point is the function called `main`.

run :: Program -> IO ()

evaluate :: EpicExpr e => Program -> e -> Expr

data CompileOptions

(Debugging) options to give to compiler

Constructors

KeepC | Keep intermediate C file
Trace | Generate trace at run-time (debug)
ShowBytecode | Show generated code
ShowParseTree | Show parse tree
MakeHeader FilePath | Output a .h file too
GCCOpt String | Extra GCC option
Debug | Generate debug info
Checking Int | Checking level (0 none)
ExternalMain | main is defined externally (in C)
MainInc FilePath | File to #include in main program
LinkObj FilePath | .o file to link with

Instances

Eq CompileOptions |

# Some basic definitions

basic_defs :: [EpicDecl]

Some default definitions: putStr, putStrLn, readStr, append, intToString