Copyright | © 2019 Elias Castegren and Kiko Fernandez-Reyes |
---|---|
License | MIT |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe |
This module includes functionality for creating an Abstract Syntax Tree (AST),
as well as helper functions for checking different
aspects of the AST. The AST abstract over their kind Phase
, where
Phase
represent the current state of the AST. For example, after parsing
the AST is of Parsed
Phase
; after type checking with tcProgram
the
returned AST is of Checked
Phase
, indicating that the AST has been
type checked.
Synopsis
- type Name = String
- isConstructorName :: [Char] -> Bool
- data Type (p :: Phase)
- newtype Program (ip :: Phase) = Program [ClassDef ip]
- data Phase
- data ClassDef (ip :: Phase) = ClassDef {}
- data Mod
- data FieldDef (p :: Phase) = FieldDef {}
- isValField :: FieldDef p -> Bool
- isVarField :: FieldDef p -> Bool
- data Param (p :: Phase) = Param {}
- data MethodDef (ip :: Phase) = MethodDef {}
- commaSep :: Show t => [t] -> String
- data Op
- data Expr (p :: Phase)
- = BoolLit { }
- | IntLit { }
- | Null { }
- | Lambda { }
- | VarAccess { }
- | FieldAccess { }
- | Assignment { }
- | MethodCall { }
- | FunctionCall { }
- | If { }
- | Let { }
- | BinOp { }
- | New { }
- | Cast { }
- thisName :: Name
- isArrowType :: Type p -> Bool
- isFieldAccess :: Expr p -> Bool
- isVarAccess :: Expr p -> Bool
- isThisAccess :: Expr p -> Bool
- isLVal :: Expr p -> Bool
- isClassType :: Type p -> Bool
- getType :: Expr Checked -> Type Checked
- setType :: Type Checked -> Expr Checked -> Expr Checked
Documentation
isConstructorName :: [Char] -> Bool Source #
Check if a name is a constructor name
AST declarations
Declaration for the Abstract Syntax Tree of the language. This section contains the type, class, methods, fields and expressions represented as an AST. The AST is produced by a parser. For more information on building parsers, we recommend to read megaparsec.
data Type (p :: Phase) Source #
Representation of types abstracting over the Phase
ClassType Name | Represents a class of name |
IntType | Represents integers |
BoolType | Represents booleans |
Arrow | Represents a function type |
UnitType | Represents the unit (void) type |
newtype Program (ip :: Phase) Source #
The representation of a program in the form of an AST node.
Instances
Typecheckable Program Source # | |
Defined in PhantomPhases.Typechecker | |
Show (Program ip) Source # | |
Phases that have already been passed. This has been thought as going through different phases of a compiler. We assume that there is a total order between phases.
data ClassDef (ip :: Phase) Source #
A representation of a class in the form of an AST node. As an example:
class Foo: val x: Int var y: Bool def main(): Int 42
the code above, after parsing, would generate the following AST:
ClassDef {cname = "Foo" ,fields = [FieldDef {fname = "x" ,ftype = IntType ,fmod = Val}] ,methods = [MethodDef {mname = "main" ,mparams = [] ,mtype = IntType ,mbody = [IntLit {etype = Nothing, ival = 42}] }]}
Instances
Typecheckable ClassDef Source # | |
Defined in PhantomPhases.Typechecker | |
Show (ClassDef ip) Source # | |
Field qualifiers in a class. It is thought for a made up syntax such as:
class Foo: val x: Int var y: Bool
This indicates that the variable x
is immutable, and y
can be mutated.
data FieldDef (p :: Phase) Source #
Representation of a field declaration in the form of an AST node. As an example, the following code:
class Foo: val x: Int
could be parsed to the following field representation:
FieldDef {fname = "x" ,ftype = IntType ,fmod = Val}
Instances
Typecheckable FieldDef Source # | |
Defined in PhantomPhases.Typechecker | |
Show (FieldDef p) Source # | |
isValField :: FieldDef p -> Bool Source #
Helper function to check whether a FieldDef
is immutable.
isVarField :: FieldDef p -> Bool Source #
Helper function to check whether a FieldDef
is mutable.
data MethodDef (ip :: Phase) Source #
Representation of a method declaration in the form of an AST. For example:
class Foo: def main(): Int 42
the code above, after parsing, would generate the following AST:
ClassDef {cname = "Foo" ,fields = [] ,methods = [MethodDef {mname = "main" ,mparams = [] ,mtype = IntType ,mbody = [IntLit {etype = Nothing, ival = 42}] }]}
Instances
Typecheckable MethodDef Source # | |
Defined in PhantomPhases.Typechecker | |
Show (MethodDef ip) Source # | |
commaSep :: Show t => [t] -> String Source #
Takes a list of things that can be shown, and creates a comma separated string.
Representation of integer operations
data Expr (p :: Phase) Source #
Representation of expressions in the form of an AST node. The language is expression-based, so there are no statements. As an example, the following identity function:
let id = \x: Int -> x in id 42
generates this Expr
:
Let {etype = Nothing ,name = "id" ,val = Lambda {etype = Nothing ,params = [Param "x" IntType] ,body = FunctionCall {etype = Nothing ,target = VarAccess Nothing "id" ,args = [IntLit Nothing 42]} } ,body :: Expr p }
BoolLit | Representation of a boolean literal |
IntLit | Representation of an integer literal |
Null | |
Lambda | |
VarAccess | |
FieldAccess | |
Assignment | |
MethodCall | |
FunctionCall | |
If | |
Let | |
BinOp | |
New | It is useful to decouple the type of the expression from the type of the
instantiated class. This distinction becomes important whenever we have
subtyping, e.g., an interface |
Cast | |
Helper functions
The helper functions of this section operate on AST nodes to check for different properties. As an example, to check whether an expression is a field, instead of having to pattern match in all places, i.e.,
exampleFunction :: Expr -> Bool exampleFunction expr = -- does some stuff ... case expr of FieldAccess expr -> True _ -> False
we define the isFieldAccess
helper function, which checks
whether a given expression is a FieldAccess
:
exampleFunction :: Expr -> Bool exampleFunction expr = -- does some stuff ... isFieldAccess expr
isArrowType :: Type p -> Bool Source #
Checks whether a Type
is a function (arrow) type
isFieldAccess :: Expr p -> Bool Source #
Checks whether an expression is a FieldAccess
.
isVarAccess :: Expr p -> Bool Source #
Checks whether an expression is a VarAccess
.
isThisAccess :: Expr p -> Bool Source #
Checks whether an expression is a VarAccess
of this
.
isClassType :: Type p -> Bool Source #
Helper function to check whether a Type
is a class