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
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
The representation of a program in the form of an AST node.
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}] }]}
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}
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.
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}] }]}
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 | |
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