-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTConverter.hs
More file actions
109 lines (82 loc) · 2.23 KB
/
ASTConverter.hs
File metadata and controls
109 lines (82 loc) · 2.23 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
module ASTConverter where
import AbsAssignment
import qualified AST as A
transIdent :: Ident -> String
transIdent x = case x of
Ident string -> string
transProg :: Prog -> A.Prog String String
transProg x = case x of
PROG funs -> A.Prog $ map transFun funs
transFun :: Fun -> A.Fun String String
transFun x = case x of
FUNS ident idents exp ->
A.Fun (
transIdent ident,
map transIdent idents,
transExp exp
)
transExp :: Exp -> A.Exp String String
transExp x = case x of
EXP_ADD exp term ->
A.ADD (transExp exp)
(transTerm term)
EXP_SUB exp term ->
A.SUB (transExp exp)
(transTerm term)
EXP_Term term ->
transTerm term
transTerm :: Term -> A.Exp String String
transTerm x = case x of
TERM_MUL term factor ->
A.MUL (transTerm term)
(transFactor factor)
TERM_DIV term factor ->
A.DIV (transTerm term)
(transFactor factor)
TERM_FACT factor ->
transFactor factor
transFactor :: Factor -> A.Exp String String
transFactor x = case x of
FACT_CONST integer ->
A.CONST (fromInteger integer::Int)
FACT_VAR ident ->
A.VAR (transIdent ident)
FACT_NEG exp ->
A.NEG (transExp exp)
FACT_APP ident exps ->
A.APP (transIdent ident)
(map transExp exps)
FACT_LET funs exp ->
A.LET (map transFun funs)
(transExp exp)
EXP_COND bexp exp1 exp2 ->
A.COND (transBExp bexp)
(transExp exp1)
(transExp exp2)
transBExp :: BExp -> A.BExp String String
transBExp x = case x of
BEXP_AND bexp bterm ->
A.AND (transBExp bexp)
(transBTerm bterm)
BEXP_OR bexp bterm ->
A.OR (transBExp bexp)
(transBTerm bterm)
BEXP_TERM bterm ->
transBTerm bterm
transBTerm :: BTerm -> A.BExp String String
transBTerm x = case x of
BTERM_GT exp1 exp2 ->
A.Gt (transExp exp1)
(transExp exp2)
BTERM_LT exp1 exp2 ->
A.Gt (transExp exp1)
(transExp exp2)
BTERM_EQ exp1 exp2 ->
A.Eq (transExp exp1)
(transExp exp2)
BTERM_NOT bexp ->
A.NOT (transBExp bexp)
BTERM_TRUE ->
A.TRUE
BTERM_FALSE ->
A.FALSE