-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAST.hs
262 lines (223 loc) · 6.3 KB
/
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
module AST
( Program(..)
, Inst(..)
, Direction(..)
, Declare(..)
, Type(..)
, Id(..)
, Exp(..)
, BinOp(..)
, UnOp(..)
, tabs
) where
import Data.List (intercalate)
import Tokens (Pos)
data Program = Program Inst deriving (Eq)
data Inst
= Assign Id Exp Pos
| Block [Declare] [Inst] Pos
| Scan Id Pos
| Print [Exp] Pos
| If Exp Inst (Maybe Inst) Pos
| RWD (Maybe Inst) Exp (Maybe Inst) Pos
| For Id Direction Exp Inst Pos
deriving (Eq, Show)
data Direction = Min | Max deriving (Eq, Show)
data Declare = Declare Type Id deriving (Eq, Show)
data Type
= BoolType
| IntType
| SetType
| StrType
| TypeError [String]
deriving (Eq)
data Id = Id String Pos deriving (Eq)
data Exp
= Binary BinOp Exp Exp
| Unary UnOp Exp
| Set [Exp] Pos
| BoolConst Bool
| IntConst Int
| StrConst String
| Var Id
deriving (Eq, Show)
data BinOp
= Plus {getPosB :: Pos}
| Minus {getPosB :: Pos}
| Times {getPosB :: Pos}
| Div {getPosB :: Pos}
| Mod {getPosB :: Pos}
| SetUnion {getPosB :: Pos}
| SetMinus {getPosB :: Pos}
| SetInter {getPosB :: Pos}
| MapPlus {getPosB :: Pos}
| MapMinus {getPosB :: Pos}
| MapTimes {getPosB :: Pos}
| MapDiv {getPosB :: Pos}
| MapMod {getPosB :: Pos}
| CompLT {getPosB :: Pos}
| CompLE {getPosB :: Pos}
| CompGT {getPosB :: Pos}
| CompGE {getPosB :: Pos}
| CompEQ {getPosB :: Pos}
| CompNE {getPosB :: Pos}
| CompAt {getPosB :: Pos}
| And {getPosB :: Pos}
| Or {getPosB :: Pos}
deriving (Eq)
data UnOp
= SetMax {getPosU :: Pos}
| SetMin {getPosU :: Pos}
| SetSize {getPosU :: Pos}
| Not {getPosU :: Pos}
| Negative {getPosU :: Pos}
deriving (Eq)
------------------------
-- Printing functions --
------------------------
tabs :: Int -> String
tabs = concat . flip replicate " "
instance Show Program where
show = show' 0
class Show' a where
show' :: Int -> a -> String
instance Show' Program where
show' n (Program inst) = "Program\n" ++ (show' (n+1) inst)
instance Show' Inst where
show' n x = (tabs n) ++
case x of
(Assign (Id v _) e _) ->
"Assign\n" ++
(tabs (n+1)) ++ "variable\n" ++
(tabs (n+2)) ++ v ++ "\n" ++
(tabs (n+1)) ++ "value\n" ++
(show' (n+2) e)
(Block [] is _) ->
"Block\n" ++
(intercalate "\n" (map (show' (n+1)) is))
(Block ds is _) ->
"Block\n" ++
(tabs (n+1)) ++ "Using\n" ++
(intercalate "\n" (map (show' (n+2)) ds)) ++ "\n" ++
(tabs (n+1)) ++ "In\n" ++
(intercalate "\n" (map (show' (n+1)) is))
(Scan (Id v _) _) ->
"Scan\n" ++
(tabs (n+1)) ++ "variable\n" ++
(tabs (n+2)) ++ v
(Print es _) ->
"Print\n" ++
(tabs (n+1)) ++ "elements\n" ++
(intercalate "\n" (map (show' (n+2)) es))
(If c t Nothing _) ->
"If\n" ++
(tabs (n+1)) ++ "condition\n" ++
(show' (n+2) c) ++ "\n" ++
(tabs (n+1)) ++ "Then\n" ++
(show' (n+2) t)
(If c t (Just e) _) ->
"If\n" ++
(tabs (n+1)) ++ "condition\n" ++
(show' (n+2) c) ++ "\n" ++
(tabs (n+1)) ++ "Then\n" ++
(show' (n+2) t) ++ "\n" ++
(tabs (n+1)) ++"Else\n" ++
(show' (n+2) e)
(RWD Nothing w (Just d) _) ->
"While\n" ++
(show' (n+1) w) ++ "\n" ++
(tabs n) ++ "Do\n" ++
(show' (n+1) d)
(RWD (Just r) w Nothing _) ->
"Repeat\n" ++
(show' (n+1) r) ++ "\n" ++
(tabs n) ++ "While\n" ++
(show' (n+1) w)
(RWD (Just r) w (Just d) _) ->
"Repeat\n" ++
(show' (n+1) r) ++ "\n" ++
(tabs n) ++ "While\n" ++
(show' (n+1) w) ++ "\n" ++
(tabs n) ++ "Do\n" ++
(show' (n+1) d)
(For (Id v _) d s i _) ->
"For\n" ++
(tabs (n+1)) ++ "variable\n" ++
(tabs (n+2)) ++ v ++ "\n" ++
(tabs (n+1)) ++ "direction\n" ++
(tabs (n+2)) ++ show d ++ "\n" ++
(tabs (n+1)) ++ "in\n" ++
(show' (n+2) s) ++ "\n" ++
(tabs (n+1)) ++ "do\n" ++
(show' (n+2) i)
instance Show' Declare where
show' n (Declare t v) =
tabs n ++ show t ++ " " ++ show v
instance Show Type where
show x =
case x of
BoolType -> "bool"
IntType -> "int"
SetType -> "set"
StrType -> "string"
TypeError _ -> "_"
instance Show Id where
show (Id v _) = v
instance Show' Exp where
show' n x = (tabs n) ++
case x of
(Binary op a b) ->
show op ++ "\n" ++
(show' (n+1) a) ++ "\n" ++
(show' (n+1) b)
(Unary op a) ->
show op ++ "\n" ++
(show' (n+1) a)
(Set es _) ->
"set\n" ++
(intercalate "\n" (map (show' (n+1)) es))
(IntConst a) ->
"int\n" ++
(tabs (n+1)) ++ show a
(BoolConst a) ->
"boolean\n" ++
(tabs (n+1)) ++ show a
(StrConst a) ->
"string\n" ++
(tabs (n+1)) ++ show a
(Var a) ->
"variable\n" ++
(tabs (n+1)) ++ (show a)
instance Show BinOp where
show x =
case x of
(Plus _) -> "Plus (+)"
(Minus _) -> "Minus (-)"
(Times _) -> "Times (*)"
(Div _) -> "Div (/)"
(Mod _) -> "Mod (%)"
(SetUnion _) -> "SetUnion (++)"
(SetMinus _) -> "SetMinus (\\)"
(SetInter _) -> "SetInter (><)"
(MapPlus _) -> "MapPlus (<+>)"
(MapMinus _) -> "MapMinus (<->)"
(MapTimes _) -> "MapTimes (<*>)"
(MapDiv _) -> "MapDiv (</>)"
(MapMod _) -> "MapMod (<%>)"
(CompLT _) -> "Less Than (<)"
(CompLE _) -> "Less or equal (<=)"
(CompGT _) -> "Greater than (>)"
(CompGE _) -> "Greater or equal (>=)"
(CompEQ _) -> "Equals (==)"
(CompNE _) -> "Not equal (/=)"
(CompAt _) -> "Is Member Of (@)"
(And _) -> "Conjunction (and)"
(Or _) -> "Disjunction (or)"
instance Show UnOp where
show x =
case x of
(SetMax _) -> "SetMax (>?)"
(SetMin _) -> "SetMin (<?)"
(SetSize _) -> "SetSize ($?)"
(Not _) -> "Negation (not)"
(Negative _) -> "Negative (-)"