-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintParser.hs
403 lines (350 loc) · 12.9 KB
/
PrintParser.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
{-
An Interpreter for the subject "Traductores e Interpretadores" (Translators and Interpreters)
of the Simon Bolivar University (USB).
Authors:
Neil Villamizar 15-11523
Jesus Wahrman 15-11540
-}
module PrintParser where
import Control.Monad.State
import AST
import Lexer
-- Data type to print the parser, it saves the string to print
-- and the block number
data PrintState = PrintState{ output :: [String] , blockN :: Int } deriving(Show)
type MyPrintStateM a = State PrintState a
-- Receives a list of BLOCKs (World or Task) and prints them
printParser :: [BLOCK] -> MyPrintStateM String
printParser [] = do
(PrintState str int ) <- get
case str of
[] -> return (taskStr ++ "\n no hay tareas\n")
otherwise -> return (unlines $ (taskStr:reverse str))
where
taskStr = "TAREAS:\n"
printParser (x:xs) = do
printBlock x
printParser xs
-- Increases the block number of the state
incBlockN :: MyPrintStateM ()
incBlockN = do
(PrintState str int ) <- get
put (PrintState str (int+1) )
return ()
-- Receives a BLOCK (World or Task) and prints it
printBlock :: BLOCK -> MyPrintStateM ()
printBlock (WORLD _ _ _) = do
incBlockN
return ()
printBlock (TASK _ taskId onWorld []) = do
incBlockN
(PrintState str int ) <- get
put (PrintState ((bNum++show(int)):ins:wId:tId:str) int )
where
tId = (getStr taskId) ++ ":"
wId = " mundo: " ++ (getStr onWorld)
ins = " bloque de instrucciones: sin instrucciones."
bNum = " identificador de bloque: "
printBlock (TASK _ taskId onWorld taskInstrs) = do
incBlockN
(PrintState str int ) <- get
put (PrintState ((bNum++show(int)):ins:wId:tId:str) int )
printInstrBlock 4 taskInstrs
where
tId = (getStr taskId) ++ ":"
wId = " mundo: " ++ (getStr onWorld)
ins = " bloque de instrucciones:"
bNum = " identificador de bloque: "
-- Receives the indentation level, a list of task instructions
-- and prints them
printInstrBlock :: Int -> [TASKINSTR] -> MyPrintStateM ()
printInstrBlock spaces [] = return ()
printInstrBlock spaces (x:xs) = do
printInstr spaces x
printInstrBlock spaces xs
-- Receives the indentation level, a task instruction
-- and prints it
printInstr :: Int -> TASKINSTR -> MyPrintStateM ()
printInstr spaces (IF _ test tInst) = do
incBlockN -- IF have his own scope
(PrintState str int) <- get
put (PrintState (cd:(bNum++show(int)):cond:str) int)
printGuard (spaces+4) test
(PrintState str' int') <- get
put (PrintState (inst:str') int')
printInstr (spaces+4) tInst
where
cond = replicate spaces ' ' ++ "CONDICIONAL IF:"
bNum = replicate (spaces+2) ' ' ++ "identificador de bloque: "
cd = replicate (spaces+2) ' ' ++ "condicion:"
inst = replicate (spaces+2) ' ' ++ "instruccion:"
printInstr spaces (IFELSE _ test tInst0 tInst1) = do
(PrintState str int) <- get
put (PrintState (cd:cond:str) int)
printGuard (spaces+4) test
incBlockN -- IF have his own scope
(PrintState str' int') <- get
put (PrintState ((bNum++show(int')):inst:str') int')
printInstr (spaces+4) tInst0
incBlockN -- ELSE have his own scope
(PrintState str'' int'') <- get
put (PrintState ((bNum++show(int'')):inst2:str'') int'')
printInstr (spaces+4) tInst1
where
cond = replicate spaces ' ' ++ "CONDICIONAL IF/ELSE:"
bNum = replicate (spaces+2) ' ' ++ "identificador de bloque: "
cd = replicate (spaces+2) ' ' ++ "condicion:"
inst = replicate (spaces+2) ' ' ++ "instruccion if :"
inst2 = replicate (spaces+2) ' ' ++ "instruccion else :"
printInstr spaces (REPEAT _ n tInst) = do
incBlockN -- REPEAT have his own scope
(PrintState str int) <- get
put (PrintState (it:nt:nv:(bNum++show(int)):cr:str) int)
printInstr (spaces+4) tInst
where
cr = replicate spaces ' ' ++ "CICLO REPEAT:"
bNum = replicate (spaces+2) ' ' ++ "identificador de bloque: "
nv = replicate (spaces+2) ' ' ++ "numero de ciclos:"
nt = replicate (spaces+4) ' ' ++ (show $ getValue n)
it = replicate (spaces+2) ' ' ++ "instruccion:"
printInstr spaces (WHILE _ test tInst) = do
incBlockN -- WHILE have his own scope
(PrintState str int) <- get
put(PrintState (gc:(bNum++show(int)):cw:str) int)
printGuard (spaces+4) test
(PrintState str' int') <- get
put(PrintState (it:str') int')
printInstr (spaces+4) tInst
where
cw = replicate spaces ' ' ++ "CICLO WHILE:"
bNum = replicate (spaces+2) ' ' ++ "identificador de bloque: "
gc = replicate (spaces+2) ' ' ++ "condicion:"
it = replicate (spaces+2) ' ' ++ "instruccion:"
printInstr spaces (BEGIN _ []) = do
(PrintState str int) <- get
put(PrintState (is:(ib++(show (int+1))):bi:str) int)
incBlockN -- BEGIN have his own scope
where
bi = replicate spaces ' ' ++ "BLOQUE BEGIN: "
ib = replicate (spaces+2) ' ' ++ "identificador de bloque: "
is = replicate (spaces+2) ' ' ++ "instrucciones: sin instrucciones."
printInstr spaces (BEGIN _ tInsts) = do
(PrintState str int) <- get
put(PrintState (is:(ib++(show (int+1))):bi:str) int)
incBlockN -- BEGIN have his own scope
printInstrBlock (spaces+4) tInsts
where
bi = replicate spaces ' ' ++ "BLOQUE BEGIN: "
ib = replicate (spaces+2) ' ' ++ "identificador de bloque: "
is = replicate (spaces+2) ' ' ++ "instrucciones:"
printInstr _ (DEFINE _ _ tInst) = do
incBlockN -- DEFINE have his own scope
traverseDefineInstr tInst
printInstr spaces (MOVE _ ) = do
(PrintState str int) <- get
put(PrintState (it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "move"
printInstr spaces (TURNLEFT _ ) = do
(PrintState str int) <- get
put(PrintState (it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "turnleft"
printInstr spaces (TURNRIGHT _ ) = do
(PrintState str int) <- get
put(PrintState (it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "turnright"
printInstr spaces (PICK _ id) = do
(PrintState str int) <- get
put(PrintState (is:io:it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "pick"
io = replicate (spaces+4) ' ' ++ "identificador:"
is = replicate (spaces+6) ' ' ++ (getStr id)
printInstr spaces (DROP _ id) = do
(PrintState str int) <- get
put(PrintState (is:io:it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "drop"
io = replicate (spaces+4) ' ' ++ "identificador:"
is = replicate (spaces+6) ' ' ++ (getStr id)
printInstr spaces (SET _ id) = do
(PrintState str int) <- get
put(PrintState (is:io:it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "set"
io = replicate (spaces+4) ' ' ++ "identificador:"
is = replicate (spaces+6) ' ' ++ (getStr id)
printInstr spaces (SETTO _ id tof) = do
(PrintState str int) <- get
put(PrintState (tf:vn:is:io:it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "set"
io = replicate (spaces+4) ' ' ++ "identificador:"
is = replicate (spaces+6) ' ' ++ (getStr id)
vn = replicate (spaces+4) ' ' ++ "valor nuevo:"
tf = replicate (spaces+6) ' ' ++ (show tof)
printInstr spaces (FLIP _ id) = do
(PrintState str int) <- get
put(PrintState (is:io:it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "flip"
io = replicate (spaces+4) ' ' ++ "identificador:"
is = replicate (spaces+6) ' ' ++ (getStr id)
printInstr spaces (CLEAR _ id) = do
(PrintState str int) <- get
put(PrintState (is:io:it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "clear"
io = replicate (spaces+4) ' ' ++ "identificador:"
is = replicate (spaces+6) ' ' ++ (getStr id)
printInstr spaces (TERMINATE _ ) = do
(PrintState str int) <- get
put(PrintState (it:ip:str) int)
where
ip = replicate spaces ' ' ++ "INSTRUCCION PRIMITIVA:"
it = replicate (spaces+2) ' ' ++ "terminate"
printInstr spaces (INSTRID _ id) = do
(PrintState str int) <- get
put(PrintState (st:li:str) int)
where
li = replicate spaces ' ' ++ "LLAMADA A INSTRUCCION:"
st = replicate (spaces+2) ' ' ++ (getStr id)
-- Receives the indentation level, a test and prints it
printGuard :: Int -> TEST -> MyPrintStateM ()
printGuard spaces (TESTTOF _ val) = do
(PrintState str int) <- get
put(PrintState (tof:str) int)
where
tof = replicate spaces ' ' ++ show val
printGuard spaces (TESTAND _ test0 test1) = do
(PrintState str int) <- get
put(PrintState (li:disy:str) int)
printGuard (spaces+4) test0
(PrintState str' int') <- get
put(PrintState (ld:str') int')
printGuard (spaces+4) test1
where
disy = replicate spaces ' ' ++ "DISYUNCION:"
li = replicate (spaces+2) ' ' ++ "lado izquiedo:"
ld = replicate (spaces+2) ' ' ++ "lado derecho:"
printGuard spaces (TESTOR _ test0 test1) = do
(PrintState str int) <- get
put(PrintState (li:conj:str) int)
printGuard (spaces+4) test0
(PrintState str' int') <- get
put(PrintState (ld:str') int')
printGuard (spaces+4) test1
where
conj = replicate spaces ' ' ++ "CONJUNCION:"
li = replicate (spaces+2) ' ' ++ "lado izquiedo:"
ld = replicate (spaces+2) ' ' ++ "lado derecho:"
printGuard spaces (TESTNOT _ test) = do
(PrintState str int) <- get
put(PrintState (exp:neg:str) int)
printGuard (spaces+4) test
where
neg = replicate spaces ' ' ++ "NEGACION:"
exp = replicate (spaces+2) ' ' ++ "expresion:"
printGuard spaces (TESTID _ id) = do
(PrintState str int) <- get
put(PrintState (idStr:ident:str) int)
where
ident = replicate spaces ' ' ++ "IDENTIFICADOR: "
idStr = replicate (spaces+2) ' ' ++ (getStr id)
printGuard spaces (FRONTCLEAR _ ) = do
(PrintState str int) <- get
put(PrintState (fc:bp:str) int)
where
bp = replicate spaces ' ' ++ "BOOLEANO PRIMITIVO:"
fc = replicate (spaces+2) ' ' ++ "front-clear"
printGuard spaces (LEFTCLEAR _ ) = do
(PrintState str int) <- get
put(PrintState (fc:bp:str) int)
where
bp = replicate spaces ' ' ++ "BOOLEANO PRIMITIVO:"
fc = replicate (spaces+2) ' ' ++ "left-clear"
printGuard spaces (RIGHTCLEAR _ ) = do
(PrintState str int) <- get
put(PrintState (fc:bp:str) int)
where
bp = replicate spaces ' ' ++ "BOOLEANO PRIMITIVO:"
fc = replicate (spaces+2) ' ' ++ "right-clear"
printGuard spaces (LOOKNORTH _ ) = do
(PrintState str int) <- get
put(PrintState (fc:bp:str) int)
where
bp = replicate spaces ' ' ++ "BOOLEANO PRIMITIVO:"
fc = replicate (spaces+2) ' ' ++ "looking-north"
printGuard spaces (LOOKEAST _ ) = do
(PrintState str int) <- get
put(PrintState (fc:bp:str) int)
where
bp = replicate spaces ' ' ++ "BOOLEANO PRIMITIVO:"
fc = replicate (spaces+2) ' ' ++ "looking-east"
printGuard spaces (LOOKSOUTH _ ) = do
(PrintState str int) <- get
put(PrintState (fc:bp:str) int)
where
bp = replicate spaces ' ' ++ "BOOLEANO PRIMITIVO:"
fc = replicate (spaces+2) ' ' ++ "looking-south"
printGuard spaces (LOOKWEST _ ) = do
(PrintState str int) <- get
put(PrintState (fc:bp:str) int)
where
bp = replicate spaces ' ' ++ "BOOLEANO PRIMITIVO:"
fc = replicate (spaces+2) ' ' ++ "looking-west"
printGuard spaces (FOUND _ id) = do
(PrintState str int) <- get
put(PrintState (io:ob:f:eb:str) int)
where
eb = replicate spaces ' ' ++ "EXPRESION BOOLEANA:"
f = replicate (spaces+2) ' ' ++ "found"
ob = replicate (spaces+4) ' ' ++ "identificador objeto:"
io = replicate (spaces+6) ' ' ++ (getStr id)
printGuard spaces (CARRYING _ id) = do
(PrintState str int) <- get
put(PrintState (io:ob:f:eb:str) int)
where
eb = replicate spaces ' ' ++ "EXPRESION BOOLEANA:"
f = replicate (spaces+2) ' ' ++ "carrying"
ob = replicate (spaces+4) ' ' ++ "identificador objeto:"
io = replicate (spaces+6) ' ' ++ (getStr id)
-- Receives a list of TASKINTRs and traverses it to update the block number
traverseDefineInstrs :: [TASKINSTR] -> MyPrintStateM ()
traverseDefineInstrs [] = return ()
traverseDefineInstrs (tInst:tInsts) = do
traverseDefineInstr tInst
traverseDefineInstrs tInsts
-- Receives a TASKINTR and traverses it to update the block number
traverseDefineInstr :: TASKINSTR -> MyPrintStateM ()
traverseDefineInstr (BEGIN _ tInsts) = do
incBlockN
traverseDefineInstrs tInsts
traverseDefineInstr (DEFINE _ _ tInst) = do
incBlockN
traverseDefineInstr tInst
traverseDefineInstr (IF _ _ tInst) = do
incBlockN
traverseDefineInstr tInst
traverseDefineInstr (IFELSE _ _ tInst0 tInst1) = do
incBlockN
traverseDefineInstr tInst0
traverseDefineInstr tInst1
traverseDefineInstr (REPEAT _ _ tInst) = do
incBlockN
traverseDefineInstr tInst
traverseDefineInstr (WHILE _ _ tInst) = do
incBlockN
traverseDefineInstr tInst
traverseDefineInstr _ = return ()