~ubuntu-branches/ubuntu/wily/gargoyle-free/wily-proposed

« back to all changes in this revision

Viewing changes to tads/tads3/test/data/preinit.t

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Beucler
  • Date: 2009-09-11 20:09:43 UTC
  • Revision ID: james.westby@ubuntu.com-20090911200943-idgzoyupq6650zpn
Tags: upstream-2009-08-25
ImportĀ upstreamĀ versionĀ 2009-08-25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}