Copyright | © 2019 Elias Castegren and Kiko Fernandez-Reyes |
---|---|
License | MIT |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
This module includes functionality for creating an Abstract Syntax Tree (AST),
backtraces (Backtrace
), 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 f)
- newtype Program (ip :: Phase f) = Program [ClassDef ip]
- data Phase (f :: * -> *) where
- data ClassDef (ip :: Phase f) = ClassDef {}
- data Mod
- data FieldDef (p :: Phase f) = FieldDef {}
- isValField :: FieldDef p -> Bool
- isVarField :: FieldDef p -> Bool
- data Param (p :: Phase f) = Param {}
- data MethodDef (ip :: Phase f) = MethodDef {}
- commaSep :: Show t => [t] -> String
- data Op
- data Expr (p :: Phase f)
- = 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
- usesVar :: Expr p -> Name -> Bool
- newtype Backtrace = Backtrace [BacktraceNode]
- emptyBt :: Backtrace
- data BacktraceNode where
- BTClass :: ClassDef p -> BacktraceNode
- BTParam :: Param p -> BacktraceNode
- BTField :: FieldDef p -> BacktraceNode
- BTMethod :: MethodDef p -> BacktraceNode
- BTExpr :: Expr p -> BacktraceNode
- BTType :: Type p -> BacktraceNode
- class Backtraceable a where
- backtrace :: a -> BacktraceNode
- push :: a -> Backtrace -> Backtrace
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 f) 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 |
Instances
Typecheckable (Type :: Phase (Proxy :: Type -> Type) -> Type) (Type :: Phase Identity -> Type) Source # | |
Defined in Final.Typechecker | |
Eq (Type p) Source # | |
Show (Type p) Source # | |
Backtraceable (Type p) Source # | |
Precheckable (Type p) (Type Checked) Source # | |
Defined in Final.Typechecker doPrecheck :: Type p -> TypecheckM (Type Checked) Source # |
newtype Program (ip :: Phase f) Source #
The representation of a program in the form of an AST node.
data Phase (f :: * -> *) where 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 f) 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 = Proxy, ival = 42}] }]}
Instances
Typecheckable (ClassDef :: Phase (Proxy :: Type -> Type) -> Type) (ClassDef :: Phase Identity -> Type) Source # | |
Defined in Final.Typechecker | |
Show (ClassDef ip) Source # | |
Backtraceable (ClassDef p) Source # | |
Precheckable (ClassDef Parsed) ClassEntry Source # | |
Defined in Final.Typechecker |
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 f) 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 :: Phase (Proxy :: Type -> Type) -> Type) (FieldDef :: Phase Identity -> Type) Source # | |
Defined in Final.Typechecker | |
Show (FieldDef p) Source # | |
Backtraceable (FieldDef p) Source # | |
Precheckable (FieldDef Parsed) FieldEntry Source # | |
Defined in Final.Typechecker |
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 Param (p :: Phase f) Source #
Representation of parameters in the form of an AST.
Instances
Typecheckable (Param :: Phase (Proxy :: Type -> Type) -> Type) (Param :: Phase Identity -> Type) Source # | |
Defined in Final.Typechecker | |
Show (Param p) Source # | |
Backtraceable (Param p) Source # | |
Precheckable (Param Parsed) (Param Checked) Source # | |
Defined in Final.Typechecker |
data MethodDef (ip :: Phase f) 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 = Proxy, ival = 42}] }]}
Instances
Typecheckable (MethodDef :: Phase (Proxy :: Type -> Type) -> Type) (MethodDef :: Phase Identity -> Type) Source # | |
Defined in Final.Typechecker | |
Show (MethodDef ip) Source # | |
Backtraceable (MethodDef p) Source # | |
Precheckable (MethodDef Parsed) MethodEntry Source # | |
Defined in Final.Typechecker |
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 f) 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
would be parsed as this Expr
:
Let {etype = Proxy ,name = "id" ,val = Lambda {etype = Proxy ,params = [Param "x" IntType] ,body = FunctionCall {etype = Proxy ,target = VarAccess Proxy "id" ,args = [IntLit Proxy 42]} } ,body :: Expr p }
BoolLit | Representation of a boolean literal |
IntLit | Representation of an integer literal |
Null | |
Lambda | Representation of a lambda expression |
VarAccess | Representation of a variable access |
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 | |
Instances
Typecheckable (Expr :: Phase (Proxy :: Type -> Type) -> Type) (Expr :: Phase Identity -> Type) Source # | |
Defined in Final.Typechecker | |
Show (Expr p) Source # | |
Backtraceable (Expr p) Source # | |
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
getType :: Expr Checked -> Type Checked Source #
Helper function to extract the type from an expression.
setType :: Type Checked -> Expr Checked -> Expr Checked Source #
Sets the type of an expression e
to t
.
usesVar :: Expr p -> Name -> Bool Source #
Helper function that checks whether an expression uses a variable. This helper function can be used to throw a warning if the variable is unused.
Backtrace declarations
Representation of a backtrace
Backtrace [BacktraceNode] | The representation of a backtrace consists of a list of backtrace nodes |
data BacktraceNode where Source #
Representation of different kinds of Backtrace
nodes. The backtrace
nodes contain all the available information that can be shown to the developer
after a crash.
BTClass :: ClassDef p -> BacktraceNode | Represents the backtrace of a class |
BTParam :: Param p -> BacktraceNode | Represents the backtrace of parameters |
BTField :: FieldDef p -> BacktraceNode | Represents the backtrace of a field |
BTMethod :: MethodDef p -> BacktraceNode | Represents the backtrace of a method |
BTExpr :: Expr p -> BacktraceNode | Represents the backtrace of an expression |
BTType :: Type p -> BacktraceNode | Represents the backtrace of a type |
Instances
Show BacktraceNode Source # | |
Defined in Final.AST showsPrec :: Int -> BacktraceNode -> ShowS show :: BacktraceNode -> String showList :: [BacktraceNode] -> ShowS |
class Backtraceable a where Source #
The type class defines how to create a Backtrace
node, combinator backtrace
,
and how to push
a new node to an existing backtrace.
backtrace :: a -> BacktraceNode Source #
Create a backtrace node
push :: a -> Backtrace -> Backtrace Source #
Create a new backtrace node and append it to the existing backtrace
Instances
Backtraceable (Expr p) Source # | |
Backtraceable (MethodDef p) Source # | |
Backtraceable (Param p) Source # | |
Backtraceable (FieldDef p) Source # | |
Backtraceable (ClassDef p) Source # | |
Backtraceable (Type p) Source # | |