forked from assalielmehdi/while-lang-interpreter
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.hs
279 lines (227 loc) · 7.57 KB
/
interpreter.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
import Data.Map
import Data.Maybe
import Text.Read
-- Grammar
data Stmt
= AssignStmt String ArithExp StmtAux
| IfStmt BoolExp Stmt Stmt StmtAux
| WhileStmt BoolExp Stmt StmtAux
deriving Show
data StmtAux
= NextStmt Stmt
| StopStmt
deriving Show
data BoolExp = BoolExp BoolTerm BoolExpAux deriving Show
data BoolExpAux
= BoolExpOr BoolTerm BoolExpAux
| StopBoolExp
deriving Show
data BoolTerm = BoolTerm BoolFactor BoolTermAux deriving Show
data BoolTermAux
= BoolTermAnd BoolFactor BoolTermAux
| StopBoolTerm
deriving Show
data BoolFactor
= BoolVal Bool
| BoolFactorPar BoolExp
| BoolFactorComp ArithComp
deriving Show
data ArithComp = ArithComp ArithExp ArithCompAux deriving Show
data ArithCompAux
= ArithCompGT ArithExp
| ArithCompLT ArithExp
deriving Show
data ArithExp = ArithExp Term ArithExpAux deriving Show
data ArithExpAux
= ArithPlus Term ArithExpAux
| ArithMinus Term ArithExpAux
| StopArith
deriving Show
data Term = Term Factor TermAux deriving Show
data TermAux
= TermMult Factor TermAux
| TermDiv Factor TermAux
| StopTerm
deriving Show
data Factor
= FactorVar String
| FactorInt Integer
| FactorPar ArithExp
deriving Show
-- Types
type SymbTable = Map String Integer
-- Statement functions
stmt :: [String] -> (Stmt, [String])
stmt ("if":tks) = do
let (b, "then":"{":tks') = boolExp tks
let (sTrue, "}":"else":"{":tks) = stmt tks'
let (sFalse, "}":tks') = stmt tks
let (next, tks) = stmtAux tks'
(IfStmt b sTrue sFalse next, tks)
stmt ("while":tks) = do
let (b, "do":"{":tks') = boolExp tks
let (s, "}":tks) = stmt tks'
let (next, tks') = stmtAux tks
(WhileStmt b s next, tks')
stmt (var:":=":tks) = do
let (a, tks') = arithExp tks
let (next, tks) = stmtAux tks'
(AssignStmt var a next, tks)
stmtAux :: [String] -> (StmtAux, [String])
stmtAux (";":tks) = do
let (s, tks') = stmt tks
(NextStmt s, tks')
stmtAux tks = (StopStmt, tks)
evalStmt :: Stmt -> SymbTable -> SymbTable
evalStmt (AssignStmt s a nxt) st = do
let val = evalArithExp a st
evalStmtAux nxt (insert s val st)
evalStmt (IfStmt b sTrue sFalse nxt) st = do
let pred = evalBoolExp b st
let st' = if pred then evalStmt sTrue st else evalStmt sFalse st
evalStmtAux nxt st'
evalStmt (WhileStmt b s nxt) st = do
let st' = evalWhileStmt b s st
evalStmtAux nxt st'
evalStmtAux :: StmtAux -> SymbTable -> SymbTable
evalStmtAux StopStmt st = st
evalStmtAux (NextStmt s) st = evalStmt s st
evalWhileStmt :: BoolExp -> Stmt -> SymbTable -> SymbTable
evalWhileStmt b s st = do
let pred = evalBoolExp b st
if not pred then
st
else do
let st' = evalStmt s st
evalWhileStmt b s st'
-- Boolean expression functions
boolExp :: [String] -> (BoolExp, [String])
boolExp tks = do
let (t, tks') = boolTerm tks
let (b, tks) = boolExpAux tks'
(BoolExp t b, tks)
boolExpAux :: [String] -> (BoolExpAux, [String])
boolExpAux ("or":tks) = do
let (t, tks') = boolTerm tks
let (b, tks) = boolExpAux tks'
(BoolExpOr t b, tks)
boolExpAux tks = (StopBoolExp, tks)
boolTerm :: [String] -> (BoolTerm, [String])
boolTerm tks = do
let (f, tks') = boolFactor tks
let (t, tks) = boolTermAux tks'
(BoolTerm f t, tks)
boolTermAux :: [String] -> (BoolTermAux, [String])
boolTermAux ("and":tks) = do
let (f, tks') = boolFactor tks
let (t, tks) = boolTermAux tks'
(BoolTermAnd f t, tks)
boolTermAux tks = (StopBoolTerm, tks)
boolFactor :: [String] -> (BoolFactor, [String])
boolFactor ("true":tks) = (BoolVal True, tks)
boolFactor ("false":tks) = (BoolVal False, tks)
boolFactor ("(":tks) = do
let (e, ")":tks') = boolExp tks
(BoolFactorPar e, tks')
boolFactor tks = do
let (c, tks') = arithComp tks
(BoolFactorComp c, tks')
arithComp :: [String] -> (ArithComp, [String])
arithComp tks = do
let (e, tks') = arithExp tks
let (a, tks) = arithCompAux tks'
(ArithComp e a, tks)
arithCompAux :: [String] -> (ArithCompAux, [String])
arithCompAux (">":tks) = do
let (e, tks') = arithExp tks
(ArithCompGT e, tks')
arithCompAux ("<":tks) = do
let (e, tks') = arithExp tks
(ArithCompLT e, tks')
evalBoolExp :: BoolExp -> SymbTable -> Bool
evalBoolExp (BoolExp t e) st = evalBoolExpAux (evalBoolTerm t st) e st
evalBoolExpAux :: Bool -> BoolExpAux -> SymbTable -> Bool
evalBoolExpAux prev StopBoolExp _ = prev
evalBoolExpAux prev (BoolExpOr t e) st = evalBoolExpAux (prev || evalBoolTerm t st) e st
evalBoolTerm :: BoolTerm -> SymbTable -> Bool
evalBoolTerm (BoolTerm f t) st = evalBoolTermAux (evalBoolFactor f st) t st
evalBoolTermAux :: Bool -> BoolTermAux -> SymbTable -> Bool
evalBoolTermAux prev StopBoolTerm _ = prev
evalBoolTermAux prev (BoolTermAnd f t) st = evalBoolTermAux (prev && evalBoolFactor f st) t st
evalBoolFactor :: BoolFactor -> SymbTable -> Bool
evalBoolFactor (BoolVal b) _ = b
evalBoolFactor (BoolFactorPar e) st = evalBoolExp e st
evalBoolFactor (BoolFactorComp c) st = evalArithComp c st
evalArithComp :: ArithComp -> SymbTable -> Bool
evalArithComp (ArithComp e c) st = evalArithCompAux (evalArithExp e st) c st
evalArithCompAux :: Integer -> ArithCompAux -> SymbTable -> Bool
evalArithCompAux prev (ArithCompGT e) st = prev > evalArithExp e st
evalArithCompAux prev (ArithCompLT e) st = prev < evalArithExp e st
-- Arithmetic expression functions
arithExp :: [String] -> (ArithExp, [String])
arithExp tks = do
let (t, tks') = term tks
let (a, tks) = arithExpAux tks'
(ArithExp t a, tks)
arithExpAux :: [String] -> (ArithExpAux, [String])
arithExpAux ("+":tks) = do
let (t, tks') = term tks
let (a, tks) = arithExpAux tks'
(ArithPlus t a, tks)
arithExpAux ("-":tks) = do
let (t, tks') = term tks
let (a, tks) = arithExpAux tks'
(ArithMinus t a, tks)
arithExpAux tks = (StopArith, tks)
term :: [String] -> (Term, [String])
term tks = do
let (f, tks') = factor tks
let (t, tks) = termAux tks'
(Term f t, tks)
termAux :: [String] -> (TermAux, [String])
termAux [] = (StopTerm, [])
termAux ("*":tks) = do
let (f, tks') = factor tks
let (t, tks) = termAux tks'
(TermMult f t, tks)
termAux ("/":tks) = do
let (f, tks') = factor tks
let (t, tks) = termAux tks'
(TermDiv f t, tks)
termAux tks = (StopTerm, tks)
factor :: [String] -> (Factor, [String])
factor ("(":tks) = do
let (a, ")":tks') = arithExp tks
(FactorPar a, tks')
factor (tk:tks)
| isNothing maybeNumber = (FactorVar tk, tks)
| otherwise = (FactorInt (read tk :: Integer), tks)
where maybeNumber = readMaybe tk :: Maybe Integer
evalArithExp :: ArithExp -> SymbTable -> Integer
evalArithExp (ArithExp t a) st = evalArithExpAux (evalTerm t st) a st
evalArithExpAux :: Integer -> ArithExpAux -> SymbTable -> Integer
evalArithExpAux left StopArith _ = left
evalArithExpAux left (ArithPlus t a) st = evalArithExpAux (left + evalTerm t st) a st
evalArithExpAux left (ArithMinus t a) st = evalArithExpAux (left - evalTerm t st) a st
evalTerm :: Term -> SymbTable -> Integer
evalTerm (Term f t) st = evalTermAux (evalFactor f st) t st
evalTermAux :: Integer -> TermAux -> SymbTable -> Integer
evalTermAux left StopTerm _ = left
evalTermAux left (TermMult f t) st = evalTermAux (left * evalFactor f st) t st
evalTermAux left (TermDiv f t) st = evalTermAux (left `div` evalFactor f st) t st
evalFactor :: Factor -> SymbTable -> Integer
evalFactor (FactorVar s) st = st ! s
evalFactor (FactorInt n) _ = n
evalFactor (FactorPar a) st = evalArithExp a st
-- Main
main :: IO ()
main = do
input <- getContents
let prgm = fst . stmt . words $ input
let st = evalStmt prgm $ fromList []
printSymbTable . assocs $ st
printSymbTable :: [(String, Integer)] -> IO ()
printSymbTable [] = putStr ""
printSymbTable ((var, val):rem) = do
putStrLn (var ++ " " ++ show val)
printSymbTable rem