-
Notifications
You must be signed in to change notification settings - Fork 235
/
test_visual_basic.py
375 lines (310 loc) · 8.94 KB
/
test_visual_basic.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
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
from m2cgen import ast
from m2cgen.interpreters import VisualBasicInterpreter
from tests import utils
def test_if_expr():
expr = ast.IfExpr(
ast.CompExpr(ast.NumVal(1), ast.FeatureRef(0), ast.CompOpType.EQ),
ast.NumVal(2),
ast.NumVal(3))
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
Dim var0 As Double
If (1) = (input_vector(0)) Then
var0 = 2
Else
var0 = 3
End If
score = var0
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_bin_num_expr():
expr = ast.BinNumExpr(
ast.BinNumExpr(
ast.FeatureRef(0), ast.NumVal(-2), ast.BinNumOpType.DIV),
ast.NumVal(2),
ast.BinNumOpType.MUL)
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
score = ((input_vector(0)) / (-2)) * (2)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_dependable_condition():
left = ast.BinNumExpr(
ast.IfExpr(
ast.CompExpr(ast.NumVal(1),
ast.NumVal(1),
ast.CompOpType.EQ),
ast.NumVal(1),
ast.NumVal(2)),
ast.NumVal(2),
ast.BinNumOpType.ADD)
right = ast.BinNumExpr(ast.NumVal(1), ast.NumVal(2), ast.BinNumOpType.DIV)
bool_test = ast.CompExpr(left, right, ast.CompOpType.GTE)
expr = ast.IfExpr(bool_test, ast.NumVal(1), ast.FeatureRef(0))
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
Dim var0 As Double
Dim var1 As Double
If (1) = (1) Then
var1 = 1
Else
var1 = 2
End If
If ((var1) + (2)) >= ((1) / (2)) Then
var0 = 1
Else
var0 = input_vector(0)
End If
score = var0
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_nested_condition():
left = ast.BinNumExpr(
ast.IfExpr(
ast.CompExpr(ast.NumVal(1),
ast.NumVal(1),
ast.CompOpType.EQ),
ast.NumVal(1),
ast.NumVal(2)),
ast.NumVal(2),
ast.BinNumOpType.ADD)
bool_test = ast.CompExpr(ast.NumVal(1), left, ast.CompOpType.EQ)
expr_nested = ast.IfExpr(bool_test, ast.FeatureRef(2), ast.NumVal(2))
expr = ast.IfExpr(bool_test, expr_nested, ast.NumVal(2))
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
Dim var0 As Double
Dim var1 As Double
If (1) = (1) Then
var1 = 1
Else
var1 = 2
End If
If (1) = ((var1) + (2)) Then
Dim var2 As Double
If (1) = (1) Then
var2 = 1
Else
var2 = 2
End If
If (1) = ((var2) + (2)) Then
var0 = input_vector(2)
Else
var0 = 2
End If
Else
var0 = 2
End If
score = var0
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_module_name():
expr = ast.NumVal(1)
expected_code = """
Module Test
Function score(ByRef input_vector() As Double) As Double
score = 1
End Function
End Module
"""
interpreter = VisualBasicInterpreter(module_name="Test")
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_raw_array():
expr = ast.VectorVal([ast.NumVal(3), ast.NumVal(4)])
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double()
Dim var0(1) As Double
var0(0) = 3
var0(1) = 4
score = var0
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_multi_output():
expr = ast.SubroutineExpr(
ast.IfExpr(
ast.CompExpr(
ast.NumVal(1),
ast.NumVal(1),
ast.CompOpType.EQ),
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.VectorVal([ast.NumVal(3), ast.NumVal(4)])))
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double()
Dim var0() As Double
If (1) = (1) Then
Dim var1(1) As Double
var1(0) = 1
var1(1) = 2
var0 = var1
Else
Dim var2(1) As Double
var2(0) = 3
var2(1) = 4
var0 = var2
End If
score = var0
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_bin_vector_expr():
expr = ast.BinVectorExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.VectorVal([ast.NumVal(3), ast.NumVal(4)]),
ast.BinNumOpType.ADD)
expected_code = """
Module Model
Function addVectors(ByRef v1() As Double, ByRef v2() As Double) As Double()
Dim resLength As Integer
resLength = UBound(v1) - LBound(v1)
Dim result() As Double
ReDim result(resLength)
Dim i As Integer
For i = LBound(v1) To UBound(v1)
result(i) = v1(i) + v2(i)
Next i
addVectors = result
End Function
Function mulVectorNumber(ByRef v1() As Double, ByVal num As Double) As Double()
Dim resLength As Integer
resLength = UBound(v1) - LBound(v1)
Dim result() As Double
ReDim result(resLength)
Dim i As Integer
For i = LBound(v1) To UBound(v1)
result(i) = v1(i) * num
Next i
mulVectorNumber = result
End Function
Function score(ByRef input_vector() As Double) As Double()
Dim var0(1) As Double
var0(0) = 1
var0(1) = 2
Dim var1(1) As Double
var1(0) = 3
var1(1) = 4
score = addVectors(var0, var1)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
expected_code = """
Module Model
Function addVectors(ByRef v1() As Double, ByRef v2() As Double) As Double()
Dim resLength As Integer
resLength = UBound(v1) - LBound(v1)
Dim result() As Double
ReDim result(resLength)
Dim i As Integer
For i = LBound(v1) To UBound(v1)
result(i) = v1(i) + v2(i)
Next i
addVectors = result
End Function
Function mulVectorNumber(ByRef v1() As Double, ByVal num As Double) As Double()
Dim resLength As Integer
resLength = UBound(v1) - LBound(v1)
Dim result() As Double
ReDim result(resLength)
Dim i As Integer
For i = LBound(v1) To UBound(v1)
result(i) = v1(i) * num
Next i
mulVectorNumber = result
End Function
Function score(ByRef input_vector() As Double) As Double()
Dim var0(1) As Double
var0(0) = 1
var0(1) = 2
score = mulVectorNumber(var0, 1)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_exp_expr():
expr = ast.ExpExpr(ast.NumVal(1.0))
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
score = Math.Exp(1.0)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_pow_expr():
expr = ast.PowExpr(ast.NumVal(2.0), ast.NumVal(3.0))
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
score = (2.0) ^ (3.0)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_tanh_expr():
expr = ast.TanhExpr(ast.NumVal(2.0))
expected_code = """
Module Model
Function Tanh(ByVal number As Double) As Double
If number > 44.0 Then ' exp(2*x) <= 2^127
Tanh = 1.0
Exit Function
End If
If number < -44.0 Then
Tanh = -1.0
Exit Function
End If
Tanh = (Math.Exp(2 * number) - 1) / (Math.Exp(2 * number) + 1)
End Function
Function score(ByRef input_vector() As Double) As Double
score = Tanh(2.0)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_reused_expr():
reused_expr = ast.ExpExpr(ast.NumVal(1.0), to_reuse=True)
expr = ast.BinNumExpr(reused_expr, reused_expr, ast.BinNumOpType.DIV)
expected_code = """
Module Model
Function score(ByRef input_vector() As Double) As Double
Dim var0 As Double
var0 = Math.Exp(1.0)
score = (var0) / (var0)
End Function
End Module
"""
interpreter = VisualBasicInterpreter()
utils.assert_code_equal(interpreter.interpret(expr), expected_code)