Debian Codegraph @

terps/tads/tads3/test/data/dyncomp.t

raw
1 #include <tads.h>
2 #include <dynfunc.h>
3 #include <bignum.h>
4 #include <strbuf.h>
5
6 class A: object
7 p1 = 1
8 name = 'A'
9 m1(x) { return 'greetings from <<name>>.m1(<<x>>)!'; }
10 ;
11
12 test1: A
13 name = 'test1'
14 ;
15
16 test2: A
17 name = 'test2'
18 ;
19
20 #define test(src, args) _test(src, nil, 'f' ## #@args, {f: f args})
21 #define test2(src, call) _test(src, nil, #@call, {f: call})
22 #define test3(src, call) _test(src, nil, #@call, new function(f) { call })
23
24 #define RTEST(a, b) 'This is RTEST(<<a>>, <<b>>)!!!'
25
26 class SavedFunc: object
27 construct(f, desc, caller)
28 {
29 f_ = f;
30 desc_ = desc;
31 caller_ = caller;
32
33 if ((src_ = f.getSource()) != nil)
34 src_ = src_.htmlify();
35 }
36
37 invoke()
38 {
39 try
40 {
41 "Source: <<src_>>\n
42 Call: <<desc_>>\n
43 Result: <<(self.caller_)(f_)>>";
44 }
45 catch (Exception exc)
46 {
47 "Execution error: <<exc.displayException>>";
48 }
49
50 "\b";
51 }
52
53 f_ = nil
54 desc_ = nil
55 caller_ = nil
56 src_ = nil
57 ;
58
59 savedFuncs: object
60 savelist = []
61 ;
62
63 modify List
64 myjoin()
65 {
66 local s = new StringBuffer();
67 for (local i = 1 ; i <= length() ; ++i)
68 {
69 if (i > 1)
70 s.append(', ');
71 s.append(self[i]);
72 }
73 return toString(s);
74 }
75 ;
76
77 main(args)
78 {
79 if (args.length() != 3 || args[2] not in ('-save', '-restore'))
80 {
81 "usage: dyncomp -save|-restore file\n";
82 return;
83 }
84
85 if (args[2] == '-restore')
86 {
87 "Restoring...\n";
88 restoreGame(args[3]);
89 foreach (local sf in savedFuncs.savelist)
90 sf.invoke();
91
92 return;
93 }
94
95 test('function(x) { return x*2; }', (7));
96 test('function(x, x, x) { return x*x; }', (7));
97 test('function(x, p, y) { return x.(p)(y + 1000); }', (test1, &m1, 456));
98 test('function(x, y) { return x.m1(y + 1000); }', (test1, 678));
99 test('function(x, y) { return x.mUndef(y + 1000); }', (test1, 678));
100 test('test1.m1(123);', ());
101 test('test2.m1(123);', ());
102 test('function(x) { return test2.m1(x + 1000); }', (789));
103 test('function(x) { return f1(x + 1000); }', (654));
104 test('new BigNumber(\'123.456\');', ());
105 test2('function(x) { local a = x*2, '
106 + '''f = {x, y: 'anon f! x=<\<x>>, y=<\<y>>, a=<\<a>>'}; '''
107 + 'a += 1; return f; }',
108 f(135)(7, 8));
109 test2('function(x, y) { '
110 + 'local f = {: ++x, ++y, \'this is anon 1: x=<\<x>>, y=<\<y>>\' }; '
111 + 'local g = {: ++x, ++y, \'this is anon 2: x=<\<x>>, y=<\<y>>\' }; '
112 + 'return [f, g]; }', testAnon(f));
113 test('function(a:, b:) { return a+b*1000; }', (b:123, a:789));
114 test('''function() { local lst = [10, 20, 30, 40, 50]; local sum = 0;
115 for (local i in lst) sum += i; return sum; }''', ());
116 test('''function() { nameTest(b:123, a:'hello'); }''', ());
117 test('''function() { local obj = new Thing('test'); obj.desc; }''', ());
118 test('''function(cnt) {
119 for (local i in 1..cnt)
120 "[\<<i>>] Embedded item number \<<one of>>one\<<or>>two\<<or
121 >>three\<<or>>four\<<cycling>>... ";
122 }''', (10));
123 test('''function([lst]) {
124 for (local x in lst)
125 "Embedded if test: x=\<<x>>, which is \<<
126 if x > 5>>greater than\<<else if x < 5>>less than\<<else
127 >>exactly\<<end>> five... ";
128 }''', (7, 3, 5, 9, 4));
129
130 Compiler.defineFunc('square', 'function(x) { return x*x; }');
131 test('function(x) { return square(x); }', (11));
132
133 test('function(x, y) { return RTEST(x+1, y+1); }', (101, 202));
134
135 localtest('function() { return i; }', 'this is the \'i\' value!');
136 localtest('function() { return i; }', 'another \'i\' value!');
137 localtest2();
138 localtest3();
139 localtest4();
140
141 saveGame(args[3]);
142 }
143
144 class Thing: object
145 construct(name) { self.name = name; }
146 name = 'Thing'
147 desc = "This is a Thing whose name is '<<name>>'!\n"
148 ;
149
150 f1(x)
151 {
152 return 'Greetings from f1(<<x>>)!';
153 }
154
155 localtest(src, i)
156 {
157 _test(src, t3GetStackTrace(1, T3GetStackDesc).frameDesc_,
158 'localtest()', {f: f()});
159 }
160
161 localtest2()
162 {
163 local i = 'original \'i\' value';
164
165 _test('function() { local orig = i; i += \' *MODIFIED!*\'; '
166 + 'return orig; }',
167 t3GetStackTrace(1, T3GetStackDesc).frameDesc_,
168 'localtest2_part1()', {f: f()});
169 _test('function() { return i; }',
170 t3GetStackTrace(1, T3GetStackDesc).frameDesc_,
171 'localtest2_part2()', {f: f()});
172
173 return i;
174 }
175
176 localtest3()
177 {
178 local i = 'first i value';
179 local f = {: i += ' [modified by anon fn]' };
180
181 _test('function() { local orig = i; i += \' [modified by dynfunc]\'; '
182 + 'return orig; }',
183 t3GetStackTrace(1, T3GetStackDesc).frameDesc_,
184 'localtest3_part1()', {f: f()});
185
186 f();
187
188 _test('function() { return i; }',
189 t3GetStackTrace(1, T3GetStackDesc).frameDesc_,
190 'localtest3_part2()', {f: f()});
191
192 return i;
193 }
194
195 localtest4()
196 {
197 local cnt = 0;
198 local frame = t3GetStackTrace(1, T3GetStackDesc).frameDesc_;
199 local f = new DynamicFunc('function(val) { if (val > 3) ++cnt; }',
200 nil, frame);
201
202 "\blocaltest4:\n";
203 local tf = function(n) {
204 ". test(<<n>>): before cnt=<<cnt>>, ";
205 f(n);
206 "after cnt=<<cnt>>\n";
207 };
208
209 tf(1);
210 tf(2);
211 tf(3);
212 tf(4);
213 tf(5);
214 }
215
216 nameTest(a:, b:)
217 {
218 "Named argument test: a=<<a>>, b=<<b>>\n";
219 }
220
221 testAnon(f)
222 {
223 local l = f(123, 456);
224 "l = f(123, 456):\n
225 type(l)=<<dataType(l)>>, l.length=<<l.length()>>\n
226 type(l[1])=<<dataType(l[1])>>, type(l[2])=<<dataType(l[2])>>\n
227 l[1]()=<<l[1]()>>, l[2]()=<<l[2]()>>\n";
228
229 for (local i = 1 ; i <= l.length() ; ++i)
230 savedFuncs.savelist += new SavedFunc(l[i], 'l[<<i>>]()', {lf: lf()});
231 }
232
233 _test(src, locals, desc, call)
234 {
235 try
236 {
237 local f = Compiler.compile(src, locals);
238 local sf = new SavedFunc(f, desc, call);
239
240 savedFuncs.savelist += sf;
241 sf.invoke();
242 }
243 catch (Exception exc)
244 {
245 "Source: <<src.htmlify()>>\n
246 Call: <<desc.htmlify()>>\n
247 Compiler error\n<<exc.displayException>>\b";
248 }
249 }