-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressao.hs
More file actions
1068 lines (958 loc) · 36.2 KB
/
Expressao.hs
File metadata and controls
1068 lines (958 loc) · 36.2 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Expressao where
import System.IO.Unsafe
import Variavel
import Funcao
import Mensagens
import Char
import Lexico
-- Executa as instruções da função de entrada.
executaFuncao :: Funcao -> [Variavel] -> [Funcao] -> String
executaFuncao (nome, retorno, argumentos, instrucoes) variaveis funcoes =
let (saida, varsTmp, valor) = avalInstrucoes
(instrucoes)
(variaveis)
(funcoes)
(nome, retorno, argumentos, instrucoes)
(nome)
("") in
saida
avalFuncao :: Funcao -> [Variavel] -> [Funcao] -> Valor
avalFuncao (nome, retorno, argumentos, instrucoes) variaveis funcoes =
let (saida, varsTmp, valor) = avalInstrucoes
(instrucoes)
(variaveis)
(funcoes)
(nome, retorno, argumentos, instrucoes)
(nome)
("") in
valor
{-
- Avalia a lista de instrucoes que recebe. Se encontrar uma instrução 'return' então retorna o
- valor seguinte a 'return'. Caso não encontre uma instrução 'return', então retorna 'Void'.
-}
avalInstrucoes :: [String] -> [Variavel] -> [Funcao] -> Funcao -> String -> String -> (String, [Variavel], Valor)
avalInstrucoes instrucoes variaveis funcoes funcao escopo saida =
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if lexema == "\0" then
( saida ,
{-( ( do x <- printVariaveis variaveis ;
y <- putStr saida ;
return x ;
return y ) ,-}
--( ( do y <- putStr saida ;
-- return y ) ,
variaveis ,
VoidV )
else if elem lexema tiposVar then
let tipoVar = lexema ; tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if isIdValido lexema variaveis funcoes tipos then
let nomeVar = lexema ; tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if lexema == ";" then
avalInstrucoes
( instrucoes )
( variaveis ++ [ (nomeVar, (stringToValor tipoVar), escopo) ] )
( funcoes )
( funcao )
( escopo )
( saida )
else
exibeMsgErro (lexemaInesperado lexema)
else
exibeMsgErro (idInvalido lexema)
else if lexema == "cout" then
let tmp = instrucoes in
let (_, instrAval, instrucoes) = retornaAte tmp [";"] in
let cout = avalCout instrAval variaveis funcoes escopo in
avalInstrucoes
( instrucoes )
( variaveis )
( funcoes )
( funcao )
( escopo )
( saida ++ cout )
else if lexema == "cin" then
-- instrucoes = [ ">>" , "a" , ">>", "b" ";","a", "=", "b" ]
let tmp = instrucoes in
let (_, instrAval, instrucoes) = retornaAte tmp [";"] in
let variaveisCin = avalCin instrAval variaveis in
avalInstrucoes
( instrucoes )
( variaveisCin )
( funcoes )
( funcao )
( escopo )
( saida )
else if lexema == "if" then
-- ( ... ) { ... } else { ... }
let tmp = instrucoes in
let (exprIf, instrucoes) = getExprIf tmp [] in
let (saidaIf, vars) = avalIf exprIf variaveis funcoes funcao escopo in
avalInstrucoes
( instrucoes )
( vars )
( funcoes )
( funcao )
( escopo )
( saida ++ saidaIf )
else if lexema == "while" then
let tmp = instrucoes in
let (exprWhile, instrucoes) = getExprWhile tmp [] in
let (saidaWhile, varsWhile) = avalWhile exprWhile variaveis funcoes funcao escopo "" in
avalInstrucoes
( instrucoes )
( varsWhile )
( funcoes )
( funcao )
( escopo )
( saida ++ saidaWhile )
else if lexema == "return" then
let tmp = instrucoes in
let (status, instrAval, instrucoes) = retornaAte tmp [";"] in
if status == True then
let tipoExpr = getTipoExpr instrAval variaveis funcoes in
let tipoEsperado = getRetornoFuncao funcao in
if equalsTipo tipoExpr tipoEsperado then
let retorno = avalExpr tipoExpr instrAval variaveis funcoes in
( saida , variaveis , retorno )
else
exibeMsgErro (tipoEsperadoExpr (tipoToString tipoExpr) (tipoToString tipoEsperado))
else
exibeMsgErro ("esperado lexema \';\'")
else
let (status, variavel, variaveisEsq, variaveisDir) =
findVariavelByNome variaveis lexema in
if status == True then
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if lexema == "=" then
let tmp = instrucoes in
let (_, instrAval, instrucoes) = retornaAte tmp [";"] in
let tipoExpr = getTipoVariavel variavel in
let valor = avalExpr tipoExpr instrAval variaveis funcoes in
let tmpVar = ( getNomeVariavel variavel,
valor,
getEscopoVariavel variavel ) in
let variavel = tmpVar in
avalInstrucoes
( instrucoes )
( variaveisEsq ++ ( variavel:variaveisDir ) )
( funcoes )
( funcao )
( escopo )
( saida )
else
exibeMsgErro (lexemaInesperadoEsperado lexema "=")
else
let (status, funcao) =
findFuncaoByNome funcoes lexema in
if status == True then
let tmp = instrucoes in
let lexema = cabeca tmp; instrucoes = cauda tmp in
if (lexema == "(") then
let tmp = instrucoes in
let (expr, instrucoes) = getExprParentese tmp in
if cabeca instrucoes == ";" then
let args = getArgumentosFromParametros expr variaveis funcoes (getArgumentosFuncao funcao) in
let saidaFuncao = executaFuncao funcao args funcoes in
avalInstrucoes
( cauda instrucoes )
( variaveis )
( funcoes )
( funcao )
( escopo )
( saida ++ saidaFuncao )
else
exibeMsgErro (lexemaInesperadoEsperado (cabeca instrucoes) ";")
else
exibeMsgErro (lexemaInesperadoEsperado lexema "(")
else
exibeMsgErro (lexemaNaoDeclarado lexema)
-- Recebe uma lista de instrucoes e uma lista de lexemas. Quando encontrar um elemento x pertencente
-- a lexemas na lista de instrucoes, retorna uma dupla (A,B), onde (A ++ [x] ++ B) == instrucoes.
retornaAte instrucoes lexemas =
retornaAteRecur [] instrucoes lexemas
retornaAteRecur :: [String] -> [String] -> [String] -> (Bool,[String], [String])
retornaAteRecur x [] lexemas = (False,x,[])
retornaAteRecur x (h:t) lexemas =
if elem h lexemas then
(True, x, t)
else
retornaAteRecur (x ++ [h]) t lexemas
avalCout instrucoes variaveis funcoes escopo =
let tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if lexema == "<<" then
if instrucoes == [] then
exibeMsgErro (esperadoArgumentoCout)
else
avalCoutAux instrucoes variaveis funcoes escopo
else
exibeMsgErro (lexemaInesperadoEsperado lexema "<<")
avalCoutAux instrucoes variaveis funcoes escopo =
let tmp = instrucoes in
let (status, instrAval, instTail) = retornaAte tmp ["<<"] in
if status == True then
let instrucoes = ["<<"] ++ instTail in
if instrAval == [] then
""
else
let tipoExpr = getTipoExpr instrAval variaveis funcoes in
let val = avalExpr tipoExpr instrAval variaveis funcoes in
if equalsTipo tipoExpr Bool then
( show (getValorBool val) ) ++ (avalCout instrucoes variaveis funcoes escopo)
else if equalsTipo tipoExpr Char then
( show (getValorChar val) ) ++ (avalCout instrucoes variaveis funcoes escopo)
else if equalsTipo tipoExpr Int then
( show (getValorInt val) ) ++ (avalCout instrucoes variaveis funcoes escopo)
else if equalsTipo tipoExpr Float then
( show (getValorFloat val) ) ++ (avalCout instrucoes variaveis funcoes escopo)
else -- if equalsTipo tipoExpr String then
(getValorString val) ++ (avalCout instrucoes variaveis funcoes escopo)
else
let instrucoes = instTail in
if instrAval == [] then
""
else
let tipoExpr = getTipoExpr instrAval variaveis funcoes in
let val = avalExpr tipoExpr instrAval variaveis funcoes in
if equalsTipo tipoExpr Bool then
( show (getValorBool val) ) ++ (avalCoutAux instrucoes variaveis funcoes escopo)
else if equalsTipo tipoExpr Char then
( show (getValorChar val) ) ++ (avalCoutAux instrucoes variaveis funcoes escopo)
else if equalsTipo tipoExpr Int then
( show (getValorInt val) ) ++ (avalCoutAux instrucoes variaveis funcoes escopo)
else if equalsTipo tipoExpr Float then
( show (getValorFloat val) ) ++ (avalCoutAux instrucoes variaveis funcoes escopo)
else -- if equalsTipo tipoExpr String then
(getValorString val) ++ (avalCoutAux instrucoes variaveis funcoes escopo)
--let variaveisCin = avalCin instrAval variaveis in
-- ">> a >> b"
--avalCin :: [String] -> [Variavel] -> (IO (), [Variavel])
avalCin instrucoes variaveis =
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if lexema == ">>" then
if instrucoes == [] then
exibeMsgErro (esperadoArgumentoCin)
else
avalCinAux instrucoes variaveis
else
exibeMsgErro (lexemaInesperadoEsperado lexema ">>")
leituraTecladoInt =
do
x <- readVal
return x
where readVal :: IO Int
readVal = readLn
leituraTecladoChar =
do
x <- readVal
return x
where readVal :: IO Char
readVal = readLn
leituraTecladoFloat =
do
x <- readVal
return x
where readVal :: IO Float
readVal = readLn
leituraTecladoString =
do
x <- readVal
return x
where readVal :: IO String
readVal = readLn
leituraTecladoBool =
do
x <- readVal
return x
where readVal :: IO Bool
readVal = readLn
-- "a >> b"
--avalCinAux :: [String] -> [Variavel] -> IO Int
avalCinAux (h:t) variaveis =
let (status, variavel, ladoEsq, ladoDir) = findVariavelByNome variaveis h in
if status == True then
if equalsTipo Int (getTipoVariavel variavel) then
let val = unsafePerformIO (leituraTecladoInt) in
ladoEsq ++ [(h, (IntV val), (getEscopoVariavel variavel))] ++ ladoDir
else if equalsTipo Float (getTipoVariavel variavel) then
let val = unsafePerformIO (leituraTecladoFloat) in
ladoEsq ++ [(h, (FloatV val), (getEscopoVariavel variavel))] ++ ladoDir
else if equalsTipo Char (getTipoVariavel variavel) then
let val = unsafePerformIO (leituraTecladoChar) in
ladoEsq ++ [(h, (CharV val), (getEscopoVariavel variavel))] ++ ladoDir
else if equalsTipo String (getTipoVariavel variavel) then
let val = unsafePerformIO (leituraTecladoString) in
ladoEsq ++ [(h, (StringV val), (getEscopoVariavel variavel))] ++ ladoDir
else if equalsTipo Bool (getTipoVariavel variavel) then
let val = unsafePerformIO (leituraTecladoBool) in
ladoEsq ++ [(h, (BoolV val), (getEscopoVariavel variavel))] ++ ladoDir
else
exibeMsgErro ("AVJIASDIAS")
else
exibeMsgErro (lexemaNaoDeclarado h)
getExprIf (h:t) expr =
if h /= "{" then
getExprIf t (expr ++ [h])
else
let (exprIf, tmp) = getExprChaves t in
if cabeca tmp == "else" then
let (exprElse, instr) = getExprChaves (cauda (cauda tmp)) in
((expr ++ ["{"] ++ exprIf ++ ["}","else","{"] ++ exprElse ++ ["}"]), instr)
else
exibeMsgErro (lexemaInesperadoEsperado (cabeca tmp) "else")
getExprWhile (h:t) expr =
if h /= "{" then
getExprWhile t (expr ++ [h])
else
let (exprWhile, tmp) = getExprChaves t in
((expr ++ ["{"] ++ exprWhile ++ ["}"]), tmp)
{-
- [(expr) { } else { }]
-}
avalIf instrucoes variaveis funcoes funcao escopo =
let tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if lexema == "(" then
let tmp = instrucoes in
let (expr, instrucoes) = getExprParentese tmp in
let aval = avalExprBool expr variaveis funcoes in
if aval == True then
let tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if lexema == "{" then
let tmp = instrucoes in
let (expr, instrucoes) = getExprChaves tmp in
let (saida, varsIf, _) = avalInstrucoes (expr ++ ["\0"]) variaveis funcoes funcao (escopo ++ ".if") "" in
(saida, varsIf)
else
exibeMsgErro (lexemaInesperado lexema)
else
let tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if lexema == "{" then
let tmp = instrucoes in
let (expr, instrucoes) = getExprChaves tmp in
let tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if lexema == "else" && (cabeca instrucoes) == "{" then
let tmp = cauda instrucoes in
let (expr, instrucoes) = getExprChaves tmp in
let (saida, vars, _) = avalInstrucoes (expr ++ ["\0"]) variaveis funcoes funcao (escopo ++ ".else") "" in
(saida, vars)
else
exibeMsgErro (lexemaInesperado lexema)
else
exibeMsgErro (lexemaInesperado lexema)
else
exibeMsgErro (lexemaInesperado lexema)
{-
- ( ... ) { ... }
-}
avalWhile instrucoes variaveis funcoes funcao escopo saida =
let backupInstrucoes = instrucoes ; tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if lexema == "(" then
let tmp = instrucoes in
let (expr, instrucoes) = getExprParentese tmp in -- ( expr ) instrucoes = { ...}
let condicao = avalExprBool expr variaveis funcoes in
if condicao then
let tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if lexema == "{" then
let tmp = instrucoes in
let (instrWhile, _) = getExprChaves tmp in
let (saidaWhile, varsWhile, _) =
avalInstrucoes (instrWhile ++ ["\0"]) variaveis funcoes funcao (escopo ++ ".while") "" in
avalWhile backupInstrucoes varsWhile funcoes funcao escopo (saida ++ saidaWhile)
else
exibeMsgErro (lexemaInesperadoEsperado lexema "{")
else
(saida, variaveis)
else
exibeMsgErro (lexemaInesperadoEsperado lexema "(")
{-let teste = unsafePerformIO(print ("oi")) in
if (teste ==()) then
else
"oiiii"
-}
-- Determina se o lexema é um id valido.
isIdValido :: String -> [Variavel] -> [Funcao] -> [String] -> Bool
isIdValido (h:t) variaveis funcoes tipos =
if ( (isAlpha h) || (h == '_') ) && (isIdValidoRecur t) then
if ( not ( elem (h:t) (projecaoFuncao funcoes getNomeFuncao) ) ) &&
( not ( elem (h:t) (projecaoVariaveis variaveis getNomeVariavel) ) ) &&
( not ( elem (h:t) tipos ) ) &&
( not ( elem (h:t) palavrasReservadas ) ) then
True
else
exibeMsgErro (nomeReservadoOuJaUsado (h:t))
else
False
-- Auxiliar recursiva para a função isIdValido.
isIdValidoRecur :: String -> Bool
isIdValidoRecur [] = True
isIdValidoRecur (h:t) =
if ( (isAlphaNum h) || (h == '_') ) then
isIdValidoRecur t
else
False
--(valor, instrucoes) = avalExpr tipoExpr instrucoes variaveis funcoes
avalExpr :: Tipo -> [String] -> [Variavel] -> [Funcao] -> Valor
avalExpr Int instrucoes variaveis funcoes = IntV (avalExprInt instrucoes variaveis funcoes False)
avalExpr Float instrucoes variaveis funcoes = FloatV (avalExprFloat instrucoes variaveis funcoes False)
avalExpr String instrucoes variaveis funcoes = StringV (avalExprString instrucoes variaveis funcoes)
avalExpr Bool instrucoes variaveis funcoes = BoolV (avalExprBool instrucoes variaveis funcoes)
avalExpr Char instrucoes variaveis funcoes = CharV (avalExprChar instrucoes variaveis funcoes)
-- reads "3422" :: [(Int, String)]
-- printValor (get1De2 (avalExprInt ["2","+","4",";"," ..."] [] []))
-- avalExprInt (stringToLexemas "funcao()") [] ["funcao",(Int),[],(stringToLexemas "return a;")]
avalExprInt :: [String] -> [Variavel] -> [Funcao] -> Bool -> Int
avalExprInt instrucoes variaveis funcoes flag =
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if isLiteralInt lexema then
let literal = (read lexema)::Int in
if flag then avalExprIntAux instrucoes variaveis funcoes (-literal) False
else avalExprIntAux instrucoes variaveis funcoes (literal) False
else
let (status, variavel, _, _) = findVariavelByNome variaveis lexema in
if status == True then
let literal = getValorVariavelInt (variavel) in
if flag then avalExprIntAux instrucoes variaveis funcoes (-literal) False
else avalExprIntAux instrucoes variaveis funcoes (literal) False
else
--avalFuncao ("funcao",(Int),[],(stringToLexemas "return 1+2;")) [] [("funcao",(Int),[],(stringToLexemas "return 1+2;"))]
--avalExprInt (stringToLexemas "funcao()") [] [("funcao",(Int),[],(stringToLexemas "return 1+2;"))]
let (status, funcao) =
findFuncaoByNome funcoes lexema in
if status == True then
if equalsTipo Int (getRetornoFuncao funcao) then
let tmp = instrucoes in
let lexema = cabeca tmp; instrucoes = cauda tmp in
if (lexema == "(") then
let tmp = instrucoes in
let (expr, instrucoes) = getExprParentese tmp in
let args = getArgumentosFromParametros expr variaveis funcoes (getArgumentosFuncao funcao) in
let (IntV literal) = avalFuncao funcao args funcoes in
if flag then avalExprIntAux instrucoes variaveis funcoes (-literal) False
else avalExprIntAux instrucoes variaveis funcoes (literal) False
else
exibeMsgErro (lexemaInesperadoEsperado lexema "(")
else
exibeMsgErro ("tipo esperado \'int\'")
else
exibeMsgErro (lexemaNaoDeclarado lexema)
-- reads "3422" :: [(Int, String)]
-- printValor (get1De2 (avalExprIntRecur ["+","4",";"," ..."] [] []))
avalExprIntAux :: [String] -> [Variavel] -> [Funcao] -> Int -> Bool -> Int
avalExprIntAux [] _ _ literal flag = if flag then (-literal) else literal
avalExprIntAux instrucoes variaveis funcoes literal flag =
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if lexema == "+" then
literal + avalExprInt instrucoes variaveis funcoes flag
else if lexema == "-" then
literal + avalExprInt instrucoes variaveis funcoes True
else if elem lexema ["*","/"] then
let op = lexema ; tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if isLiteralInt lexema then
let fator = (read lexema)::Int in
if op == "*" then
avalExprIntAux instrucoes variaveis funcoes (literal * fator) flag
else
avalExprIntAux instrucoes variaveis funcoes (div literal fator) flag
else
let (status, variavel, _, _) = findVariavelByNome variaveis lexema in
if status == True then
let fator = getValorVariavelInt (variavel) in
if op == "*" then
avalExprIntAux instrucoes variaveis funcoes (literal * fator) flag
else
avalExprIntAux instrucoes variaveis funcoes (div literal fator) flag
else
exibeMsgErro (lexemaInesperado lexema)
else
exibeMsgErro (lexemaInesperado lexema)
-- max_int = 2147483647
-- min_int = 2147483648
isLiteralInt (h:t) =
if isDigit h then
isLiteralIntRecur t
else
False
isLiteralIntRecur [] = True
isLiteralIntRecur (h:t) =
if isDigit h then
isLiteralIntRecur t
else
False
-- avalExprFloat (stringToLexemas "2*2 - 1 + 10") [] []
avalExprFloat :: [String] -> [Variavel] -> [Funcao] -> Bool -> Float
avalExprFloat instrucoes variaveis funcoes flag =
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if isLiteralFloat lexema then
let literal = (read lexema)::Float in
if flag then avalExprFloatAux instrucoes variaveis funcoes (-literal) False
else avalExprFloatAux instrucoes variaveis funcoes literal False
else
let (status, variavel, _, _) = findVariavelByNome variaveis lexema in
if status == True then
let literal = getValorVariavelFloat (variavel) in
if flag then avalExprFloatAux instrucoes variaveis funcoes (-literal) False
else avalExprFloatAux instrucoes variaveis funcoes literal False
else
let (status, funcao) =
findFuncaoByNome funcoes lexema in
if status == True then
if equalsTipo Float (getRetornoFuncao funcao) then
let tmp = instrucoes in
let lexema = cabeca tmp; instrucoes = cauda tmp in
if (lexema == "(") then
let tmp = instrucoes in
let (expr, instrucoes) = getExprParentese tmp in
let args = getArgumentosFromParametros expr variaveis funcoes (getArgumentosFuncao funcao) in
let (FloatV literal) = avalFuncao funcao args funcoes in
if flag then avalExprFloatAux instrucoes variaveis funcoes (-literal) False
else avalExprFloatAux instrucoes variaveis funcoes (literal) False
else
exibeMsgErro (lexemaInesperadoEsperado lexema "(")
else
exibeMsgErro ("tipo esperado \'float\'")
else
exibeMsgErro (lexemaNaoDeclarado lexema)
avalExprFloatAux :: [String] -> [Variavel] -> [Funcao] -> Float -> Bool -> Float
avalExprFloatAux [] _ _ literal flag = if flag then -literal else literal
avalExprFloatAux instrucoes variaveis funcoes literal flag =
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if lexema == "+" then
literal + (avalExprFloat instrucoes variaveis funcoes flag)
else if lexema == "-" then
literal + (avalExprFloat instrucoes variaveis funcoes True)
else if elem lexema ["*","/"] then
let op = lexema ; tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if isLiteralFloat lexema then
let fator = (read lexema)::Float in
if op == "*" then
avalExprFloatAux instrucoes variaveis funcoes (literal * fator) flag
else
avalExprFloatAux instrucoes variaveis funcoes (literal / fator) flag
else
let (status, variavel, _, _) = findVariavelByNome variaveis lexema in
if status == True then
let fator = getValorVariavelFloat (variavel) in
if op == "*" then
avalExprFloatAux instrucoes variaveis funcoes (literal * fator) flag
else
avalExprFloatAux instrucoes variaveis funcoes (literal / fator) flag
else
exibeMsgErro (lexemaInesperado lexema)
else
exibeMsgErro (lexemaInesperado lexema)
isLiteralFloat (h:t) =
if (isDigit h) then
isLiteralFloatBeforePonto t
else
False
isLiteralFloatBeforePonto [] = True
isLiteralFloatBeforePonto (h:t) =
if (isDigit h) then
isLiteralFloatBeforePonto t
else if (h == '.') && (t /= []) then
isLiteralFloatAfterPonto t
else
False
isLiteralFloatAfterPonto [] = True
isLiteralFloatAfterPonto (h:t) =
if (isDigit h) then
isLiteralFloatAfterPonto t
else
False
avalExprString :: [String] -> [Variavel] -> [Funcao] -> String
avalExprString instrucoes variaveis funcoes =
let tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if isString lexema then -- Se for string "nome"
if instrucoes == [] then
removeAspasString lexema
else
exibeMsgErro (lexemaInesperado (cabeca instrucoes))
else -- Se for uma variável
let (status, variavel, _, _) = findVariavelByNome variaveis lexema in
if status == True then
getValorVariavelString (variavel)
else
exibeMsgErro (lexemaInesperado lexema)
avalExprChar :: [String] -> [Variavel] -> [Funcao] -> Char
avalExprChar instrucoes variaveis funcoes =
let tmp = instrucoes in
let lexema = cabeca tmp ; instrucoes = cauda tmp in
if isChar lexema then -- Se for Char 'a'
if instrucoes == [] then
read (lexema)::Char
else
exibeMsgErro (lexemaInesperado (cabeca instrucoes))
else -- Se for uma variável
let (status, variavel, _, _) = findVariavelByNome variaveis lexema in
if status == True then
getValorVariavelChar (variavel)
else
exibeMsgErro (lexemaInesperado lexema)
-- Lê uma expressão e retorna seu tipo.
getTipoExpr :: [String] -> [Variavel] -> [Funcao] -> Tipo
getTipoExpr instrucoes variaveis funcoes =
if isExprString instrucoes variaveis funcoes then
String
else if isExprBool instrucoes variaveis funcoes then
Bool
else if isExprChar instrucoes variaveis funcoes then
Char
else
let (status, tipo) = isExprAritmetica instrucoes variaveis funcoes in
if status == True then
if tipo == "int" then
Int
else
Float
else
exibeMsgErro (expressaoInvalida)
isExprChar instrucoes variaveis funcoes =
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if isChar lexema then
True
else
let (status, variavel, _, _) = findVariavelByNome variaveis lexema in
if status == True then
equalsTipo (getTipoVariavel variavel) Char
else
False -- TODO verificar funcao
isExprString instrucoes variaveis funcoes =
let tmp = instrucoes in
let instrucoes = cauda tmp ; lexema = cabeca tmp in
if isString lexema then
True
else
let (status, variavel, _, _) = findVariavelByNome variaveis lexema in
if status == True then
equalsTipo (getTipoVariavel variavel) String
else
--let (status, funcao) = findFuncaoByNome funcoes lexema in
--if status == True then
-- equalsTipo (getRetornoFuncao funcao) String
--else
False -- TODO verificar funcao
isString (h:t) = h == '"'
isChar (h:t) = h == '\''
removeAspasString (h:t) = removeAspasStringAux "" t
removeAspasStringAux esq (h:t) = if t == "" then esq else removeAspasStringAux (esq ++ [h]) t
isLiteralBool lexema = elem lexema ["true","false"]
{-
- Possíveis operadores booleanos:
-
- bool [||,&&] bool
- bool,int,float,char [==,!=] bool,int,float,char
- ! bool
- [int,float,char] [>,>=,<,<=] [int,float,char]
-
- Exemplos:
- ! True != False --> False != False --> True
- ! 5 > 6
-}
avalExprBool :: [String] -> [Variavel] -> [Funcao] -> Bool
avalExprBool (h:t) variaveis funcoes =
-- ! -> not (avalExprBool cauda ate axar a primeira expr booleana a frente)
if h == "!" then
let (lhs, rhs) = getFirstExprBool t variaveis funcoes in
let result = not (avalExprBool lhs variaveis funcoes) in
avalExprCmpBool rhs variaveis funcoes result
else if h == "(" then
let (lhs, rhs) = getExprParentese t in
let result = avalExprBool lhs variaveis funcoes in
avalExprCmpBool rhs variaveis funcoes result
-- Literal booleano
else if isLiteralBool h then
if h == "false" then
avalExprCmpBool t variaveis funcoes False
else
avalExprCmpBool t variaveis funcoes True
-- Literal inteiro
else if isLiteralFloat h then
let (tipo, op1, op, op2, exprTail) =
getFirstExprCmpBoolAritmetica (h:t) variaveis funcoes in
if tipo == "int" then
let avalOp1 = avalExprInt op1 variaveis funcoes False ;
avalOp2 = avalExprInt op2 variaveis funcoes False in
avalExprCmpBool exprTail variaveis funcoes (avalExprCmpInt avalOp1 op avalOp2)
else -- tipo == "float"
let avalOp1 = avalExprFloat op1 variaveis funcoes False ;
avalOp2 = avalExprFloat op2 variaveis funcoes False in
avalExprCmpBool exprTail variaveis funcoes (avalExprCmpFloat avalOp1 op avalOp2)
else
let (status, variavel, _, _) = findVariavelByNome variaveis h in
if status == True then
if equalsTipo Bool (getTipoVariavel variavel) then
avalExprCmpBool t variaveis funcoes (getValorVariavelBool variavel)
else if ( equalsTipo Int (getTipoVariavel variavel) ||
equalsTipo Float (getTipoVariavel variavel) ) then
let (tipo, op1, op, op2, exprTail) =
getFirstExprCmpBoolAritmetica (h:t) variaveis funcoes in
if tipo == "int" then
let avalOp1 = avalExprInt op1 variaveis funcoes False ;
avalOp2 = avalExprInt op2 variaveis funcoes False in
avalExprCmpBool exprTail variaveis funcoes (avalExprCmpInt avalOp1 op avalOp2)
else -- tipo == "float"
let avalOp1 = avalExprFloat op1 variaveis funcoes False ;
avalOp2 = avalExprFloat op2 variaveis funcoes False in
avalExprCmpBool exprTail variaveis funcoes (avalExprCmpFloat avalOp1 op avalOp2)
else
exibeMsgErro (tipoInvalidoOpAritmetica (tipoToString (getTipoVariavel variavel)))
else
let (status, funcao) =
findFuncaoByNome funcoes h in
if status == True then
if equalsTipo Bool (getRetornoFuncao funcao) then
let tmp = t in
let lexema = cabeca tmp; instrucoes = cauda tmp in
if (lexema == "(") then
let tmp = instrucoes in
let (expr, instrucoes) = getExprParentese tmp in
let args = getArgumentosFromParametros expr variaveis funcoes (getArgumentosFuncao funcao) in
let (BoolV literal) = avalFuncao funcao args funcoes in
avalExprCmpBool instrucoes variaveis funcoes (literal)
else
exibeMsgErro (lexemaInesperadoEsperado h "(")
else
exibeMsgErro ("tipo esperado \'bool\'")
else
exibeMsgErro (lexemaNaoDeclarado h)
-- bool,int,float,char [==,!=] bool,int,float,char
-- ! bool
-- [int,float,char] [>,>=,<,<=] [int,float,char]
--avalExprCmp :: [String] -> [Variavel] -> [Funcao] -> Valor -> Valor
--avalExprCmp instrucoes variaveis funcoes (BoolV aval) =
-- BoolV (avalExprCmpBool instrucoes variaveis funcoes aval)
avalExprCmpBool :: [String] -> [Variavel] -> [Funcao] -> Bool -> Bool
avalExprCmpBool [] variaveis funcoes op1 = op1
avalExprCmpBool (h:t) variaveis funcoes op1 =
if h == "==" then
let (lhs, rhs) = getFirstExprBool t variaveis funcoes in
let op2 = avalExprBool lhs variaveis funcoes in
avalExprCmpBool rhs variaveis funcoes (op1 == op2)
else if h == "!=" then
let (lhs, rhs) = getFirstExprBool t variaveis funcoes in
let op2 = avalExprBool lhs variaveis funcoes in
avalExprCmpBool rhs variaveis funcoes (op1 /= op2)
else if h == "&&" then
let (lhs, rhs) = getFirstExprBool t variaveis funcoes in
if (rhs /= []) && (cabeca rhs == "||") then
let op2 = avalExprBool lhs variaveis funcoes in
avalExprCmpBool rhs variaveis funcoes (op1 && op2)
else
op1 && (avalExprBool t variaveis funcoes)
else if h == "||" then
op1 || (avalExprBool t variaveis funcoes)
else
exibeMsgErro (lexemaInesperado h)
avalExprCmpInt :: Int -> String -> Int -> Bool
avalExprCmpInt op1 op op2 =
if op == ">" then (op1 > op2)
else if op == ">=" then (op1 >= op2)
else if op == "<" then (op1 < op2)
else if op == "<=" then (op1 <= op2)
else if op == "==" then (op1 == op2)
else (op1 /= op2) -- h == "!="
avalExprCmpFloat :: Float -> String -> Float -> Bool
avalExprCmpFloat op1 op op2 =
if op == ">" then (op1 > op2)
else if op == ">=" then (op1 >= op2)
else if op == "<" then (op1 < op2)
else if op == "<=" then (op1 <= op2)
else if op == "==" then (op1 == op2)
else (op1 /= op2) -- h == "!="
-- <operador> <bool>
avalExprBoolAux :: [String] -> [Variavel] -> [Funcao] -> Bool -> Bool
avalExprBoolAux [] variaveis funcoes aval = aval
--avalExprBoolAux (h:t) variaveis funcoes aval =
-- Retorna a primeira expressão booleana que encontrar na lista de lexemas.
-- true != false && 5 > 2
-- a != true
-- retorna: (FirstExprBool , LexemasRestantes)
getFirstExprBool :: [String] -> [Variavel] -> [Funcao] -> ([String],[String])
getFirstExprBool lexemas variaveis funcoes =
getFirstExprBoolAux lexemas variaveis funcoes []
getFirstExprBoolAux :: [String] -> [Variavel] -> [Funcao] -> [String] -> ([String],[String])
getFirstExprBoolAux (h:t) variaveis funcoes expr =
if h == "!" then
getFirstExprBoolAux t variaveis funcoes (expr ++ ["!"])
else if h == "(" then
getExprParentese t
else if isLiteralBool h then
(expr ++ [h], t)
else if isLiteralFloat h then
let (tipo, op1, op, op2, exprTail) =
getFirstExprCmpBoolAritmetica (h:t) variaveis funcoes in
(expr ++ op1 ++ [op] ++ op2, exprTail)
{-
let (status, variavel, _, _) = findVariavelByNome variaveis h in
if status == True then
if equalsTipo Bool (getTipoVariavel variavel) then
([h], t)
else
exibeMsgErro (tipoEsperadoBool h (tipoToString (getTipoVariavel variavel)))
else-}
else
exibeMsgErro (lexemaInesperado h)
getFirstExprCmpBoolAritmetica :: [String] -> [Variavel] -> [Funcao] ->
(String,[String],String,[String],[String])
getFirstExprCmpBoolAritmetica expr variaveis funcoes =
let (tipo1, op1, exprTail) = getFirstExprAritmetica expr variaveis funcoes in
if elem (cabeca exprTail) [">",">=","<","<=","==","!="] then
let (tipo2, op2, expr) = getFirstExprAritmetica (cauda exprTail) variaveis funcoes in
if tipo1 == "int" && tipo2 == "int" then
("int", op1, (cabeca exprTail), op2, expr)
else if (tipo1 == "int" && tipo2 == "float") ||
(tipo1 == "float" && tipo2 == "int") ||
(tipo1 == "float" && tipo2 == "float") then
("float", op1, (cabeca exprTail), op2, expr)
else
exibeMsgErro (tiposDiferentesCmp (cabeca exprTail))
else
exibeMsgErro (lexemaInesperado (cabeca exprTail))
-- Obtem a primeira expressão aritmética que encontrar.
getFirstExprAritmetica :: [String] -> [Variavel] -> [Funcao] -> (String,[String],[String])
getFirstExprAritmetica (h:t) variaveis funcoes =
getFirstExprAritmeticaAux (h:t) variaveis funcoes [] "int"
getFirstExprAritmeticaAux :: [String] -> [Variavel] -> [Funcao] ->
[String] -> String -> (String,[String],[String])
getFirstExprAritmeticaAux [] variaveis funcoes expr tipo = (tipo, expr, [])
getFirstExprAritmeticaAux (h:t) variaveis funcoes expr tipo =
if elem h [">",">=","<","<=","==","!=","&&","||"] then
(tipo, expr, (h:t))
else if (isLiteralFloat h) && (not (isLiteralInt h)) then
getFirstExprAritmeticaAux t variaveis funcoes (expr ++ [h]) "float"
else
let (status, variavel, _, _) = findVariavelByNome variaveis h in
if status == True then
if equalsTipo Float (getTipoVariavel variavel) then
getFirstExprAritmeticaAux t variaveis funcoes (expr ++ [h]) "float"
else
getFirstExprAritmeticaAux t variaveis funcoes (expr ++ [h]) tipo
else
getFirstExprAritmeticaAux t variaveis funcoes (expr ++ [h]) tipo
getExprParentese :: [String] -> ([String], [String])
getExprParentese lexemas =
getExprParenteseAux lexemas [] 0
getExprParenteseAux :: [String] -> [String] -> Int -> ([String], [String])
getExprParenteseAux [] expr n = exibeMsgErro (expressaoInvalida)
getExprParenteseAux (h:t) expr n =
if h == ")" then
if n == 0 then
(expr, t)
else
getExprParenteseAux t (expr ++ [h]) (n-1)
else if h == "(" then
getExprParenteseAux t (expr ++ [h]) (n+1)
else
getExprParenteseAux t (expr ++ [h]) n
getExprChaves :: [String] -> ([String], [String])
getExprChaves lexemas =
getExprChavesAux lexemas [] 0
getExprChavesAux :: [String] -> [String] -> Int -> ([String], [String])
getExprChavesAux [] expr n = exibeMsgErro (expressaoInvalida)
getExprChavesAux (h:t) expr n =
if h == "}" then
if n == 0 then
(expr, t)
else
getExprChavesAux t (expr ++ [h]) (n-1)
else if h == "{" then
getExprChavesAux t (expr ++ [h]) (n+1)
else
getExprChavesAux t (expr ++ [h]) n
isExprBool (h:t) variaveis funcoes =
if t == [] then
if isLiteralBool h then
True
else
let (status, variavel, _, _) = findVariavelByNome variaveis h in
if status == True then
equalsTipo (getTipoVariavel variavel) Bool
else
False
else
isExprBoolAux (h:t) variaveis funcoes
isExprBoolAux [] variaveis funcoes = False
isExprBoolAux (h:t) variaveis funcoes =
if elem h ["==","!","!=",">",">=","<","<=","||","&&"] then
True
else
isExprBoolAux t variaveis funcoes