-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcool-ast.hs
91 lines (79 loc) · 2.72 KB
/
cool-ast.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- Solution to Stanford Compilers Course.
-- (c) Copyright 2012 Michael Starzinger. All Rights Reserved.
module CoolAST(
Clazz(..),
Type(..),
Attribute(..),
Method(..),
Expression(..),
UnaryOp(..),
BinaryOp(..),
ComparisonOp(..),
annotation,
no,
cName,
cType,
tName,
) where
import CoolLexer (Token)
-- The data type representing parsed classes.
data Clazz a = Clazz Type (Maybe Type) [Either (Attribute a) (Method a)]
deriving Show
-- The data type representing parsed types (class names).
data Type = Type String
deriving (Eq,Show,Ord)
-- The data type representing parsed attributes (class features).
data Attribute a = Attribute String Type (Maybe (Expression a))
deriving Show
-- The data type representing parsed methods (class features).
data Method a = Method String [(String,Type)] Type (Expression a)
deriving Show
-- The data type representing parsed expressions.
data Expression a =
ConstantExpr a Token |
IdentifierExpr a String |
AssignmentExpr a String (Expression a) |
DispatchExpr a (Expression a) (Maybe Type) String [Expression a] |
ConditionalExpr a (Expression a) (Expression a) (Expression a) |
LoopExpr a (Expression a) (Expression a) |
BlockExpr a [Expression a] |
LetExpr a String Type (Maybe (Expression a)) (Expression a) |
CaseExpr a (Expression a) [(String,Type,Expression a)] |
NewExpr a Type |
IsVoidExpr a (Expression a) |
UnaryOpExpr a UnaryOp (Expression a) |
BinaryOpExpr a BinaryOp (Expression a) (Expression a) |
ComparisonOpExpr a ComparisonOp (Expression a) (Expression a)
deriving Show
-- Some helper data types for operations.
data UnaryOp = IntNegOp | BoolNegOp deriving Show
data BinaryOp = AddOp | SubOp | MulOp | DivOp deriving Show
data ComparisonOp = LessOp | LessOrEqualOp | EqualOp deriving Show
-- Returns the annotation from an expression.
annotation :: Expression a -> a
annotation (ConstantExpr a _) = a
annotation (IdentifierExpr a _) = a
annotation (AssignmentExpr a _ _) = a
annotation (DispatchExpr a _ _ _ _) = a
annotation (ConditionalExpr a _ _ _) = a
annotation (LoopExpr a _ _) = a
annotation (BlockExpr a _) = a
annotation (LetExpr a _ _ _ _) = a
annotation (CaseExpr a _ _) = a
annotation (NewExpr a _) = a
annotation (IsVoidExpr a _) = a
annotation (UnaryOpExpr a _ _) = a
annotation (BinaryOpExpr a _ _ _) = a
annotation (ComparisonOpExpr a _ _ _) = a
-- Empty annotation for the above data types.
no :: ()
no = ()
-- Helper to retrieve name from a class.
cName :: Clazz a -> String
cName = tName . cType
-- Helper to retrieve type from a class.
cType :: Clazz a -> Type
cType (Clazz t _ _) = t
-- Helper to retrieve name from a type.
tName :: Type -> String
tName (Type s) = s