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.
Synopsis
- type Name = String
- isConstructorName :: [Char] -> Bool
- data Type
- newtype Program = Program [ClassDef]
- data ClassDef = ClassDef {}
- data Mod
- data FieldDef = FieldDef {}
- isValField :: FieldDef -> Bool
- isVarField :: FieldDef -> Bool
- data Param = Param {}
- data MethodDef = MethodDef {}
- commaSep :: Show t => [t] -> String
- data Op
- data Expr
- = BoolLit { }
- | IntLit { }
- | Null { }
- | Lambda { }
- | VarAccess { }
- | FieldAccess { }
- | Assignment { }
- | MethodCall { }
- | FunctionCall { }
- | If { }
- | Let { }
- | BinOp { }
- | New { }
- | Cast { }
- thisName :: Name
- isArrowType :: Type -> Bool
- isFieldAccess :: Expr -> Bool
- isVarAccess :: Expr -> Bool
- isThisAccess :: Expr -> Bool
- isLVal :: Expr -> Bool
- isClassType :: Type -> Bool
- getType :: Expr -> Type
- setType :: Type -> Expr -> Expr
- newtype Backtrace = Backtrace [BacktraceNode]
- emptyBt :: Backtrace
- data BacktraceNode
- class Backtraceable a where
- backtrace :: a -> BacktraceNode
- push :: a -> Backtrace -> Backtrace
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.
isConstructorName :: [Char] -> Bool Source #
Check if a name is a constructor name
Representation of types
Instances
Eq Type Source # | |
Show Type Source # | |
Backtraceable Type Source # | |
Typecheckable Type Source # | |
Defined in Backtrace.Typechecker doTypecheck :: Type -> TypecheckM Type Source # |
The representation of a program in the form of an AST node.
Instances
Show Program Source # | |
Typecheckable Program Source # | |
Defined in Backtrace.Typechecker doTypecheck :: Program -> TypecheckM Program 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
Show ClassDef Source # | |
Backtraceable ClassDef Source # | |
Typecheckable ClassDef Source # | |
Defined in Backtrace.Typechecker doTypecheck :: ClassDef -> TypecheckM ClassDef 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.
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
Show FieldDef Source # | |
Backtraceable FieldDef Source # | |
Typecheckable FieldDef Source # | |
Defined in Backtrace.Typechecker doTypecheck :: FieldDef -> TypecheckM FieldDef Source # |
isValField :: FieldDef -> Bool Source #
Helper function to check whether a FieldDef
is immutable.
isVarField :: FieldDef -> Bool Source #
Helper function to check whether a FieldDef
is mutable.
Representation of parameters in the form of an AST.
Instances
Show Param Source # | |
Backtraceable Param Source # | |
Typecheckable Param Source # | |
Defined in Backtrace.Typechecker doTypecheck :: Param -> TypecheckM Param 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
Show MethodDef Source # | |
Backtraceable MethodDef Source # | |
Typecheckable MethodDef Source # | |
Defined in Backtrace.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
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 p1 }
BoolLit | Representation of a boolean literal |
IntLit | Representation of an integer literal |
Null | Representation of a null expression |
Lambda | Representation of a null 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
Show Expr Source # | |
Backtraceable Expr Source # | |
Typecheckable Expr Source # | |
Defined in Backtrace.Typechecker doTypecheck :: Expr -> TypecheckM Expr 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 -> Bool Source #
Checks whether a Type
is a function (arrow) type
isFieldAccess :: Expr -> Bool Source #
Checks whether an expression is a FieldAccess
.
isVarAccess :: Expr -> Bool Source #
Checks whether an expression is a VarAccess
.
isThisAccess :: Expr -> Bool Source #
Checks whether an expression is a VarAccess
of this
.
isClassType :: Type -> Bool Source #
Helper function to check whether a Type
is a class
Representation of backtrace
Backtrace [BacktraceNode] | A backtrace is made of a list of |
data BacktraceNode Source #
Different kinds of backtrace nodes. Whenever the type checking function
adds a backtrace, it needs to add the correspondingly correct backtrace.
This logic is handle in the type class Backtraceable
.
Instances
Show BacktraceNode Source # | |
Defined in Backtrace.AST showsPrec :: Int -> BacktraceNode -> ShowS show :: BacktraceNode -> String showList :: [BacktraceNode] -> ShowS |
class Backtraceable a where Source #
Type class defines how to create and push a backtrace node
backtrace :: a -> BacktraceNode Source #
Creation of a backtrace node
push :: a -> Backtrace -> Backtrace Source #
Update of a Backtrace
, by creation and pushing of a new BacktraceNode