-
Notifications
You must be signed in to change notification settings - Fork 24
/
closure_test.go
174 lines (156 loc) · 2.62 KB
/
closure_test.go
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
package gscript
import "testing"
func TestClosure(t *testing.T) {
script := `
int varExternal =10;
func int(int) f1(){
int varInner = 20;
int innerFun(int a){
print(a);
int c=100;
varExternal++;
varInner++;
return varInner;
}
return innerFun;
}
func int(int) f2 = f1();
for(int i=0;i<2;i++){
print("varInner=" + f2(i) + ", varExternal=" + varExternal);
}
print("=======");
func int(int) f3 = f1();
for(int i=0;i<2;i++){
print("varInner=" + f3(i) + ", varExternal=" + varExternal);
}
`
NewCompiler().Compiler(script)
}
func TestClosure2(t *testing.T) {
script := `
int varExternal =10;
func int() f1(){
int varInner = 20;
int innerFun(){
return varInner;
}
return innerFun;
}
func int() f2 = f1();
print(f2());
assertEqual(f2(), 20);
`
NewCompiler().Compiler(script)
}
func TestClosure3(t *testing.T) {
script := `
string fun1(int a){
print("fun1 a=" + a);
return "test";
}
fun2 (func string(int) f){
string ret = f(6);
print("fun2 ret =" + ret);
}
func string(int) f = fun1;
fun2(f);
fun2(fun1);
`
NewCompiler().Compiler(script)
}
func TestClosure5(t *testing.T) {
script := `
class Test{
fun2 (func string(int) f){
string ret = f(6);
print("fun2 ret =" + ret);
}
}
string fun1(int a){
print("fun1 a=" + a);
return "test";
}
func string(int) f = fun1;
Test t = Test();
t.fun2(f);
`
NewCompiler().Compiler(script)
}
func TestClosure6(t *testing.T) {
script := `
class Test{
func string(int) fun;
}
string fun1(int a){
print("fun1 a=" + a);
return "test";
}
fun2 (func string(int) f){
string ret = f(6);
println("fun2 ret =" + ret);
}
func string(int) f = fun1;
Test t = Test();
t.fun= fun1;
print(t.fun(100));
`
NewCompiler().Compiler(script)
}
func TestClosure7(t *testing.T) {
script := `
class Test{
int value=0;
Test(int v){
value=v;
}
int map(func int(int) f){
return f(value);
}
}
int square(int v){
return v*v;
}
int add(int v){
return v++;
}
int add2(int v){
v=v+2;
return v;
}
Test t =Test(100);
func int(int) s=square;
func int(int) a=add;
func int(int) a2=add2;
print(t.map(s));
assertEqual(t.map(s),10000);
print(t.map(a));
assertEqual(t.map(a),101);
print(t.map(a2));
assertEqual(t.map(a2),102);
// todo crossoverJie 这种情况编译报错
//print(t.map(add2));
//assertEqual(t.map(add2),102);
`
NewCompiler().Compiler(script)
}
func TestClosure4(t *testing.T) {
script := `
void fun(){
int a = 0;
int b = 1;
int fibonacci(){
int c = a;
a = b;
b = a+c;
return c;
}
return fibonacci;
}
func int() fib = fun();
//打印斐波那契数列
for (int i = 0; i < 10; i++){
print(fib());
}
`
NewCompiler().Compiler(script)
}