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

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
#include "tads.h"
2
#include "t3.h"
3
4
/* 
5
 *   global variables 
6
 */
7
global: object
8
    /* flag: we've run the 'preinit' function */
9
    preinited_ = nil
10
;
11
12
export RuntimeError;
13
export exceptionMessage;
14
15
class RuntimeError: object
16
    construct(errno, ...) { errno_ = errno; }
17
    exceptionMessage = nil
18
    errno_ = 0
19
;
20
21
room1: object
22
    contents = []
23
;
24
25
book: object
26
    sdesc = "book"
27
    location = room1
28
;
29
30
ball: object
31
    sdesc = "ball"
32
    location = room1
33
;
34
35
_say_embed(str) { tadsSay(str); }
36
37
/*
38
 *   main entrypoint 
39
 */
40
function _main(args)
41
{
42
    t3SetSay(_say_embed);
43
    try
44
    {
45
        /* if haven't yet run preinit, do so now */
46
        if (!global.preinited_)
47
        {
48
            /* run preinit */
49
            preinit();
50
            
51
            /* make a note that we've completed pre-initialization */
52
            global.preinited_ = true;
53
            
54
            tadsSay('global.preinited_ = '
55
                    + (global.preinited_ ? 'true' : 'false')
56
                    + '\n');
57
        }
58
        
59
        /* if we're in preinit-only mode, we're done */
60
        if (t3GetVMPreinitMode())
61
            return;
62
        
63
        /* invoke the user's main program */
64
        main();
65
    }
66
    catch(RuntimeError rt)
67
    {
68
        tadsSay('\n!!! RuntimeError: ' + rt.errno_ + '\n');
69
    }
70
}
71
72
/*
73
 *   user pre-initialization routine 
74
 */
75
function preinit()
76
{
77
    local str;
78
    
79
    tadsSay('this is preinit!!!\n');
80
81
    obj1.prop1 = 'Hello';
82
    obj1.prop1 += '!!!';
83
    obj1.prop2 = [obj1.prop1, obj1.prop1, obj1.prop1];
84
85
    str = 'Test';
86
    str += ' ';
87
    str += 'Instance';
88
    str += '!';
89
    obj1.prop3 = new MyClass(str);
90
91
    /* build contents lists */
92
    for (local obj = firstObj() ; obj != nil ; obj = nextObj(obj))
93
    {
94
        if (obj.location != nil)
95
            obj.location.contents += obj;
96
    }
97
}
98
99
/*
100
 *   user main program 
101
 */
102
function main()
103
{
104
    tadsSay('this is the main entrypoint!!!\n');
105
106
    tadsSay('obj1.prop1 = ' + obj1.prop1 + '\n');
107
108
    tadsSay('obj1.prop2 = ');
109
    sayList(obj1.prop2);
110
    tadsSay('\n');
111
112
    tadsSay('obj1.prop3 = nil? ' + (obj1.prop3 == nil ? 'yes' : 'no') + '\n');
113
    tadsSay('obj1.prop3.instName = ' + obj1.prop3.instName + '\n');
114
115
    tadsSay('contents of room1:\n');
116
    for (local lst = room1.contents ; lst.length() != 0 ; lst = lst.cdr())
117
        "\t<<lst.car().sdesc>>\n";
118
}
119
120
obj1: object
121
    prop1 = '<init>'
122
    prop2 = '<init>'
123
    prop3 = '<init>'
124
;
125
126
class MyClass: object
127
    sdesc { tadsSay('This is MyClass: instName = ' + instName); }
128
    instName = '(none)'
129
130
    construct(nm) { instName = nm; }
131
;
132
133
function sayList(lst)
134
{
135
    tadsSay('[');
136
    for (local i = 1, local cnt = lst.length() ; i <= cnt ; ++i)
137
    {
138
        tadsSay(lst[i]);
139
        if (i < cnt)
140
            tadsSay(', ');
141
    }
142
    tadsSay(']');
143
}