stgi-1.0.1: Educational implementation of the STG (Spineless Tagless G-machine)

Safe HaskellNone
LanguageHaskell2010

Stg.Parser.Parser

Contents

Description

A parser for the STG language, modeled after the grammar given in the description in the 1992 paper (link) with a couple of differences to enhance usability:

  • Function application uses no parentheses or commas like in Haskell (f x y z), not with curly parentheses and commas like in the paper (f {x,y,z}).
  • Comment syntax like in Haskell: -- inline, {- multiline -}.
  • Constructors may end with a # to allow labelling primitive boxes e.g. with Int#.
  • A lambda's head is written \(free) bound -> body, where free and bound are space-separated variable lists, instead of the paper's {free} \n {bound} -> body, which uses comma-separated lists. The update flag \u is signified using a double arrow => instead of the normal arrow ->.

Synopsis

General parsing

parse :: StgParser ast -> Text -> Either Doc ast Source

Parse STG source using a user-specified parser. To parse a full program, use parse program.

>>> parse program "id = \\x -> x"
Right (Program (Binds [(Var "id",LambdaForm [] NoUpdate [Var "x"] (AppF (Var "x") []))]))

Parser rules

program :: (Monad parser, TokenParsing parser) => parser Program Source

Parse an STG program.

binds :: (Monad parser, TokenParsing parser) => parser Binds Source

Parse a collection of bindings, used by let(rec) expressions and at the top level of a program.

lambdaForm :: (Monad parser, TokenParsing parser) => parser LambdaForm Source

Parse a lambda form, consisting of a list of free variables, and update flag, a list of bound variables, and the function body.

expr :: (Monad parser, TokenParsing parser) => parser Expr Source

Parse an expression, which can be

  • let, let(rec) ... in ...
  • case, case ... of ...
  • function application, f (...)
  • constructor application, C (...)
  • primitive application, p# (...)
  • literal, 1#

alts :: (Monad parser, TokenParsing parser) => parser Alts Source

Parse the alternatives given in a case expression.

nonDefaultAlts :: (Monad parser, TokenParsing parser) => parser NonDefaultAlts Source

Parse non-default alternatives. The list of alternatives can be either empty, all algebraic, or all primitive.

Nil -> ...
Cons x xs -> ...
1# -> ...
2# -> ...

algebraicAlt :: (Monad parser, TokenParsing parser) => parser AlgebraicAlt Source

Parse a single algebraic alternative.

Cons x xs -> ...

primitiveAlt :: (Monad parser, TokenParsing parser) => parser PrimitiveAlt Source

Parse a single primitive alternative, such as 1#.

1# -> ...

defaultAlt :: (Monad parser, TokenParsing parser) => parser DefaultAlt Source

Parse the default alternative, taken if none of the other alternatives in a case expression match.

default -> ...
v -> ...

literal :: TokenParsing parser => parser Literal Source

primOp :: TokenParsing parser => parser PrimOp Source

Parse a primitive operation.

+#

atom :: (Monad parser, TokenParsing parser) => parser Atom Source

var :: (Monad parser, TokenParsing parser) => parser Var Source

Parse a variable identifier. Variables start with a lower-case letter or _, followed by a string consisting of alphanumeric characters or ', _.

con :: (Monad parser, TokenParsing parser) => parser Constr Source

Parse a constructor identifier. Constructors follow the same naming conventions as variables, but start with an upper-case character instead, and may end with a # symbol.