-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMiniRubyInterpreterSpec.hs
422 lines (373 loc) · 12 KB
/
MiniRubyInterpreterSpec.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import MiniRubyAST
import MiniRubyInterpreter
import Data.String
-- Run the tests
-- Can evaluate simple expressions
test1 = runProg $ simpleExpr (DividedBy (Times (Plus (IntConst 3) (IntConst 4)) (IntConst 2)) (IntConst 3))
res1 = Right ("4\n")
-- Can print Self
test5 = runProg $ simpleExpr (Self)
res5 = Right ("#<object 0>\n")
-- Can set and read fields
test7 = runProg $ simpleMethod [(SetField "x" (IntConst 10)),(CallMethod (ReadField "x") "puts" [])]
res7 = Right ("10\n")
-- Can set and read vars
test8 = runProg $ simpleMethod [(SetVar "x" (IntConst 10)),(CallMethod (ReadVar "x") "puts" [])]
res8 = Right ("10\n")
-- Can match against strings
test10 = runProg $ simpleMethod [ Match (StringConst "x") [ (ConstString "x", [(CallMethod (IntConst 1) "puts" [])]) ] ]
res10 = Right ("1\n")
-- Can match against ints
test11 = runProg $ simpleMethod [ Match (IntConst 1) [ (ConstInt 1, [(CallMethod (IntConst 1) "puts" [])]) ] ]
res11 = Right ("1\n")
-- Cannot match against unequal ints
test14 = runProg $ simpleMethod [ Match (IntConst 2) [ (ConstInt 1, [(CallMethod (IntConst 1) "puts" [])]) ] ]
res14 = Right ("")
-- Can match against anyvalues, and they are bound
test12 = runProg $ simpleMethod [ Match (IntConst 1) [ (AnyValue "k", [(CallMethod (ReadVar "k") "puts" [])]) ] ]
res12 = Right ("1\n")
-- Can match against anyvalues, and they are bound
test21 = runProg $ simpleMethod [ Match (TermLiteral "foo" [(StringConst "hej"),(StringConst "Jens")]) [ (TermPattern "foo" ["a","b"], [(CallMethod (ReadVar "b") "puts" [])]) ] ]
res21 = Right ("Jens\n")
-- Cannot match strings against ints
test13 = runProg $ simpleMethod [ Match (StringConst "1") [ (ConstInt 1, [(CallMethod (IntConst 1) "puts" [])]) ] ]
res13 = Right ("")
-- Can call a method in the main class
test15 = runProg $ simpleMethod2 [(CallMethod (Self) "SayTwo" [(IntConst 1),(IntConst 2)])]
res15 = Right "1\n2\n"
-- Can create a new object
test16 = runProg $ simpleProgram [(New "Person" [(StringConst "Mr.")])]
res16 = Right "My title is:\nMr.\n"
-- Can create a new object and call its method
test17 = runProg $ simpleProgram [(CallMethod (New "Person" [(StringConst "Mr.")]) "dinner" [(StringConst "burger"),(StringConst"cola")])]
res17 = Right "My title is:\nMr.\neating:\nburger\ndrinkings:\ncola\n"
-- Unknown methods hits the receive method
test23 = runProg $ simpleProgram3
res23 = Right "hej(du)\n"
---- Testing: Errors
-- Cannot call methods of non-object values (only puts)
test24 = runProg $ simpleExpr (CallMethod (StringConst "What?") "noMethod" [])
res24 = Left (Error {unError = "Cannot call methods on non-objects. (only puts)" })
-- Cannot call an unknown method, when no receive method is defined.
test22 = runProg $ simpleProgram [(CallMethod (New "Person" [(StringConst "Mr.")]) "jump" [(StringConst "burger"),(StringConst"cola")])]
res22 = Left (Error {unError = "No receive method defined"})
-- Cannot read unset variable
test2 = runProg $ simpleExpr (ReadVar "hej")
res2 = Left (Error "Variable or parameter `hej` could not be found")
-- Cannot read unset field
test9 = runProg $ simpleExpr (ReadField "hej")
res9 = Left (Error "Field `hej` could not be found")
-- Cannot find the class
test3 = runProg $ simpleMethod [(New "Person" [])]
res3 = Left (Error "Could not find a `Person` class.")
-- Wrong number of args
test4 = runProg $ simpleMethod2 [(CallMethod (Self) "SayTwo" [])]
res4 = Left (Error "Wrong number of arguments!")
-- Cannot do arithmetic with non-integers
test6 = runProg $ simpleExpr (Plus (StringConst "x") (IntConst 4))
res6 = Left (Error "Cannot do arithmetic with non-integers")
-- There are doublicate classes
test18 = runProg $ dublicateClassesProgram
res18 = Left (Error "There are dublicate class definitions")
-- Integration test
test19 = runProg $ simpleProgram2
res19 = Right "4\n2\n"
-- Can return with Return
test20 = runProg $ simpleMethod [(Return (CallMethod (StringConst "1") "puts" [])),(CallMethod (StringConst "2") "puts" [])]
res20 = Right "1\n"
-- zend works
test25 = runProg $ simpleProgram4
res25 = Right "tack\nnoMethod(tuck)\n"
---- Main
main = do
putStrLn $ show (test1 == res1) ++ ": " ++ (show test1)
putStrLn $ show (test2 == res2) ++ ": " ++ (show test2)
putStrLn $ show (test3 == res3) ++ ": " ++ (show test3)
putStrLn $ show (test4 == res4) ++ ": " ++ (show test4)
putStrLn $ show (test5 == res5) ++ ": " ++ (show test5)
putStrLn $ show (test6 == res6) ++ ": " ++ (show test6)
putStrLn $ show (test7 == res7) ++ ": " ++ (show test7)
putStrLn $ show (test8 == res8) ++ ": " ++ (show test8)
putStrLn $ show (test9 == res9) ++ ": " ++ (show test9)
putStrLn $ show (test10 == res10) ++ ": " ++ (show test10)
putStrLn $ show (test11 == res11) ++ ": " ++ (show test11)
putStrLn $ show (test12 == res12) ++ ": " ++ (show test12)
putStrLn $ show (test13 == res13) ++ ": " ++ (show test13)
putStrLn $ show (test14 == res14) ++ ": " ++ (show test14)
putStrLn $ show (test15 == res15) ++ ": " ++ (show test15)
putStrLn $ show (test16 == res16) ++ ": " ++ (show test16)
putStrLn $ show (test17 == res17) ++ ": " ++ (show test17)
putStrLn $ show (test18 == res18) ++ ": " ++ (show test18)
putStrLn $ show (test19 == res19) ++ ": " ++ (show test19)
putStrLn $ show (test20 == res20) ++ ": " ++ (show test20)
putStrLn $ show (test21 == res21) ++ ": " ++ (show test21)
putStrLn $ show (test22 == res22) ++ ": " ++ (show test22)
putStrLn $ show (test23 == res23) ++ ": " ++ (show test23)
putStrLn $ show (test24 == res24) ++ ": " ++ (show test24)
putStrLn $ show (test25 == res25) ++ ": " ++ (show test25)
---- Utilities
simpleExpr :: Expr -> Prog
simpleExpr expr = [
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = [(CallMethod expr "puts" [])]
}
),
classMethods = [],
classReceive = Nothing
}
]
simpleMethod :: Exprs -> Prog
simpleMethod exprs = [
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = exprs
}
),
classMethods = [],
classReceive = Nothing
}
]
----------------- Dublicate classes program
dublicateClassesProgram :: Prog
dublicateClassesProgram = [
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = []
}
),
classMethods = [],
classReceive = Nothing
},
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = []
}
),
classMethods = [],
classReceive = Nothing
}
]
----------------------------------------------------
simpleMethod2 :: Exprs -> Prog
simpleMethod2 exprs = [
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = exprs
}
),
classMethods = [
NamedMethodDecl "SayTwo"
(MethodDecl {
methodParameters = ["x","y"],
methodBody = [
(CallMethod (ReadVar "x") "puts" []),
(CallMethod (ReadVar "y") "puts" [])
]
})
],
classReceive = Nothing
}
]
--------------------------------------------------
simpleProgram :: Exprs -> Prog
simpleProgram exprs = [
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = exprs
}
),
classMethods = [],
classReceive = Nothing
},
ClassDecl {
className = "Person",
classConstructor = Just (
MethodDecl {
methodParameters = ["title"],
methodBody = [
(CallMethod (StringConst "My title is:") "puts" []),
(CallMethod (ReadVar "title") "puts" [])
]
}
),
classMethods = [
NamedMethodDecl "dinner"
(MethodDecl {
methodParameters = ["food","drinks"],
methodBody = [
(CallMethod (StringConst "eating:") "puts" []),
(CallMethod (ReadVar "food") "puts" []),
(CallMethod (StringConst "drinkings:") "puts" []),
(CallMethod (ReadVar "drinks") "puts" [])
]
})
],
classReceive = Nothing
}
]
-------------------------------------------------
--
--
simpleProgram4 = [
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = [
(CallMethod (New "Clock" []) "zend" [(StringConst "tick"),(StringConst "tack")]),
(CallMethod (New "Clock" []) "zend" [(StringConst "noMethod"),(StringConst "tuck")])
]
}
),
classMethods = [],
classReceive = Nothing
},
ClassDecl {
className = "Clock",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = [
(SetField "counter" (IntConst 0))
]
}
),
classMethods = [
(NamedMethodDecl "tick"
(MethodDecl {
methodParameters = ["sound"],
methodBody = [
(CallMethod (ReadVar "sound") "puts" [])
]
}))
],
classReceive = Just (
ReceiveDecl {
receiveParam = "msg",
receiveBody = [
(CallMethod (ReadVar "msg") "puts" [])
]
}
)
}
]
----
simpleProgram3 = [
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = [
(CallMethod (New "Clock" []) "hej" [(StringConst "du")])
]
}
),
classMethods = [],
classReceive = Nothing
},
ClassDecl {
className = "Clock",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = [
(SetField "counter" (IntConst 0))
]
}
),
classMethods = [],
classReceive = Just (
ReceiveDecl {
receiveParam = "msg",
receiveBody = [
(CallMethod (ReadVar "msg") "puts" [])
]
}
)
}
]
--
simpleProgram2 = [
ClassDecl {
className = "Main",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = [
(SetVar "x" (New "Clock" [])),
(SetVar "y" (New "Clock" [])),
(CallMethod (ReadVar "x") "inc" [(IntConst 1)]),
(CallMethod (ReadVar "y") "inc" [(IntConst 2)]),
(CallMethod (ReadVar "x") "inc" [(IntConst 1)]),
(CallMethod (ReadVar "y") "inc" [(IntConst 2)]),
(CallMethod (ReadVar "y") "show" []),
(CallMethod (ReadVar "x") "show" [])
]
}
),
classMethods = [],
classReceive = Nothing
},
ClassDecl {
className = "Clock",
classConstructor = Just (
MethodDecl {
methodParameters = [],
methodBody = [
(SetField "counter" (IntConst 0))
]
}
),
classMethods = [
(NamedMethodDecl "inc"
(MethodDecl {
methodParameters = ["x"],
methodBody = [
(SetField "counter" (Plus (ReadField "counter") (ReadVar "x")))
]
})),
(NamedMethodDecl "show"
(MethodDecl {
methodParameters = [],
methodBody = [
(CallMethod (ReadField "counter") "puts" [])
]
}))
],
classReceive = Just (
ReceiveDecl {
receiveParam = "msg",
receiveBody = [
Match (ReadField "log")
[(TermPattern "true" [],
[CallMethod
(StringConst "Method call:")
"puts" [],
CallMethod (ReadVar "msg") "puts" []])],
SendMessage (ReadField "receiver") (ReadVar "msg")
]
}
)
}
]