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
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package eval
import (
"bignum";
"fmt";
"go/parser";
"go/scanner";
"go/token";
"log";
"os";
"reflect";
"testing";
)
// Print each statement or expression before parsing it
const noisy = false
/*
* Generic statement/expression test framework
*/
type test struct {
code string;
rterr string;
exprs []exprTest;
cerr string;
}
type exprTest struct {
code string;
val interface{};
rterr string;
}
func runTests(t *testing.T, baseName string, tests []test) {
for i, test := range tests {
name := fmt.Sprintf("%s[%d]", baseName, i);
test.run(t, name);
}
}
func (a *test) run(t *testing.T, name string) {
sc := newTestScope();
var fr *Frame;
var cerr os.Error;
if a.code != "" {
if noisy {
println(a.code);
}
// Compile statements
asts, err := parser.ParseStmtList(name, a.code);
if err != nil && cerr == nil {
cerr = err;
}
code, err := CompileStmts(sc, asts);
if err != nil && cerr == nil {
cerr = err;
}
// Execute statements
if cerr == nil {
fr = sc.NewFrame(nil);
rterr := code.Exec(fr);
if a.rterr == "" && rterr != nil {
t.Errorf("%s: expected %s to run, got runtime error %v", name, a.code, rterr);
return;
} else if !checkRTError(t, name, a.code, rterr, a.rterr) {
return;
}
}
}
if fr == nil {
fr = sc.NewFrame(nil);
}
for _, e := range a.exprs {
if cerr != nil {
break;
}
if noisy {
println(e.code);
}
// Compile expression
ast, err := parser.ParseExpr(name, e.code);
if err != nil && cerr == nil {
cerr = err;
}
code, err := CompileExpr(sc, ast);
if err != nil && cerr == nil {
cerr = err;
}
// Evaluate expression
if cerr == nil {
val, rterr := code.Eval(fr);
if e.rterr == "" && rterr != nil {
t.Errorf("%s: expected %q to have value %T(%v), got runtime error %v", name, e.code, e.val, e.val, rterr);
} else if !checkRTError(t, name, e.code, rterr, e.rterr) {
continue;
}
if e.val != nil {
wantval := toValue(e.val);
if !reflect.DeepEqual(val, wantval) {
t.Errorf("%s: expected %q to have value %T(%v), got %T(%v)", name, e.code, wantval, wantval, val, val);
}
}
}
}
// Check compile errors
switch {
case cerr == nil && a.cerr == "":
// Good
case cerr == nil && a.cerr != "":
t.Errorf("%s: expected compile error matching %q, got no errors", name, a.cerr);
case cerr != nil && a.cerr == "":
t.Errorf("%s: expected no compile error, got error %v", name, cerr);
case cerr != nil && a.cerr != "":
cerr := cerr.(scanner.ErrorList);
if len(cerr) > 1 {
t.Errorf("%s: expected 1 compile error matching %q, got %v", name, a.cerr, cerr);
break;
}
m, err := testing.MatchString(a.cerr, cerr.String());
if err != "" {
t.Fatalf("%s: failed to compile regexp %q: %s", name, a.cerr, err);
}
if !m {
t.Errorf("%s: expected compile error matching %q, got compile error %v", name, a.cerr, cerr);
}
}
}
func checkRTError(t *testing.T, name string, code string, rterr os.Error, pat string) bool {
switch {
case rterr == nil && pat == "":
return true;
case rterr == nil && pat != "":
t.Errorf("%s: expected %s to fail with runtime error matching %q, got no error", name, code, pat);
return false;
case rterr != nil && pat != "":
m, err := testing.MatchString(pat, rterr.String());
if err != "" {
t.Fatalf("%s: failed to compile regexp %q: %s", name, pat, err);
}
if !m {
t.Errorf("%s: expected runtime error matching %q, got runtime error %v", name, pat, rterr);
return false;
}
return true;
}
panic("rterr != nil && pat == \"\" should have been handled by the caller");
}
/*
* Test constructors
*/
// Expression compile error
func EErr(expr string, cerr string) test {
return test{"", "", []exprTest{exprTest{expr, nil, ""}}, cerr};
}
// Expression runtime error
func ERTErr(expr string, rterr string) test {
return test{"", "", []exprTest{exprTest{expr, nil, rterr}}, ""};
}
// Expression value
func Val(expr string, val interface{}) test {
return test{"", "", []exprTest{exprTest{expr, val, ""}}, ""};
}
// Statement compile error
func SErr(stmts string, cerr string) test {
return test{stmts, "", nil, cerr};
}
// Statement runtime error
func SRTErr(stmts string, rterr string) test {
return test{stmts, rterr, nil, ""};
}
// Statement runs without error
func SRuns(stmts string) test {
return test{stmts, "", nil, ""};
}
// Statement runs and test one expression's value
func Val1(stmts string, expr1 string, val1 interface{}) test {
return test{stmts, "", []exprTest{exprTest{expr1, val1, ""}}, ""};
}
// Statement runs and test two expressions' values
func Val2(stmts string, expr1 string, val1 interface{}, expr2 string, val2 interface{}) test {
return test{stmts, "", []exprTest{exprTest{expr1, val1, ""}, exprTest{expr2, val2, ""}}, ""};
}
/*
* Value constructors
*/
type vstruct []interface{}
type varray []interface{}
type vslice struct {
arr varray;
len, cap int;
}
func toValue(val interface{}) Value {
switch val := val.(type) {
case bool:
r := boolV(val);
return &r;
case uint8:
r := uint8V(val);
return &r;
case uint:
r := uintV(val);
return &r;
case int:
r := intV(val);
return &r;
case *bignum.Integer:
return &idealIntV{val};
case float:
r := floatV(val);
return &r;
case *bignum.Rational:
return &idealFloatV{val};
case string:
r := stringV(val);
return &r;
case vstruct:
elems := make([]Value, len(val));
for i, e := range val {
elems[i] = toValue(e);
}
r := structV(elems);
return &r;
case varray:
elems := make([]Value, len(val));
for i, e := range val {
elems[i] = toValue(e);
}
r := arrayV(elems);
return &r;
case vslice:
return &sliceV{Slice{toValue(val.arr).(ArrayValue), int64(val.len), int64(val.cap)}};
case Func:
return &funcV{val};
}
log.Crashf("toValue(%T) not implemented", val);
panic();
}
/*
* Default test scope
*/
type testFunc struct {};
func (*testFunc) NewFrame() *Frame {
return &Frame{nil, &[2]Value {}};
}
func (*testFunc) Call(t *Thread) {
n := t.f.Vars[0].(IntValue).Get();
res := n + 1;
t.f.Vars[1].(IntValue).Set(res);
}
type oneTwoFunc struct {};
func (*oneTwoFunc) NewFrame() *Frame {
return &Frame{nil, &[2]Value {}};
}
func (*oneTwoFunc) Call(t *Thread) {
t.f.Vars[0].(IntValue).Set(1);
t.f.Vars[1].(IntValue).Set(2);
}
type voidFunc struct {};
func (*voidFunc) NewFrame() *Frame {
return &Frame{nil, []Value {}};
}
func (*voidFunc) Call(t *Thread) {
}
func newTestScope() *Scope {
sc := universe.ChildScope();
p := token.Position{"<testScope>", 0, 0, 0};
def := func(name string, t Type, val interface{}) {
v, _ := sc.DefineVar(name, p, t);
v.Init = toValue(val);
};
sc.DefineConst("c", p, IdealIntType, toValue(bignum.Int(1)));
def("i", IntType, 1);
def("i2", IntType, 2);
def("u", UintType, uint(1));
def("f", FloatType, 1.0);
def("s", StringType, "abc");
def("t", NewStructType([]StructField {StructField{"a", IntType, false}}), vstruct{1});
def("ai", NewArrayType(2, IntType), varray{1, 2});
def("aai", NewArrayType(2, NewArrayType(2, IntType)), varray{varray{1,2}, varray{3,4}});
def("aai2", NewArrayType(2, NewArrayType(2, IntType)), varray{varray{5,6}, varray{7,8}});
def("fn", NewFuncType([]Type{IntType}, false, []Type {IntType}), &testFunc{});
def("oneTwo", NewFuncType([]Type{}, false, []Type {IntType, IntType}), &oneTwoFunc{});
def("void", NewFuncType([]Type{}, false, []Type {}), &voidFunc{});
def("sli", NewSliceType(IntType), vslice{varray{1, 2, 3}, 2, 3});
return sc;
}
|