-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ast.hs
206 lines (178 loc) · 7.23 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
module Ast
( Ident
, Command(..)
, BoolExp(..)
, NumExp(..)
, readCommand
) where
type Ident = String
data NumExp a =
Const a |
Var Ident |
(NumExp a) `Div` (NumExp a) |
(NumExp a) `Times` (NumExp a) |
(NumExp a) `Plus` (NumExp a) |
(NumExp a) `Minus` (NumExp a)
instance Show a => Show (NumExp a) where
show (Const x) = show x
show (Var x) = x
show (ex1 `Div` ex2) = show ex1 ++ " / " ++ show ex2
show (ex1 `Times` ex2) = show ex1 ++ " * " ++ show ex2
show (ex1 `Plus` ex2) = show ex1 ++ " + " ++ show ex2
show (ex1 `Minus` ex2) = show ex1 ++ " - " ++ show ex2
data BoolExp a =
NOT (BoolExp a) |
(BoolExp a) `OR` (BoolExp a) |
(BoolExp a) `AND` (BoolExp a) |
(NumExp a) `Gt` (NumExp a) |
(NumExp a) `Eq` (NumExp a)
instance Show a => Show (BoolExp a) where
show (NOT ex) = "NOT " ++ show ex
show (ex1 `OR` ex2) = show ex1 ++ " OR " ++ show ex2
show (ex1 `AND` ex2) = show ex1 ++ " AND " ++ show ex2
show (ex1 `Gt` ex2) = show ex1 ++ " > " ++ show ex2
show (ex1 `Eq` ex2) = show ex1 ++ " = " ++ show ex2
data Command a =
Input Ident |
Print Ident |
Seq [Command a] |
Ident `Assign` (NumExp a) |
Cond [BoolExp a] [Command a] |
Loop (BoolExp a) (Command a)
instance Show a => Show (Command a) where
show = prettyPrint 0
prettyPrint :: Show a => Int -> Command a -> String
prettyPrint n (Input var) = ident n ++ "INPUT " ++ var ++ ";\n"
prettyPrint n (Print var) = ident n ++ "PRINT " ++ var ++ ";\n"
prettyPrint n (var `Assign` ex) = ident n ++ var ++ " := " ++ show ex ++ ";\n"
prettyPrint n (Seq xs) = concatMap (prettyPrint n) xs
prettyPrint n (Cond x y) = printIfClauses n x y True
prettyPrint n (Loop ex command) = header ++ body ++ end
where
header = ident n ++ "WHILE " ++ show ex ++ "\n"++ ident n ++ "DO\n"
body = prettyPrint (n+1) command
end = ident n ++ "END\n"
printIfClauses :: Show a => Int -> [BoolExp a] -> [Command a] -> Bool -> String
printIfClauses n [] [] False = ident n ++ "END\n"
printIfClauses n [] (command:[]) False = header ++ body ++ nextClauses
where
header = ident n ++ "ELSE\n"
body = prettyPrint (n+1) command
nextClauses = (printIfClauses :: Int -> [BoolExp ()] -> [Command ()] -> Bool -> String) n [] [] False
printIfClauses n (x:xs) (command:ys) False = header ++ body ++ nextClauses
where
header = ident n ++ "ELIF " ++ show x ++ " THEN\n"
body = prettyPrint (n+1) command
nextClauses = printIfClauses n xs ys False
printIfClauses n (x:xs) (command:ys) True = header ++ body ++ nextClauses
where
header = ident n ++ "IF " ++ show x ++ " THEN\n"
body = prettyPrint (n+1) command
nextClauses = printIfClauses n xs ys False
ident :: Int -> String
ident n = replicate (2*n) ' '
grabThisBody :: Int -> [String] -> ([String],[String])
grabThisBody 0 ("END":xs) = ([],[])
grabThisBody 0 ("ELIF":xs) = ([],"ELIF":xs)
grabThisBody 0 ("ELSE":xs) = ([],"ELSE":xs)
grabThisBody n ("END":xs) = ("END":ys, zs)
where (ys, zs) = grabThisBody (n-1) xs
grabThisBody n (x:xs)
| x == "IF" || x == "WHILE" = (x:ys, zs)
where (ys, zs) = grabThisBody (n+1) xs
grabThisBody n (x:xs) = (x:ys,zs)
where (ys, zs) = grabThisBody n xs
grabCondition :: Int -> [String] -> ([String],[String])
grabCondition 0 ("END":xs) = (["END"],xs)
grabCondition n ("END":xs) = ("END":ys, zs)
where (ys, zs) = grabCondition (n-1) xs
grabCondition n (x:xs)
| x == "IF" || x == "WHILE" = (x:ys, zs)
where (ys, zs) = grabCondition (n+1) xs
grabCondition n (x:xs) = (x:ys,zs)
where (ys, zs) = grabCondition n xs
grabInstruction :: [String] -> ([String],[String])
grabInstruction prog = (instruction, rest)
where (instruction, (";":rest)) = span (/=";") prog
parseNumExp :: Read a => [String] -> NumExp a
parseNumExp xs
| not (null rest) && head rest == "+" = parsedLeftNumExp `Plus` (parseNumExp (tail rest))
| not (null rest) && head rest == "-" = parsedLeftNumExp `Minus` (parseNumExp (tail rest))
where
notNumOp x = x/="+" && x/="-"
(leftNumExp, rest) = span notNumOp xs
parsedLeftNumExp = parseNumExp leftNumExp
parseNumExp xs
| not (null rest) && head rest == "*" = parsedLeftNumExp `Times` (parseNumExp (tail rest))
| not (null rest) && head rest == "/" = parsedLeftNumExp `Div` (parseNumExp (tail rest))
where
notNumOp x = x/="*" && x/="/"
(leftNumExp, rest) = span notNumOp xs
parsedLeftNumExp = parseNumExp leftNumExp
parseNumExp (num@(digit:rest):[])
| '0' <= digit && digit <= '9' = Const $ read num
parseNumExp (x:[]) = Var x
parseBoolExp :: Read a => [String] -> BoolExp a
parseBoolExp ("NOT":xs) = NOT (parseBoolExp xs)
parseBoolExp xs
| not (null rest) && head rest == "OR" = parsedLeftBoolExp `OR` (parseBoolExp (tail rest))
| not (null rest) && head rest == "AND" = parsedLeftBoolExp `AND` (parseBoolExp (tail rest))
where
notBoolOp x = x/="OR" && x/="AND"
(leftBoolExp, rest) = span notBoolOp xs
parsedLeftBoolExp = parseBoolExp leftBoolExp
parseBoolExp xs
| not (null rest) && head rest == ">" = parsedLeftNumExp `Gt` (parseNumExp (tail rest))
| not (null rest) && head rest == "=" = parsedLeftNumExp `Eq` (parseNumExp (tail rest))
where
notBoolOp x = x/=">" && x/="="
(leftNumExp, rest) = span notBoolOp xs
parsedLeftNumExp = parseNumExp leftNumExp
parse :: Read a => [String] -> Command a
parse [] = Cond [] []
parse ("IF":xs) = Cond (thisExpression : ys) (thisCommand : zs)
where
(expression, ("THEN":rest)) = span (/="THEN") xs
(thisBody, nextClause) = grabThisBody 0 rest
thisExpression = parseBoolExp expression
thisCommand = Seq $ map parse $ blocks thisBody
(Cond ys zs) = parse nextClause
parse ("ELIF":xs) = Cond (thisExpression : ys) (thisCommand : zs)
where
(expression, ("THEN":rest)) = span (/="THEN") xs
(thisBody, nextClause) = grabThisBody 0 rest
thisExpression = parseBoolExp expression
thisCommand = Seq $ map parse $ blocks thisBody
(Cond ys zs) = parse nextClause
parse ("ELSE":xs) = Cond [] $ [Seq (map parse (blocks (init xs)))]
parse ("WHILE":xs) = Loop (parseBoolExp expression) $ Seq $ map parse $ blocks $ init rest
where (expression, ("DO":rest)) = span (/="DO") xs
parse (x:y:xs)
| x == "INPUT" = Input y
| x == "PRINT" = Print y
| y == ":=" = Assign x $ parseNumExp xs
blocks :: [String] -> [[String]]
blocks [] = []
blocks prog@(x:xs)
| x == "IF" =
let (block, rest) = grabCondition 0 xs
in ("IF" : block) : blocks rest
| x == "WHILE" =
let (block, rest) = grabCondition 0 xs
in ("WHILE" : block) : blocks rest
| otherwise =
let (block, rest) = grabInstruction prog
in block : blocks rest
putCol :: [String] -> [String]
putCol [] = []
putCol (x:"END":xs)
| x /= ";" && x /= "END" = x : ";" : "END" : putCol xs
putCol (x:xs) = x : putCol xs
separateCol :: String -> String
separateCol [] = []
separateCol (';':xs) = ' ' : ';' : separateCol xs
separateCol (x:xs) = x : separateCol xs
prepare :: String -> [String]
prepare = putCol . words . separateCol
readCommand :: Read a => String -> Command a
readCommand input = Seq $ map parse $ blocks $ prepare input