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

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
/*
2
 *   grammar test
3
 */
4
5
#include "tads.h"
6
#include "t3.h"
7
#include "dict.h"
8
#include "gramprod.h"
9
#include "tok.h"
10
11
12
dictionary gDict;
13
dictionary property noun, adjective, plural;
14
15
/* copied from _main.t, which we don't include in this test */
16
dataTypeXlat(val)
17
{
18
    /* get the base type */
19
    local t = dataType(val);
20
    
21
    /* if it's an anonymous function, return TypeFuncPtr */
22
    if (t == TypeObject && val.ofKind(AnonFuncPtr))
23
        return TypeFuncPtr;
24
25
    /* otherwise, just return the base type */
26
    return t;
27
}
28
29
indent(level)
30
{
31
    for ( ; level != 0 ; --level)
32
        "\ \ ";
33
}
34
35
grammar command: predicate->pred_
36
    | predicate->pred_ cmdTerminator * : object
37
    debugPrint(level)
38
        { indent(level); "command: pred\n"; pred_.debugPrint(level+1); }
39
;
40
41
grammar command: cmdTerminator * : object
42
    debugPrint(level) { indent(level); "command (empty)\n"; }
43
;   
44
45
grammar cmdTerminator: 'then' | '.' | '!' | ';' | '?' | ',' | 'and' : object
46
;
47
48
grammar predicate: 'say' tokString->str_ : object
49
    debugPrint(level)
50
        { indent(level); "predicate say(str_ = <<str_>>)\n"; }
51
    execute()
52
    {
53
        "Okay, \"<<str_>>\"\n";
54
    }
55
;
56
57
grammar predicate: 'take' nounPhrase->np_ : object
58
    debugPrint(level)
59
        { indent(level); "predicate take np:\n"; np_.debugPrint(level+1); }
60
;
61
62
grammar predicate: 'give' nounPhrase->dobj_ 'to' nounPhrase->iobj_
63
                 | 'give' nounPhrase->iobj_ nounPhrase->dobj_ : object
64
    debugPrint(level)
65
    {
66
        indent(level);
67
        "predicate give dobj, iobj:\n";
68
        dobj_.debugPrint(level+1);
69
        iobj_.debugPrint(level+1);
70
    }
71
;
72
73
grammar nounPhrase: noun->noun_ : object
74
    debugPrint(level)
75
        { indent(level); "nounPhrase(noun = <<noun_>>)\n"; }
76
    resolveObjects()
77
    {
78
        /* return the objects matching my noun */
79
        return gDict.findWord(noun_, &noun);
80
    }
81
;
82
83
grammar nounPhrase: [badness 0+10] anyPhrase->any_: object
84
    debugPrint(level)
85
    {
86
        indent(level);
87
        "nounPhrase any:\n";
88
        any_.debugPrint(level + 1);
89
    }
90
;
91
92
grammar nounPhrase: adjective->adj_ nounPhrase->np_ : object
93
    debugPrint(level)
94
    {
95
        indent(level);
96
        "nounPhrase(adj = <<adj_>>) np:\n";
97
        np_.debugPrint(level+1);
98
    }
99
    resolveObjects()
100
    {
101
        local match1;
102
        local match2;
103
        
104
        /* get the objects matching my adjective */
105
        match1 = gDict.findWord(adj_, &adjective);
106
107
        /* get the objects matching the rest of the noun phrase */
108
        match2 = np_.resolveObjects();
109
110
        np_.adj_ = true;//$$$ test only - assign property of variable
111
        match1.adj_ = true;
112
113
        /* intersect the lists to yield objects matching all words */
114
        return match1.intersect(match2);
115
    }
116
;
117
118
grammar anyPhrase: tokWord->txt_: object
119
    debugPrint(level) { indent(level); "anyPhrase(<<txt_>>)\n"; }
120
;
121
122
grammar anyPhrase: tokWord->txt_ anyPhrase->phrase_: object
123
    debugPrint(level)
124
    {
125
        indent(level);
126
        "anyPhrase(<<txt_>>) phrase:\n";
127
        phrase_.debugPrint(level + 1);
128
    }
129
;
130
131
class Item: object
132
;
133
134
redBall: Item noun = 'ball' adjective = 'red';
135
blueBall: Item noun = 'ball' adjective = 'blue';
136
137
main(args)
138
{
139
    local cmds =
140
    [
141
        'take red ball',
142
        'give red ball to blue ball',
143
        'give red ball blue ball',
144
        'say "hello there!"',
145
        'say "that\'s about all" and take ball'
146
    ];
147
    local curcmd;
148
        
149
    for (curcmd = 1 ; curcmd <= cmds.length() ; ++curcmd)
150
    {
151
        local str, toks;
152
        local match;
153
154
        /* display the next input line */
155
        str = cmds[curcmd];
156
        "><<str>>\n";
157
158
        /* tokenize the string */
159
        toks = Tokenizer.tokenize(str);
160
161
        /* if it's 'quit' or 'q', stop */
162
        if (toks.length() == 1
163
            && (getTokVal(toks[1]) is in ('q', 'quit')))
164
            break;
165
166
        /* keep going until we parse all commands in the string */
167
        for (;;)
168
        {
169
            local used;
170
            
171
            /* parse it */
172
            match = command.parseTokens(toks, gDict);
173
174
            /* if we didn't get anything, say so */
175
            if (match.length() == 0)
176
            {
177
                "That command is not recognized. ";
178
                break;
179
            }
180
181
            /* display the matches */
182
            for (local i = 1, local cnt = match.length() ; i <= cnt ; ++i)
183
            {
184
                /* get the token count */
185
                used = match[i].lastTokenIndex - match[i].firstTokenIndex + 1;
186
187
                /* display this match */
188
                "[match <<i>>: token count = <<used>>\n";
189
                match[i].debugPrint(1);
190
                "\b";
191
            }
192
193
            /* if more follows, move on */
194
            if (used < toks.length())
195
            {
196
                /* remove the used parts from the lists */
197
                toks = toks.sublist(used + 1);
198
            }
199
            else
200
            {
201
                /* we're done */
202
                break;
203
            }
204
        }
205
    }
206
}
207
208
preinit()
209
{
210
}
211
212
/* ------------------------------------------------------------------------ */
213
/*
214
 *   some boilerplate setup stuff 
215
 */
216
217
class Exception: object
218
    display = "Unknown exception"
219
;
220
221
class RuntimeError: Exception
222
    construct(errno, ...) { errno_ = errno; }
223
    display = "Runtime error: <<exceptionMessage>>"
224
    errno_ = 0
225
    exceptionMessage = ''
226
;
227
228
_say_embed(str) { tadsSay(str); }
229
230
_main(args)
231
{
232
    try
233
    {
234
        t3SetSay(_say_embed);
235
        if (!global.preinited_)
236
        {
237
            preinit();
238
            global.preinited_ = true;
239
        }
240
        if (!t3GetVMPreinitMode())
241
            main(args);
242
    }
243
    catch (RuntimeError rte)
244
    {
245
        "\n<<rte.display>>\n";
246
    }
247
}
248
249
global: object
250
    preinited_ = nil
251
;