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

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
#include "tads.h"
2
#include "lookup.h"
3
4
myObj: object
5
    prop1 = 'hello'
6
    prop2 = 789
7
    prop3 = true
8
    prop4 = [1, 2, 3]
9
;
10
11
otherObj: object
12
    prop1 = 'goodbye'
13
    prop7 = [9, 8, 7, 6, 5]
14
    method1(x, y) { return x * y; }
15
    method2(a, b, c) { return a + b + c; }
16
    method3(a, ...) { return a + argcount; }
17
;
18
19
globals: PreinitObject
20
    execute()
21
    {
22
        local symtab;
23
        
24
        /* get the global symbol table */
25
        symtab = t3GetGlobalSymbols();
26
27
        /* create our value-to-symbol tables */
28
        propToSym = new LookupTable(128, 256);
29
        objToSym = new LookupTable(128, 256);
30
31
        /* build our reverse tables */
32
        symtab.forEachAssoc(new function(sym, val)
33
        {
34
            switch(dataType(val))
35
            {
36
            case TypeObject:
37
                /* add it to the object name table */
38
                objToSym[val] = sym;
39
                break;
40
41
            case TypeProp:
42
                /* add it to the property name table */
43
                propToSym[val] = sym;
44
                break;
45
            }
46
        });
47
    }
48
    propToSym = nil
49
    objToSym = nil
50
    namelist = []
51
;
52
53
main(args)
54
{
55
    showProps(myObj);
56
    showProps(otherObj);
57
}
58
59
showProps(obj)
60
{
61
    /* show the object name */
62
    "object <<globals.objToSym[obj]>>:\n";
63
64
    /* show the properties of this object */
65
    showObjProps(obj, obj, 1);
66
}
67
68
showObjProps(selfObj, obj, level)
69
{
70
    local lst;
71
72
    /* get this object's properties */
73
    lst = obj.getPropList();
74
75
    /* show each */
76
    foreach (local prop in lst)
77
    {
78
        local args;
79
80
        /* indent */
81
        indent(level);
82
83
        /* show the property name */
84
        "<<globals.propToSym[prop]>>";
85
86
        /* show the arg list, if there is one */
87
        args = selfObj.getPropParams(prop);
88
        if (args[1] != 0 || args[2] != 0 || args[3])
89
        {
90
            local len;
91
92
            /* start with the open paren */
93
            "(";
94
95
            /* list arguments - name them starting at 'a' (unicode=97) */
96
            for (local i = 1, local nm = 97, len = 1 ;
97
                 i <= args[1] + args[2] ; ++i, ++nm)
98
            {
99
                /* add a comma separator if this isn't the first one */
100
                if (i != 1)
101
                    ", ";
102
103
                /* show this one's name (a, b, ...) */
104
                "<<makeString(nm, len)>>";
105
106
                /* add '?' if this one's optional (past the minimum count) */
107
                if (i > args[1])
108
                    "?";
109
110
                /* 
111
                 *   if we've reached 'z' (unicode=122), go back to 'a'
112
                 *   and increase the length, so we get x, y, z, aa, bb,
113
                 *   cc, ..., zz, aaa, bbb, ... 
114
                 */
115
                if (nm == 122)
116
                {
117
                    /* go back to a minus one (we're about to increment) */
118
                    nm = 96;
119
120
                    /* increase the length */
121
                    ++len;
122
                }
123
            }
124
125
            /* if it's varargs, add the varargs signifier */
126
            if (args[3])
127
            {
128
                /* show a comma if there were other arguments */
129
                if (args[1] != 0 || args[2] != 0)
130
                    ", ";
131
132
                /* show the varargs ellipsis */
133
                "...";
134
            }
135
136
            /* add the closing paren */
137
            ")";
138
        }
139
        "\n";
140
    }
141
142
    /* go through our superclass list */
143
    foreach (local sc in obj.getSuperclassList())
144
    {
145
        /* indent */
146
        indent(level);
147
        
148
        /* show the header */
149
        "inherited from <<globals.objToSym[sc]>>:\n";
150
151
        /* show the list */
152
        showObjProps(selfObj, sc, level + 1);
153
    }
154
}
155
156
indent(level)
157
{
158
    for (local i = 0 ; i < level ; ++i)
159
        "\ \ ";
160
}