-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_interpreter.py
executable file
·204 lines (163 loc) · 7.75 KB
/
test_interpreter.py
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
import pytest
from interpreter import evaluate
from dsl_parser import dsl_parse
import language as lang
from typecheck_errors import BindingUndefinedError
from env import Env
def base_env():
return Env(defaults=lang.builtin_fn_vals)
def test_seq_eval():
prog = dsl_parse("(true 3)")
assert evaluate(base_env(), prog) == 3
def test_define_variable():
prog = dsl_parse("(defvar x (un val footype) 3)")
assert evaluate(base_env(), prog) is None
prog = dsl_parse("((defvar x (un val footype) 3) x)")
assert evaluate(base_env(), prog) == 3
def test_var_access():
prog = dsl_parse("( (defvar x (lin val footype) 0) (set x 3) x )")
assert evaluate(base_env(), prog) == 3
prog = dsl_parse("( (defvar x (aff val footype) 0) (set x 4) (set x 3) 3)")
assert evaluate(base_env(), prog) == 3
def test_calling_functions():
prog = dsl_parse("(apply + 3 4)")
assert evaluate(base_env(), prog) == 7
prog = dsl_parse("(apply <= 5 4)")
assert evaluate(base_env(), prog) == False
def test_defining_functions():
prog = dsl_parse("( (defun add-3 (un val int) ((x (un val int))) (apply + x 3)) (apply add-3 7) )")
assert evaluate(base_env(), prog) == 10
prog = dsl_parse("( (defvar x (un val foobar) 0) "
"(set x 1000) "
"(defun add-3 (un val int) ((x (un val int))) (apply + x 3))"
"(apply add-3 7))")
assert evaluate(base_env(), prog) == 10
prog = dsl_parse("( (defvar x (un val foobar) 0) "
"(set x 1000) "
"(defun add-3 (un val int) ((x (un val int))) (apply + x 3))"
"(apply add-3 7) x)")
assert evaluate(base_env(), prog) == 1000
prog = dsl_parse("( (defvar x (un val foobar) 0) "
"(set x 1000) "
"(defun add-3 (un val int) ((x (un val int))) (apply + x 3))"
"(apply add-3 x))")
assert evaluate(base_env(), prog) == 1003
prog = dsl_parse("( (defun add-3 (un val int) ((x (un val int))) (apply + x 3)) "
"(defun add-4 (un val int) ((x (un val int))) (apply add-3 (apply + x 1)))"
"(apply add-4 6) )")
assert evaluate(base_env(), prog) == 10
def test_if_statement():
prog = dsl_parse("( if true 10 12 )")
assert evaluate(base_env(), prog) == 10
prog = dsl_parse("( (defvar x (un val foobar) 0) "
" (set x 1000) "
" (defun add-3 (un val int) ((x (un val int))) (apply + x 3))"
" (if (apply = 3 3) (apply add-3 x) (apply add-3 (apply add-3 x)))"
")")
assert evaluate(base_env(), prog) == 1003
prog = dsl_parse("( (defvar x (un val foobar) 0) "
" (set x 1000) "
" (defun add-3 (un val int) ((x (un val int))) (apply + x 3))"
" (if (apply = 3 4) (apply add-3 x) (apply add-3 (apply add-3 x)))"
")")
assert evaluate(base_env(), prog) == 1006
prog = dsl_parse("( (defvar x (lin val foobar) 0) "
" (set x 1000) "
" (defun add-3 (un val int) ((x (un val int))) (apply + x 3))"
" (if (apply = 3 4) "
"((defvar z (un val foobar) 10) (apply add-3 x)) "
"(apply add-3 (apply add-3 z)))"
")")
with pytest.raises(BindingUndefinedError):
evaluate(base_env(), prog)
prog = dsl_parse("( (defvar x (aff val foobar) 0) "
" (set x 1000) "
" (defun add-3 (un val int) ((x (un val int))) (apply + x 3))"
" (if (apply = 3 4) "
"(apply add-3 x) "
"((defvar z (un val foobar) 10) (apply add-3 (apply add-3 z)))) z"
")")
with pytest.raises(BindingUndefinedError):
evaluate(base_env(), prog)
def test_while_statement():
prog = dsl_parse("( (defvar x (un val foobar) 0) "
" (defvar y (un val foobar) 10) "
" (while (apply < x 10) -2 ( (set y (apply + y 1)) (set x (apply + x 1)) y) ))")
assert evaluate(base_env(), prog) == 20
prog = dsl_parse("( (defvar x (un val foobar) 0) "
" (defvar y (un val foobar) 10) "
" (while (apply < x -4) -2 ( (set y (apply + y 1)) (set x (apply + x 1)) y) ))")
assert evaluate(base_env(), prog) == -2
prog = dsl_parse("( (defvar x (un val foobar) 0) "
" (defvar y (un val foobar) 10) "
" (while (apply < x -4) -2 ((defvar z (un val foobar) 10) "
" ( (set y (apply + y 1)) (set x (apply + x 1)) y) )) z)")
with pytest.raises(BindingUndefinedError):
evaluate(base_env(), prog)
prog = dsl_parse("( (defvar x (un val foobar) 0) "
" (defvar y (un val foobar) 10) "
" (while (apply < x -4) -2 ((defvar z (un val foobar) 10) "
" ( (defvar 1 (un val foobar) 10) (set y (apply + y 1)) (set x (apply + x 1)) y) )) q)")
with pytest.raises(BindingUndefinedError):
evaluate(base_env(), prog)
def test_ref():
prog = dsl_parse("((defvar x (un val int) 3)"
"(defvar xref (un ref (un val int)) (mkref x)) "
"(setrefval xref (apply + (deref xref) 1))"
"x)")
assert evaluate(base_env(), prog) == 4
def test_ref_fun():
prog = dsl_parse("((defvar x (un val int) 3)"
"(defvar xref (un ref (un val int)) (mkref x)) "
"(defun foo (un val int) ((y (un ref (un val int)))) (setrefval y (apply + (deref y) 1)))"
"(apply foo xref)"
"x)")
assert evaluate(base_env(), prog) == 4
def test_recursive_fun():
prog = dsl_parse("("
"(defun fib (un val int) ((n (un val int))) "
" (defvar ret (un val int) 0) "
" (if (apply = 0 n) (set ret 0) "
" (if (apply = 1 n) (set ret 1) "
" (set ret (apply + (apply fib (apply - n 1)) (apply fib (apply - n 2)) ))))"
" ret"
")"
"(apply fib 0))")
assert evaluate(base_env(), prog) == 0
prog = dsl_parse("("
"(defun fib (un val int) ((n (un val int))) "
" (defvar ret (un val int) 0) "
" (if (apply = 0 n) "
" (set ret 0) "
" (if (apply = 1 n) "
" (set ret 1) "
" (set ret (apply + (apply fib (apply - n 1)) (apply fib (apply - n 2)) ))))"
" ret"
")"
"(apply fib 2))")
assert evaluate(base_env(), prog) == 1
prog = dsl_parse("("
"(defun fib (un val int) ((n (un val int))) "
" (defvar ret (un val int) 0) "
" (if (apply = 0 n) "
" (set ret 0) "
" (if (apply = 1 n) "
" (set ret 1) "
" (set ret (apply + (apply fib (apply - n 1)) (apply fib (apply - n 2)) ))))"
" ret"
")"
"(apply fib 4))")
assert evaluate(base_env(), prog) == 3
def test_file_stuff():
prog = dsl_parse(
"""
(
(defvar f _ (apply fopen 3))
(scope
(defvar fref _ (mkref f))
(apply fwrite fref 33)
)
(apply fclose f)
)
""")
evaluate(base_env(), prog)