cfad47cfa3/t3compiler/tads3/test/data/anon.t

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
/*
2
 *   anonymous functions
3
 */
4
5
#include "tads.h"
6
#include "t3.h"
7
8
preinit()
9
{
10
}
11
12
main(args)
13
{
14
    local f1;
15
    local f2;
16
    local i = 1;
17
    local cnt = 0;
18
19
    "Long Form:\b";
20
21
    "Before iterate: cnt = <<cnt>>\n";
22
    iterate(10, new function { "."; ++cnt; });
23
    "After iterate(10): cnt = <<cnt>>\n";
24
25
    f1 = new function(x) { "this is anon1(<<x>>)!!!\n"; };
26
    f2 = new function(x, y) { "this is anon2(<<x>>, <<y>>)!!!\n"; };
27
28
    f1(1);
29
    f1(2);
30
    f2(1, 2);
31
    f2(3, 4);
32
33
    callback(f1, 111);
34
    callback(f2, 222, 333);
35
    callback(new function(x, y, z)
36
             { "this is anon3(<<x>>, <<y>>, <<z>>)!!!!!\n"; },
37
             'one', 'two', 'three');
38
39
    callback(new function { for (local i = 1 ; i < 10 ; ++i) "+"; "\n"; } );
40
    "after callback: i = <<i>>\n";
41
42
    callback(new function { iterate(5, new function { "@"; ++cnt; }); });
43
    "\ncnt = <<cnt>>\n";
44
45
    Sub.m1(888);
46
    Sub.m1(999);
47
48
    "\bShort Form:\b";
49
50
    "iterating...\n";
51
    iterate(5, {: "- count = <<++cnt>>\n"});
52
    "...done\n";
53
}
54
55
iterate(cnt, f, [args])
56
{
57
    for (local i = 0 ; i < cnt ; ++i)
58
        f(args...);
59
}
60
61
callback(f, [args])
62
{
63
    "Callback: ";
64
    f(args...);
65
}
66
67
class Base: object
68
    sdesc = "Base"
69
    m1(x)
70
    {
71
        "This is Base.m1(<<x>>) - self = <<self.sdesc>>";
72
    }
73
;
74
75
Sub: Base
76
    sdesc = "Sub"
77
    m1(x)
78
    {
79
        "This is Sub.m1(<<x>>) - self = <<self.sdesc>>\n";
80
        iterate(5, new function { "Sub.m1 Iter: self=<<self.sdesc>>\n"; });
81
        callback(new function{ "Sub.m1 ["; inherited.m1(x+1); "] done\n"; });
82
        callback(new function{ "Prop test: subProp = <<subProp>>\n"; });
83
    }
84
    subProp = "This is Sub.p!"
85
;
86