… | |
… | |
25 | |
25 | |
26 | Find all potions with spell objects inside them in someones inventory: |
26 | Find all potions with spell objects inside them in someones inventory: |
27 | |
27 | |
28 | type=SPELL in type=POTION in inv |
28 | type=SPELL in type=POTION in inv |
29 | |
29 | |
30 | Find all potions inside someones inventory, or inside applied containers: |
30 | Find all scrolls inside someones inventory, or inside applied scroll |
|
|
31 | containers: |
31 | |
32 | |
32 | type=POTION also in type=CONTAINER and applied in inv |
33 | type=SCROLL also in applied type=CONTAINER race="scroll" in inv |
|
|
34 | |
|
|
35 | Find all unpaid items, anywhere, even deeply nested inside other items, in |
|
|
36 | the originator: |
|
|
37 | |
|
|
38 | unpaid also deep in inv of originator |
33 | |
39 | |
34 | =head1 MATCH EXPRESSIONS |
40 | =head1 MATCH EXPRESSIONS |
35 | |
41 | |
36 | =head2 STRUCTURE |
42 | =head2 STRUCTURE |
37 | |
43 | |
38 | The two main structures are the C<match>, which selects objects matching |
44 | The two main structures are the C<match>, which selects objects matching |
39 | various criteria, and the C<condition, which determines if an object |
45 | various criteria, and the C<condition, which determines if an object |
40 | matches some desired properties: |
46 | matches some desired properties: |
41 | |
47 | |
42 | condition |
48 | condition |
43 | condition in set |
49 | condition in set-modifier |
|
|
50 | condition of root-object |
44 | |
51 | |
45 | A C<cond> receives a set of "context objects" that it is applied to. This |
52 | A C<condition> receives a set of "context objects" that it is applied |
46 | is initially just one object - for altars, it is the object dropped on it, |
53 | to. This is initially just one object - by default, for altars, it is the |
47 | for pedestals, the object on top of it and so on. |
54 | object dropped on it, for pedestals, the object on top of it and so on. |
48 | |
55 | |
49 | This set of context objects can be modified in various ways, for example |
56 | This set of context objects can be modified in various ways, for example |
50 | by replacing it with the inventories of all objects, or all objects on the |
57 | by replacing it with the inventories of all objects, or all items on the |
51 | same mapspace, and so on, by using the C<in> operator: |
58 | same mapspace, and so on, by using the C<in> operator: |
52 | |
59 | |
53 | condition in inv |
60 | condition in inv |
|
|
61 | condition in map |
|
|
62 | |
|
|
63 | Also, besides the default root object where all this begins, you can start |
|
|
64 | elsewhere, for example in the I<originator> (usually the player): |
|
|
65 | |
54 | condition in inv in originator |
66 | condition in inv of originator |
55 | |
67 | |
56 | Once the set of context objects has been established, each object is |
68 | Once the final set of context objects has been established, each object |
57 | matched against the C<cond> expression. Sometimes the server is only |
69 | is matched against the C<condition>. |
58 | interested in knowing whether I<anything> matches, and sometimes the |
70 | |
59 | server is interested in I<all> objects that match. |
71 | It is possible to chain modifiers from right-to-left, so this example |
|
|
72 | would start with the originator, take it's inventory, find all inventory |
|
|
73 | items which are potions, looks into their inventory, and then finds all |
|
|
74 | spells. |
|
|
75 | |
|
|
76 | type=SPELL in type=POTION in inv of originator |
|
|
77 | |
|
|
78 | Sometimes the server is only interested in knowing whether I<anything> |
|
|
79 | matches, and sometimes the server is interested in I<all> objects that |
|
|
80 | match. |
60 | |
81 | |
61 | =head2 OPERATORS |
82 | =head2 OPERATORS |
62 | |
83 | |
63 | =over 4 |
84 | =over 4 |
64 | |
85 | |
65 | =item and, or, not, () |
86 | =item and, or, not, () |
66 | |
87 | |
67 | Conditions can be combined with C<and> or C<or> to build larger |
88 | Conditions can be combined with C<and> or C<or> to build larger |
68 | expressions. C<not> negates the expression, and parentheses can be used to |
89 | expressions. C<not> negates the condition, and parentheses can be used to |
69 | group conditions. |
90 | override operator precedence and execute submatches. |
|
|
91 | |
|
|
92 | Not that C<not> only negates a condition and not the whole match |
|
|
93 | expressions, thus |
|
|
94 | |
|
|
95 | not applied in inv |
|
|
96 | |
|
|
97 | is true if there is I<any> non-object in the inventory. To negate a whole |
|
|
98 | match, you have to use a sub-match. To check whether there is I<no> |
|
|
99 | applied object in someones inventory, write this: |
|
|
100 | |
|
|
101 | not (applied in inv) |
70 | |
102 | |
71 | Example: match applied weapons. |
103 | Example: match applied weapons. |
72 | |
104 | |
73 | type=WEAPON and applied |
105 | applied type=WEAPON |
74 | |
106 | |
75 | Example: match horns or rods. |
107 | Example: match horns or rods. |
76 | |
108 | |
77 | type=HORN or type=ROD |
109 | type=HORN or type=ROD |
78 | |
110 | |
… | |
… | |
84 | the match, the C<in> operator lets you look at other sets of objects, most |
116 | the match, the C<in> operator lets you look at other sets of objects, most |
85 | often the inventory. |
117 | often the inventory. |
86 | |
118 | |
87 | =over 4 |
119 | =over 4 |
88 | |
120 | |
89 | =item in object |
|
|
90 | |
|
|
91 | Replaces all objects by the default object - this is the object passed to |
|
|
92 | the match to match against by default. All matches have an explicit C<in |
|
|
93 | ctx> appended. |
|
|
94 | |
|
|
95 | This must be the last C<in> expression in a match. |
|
|
96 | |
|
|
97 | =item in source |
|
|
98 | |
|
|
99 | Replaces all objects by the source object - this object is sometimes |
|
|
100 | passed to matches and represents the object is the source of the action, |
|
|
101 | such as a rod or a potion when it is applied. Often, the I<source> is the |
|
|
102 | same as the I<originator>. |
|
|
103 | |
|
|
104 | This must be the last C<in> expression in a match. |
|
|
105 | |
|
|
106 | =item in originator |
|
|
107 | |
|
|
108 | Replaces all objects by the originator object - one step farther removed |
|
|
109 | than the I<source>, the I<originator> is sometimes passed to matches and |
|
|
110 | represents the original initiator of an action, most commonly a player or |
|
|
111 | monster. |
|
|
112 | |
|
|
113 | =item in self |
|
|
114 | |
|
|
115 | Replaces all objects by the object initiating/asking for the match - this |
|
|
116 | is basically always the object thatt he match expression is attached to. |
|
|
117 | |
|
|
118 | This must be the last C<in> expression in a match. |
|
|
119 | |
|
|
120 | =item in inv |
121 | =item in inv |
121 | |
122 | |
122 | Replaces all objects by their inventory. |
123 | Replaces all objects by their inventory. |
123 | |
124 | |
124 | Example: find all spell objects inside the object to be matched. |
125 | Example: find all spell objects inside the object to be matched. |
… | |
… | |
127 | |
128 | |
128 | =item in env |
129 | =item in env |
129 | |
130 | |
130 | Replaces all objects by their containing object, if they have one. |
131 | Replaces all objects by their containing object, if they have one. |
131 | |
132 | |
|
|
133 | =item in arch |
|
|
134 | |
|
|
135 | Replaces all objects by their archetypes. |
|
|
136 | |
132 | =item in map |
137 | =item in map |
133 | |
138 | |
134 | Replaces all objects by the objects that are on the same mapspace as them. |
139 | Replaces all objects by the objects that are on the same mapspace as them. |
135 | |
140 | |
|
|
141 | =item in head |
|
|
142 | |
|
|
143 | Replaces all objects by their head objects. |
|
|
144 | |
136 | =item in <cond> |
145 | =item in <condition> |
137 | |
146 | |
138 | Finds all context objects matching the condition, and then puts their |
147 | Finds all context objects matching the condition, and then puts their |
139 | inventories into the context set. |
148 | inventories into the context set. |
140 | |
149 | |
141 | Note that C<in inv> is simply a special case of an C<< in <cond> >> that |
150 | Note that C<in inv> is simply a special case of an C<< in <condition> >> that |
142 | matches any object. |
151 | matches any object. |
143 | |
152 | |
144 | Example: find all spells inside potions inside the inventory of the context |
153 | Example: find all spells inside potions inside the inventory of the context |
145 | object(s). |
154 | object(s). |
146 | |
155 | |
… | |
… | |
153 | |
162 | |
154 | Example: check if the context object I<is> a spell, or I<contains> a spell. |
163 | Example: check if the context object I<is> a spell, or I<contains> a spell. |
155 | |
164 | |
156 | type=SPELL also in inv |
165 | type=SPELL also in inv |
157 | |
166 | |
158 | =item repeatedly in ... |
167 | =item also deep in ... |
159 | |
168 | |
160 | Repeats the operation as many times as possible. This can be used to |
169 | Repeats the operation as many times as possible. This can be used to |
161 | recursively look into objects. |
170 | recursively look into objects. |
162 | |
171 | |
163 | =item also repeatedly in ... |
172 | So for example, C<also deep in inv> means to take the inventory of all |
|
|
173 | objects, taking their inventories, and so on, and adding all these objects |
|
|
174 | to the context set. |
164 | |
175 | |
165 | C<also> and C<repeatedly> can be combined. |
176 | Similarly, C<also deep in env> means to take the environment object, their |
|
|
177 | environemnt object and so on. |
166 | |
178 | |
167 | Example: check if there are any unpaid items in an inventory, |
179 | Example: check if there are any unpaid items in an inventory, |
168 | or in the inventories of the inventory objects, and so on. |
180 | or in the inventories of the inventory objects, and so on. |
169 | |
181 | |
170 | unpaid also repeatedly in inv |
182 | unpaid also deep in inv |
171 | |
183 | |
172 | Example: check if a object is inside a player. |
184 | Example: check if a object is inside a player. |
173 | |
185 | |
174 | type=PLAYER also repeatedly in env |
186 | type=PLAYER also deep in env |
175 | |
187 | |
176 | =back |
188 | =back |
|
|
189 | |
|
|
190 | =item of ... |
|
|
191 | |
|
|
192 | By default, all matches are applied to the "obviously appropriate" object, |
|
|
193 | such as the item dropped on a button or moving over a detector. This can |
|
|
194 | be changed to a number of other objects - not all of them are available |
|
|
195 | for each match (when not available, the match will simply fail). |
|
|
196 | |
|
|
197 | An C<of> term ends a match, nothing is allowed to follow. |
|
|
198 | |
|
|
199 | =over 4 |
|
|
200 | |
|
|
201 | =item of object |
|
|
202 | |
|
|
203 | Starts with the default object - this is the object passed to the match to |
|
|
204 | match against by default. Matches have an explicit C<of object> appended, |
|
|
205 | but submatches start at the current object, and in this case C<of object> |
|
|
206 | can be used to start at the original object once more. |
|
|
207 | |
|
|
208 | =item of source |
|
|
209 | |
|
|
210 | Starts with the I<source> object - this object is sometimes passed to |
|
|
211 | matches and represents the object that is the source of the action, such |
|
|
212 | as a rod or a potion when it is applied. Often, the I<source> is the same |
|
|
213 | as the I<originator>. |
|
|
214 | |
|
|
215 | =item of originator |
|
|
216 | |
|
|
217 | Starts with the I<originator> - one step farther removed than the |
|
|
218 | I<source>, the I<originator> is sometimes passed to matches and represents |
|
|
219 | the original initiator of an action, most commonly a player or monster. |
|
|
220 | |
|
|
221 | This object is often identical to the I<source> (e.g. when a player casts |
|
|
222 | a spell, the player is both source and originator). |
|
|
223 | |
|
|
224 | =item of self |
|
|
225 | |
|
|
226 | Starts with the object initiating/asking for the match - this is basically |
|
|
227 | always the object that the match expression is attached to. |
177 | |
228 | |
178 | =back |
229 | =back |
179 | |
230 | |
180 | =head2 EXPRESSIONS |
231 | =head2 EXPRESSIONS |
181 | |
232 | |
… | |
… | |
212 | =item { BLOCK } |
263 | =item { BLOCK } |
213 | |
264 | |
214 | You can specify perl code to execute by putting it inside curly |
265 | You can specify perl code to execute by putting it inside curly |
215 | braces. The last expression evaluated inside will become the result. |
266 | braces. The last expression evaluated inside will become the result. |
216 | |
267 | |
|
|
268 | The perlcode can access C<$_>, which rferes to the object currently being |
|
|
269 | matches, and the C<$object>, C<$self>, C<$source> and C<$originator>. |
|
|
270 | |
|
|
271 | Example: check whether the slaying field consists of digits only. |
|
|
272 | |
|
|
273 | { $_->slaying =~ /^\d+$/ } |
|
|
274 | |
217 | =item comparisons, <, <=, ==, =, !=, =>, > |
275 | =item comparisons, <, <=, ==, =, !=, =>, > |
218 | |
276 | |
219 | You can compare expressions against constants via any of these |
277 | You can compare expressions against constants via any of these |
220 | operators. If the constant is a string, then a string compare will be |
278 | operators. If the constant is a string, then a string compare will be |
221 | done, otherwise a numerical comparison is used. |
279 | done, otherwise a numerical comparison is used. |
… | |
… | |
243 | =item any |
301 | =item any |
244 | |
302 | |
245 | This simply evaluates to true, and simply makes matching I<any> object a |
303 | This simply evaluates to true, and simply makes matching I<any> object a |
246 | bit easier to read. |
304 | bit easier to read. |
247 | |
305 | |
|
|
306 | =item none |
|
|
307 | |
|
|
308 | This simply evaluates to false, and simply makes matching I<never> a bit |
|
|
309 | easier to read. |
|
|
310 | |
248 | =item has(cond) |
311 | =item has(condition) |
249 | |
312 | |
250 | True iff the object has a matching inventory object. |
313 | True iff the object has a matching inventory object. |
251 | |
314 | |
252 | =item count(match) |
315 | =item count(match) |
253 | |
316 | |
254 | Number of matching objects - the context object for the C<match> are the |
317 | Number of matching objects - the context object for the C<match> is the |
255 | original context objects for the overall C<match>. # TODO bullshit |
318 | currently tested object - you can override this with an C<in object> for |
|
|
319 | example. |
256 | |
320 | |
257 | =item match(match) |
321 | =item dump() |
258 | |
322 | |
259 | An independent match - unlike C<count>, it only matters whether the match |
323 | Dumps the object to the server log when executed, and evaluates to true. |
260 | finds any object (which is faster). |
324 | |
|
|
325 | Note that logical operations are short-circuiting, so this only dumps |
|
|
326 | potions: |
|
|
327 | |
|
|
328 | type=POTION and dump() |
261 | |
329 | |
262 | =back |
330 | =back |
263 | |
331 | |
264 | =head2 GRAMMAR |
332 | =head2 GRAMMAR |
265 | |
333 | |
… | |
… | |
267 | module. It is meant to be easily readable by humans, not to implement it |
335 | module. It is meant to be easily readable by humans, not to implement it |
268 | exactly as-is. |
336 | exactly as-is. |
269 | |
337 | |
270 | # object matching and selecting |
338 | # object matching and selecting |
271 | |
339 | |
272 | match = set |
340 | match = chain |
|
|
341 | | chain 'of' root |
|
|
342 | root = 'object' | 'self' | 'source' | 'originator' |
|
|
343 | chain = condition |
273 | | match also rep 'in' set |
344 | | chain also deep 'in' modifier |
274 | also = nothing | 'also' |
345 | also = nothing | 'also' |
275 | rep = nothing | 'rep' | 'repeatedly' |
346 | deep = nothing | 'deep' |
|
|
347 | modifier ='inv' | 'env' | 'arch' | 'map' | 'head' |
276 | |
348 | |
277 | set = 'inv' | 'env' | 'map' |
349 | nothing = |
278 | | 'object' | 'source' | 'originator' | 'self' |
|
|
279 | |
|
|
280 | empty = |
|
|
281 | |
350 | |
282 | # boolean matching condition |
351 | # boolean matching condition |
283 | |
352 | |
284 | condition = factor |
353 | condition = factor |
285 | | factor 'and'? cond |
354 | | factor 'and'? condition |
286 | | factor 'or' cond |
355 | | factor 'or' condition |
287 | |
356 | |
288 | factor = 'not' factor |
357 | factor = 'not' factor |
289 | | '(' cond ')' |
358 | | '(' match ')' |
290 | | expr |
359 | | expr |
291 | | expr operator constant |
360 | | expr operator constant |
292 | |
361 | |
293 | operator = '=' | '==' | '!=' | '<' | '<=' | '>' | '>=' |
362 | operator = '=' | '==' | '!=' | '<' | '<=' | '>' | '>=' |
294 | |
363 | |
295 | expr = flag |
364 | expr = flag |
296 | | sattr |
365 | | sattr |
297 | | aattr '[' <constant> ']' |
366 | | aattr '[' <constant> ']' |
|
|
367 | | 'stat.' statattr |
298 | | special |
368 | | special |
299 | | func '(' args ')' |
369 | | func '(' args ')' |
300 | | '{' perl code block '}' |
370 | | '{' perl code block '}' |
301 | |
371 | |
302 | func = <any function name> |
372 | func = <any function name> |
303 | sattr = <any scalar object attribute> |
373 | sattr = <any scalar object attribute> |
304 | aattr = <any array object attribute> |
374 | aattr = <any array object attribute> |
305 | flag = <any object flag> |
375 | flag = <any object flag> |
|
|
376 | statattr = <any stat attribute: exp, food, str, dex, hp, maxhp...> |
306 | special = <any ()-less "function"> |
377 | special = <any ()-less "function"> |
307 | |
378 | |
308 | constant = <number> | '"' <string> '"' | <uppercase cf::XXX name> |
379 | constant = <number> | '"' <string> '"' | <uppercase cf::XXX name> |
309 | args = <depends on function> |
380 | args = <depends on function> |
310 | |
381 | |
311 | TODO: repeatedly, env, contains, possbly matches |
382 | TODO: contains, matches, query_name, selling_price, buying_price? |
312 | |
383 | |
313 | =cut |
384 | =cut |
314 | |
385 | |
|
|
386 | =head2 PERL FUNCTIONS |
|
|
387 | |
|
|
388 | =over 4 |
|
|
389 | |
|
|
390 | =cut |
315 | |
391 | |
316 | package cf::match; |
392 | package cf::match; |
317 | |
393 | |
318 | use common::sense; |
394 | use common::sense; |
319 | |
395 | |
320 | use List::Util (); |
396 | use List::Util (); |
321 | |
|
|
322 | # parser state |
|
|
323 | # $_ # string to be parsed |
|
|
324 | our $all; # find all, or just the first matching object |
|
|
325 | |
397 | |
326 | { |
398 | { |
327 | package cf::match::exec; |
399 | package cf::match::exec; |
328 | |
400 | |
329 | use List::Util qw(first); |
401 | use List::Util qw(first); |
330 | |
|
|
331 | sub env_chain { |
|
|
332 | my @res; |
|
|
333 | push @res, $_ |
|
|
334 | while $_ = $_->env; |
|
|
335 | @res |
|
|
336 | } |
|
|
337 | |
402 | |
338 | package cf::match::parser; |
403 | package cf::match::parser; |
339 | |
404 | |
340 | use common::sense; |
405 | use common::sense; |
341 | |
406 | |
342 | sub ws { |
407 | sub ws { |
343 | /\G\s+/gc; |
408 | /\G\s+/gc; |
344 | } |
409 | } |
345 | |
410 | |
|
|
411 | sub condition (); |
|
|
412 | sub match ($$); |
|
|
413 | |
346 | our %func = ( |
414 | our %func = ( |
347 | has => sub { |
415 | has => sub { |
348 | 'first { ' . &condition . ' } $_->inv' |
416 | 'first { ' . condition . ' } $_->inv' |
349 | }, |
417 | }, |
350 | count => sub { |
418 | count => sub { |
351 | local $all = 1; |
|
|
352 | '(scalar ' . &match . ')' |
419 | '(scalar ' . (match 1, '$_') . ')' |
353 | }, |
420 | }, |
354 | match => sub { |
421 | dump => sub { |
355 | local $all = 0; |
422 | 'do { |
356 | '(scalar ' . &match . ')' |
423 | warn "cf::match::match dump:\n" |
|
|
424 | . "self: " . eval { $self->name } . "\n" |
|
|
425 | . $_->as_string; |
|
|
426 | 1 |
|
|
427 | }'; |
357 | }, |
428 | }, |
358 | ); |
429 | ); |
359 | |
430 | |
360 | our %special = ( |
431 | our %special = ( |
361 | any => sub { |
432 | any => sub { |
362 | 1 |
433 | 1 |
363 | }, |
434 | }, |
|
|
435 | none => sub { |
|
|
436 | 0 |
|
|
437 | }, |
364 | ); |
438 | ); |
365 | |
439 | |
366 | sub constant { |
440 | sub constant { |
367 | ws; |
441 | ws; |
368 | |
442 | |
… | |
… | |
376 | } |
450 | } |
377 | |
451 | |
378 | our $flag = $cf::REFLECT{object}{flags}; |
452 | our $flag = $cf::REFLECT{object}{flags}; |
379 | our $sattr = $cf::REFLECT{object}{scalars}; |
453 | our $sattr = $cf::REFLECT{object}{scalars}; |
380 | our $aattr = $cf::REFLECT{object}{arrays}; |
454 | our $aattr = $cf::REFLECT{object}{arrays}; |
|
|
455 | our $lattr = $cf::REFLECT{living}{scalars}; |
381 | |
456 | |
382 | sub expr { |
457 | sub expr { |
383 | # ws done by factor |
458 | # ws done by factor |
384 | my $res; |
459 | my $res; |
385 | |
460 | |
… | |
… | |
388 | |
463 | |
389 | my $expr = $1; |
464 | my $expr = $1; |
390 | |
465 | |
391 | $res .= $expr =~ /\{([^;]+)\}/ ? $1 : "do $expr"; |
466 | $res .= $expr =~ /\{([^;]+)\}/ ? $1 : "do $expr"; |
392 | |
467 | |
|
|
468 | } elsif (/\Gstats\.([A-Za-z0-9_]+)/gc) { |
|
|
469 | |
|
|
470 | if (exists $lattr->{$1}) { |
|
|
471 | $res .= "\$_->stats->$1"; |
|
|
472 | } elsif (exists $lattr->{"\u$1"}) { |
|
|
473 | $res .= "\$_->stats->\u$1"; |
|
|
474 | } else { |
|
|
475 | die "living statistic name expected (str, pow, hp, sp...)\n"; |
|
|
476 | } |
|
|
477 | |
393 | } elsif (/\G([A-Za-z0-9_]+)/gc) { |
478 | } elsif (/\G([A-Za-z0-9_]+)/gc) { |
394 | |
479 | |
395 | if (my $func = $func{$1}) { |
480 | if (my $func = $func{$1}) { |
396 | /\G\s*\(/gc |
481 | /\G\s*\(/gc |
397 | or die "'(' expected after function name\n"; |
482 | or die "'(' expected after function name\n"; |
… | |
… | |
425 | } else { |
510 | } else { |
426 | $res .= constant; |
511 | $res .= constant; |
427 | } |
512 | } |
428 | |
513 | |
429 | } else { |
514 | } else { |
|
|
515 | Carp::cluck;#d# |
430 | die "expr expected\n"; |
516 | die "expr expected\n"; |
431 | } |
517 | } |
432 | |
518 | |
433 | $res |
519 | $res |
434 | } |
520 | } |
… | |
… | |
451 | $res .= "!"; |
537 | $res .= "!"; |
452 | } |
538 | } |
453 | |
539 | |
454 | if (/\G\(/gc) { |
540 | if (/\G\(/gc) { |
455 | # () |
541 | # () |
456 | $res .= &condition; |
542 | |
457 | ws; |
543 | $res .= '(' . (match 0, '$_') . ')'; |
|
|
544 | |
458 | /\G\)/gc or die "')' expected\n"; |
545 | /\G\s*\)/gc or die "closing ')' expected\n"; |
459 | |
546 | |
460 | } else { |
547 | } else { |
461 | my $expr = expr; |
548 | my $expr = expr; |
462 | |
549 | |
463 | $res .= $expr; |
550 | $res .= $expr; |
… | |
… | |
474 | } |
561 | } |
475 | |
562 | |
476 | "($res)" |
563 | "($res)" |
477 | } |
564 | } |
478 | |
565 | |
479 | sub condition { |
566 | sub condition () { |
480 | my $res = factor; |
567 | my $res = factor; |
481 | |
568 | |
482 | while () { |
569 | while () { |
483 | ws; |
570 | ws; |
|
|
571 | |
|
|
572 | # first check some stop-symbols, so we don't have to backtrack |
484 | if (/\G(?=also\b|in\b|\)|$)/gc) { |
573 | if (/\G(?=also\b|deep\b|in\b|of\b|\)|\z)/gc) { |
485 | # early stop => faster and requires no backtracking |
574 | pos = pos; # argh. the misop hits again. again. again. again. you die. |
486 | last; |
575 | last; |
|
|
576 | |
487 | } elsif (/\Gor\b/gc) { |
577 | } elsif (/\Gor\b/gc) { |
488 | $res .= " || "; |
578 | $res .= " || "; |
|
|
579 | |
489 | } else { |
580 | } else { |
490 | /\Gand\b/gc; |
581 | /\Gand\b/gc; |
491 | $res .= " && "; |
582 | $res .= " && "; |
492 | } |
583 | } |
493 | $res .= factor; |
584 | $res .= factor; |
494 | } |
585 | } |
495 | |
586 | |
496 | $res |
587 | $res |
497 | } |
588 | } |
498 | |
589 | |
499 | sub match { |
590 | sub match ($$) { |
500 | my $res; |
591 | my ($wantarray, $defctx) = @_; |
501 | |
592 | |
502 | my $also; # undef means first iteration |
593 | my $res = condition; |
|
|
594 | |
|
|
595 | # if nothing follows, we have a simple condition, so |
|
|
596 | # optimise a comon case. |
|
|
597 | if ($defctx eq '$_' and /\G\s*(?=\)|$)/gc) { |
|
|
598 | return $wantarray |
|
|
599 | ? "$res ? \$_ : ()" |
|
|
600 | : $res; |
|
|
601 | } |
|
|
602 | |
|
|
603 | $res = ($wantarray ? " grep { " : " first { ") . $res . "}"; |
|
|
604 | |
503 | while () { |
605 | while () { |
504 | if (/\G\s*(inv|env|map|object|subject|originator)\b/gc) { |
606 | ws; |
|
|
607 | |
|
|
608 | my $also = /\Galso\s+/gc + 0; |
|
|
609 | my $deep = /\Gdeep\s+/gc + 0; |
|
|
610 | |
|
|
611 | if (/\Gin\s+/gc) { |
|
|
612 | my $expand; |
|
|
613 | |
|
|
614 | if (/\G(inv|env|map|arch|head)\b/gc) { |
505 | if ($1 eq "inv") { |
615 | if ($1 eq "inv") { |
506 | $res .= " map+(${also}\$_->inv),"; |
616 | $expand = "map \$_->inv,"; |
507 | } elsif ($1 eq "env") { |
617 | } elsif ($1 eq "env") { |
508 | $res .= " map+(${also}env_chain), "; # TODO |
618 | $expand = "map \$_->env // (),"; |
|
|
619 | } elsif ($1 eq "head") { |
|
|
620 | $expand = "map \$_->head,"; |
|
|
621 | $deep = 0; # infinite loop otherwise |
|
|
622 | } elsif ($1 eq "arch") { |
|
|
623 | $expand = "map \$_->arch,"; |
|
|
624 | $deep = 0; # infinite loop otherwise |
509 | } elsif ($1 eq "map") { |
625 | } elsif ($1 eq "map") { |
510 | $res .= " map+(${also}\$_->map->at (\$_->x, \$_->y)),"; |
626 | $expand = "map \$_->map->at (\$_->x, \$_->y),"; |
511 | } elsif ($1 eq "self") { |
627 | $deep = 0; # infinite loop otherwise |
512 | return "$res \$self"; |
628 | } |
513 | } elsif ($1 eq "object") { |
629 | } else { |
514 | last; # default |
630 | $expand = "map \$_->inv, grep { " . condition . " }"; |
515 | } elsif ($1 eq "source") { |
|
|
516 | return "$res \$source"; |
|
|
517 | } elsif ($1 eq "originator") { |
|
|
518 | return "$res \$originator"; |
|
|
519 | } |
631 | } |
520 | last unless /\G\s*in\b/gc; |
632 | |
|
|
633 | if ($also || $deep) { |
|
|
634 | $res .= " do {\n" |
|
|
635 | . " my \@res;\n"; |
|
|
636 | $res .= " while (\@_) {\n" if $deep; |
|
|
637 | $res .= " push \@res, \@_;\n" if $also; |
|
|
638 | $res .= " \@_ = $expand \@_;\n"; |
|
|
639 | $res .= " }\n" if $deep; |
|
|
640 | $res .= " (\@res, \@_)\n" |
|
|
641 | . "}"; |
|
|
642 | } else { |
|
|
643 | $res .= " $expand"; |
|
|
644 | } |
521 | } else { |
645 | } else { |
522 | $res .= " map+($also\$_->inv)," if defined $also; |
|
|
523 | $res .= $all ? " grep { " : " first {"; |
|
|
524 | $res .= condition; |
|
|
525 | $res .= "}"; |
|
|
526 | |
646 | |
527 | $also = /\G\s*also\b/gc ? '$_, ' : ''; |
647 | if (/\Gof\s+(self|object|source|originator)\b/gc) { |
528 | last unless /\G\s*in\b/gc; |
648 | $also || $deep |
|
|
649 | and die "neither 'also' nor 'deep' can be used with 'of'\n"; |
|
|
650 | |
|
|
651 | if ($1 eq "self") { |
|
|
652 | return "$res \$self // ()"; |
|
|
653 | } elsif ($1 eq "object") { |
|
|
654 | return "$res \$object"; |
|
|
655 | } elsif ($1 eq "source") { |
|
|
656 | return "$res \$source // ()"; |
|
|
657 | } elsif ($1 eq "originator") { |
|
|
658 | return "$res \$originator // \$source // ()"; |
|
|
659 | } |
|
|
660 | } else { |
|
|
661 | return "$res $defctx"; |
|
|
662 | } |
529 | } |
663 | } |
530 | } |
664 | } |
531 | |
|
|
532 | "$res \$object" |
|
|
533 | } |
665 | } |
534 | |
|
|
535 | } |
666 | } |
536 | |
667 | |
537 | sub parse($;$) { |
668 | sub parse($$) { # wantarray, matchexpr |
538 | local $_ = shift; |
669 | my $res; |
539 | local $all = shift; |
|
|
540 | |
670 | |
|
|
671 | local $_ = $_[1]; |
|
|
672 | |
|
|
673 | eval { |
541 | my $res = cf::match::parser::match; |
674 | $res = cf::match::parser::match $_[0], "\$object"; |
|
|
675 | |
|
|
676 | /\G$/gc |
|
|
677 | or die "unexpected trailing characters after match\n"; |
|
|
678 | }; |
542 | |
679 | |
543 | if ($@) { |
680 | if ($@) { |
544 | my $ctx = 20; |
681 | my $ctx = 20; |
545 | my $str = substr $_, (List::Util::max 0, (pos) - $ctx), $ctx * 2; |
682 | my $str = substr $_, (List::Util::max 0, (pos) - $ctx), $ctx * 2; |
546 | substr $str, (List::Util::min $ctx, pos), 0, "<-- HERE -->"; |
683 | substr $str, (List::Util::min $ctx, pos), 0, "<-- HERE -->"; |
… | |
… | |
550 | } |
687 | } |
551 | |
688 | |
552 | $res |
689 | $res |
553 | } |
690 | } |
554 | |
691 | |
|
|
692 | if (0) {#d# |
|
|
693 | die parse 1, 'stats.pow'; |
|
|
694 | exit 0; |
|
|
695 | } |
|
|
696 | |
555 | our %CACHE; |
697 | our %CACHE; |
556 | |
698 | |
557 | sub match($$$;$$) { |
699 | sub compile($$) { |
558 | my ($self, $match, $object, $source, $originator) = @_; |
700 | my ($wantarray, $match) = @_; |
559 | my $all = wantarray+0; |
701 | my $expr = parse $wantarray, $match; |
560 | |
702 | warn "MATCH DEBUG $match,$wantarray => $expr\n";#d# |
561 | $originator ||= $source; |
703 | $expr = eval " |
562 | |
704 | package cf::match::exec; |
563 | &{ |
705 | sub { |
564 | $CACHE{"$all$match"} ||= do { |
706 | my (\$object, \$self, \$source, \$originator) = \@_; |
565 | my $expr = parse $match, $all; |
|
|
566 | warn "$match,$all => $expr\n";#d# |
|
|
567 | $expr = eval "package cf::match::exec; sub { $expr }"; |
|
|
568 | die if $@; |
|
|
569 | $expr |
707 | $expr |
570 | } |
708 | } |
|
|
709 | "; |
|
|
710 | die if $@; |
|
|
711 | |
|
|
712 | $expr |
|
|
713 | } |
|
|
714 | |
|
|
715 | =item cf::match::match $match, $object[, $self[, $source[, $originator]]] |
|
|
716 | |
|
|
717 | Compiles (and caches) the C<$match> expression and matches it against |
|
|
718 | the C<$object>. C<$self> should be the object initiating the match (or |
|
|
719 | C<undef>), C<$source> should be the actor/source and C<$originator> the |
|
|
720 | object that initiated the action (such as the player). C<$originator> |
|
|
721 | defaults to C<$source> when not given. |
|
|
722 | |
|
|
723 | In list context it finds and returns all matching objects, in scalar |
|
|
724 | context only a true or false value. |
|
|
725 | |
|
|
726 | =cut |
|
|
727 | |
|
|
728 | sub match($$;$$$) { |
|
|
729 | my $match = shift; |
|
|
730 | my $wantarray = wantarray+0; |
|
|
731 | |
|
|
732 | &{ |
|
|
733 | $CACHE{"$wantarray$match"} ||= compile $wantarray, $match |
571 | } |
734 | } |
572 | } |
735 | } |
573 | |
736 | |
574 | #match undef, "any", undef; |
737 | our $CACHE_CLEARER = AE::timer 3600, 3600, sub { |
575 | #exit 0; |
738 | %CACHE = (); |
|
|
739 | }; |
|
|
740 | |
|
|
741 | #d# $::schmorp=cf::player::find "schmorp"& |
|
|
742 | #d# cf::match::match '', $::schmorp->ob |
|
|
743 | |
|
|
744 | |
|
|
745 | =back |
|
|
746 | |
|
|
747 | =head1 AUTHOR |
|
|
748 | |
|
|
749 | Marc Lehmann <schmorp@schmorp.de> |
|
|
750 | http://home.schmorp.de/ |
|
|
751 | |
|
|
752 | =cut |
576 | |
753 | |
577 | 1; |
754 | 1; |
578 | |
755 | |