Debian Codegraph @

terps/tads/tads3/test/data/template.t

raw
1 #include "tads.h"
2 #include "t3.h"
3 #include "dict.h"
4
5 /* ------------------------------------------------------------------------ */
6 /*
7 * Default Dictionary
8 */
9 dictionary gDict;
10
11 dictionary property noun, adjective, plural;
12
13
14 /* ------------------------------------------------------------------------ */
15 /*
16 * set the location property
17 */
18 + property location;
19
20
21 /* ------------------------------------------------------------------------ */
22 /*
23 * define some object templates
24 */
25 object template 'name_';
26 object template 'name_' "sdesc";
27 object template 'name_' "sdesc" "ldesc";
28 object template "sdesc" "ldesc";
29 object template [lst];
30
31 object template @location 'name_';
32 object template @location 'name_' "sdesc";
33 object template @location 'name_' "sdesc" "ldesc";
34
35
36 /* ------------------------------------------------------------------------ */
37 /*
38 * Main Entrypoint
39 */
40 main(args)
41 {
42 "Containment graph:\n";
43 forEachInstance(Room,
44 { obj: "Room: <<obj.sdesc>><<obj.listCont(1)>>\b" });
45
46 "Long Descriptions:\n";
47 forEachInstance(CObject, { obj: "\(<<obj.sdesc>>:\) <<obj.ldesc>>\b" });
48 }
49
50
51 /* ------------------------------------------------------------------------ */
52 /*
53 * preinitialization
54 */
55 PreinitObject
56 execute()
57 {
58 /* initialize the location for every object */
59 forEachInstance(CObject, { obj: obj.initLocation() });
60
61 /* initialize vocabulary */
62 forEachInstance(CObject, { obj: obj.initVocabFromName(gDict) });
63 }
64 ;
65
66
67 /* ------------------------------------------------------------------------ */
68 /*
69 * root object
70 */
71 class CObject: object
72 /* list the contents */
73 listCont(level)
74 {
75 for (local i = 1, local cnt = contents.length() ; i <= cnt ; ++i)
76 {
77 /* indent */
78 for (local j = 1 ; j <= level ; ++j)
79 "\ \ ";
80
81 /* show this object and end the line */
82 contents[i].sdesc;
83 "\n";
84
85 /* show the contents of this object */
86 contents[i].listCont(level + 1);
87 }
88 }
89
90 /* initialize location - add myself to my location's contents list */
91 initLocation()
92 {
93 if (self.propType(&location) == TypeObject)
94 location.contents += self;
95 }
96
97 /* initialize vocabulary from the 'name_' property */
98 initVocabFromName(dict)
99 {
100 local str;
101
102 /* if 'name' isn't defined for this object, ignore it */
103 if (!self.propDefined(&name_))
104 return;
105
106 /* add empty lists for our undefined dictionary properties */
107 if (!self.propDefined(&noun))
108 noun = [];
109 if (!self.propDefined(&adjective))
110 adjective = [];
111 if (!self.propDefined(&plural))
112 plural = [];
113
114 /* parse the name */
115 for (str = name_ ; str != '' ; )
116 {
117 local len;
118 local grp;
119
120 /*
121 * parse all of the adjectives in the string - an adjective
122 * is a leading word followed by another word
123 */
124 for (;;)
125 {
126 /* check for another adjective */
127 if ((len = rexMatch('((%w|[-\'])+) +%w', str)) == nil)
128 {
129 /* no more adjectives in this part - stop looking */
130 break;
131 }
132
133 /* add this adjective */
134 grp = rexGroup(1);
135 adjective += grp[3];
136 dict.addWord(self, grp[3], &adjective);
137
138 /*
139 * remove it from the string - note that our expression
140 * has one extra character that we don't use (the first
141 * character of the next word), so we must deduct one
142 * from our length; then we must add one to get the
143 * index of the start of the next word
144 */
145 str = str.substr(len - 1 + 1);
146 }
147
148 /*
149 * pull out the next word, which goes up to the '/', '(', or
150 * the end of the string
151 */
152 if ((len = rexMatch('((%w|[-\'])+) *([/(]|$)', str)) == nil)
153 break;
154
155 /* add this word to the nouns */
156 grp = rexGroup(1);
157 noun += grp[3];
158 dict.addWord(self, grp[3], &noun);
159
160 /* remove it from the string */
161 str = str.substr(rexGroup(3)[1]);
162
163 /* if the string is empty now, we're done */
164 if (str == '')
165 break;
166
167 /* if we have a slash, remove it and move on */
168 if ((len = rexMatch('/ *', str)) != nil)
169 {
170 /* remove the slash and following spaces */
171 str = str.substr(len + 1);
172 }
173
174 /* if the string is now empty, we're done */
175 if (str == '')
176 break;
177
178 /* check for a paren group, indicating a plural */
179 if ((len = rexMatch('%((.+)%)', str)) != nil)
180 {
181 local pstr;
182
183 /* get the plural section */
184 pstr = rexGroup(1)[3];
185
186 /* remove the plurals from the rest of the string */
187 str = str.substr(len + 1);
188
189 /* scan the plurals */
190 while (pstr != '')
191 {
192 /* get the next word */
193 if ((len = rexMatch('((%w|[-\'])+)( +|$)', pstr)) == nil)
194 break;
195
196 /* add it to the plurals */
197 grp = rexGroup(1);
198 plural += grp[3];
199 dict.addWord(self, grp[3], &plural);
200 }
201 }
202 }
203
204 /* if we didn't empty out the string, show an error */
205 if (str != '')
206 "warning: <<sdesc>>.name_ is invalid from '<<str>>'\n";
207 }
208
209 /* contents list */
210 contents = []
211 ;
212
213 class Item: CObject
214 sdesc = "<item>"
215 ldesc = "It looks like an ordinary <<sdesc>>. "
216 ;
217
218 class Container: Item
219 capacity = 10
220 ;
221
222 class Room: CObject
223 sdesc = "<Room>"
224 ;
225
226 /* ------------------------------------------------------------------------ */
227 /*
228 * define some objects
229 */
230
231
232 object sdesc = "anonymous base object" ;
233 + object sdesc = "contained anonymous base object" ;
234
235 iceCave: Room "Ice Cave"
236 "A glacier extends into this cavern from the north, completely
237 blocking any exits save the one to the south. Piles of pulverized
238 rock lie at the front of the glacier. "
239 ;
240
241 Item @iceCave 'glacier' "glacier";
242 + Item 'pulverized pile/rock/piles/rocks' "pile of rocks" ;
243
244 nsPassage: Room "North-South Passage"
245 "This narrow, twisting passage leads north and south. "
246 ;
247
248 + Item 'ice pick' "ice pick";
249 + Container 'backpack' "backpack"
250 capacity = 100
251 ;
252 + Container 'bag' "bag" ;
253