|
|
1 | # |
|
|
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | # |
|
|
4 | # Copyright (©) 2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | # |
|
|
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
|
|
7 | # the terms of the Affero GNU General Public License as published by the |
|
|
8 | # Free Software Foundation, either version 3 of the License, or (at your |
|
|
9 | # option) any later version. |
|
|
10 | # |
|
|
11 | # This program is distributed in the hope that it will be useful, |
|
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
14 | # GNU General Public License for more details. |
|
|
15 | # |
|
|
16 | # You should have received a copy of the Affero GNU General Public License |
|
|
17 | # and the GNU General Public License along with this program. If not, see |
|
|
18 | # <http://www.gnu.org/licenses/>. |
|
|
19 | # |
|
|
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
|
|
21 | # |
|
|
22 | |
1 | =head1 NAME |
23 | =head1 NAME |
2 | |
24 | |
3 | cf::match - object matching language |
25 | cf::match - object matching language |
4 | |
26 | |
5 | =head1 DESCRIPTION |
27 | =head1 DESCRIPTION |
… | |
… | |
66 | condition in inv of originator |
88 | condition in inv of originator |
67 | |
89 | |
68 | Once the final set of context objects has been established, each object |
90 | Once the final set of context objects has been established, each object |
69 | is matched against the C<condition>. |
91 | is matched against the C<condition>. |
70 | |
92 | |
|
|
93 | It is possible to chain modifiers from right-to-left, so this example |
|
|
94 | would start with the originator, take it's inventory, find all inventory |
|
|
95 | items which are potions, looks into their inventory, and then finds all |
|
|
96 | spells. |
|
|
97 | |
|
|
98 | type=SPELL in type=POTION in inv of originator |
|
|
99 | |
71 | Sometimes the server is only interested in knowing whether I<anything> |
100 | Sometimes the server is only interested in knowing whether I<anything> |
72 | matches, and sometimes the server is interested in I<all> objects that |
101 | matches, and sometimes the server is interested in I<all> objects that |
73 | match. |
102 | match. |
74 | |
103 | |
75 | =head2 OPERATORS |
104 | =head2 OPERATORS |
… | |
… | |
77 | =over 4 |
106 | =over 4 |
78 | |
107 | |
79 | =item and, or, not, () |
108 | =item and, or, not, () |
80 | |
109 | |
81 | Conditions can be combined with C<and> or C<or> to build larger |
110 | Conditions can be combined with C<and> or C<or> to build larger |
82 | expressions. C<not> negates the expression, and parentheses can be used to |
111 | expressions. C<not> negates the condition, and parentheses can be used to |
83 | group conditions. |
112 | override operator precedence and execute submatches. |
|
|
113 | |
|
|
114 | Not that C<not> only negates a condition and not the whole match |
|
|
115 | expressions, thus |
|
|
116 | |
|
|
117 | not applied in inv |
|
|
118 | |
|
|
119 | is true if there is I<any> non-applied object in the inventory. To negate |
|
|
120 | a whole match, you have to use a sub-match: To check whether there is |
|
|
121 | I<no> applied object in someones inventory, write this: |
|
|
122 | |
|
|
123 | not (applied in inv) |
84 | |
124 | |
85 | Example: match applied weapons. |
125 | Example: match applied weapons. |
86 | |
126 | |
87 | applied type=WEAPON |
127 | applied type=WEAPON |
88 | |
128 | |
89 | Example: match horns or rods. |
129 | Example: match horns or rods. |
90 | |
130 | |
91 | type=HORN or type=ROD |
131 | type=HORN or type=ROD |
|
|
132 | |
|
|
133 | Example: see if the originator is a player. |
|
|
134 | |
|
|
135 | type=PLAYER of originator |
92 | |
136 | |
93 | =item in ... |
137 | =item in ... |
94 | |
138 | |
95 | The in operator takes the context set and modifies it in various ways. As |
139 | The in operator takes the context set and modifies it in various ways. As |
96 | a less technical description, think of the C<in> as being a I<look into> |
140 | a less technical description, think of the C<in> as being a I<look into> |
… | |
… | |
110 | |
154 | |
111 | =item in env |
155 | =item in env |
112 | |
156 | |
113 | Replaces all objects by their containing object, if they have one. |
157 | Replaces all objects by their containing object, if they have one. |
114 | |
158 | |
|
|
159 | =item in arch |
|
|
160 | |
|
|
161 | Replaces all objects by their archetypes. |
|
|
162 | |
115 | =item in map |
163 | =item in map |
116 | |
164 | |
117 | Replaces all objects by the objects that are on the same mapspace as them. |
165 | Replaces all objects by the objects that are on the same mapspace as them. |
118 | |
166 | |
|
|
167 | =item in head |
|
|
168 | |
|
|
169 | Replaces all objects by their head objects. |
|
|
170 | |
119 | =item in <cond> |
171 | =item in <condition> |
120 | |
172 | |
121 | Finds all context objects matching the condition, and then puts their |
173 | Finds all context objects matching the condition, and then puts their |
122 | inventories into the context set. |
174 | inventories into the context set. |
123 | |
175 | |
124 | Note that C<in inv> is simply a special case of an C<< in <cond> >> that |
176 | Note that C<in inv> is simply a special case of an C<< in <condition> >> that |
125 | matches any object. |
177 | matches any object. |
126 | |
178 | |
127 | Example: find all spells inside potions inside the inventory of the context |
179 | Example: find all spells inside potions inside the inventory of the context |
128 | object(s). |
180 | object(s). |
129 | |
181 | |
… | |
… | |
136 | |
188 | |
137 | Example: check if the context object I<is> a spell, or I<contains> a spell. |
189 | Example: check if the context object I<is> a spell, or I<contains> a spell. |
138 | |
190 | |
139 | type=SPELL also in inv |
191 | type=SPELL also in inv |
140 | |
192 | |
141 | =item deep in ... |
193 | =item also deep in ... |
142 | |
194 | |
143 | Repeats the operation as many times as possible. This can be used to |
195 | Repeats the operation as many times as possible. This can be used to |
144 | recursively look into objects. |
196 | recursively look into objects. |
145 | |
197 | |
146 | =item also deep in ... |
198 | So for example, C<also deep in inv> means to take the inventory of all |
|
|
199 | objects, taking their inventories, and so on, and adding all these objects |
|
|
200 | to the context set. |
147 | |
201 | |
148 | C<also> and C<deep> can be combined. |
202 | Similarly, C<also deep in env> means to take the environment object, their |
|
|
203 | environemnt object and so on. |
149 | |
204 | |
150 | Example: check if there are any unpaid items in an inventory, |
205 | Example: check if there are any unpaid items in an inventory, |
151 | or in the inventories of the inventory objects, and so on. |
206 | or in the inventories of the inventory objects, and so on. |
152 | |
207 | |
153 | unpaid also deep in inv |
208 | unpaid also deep in inv |
… | |
… | |
272 | =item any |
327 | =item any |
273 | |
328 | |
274 | This simply evaluates to true, and simply makes matching I<any> object a |
329 | This simply evaluates to true, and simply makes matching I<any> object a |
275 | bit easier to read. |
330 | bit easier to read. |
276 | |
331 | |
|
|
332 | =item none |
|
|
333 | |
|
|
334 | This simply evaluates to false, and simply makes matching I<never> a bit |
|
|
335 | easier to read. |
|
|
336 | |
277 | =item has(condition) |
337 | =item has(condition) |
278 | |
338 | |
279 | True iff the object has a matching inventory object. |
339 | True iff the object has a matching inventory object. |
280 | |
340 | |
281 | =item count(match) |
341 | =item count(match) |
282 | |
342 | |
283 | Number of matching objects - the context object for the C<match> is the |
343 | Number of matching objects - the context object for the C<match> is the |
284 | currently tested object - you can override this with an C<in object> for |
344 | currently tested object - you can override this with an C<in object> for |
285 | example. |
345 | example. |
286 | |
346 | |
287 | =item match(match) |
|
|
288 | |
|
|
289 | An independent match - semantics like C<count>, except it only matters |
|
|
290 | whether the match finds any object (which is faster). |
|
|
291 | |
|
|
292 | =item dump |
347 | =item dump() |
293 | |
348 | |
294 | Dumps the object to the server log when executed, and evaluates to true. |
349 | Dumps the object to the server log when executed, and evaluates to true. |
295 | |
350 | |
296 | Note that logical operations are short-circuiting, so this only dumps |
351 | Note that logical operations are short-circuiting, so this only dumps |
297 | potions: |
352 | potions: |
298 | |
353 | |
299 | type=POTION and dump |
354 | type=POTION and dump() |
300 | |
355 | |
301 | =back |
356 | =back |
302 | |
357 | |
303 | =head2 GRAMMAR |
358 | =head2 GRAMMAR |
304 | |
359 | |
… | |
… | |
310 | |
365 | |
311 | match = chain |
366 | match = chain |
312 | | chain 'of' root |
367 | | chain 'of' root |
313 | root = 'object' | 'self' | 'source' | 'originator' |
368 | root = 'object' | 'self' | 'source' | 'originator' |
314 | chain = condition |
369 | chain = condition |
315 | | chain also deep 'in' set |
370 | | chain also deep 'in' modifier |
316 | also = nothing | 'also' |
371 | also = nothing | 'also' |
317 | deep = nothing | 'deep' |
372 | deep = nothing | 'deep' |
318 | set = 'inv' | 'env' | 'map' |
373 | modifier ='inv' | 'env' | 'arch' | 'map' | 'head' |
319 | |
374 | |
320 | empty = |
375 | nothing = |
321 | |
376 | |
322 | # boolean matching condition |
377 | # boolean matching condition |
323 | |
378 | |
324 | condition = factor |
379 | condition = factor |
325 | | factor 'and'? cond |
380 | | factor 'and'? condition |
326 | | factor 'or' cond |
381 | | factor 'or' condition |
327 | |
382 | |
328 | factor = 'not' factor |
383 | factor = 'not' factor |
329 | | '(' cond ')' |
384 | | '(' match ')' |
330 | | expr |
385 | | expr |
331 | | expr operator constant |
386 | | expr operator constant |
332 | |
387 | |
333 | operator = '=' | '==' | '!=' | '<' | '<=' | '>' | '>=' |
388 | operator = '=' | '==' | '!=' | '<' | '<=' | '>' | '>=' |
334 | |
389 | |
335 | expr = flag |
390 | expr = flag |
336 | | sattr |
391 | | sattr |
337 | | aattr '[' <constant> ']' |
392 | | aattr '[' <constant> ']' |
|
|
393 | | 'stat.' statattr |
338 | | special |
394 | | special |
339 | | func '(' args ')' |
395 | | func '(' args ')' |
340 | | '{' perl code block '}' |
396 | | '{' perl code block '}' |
341 | |
397 | |
342 | func = <any function name> |
398 | func = <any function name> |
343 | sattr = <any scalar object attribute> |
399 | sattr = <any scalar object attribute> |
344 | aattr = <any array object attribute> |
400 | aattr = <any array object attribute> |
345 | flag = <any object flag> |
401 | flag = <any object flag> |
|
|
402 | statattr = <any stat attribute: exp, food, str, dex, hp, maxhp...> |
346 | special = <any ()-less "function"> |
403 | special = <any ()-less "function"> |
347 | |
404 | |
348 | constant = <number> | '"' <string> '"' | <uppercase cf::XXX name> |
405 | constant = <number> | '"' <string> '"' | <uppercase cf::XXX name> |
349 | args = <depends on function> |
406 | args = <depends on function> |
350 | |
407 | |
… | |
… | |
361 | package cf::match; |
418 | package cf::match; |
362 | |
419 | |
363 | use common::sense; |
420 | use common::sense; |
364 | |
421 | |
365 | use List::Util (); |
422 | use List::Util (); |
366 | |
|
|
367 | # parser state |
|
|
368 | # $_ # string to be parsed |
|
|
369 | our $all; # find all, or just the first matching object |
|
|
370 | |
423 | |
371 | { |
424 | { |
372 | package cf::match::exec; |
425 | package cf::match::exec; |
373 | |
426 | |
374 | use List::Util qw(first); |
427 | use List::Util qw(first); |
… | |
… | |
379 | |
432 | |
380 | sub ws { |
433 | sub ws { |
381 | /\G\s+/gc; |
434 | /\G\s+/gc; |
382 | } |
435 | } |
383 | |
436 | |
|
|
437 | sub condition (); |
|
|
438 | sub match ($$); |
|
|
439 | |
384 | our %func = ( |
440 | our %func = ( |
385 | has => sub { |
441 | has => sub { |
386 | 'first { ' . &condition . ' } $_->inv' |
442 | 'first { ' . condition . ' } $_->inv' |
387 | }, |
443 | }, |
388 | count => sub { |
444 | count => sub { |
389 | local $all = 1; |
|
|
390 | '(scalar ' . &match ('$_') . ')' |
445 | '(scalar ' . (match 1, '$_') . ')' |
391 | }, |
|
|
392 | match => sub { |
|
|
393 | local $all = 0; |
|
|
394 | '(scalar ' . &match ('$_') . ')' |
|
|
395 | }, |
|
|
396 | ); |
|
|
397 | |
|
|
398 | our %special = ( |
|
|
399 | any => sub { |
|
|
400 | 1 |
|
|
401 | }, |
446 | }, |
402 | dump => sub { |
447 | dump => sub { |
403 | 'do { |
448 | 'do { |
404 | warn "cf::match::match dump:\n" |
449 | warn "cf::match::match dump:\n" |
405 | . "self: " . eval { $self->name } . "\n" |
450 | . "self: " . eval { $self->name } . "\n" |
… | |
… | |
407 | 1 |
452 | 1 |
408 | }'; |
453 | }'; |
409 | }, |
454 | }, |
410 | ); |
455 | ); |
411 | |
456 | |
|
|
457 | our %special = ( |
|
|
458 | any => sub { |
|
|
459 | 1 |
|
|
460 | }, |
|
|
461 | none => sub { |
|
|
462 | 0 |
|
|
463 | }, |
|
|
464 | ); |
|
|
465 | |
412 | sub constant { |
466 | sub constant { |
413 | ws; |
467 | ws; |
414 | |
468 | |
415 | return $1 if /\G([\-\+0-9\.]+)/gc; |
469 | return $1 if /\G([\-\+0-9\.]+)/gc; |
416 | return "cf::$1" if /\G([A-Z0-9_]+)/gc; |
470 | return "cf::$1" if /\G([A-Z0-9_]+)/gc; |
… | |
… | |
421 | die "number, string or uppercase constant name expected\n"; |
475 | die "number, string or uppercase constant name expected\n"; |
422 | } |
476 | } |
423 | |
477 | |
424 | our $flag = $cf::REFLECT{object}{flags}; |
478 | our $flag = $cf::REFLECT{object}{flags}; |
425 | our $sattr = $cf::REFLECT{object}{scalars}; |
479 | our $sattr = $cf::REFLECT{object}{scalars}; |
|
|
480 | # quick hack to support archname, untested |
|
|
481 | $sattr->{archname} = "W"; |
426 | our $aattr = $cf::REFLECT{object}{arrays}; |
482 | our $aattr = $cf::REFLECT{object}{arrays}; |
|
|
483 | our $lattr = $cf::REFLECT{living}{scalars}; |
427 | |
484 | |
428 | sub expr { |
485 | sub expr { |
429 | # ws done by factor |
486 | # ws done by factor |
430 | my $res; |
487 | my $res; |
431 | |
488 | |
… | |
… | |
434 | |
491 | |
435 | my $expr = $1; |
492 | my $expr = $1; |
436 | |
493 | |
437 | $res .= $expr =~ /\{([^;]+)\}/ ? $1 : "do $expr"; |
494 | $res .= $expr =~ /\{([^;]+)\}/ ? $1 : "do $expr"; |
438 | |
495 | |
|
|
496 | } elsif (/\Gstats\.([A-Za-z0-9_]+)/gc) { |
|
|
497 | |
|
|
498 | if (exists $lattr->{$1}) { |
|
|
499 | $res .= "\$_->stats->$1"; |
|
|
500 | } elsif (exists $lattr->{"\u$1"}) { |
|
|
501 | $res .= "\$_->stats->\u$1"; |
|
|
502 | } else { |
|
|
503 | die "living statistic name expected (str, pow, hp, sp...)\n"; |
|
|
504 | } |
|
|
505 | |
439 | } elsif (/\G([A-Za-z0-9_]+)/gc) { |
506 | } elsif (/\G([A-Za-z0-9_]+)/gc) { |
440 | |
507 | |
441 | if (my $func = $func{$1}) { |
508 | if (my $func = $func{$1}) { |
442 | /\G\s*\(/gc |
509 | /\G\s*\(/gc |
443 | or die "'(' expected after function name\n"; |
510 | or die "'(' expected after function name\n"; |
… | |
… | |
471 | } else { |
538 | } else { |
472 | $res .= constant; |
539 | $res .= constant; |
473 | } |
540 | } |
474 | |
541 | |
475 | } else { |
542 | } else { |
|
|
543 | Carp::cluck;#d# |
476 | die "expr expected\n"; |
544 | die "expr expected\n"; |
477 | } |
545 | } |
478 | |
546 | |
479 | $res |
547 | $res |
480 | } |
548 | } |
… | |
… | |
497 | $res .= "!"; |
565 | $res .= "!"; |
498 | } |
566 | } |
499 | |
567 | |
500 | if (/\G\(/gc) { |
568 | if (/\G\(/gc) { |
501 | # () |
569 | # () |
502 | $res .= &condition; |
570 | |
503 | ws; |
571 | $res .= '(' . (match 0, '$_') . ')'; |
|
|
572 | |
504 | /\G\)/gc or die "')' expected\n"; |
573 | /\G\s*\)/gc or die "closing ')' expected\n"; |
505 | |
574 | |
506 | } else { |
575 | } else { |
507 | my $expr = expr; |
576 | my $expr = expr; |
508 | |
577 | |
509 | $res .= $expr; |
578 | $res .= $expr; |
… | |
… | |
520 | } |
589 | } |
521 | |
590 | |
522 | "($res)" |
591 | "($res)" |
523 | } |
592 | } |
524 | |
593 | |
525 | sub condition { |
594 | sub condition () { |
526 | my $res = factor; |
595 | my $res = factor; |
527 | |
596 | |
528 | while () { |
597 | while () { |
529 | ws; |
598 | ws; |
530 | |
599 | |
531 | # first check some stop-symbols, so we don't have to backtrack |
600 | # first check some stop-symbols, so we don't have to backtrack |
532 | if (/\G(?=also\b|deep\b|in\b|of\b\)|$)/gc) { |
601 | if (/\G(?=also\b|deep\b|in\b|of\b|\)|\z)/gc) { |
|
|
602 | pos = pos; # argh. the misop hits again. again. again. again. you die. |
533 | last; |
603 | last; |
534 | |
604 | |
535 | } elsif (/\Gor\b/gc) { |
605 | } elsif (/\Gor\b/gc) { |
536 | $res .= " || "; |
606 | $res .= " || "; |
537 | |
607 | |
… | |
… | |
543 | } |
613 | } |
544 | |
614 | |
545 | $res |
615 | $res |
546 | } |
616 | } |
547 | |
617 | |
548 | sub match { |
618 | sub match ($$) { |
549 | my $default = shift; |
619 | my ($wantarray, $defctx) = @_; |
550 | |
620 | |
|
|
621 | my $res = condition; |
|
|
622 | |
|
|
623 | # if nothing follows, we have a simple condition, so |
|
|
624 | # optimise a comon case. |
|
|
625 | if ($defctx eq '$_' and /\G\s*(?=\)|$)/gc) { |
|
|
626 | return $wantarray |
|
|
627 | ? "$res ? \$_ : ()" |
|
|
628 | : $res; |
|
|
629 | } |
|
|
630 | |
551 | my $res = ($all ? " grep { " : " first {") . condition . " }"; |
631 | $res = ($wantarray ? " grep { " : " first { ") . $res . "}"; |
552 | |
632 | |
553 | while () { |
633 | while () { |
554 | ws; |
634 | ws; |
555 | |
635 | |
556 | my $also = /\Galso\s+/gc + 0; |
636 | my $also = /\Galso\s+/gc + 0; |
557 | my $deep = /\Gdeep\s+/gc + 0; |
637 | my $deep = /\Gdeep\s+/gc + 0; |
558 | |
638 | |
559 | if (/\Gin\s+/gc) { |
639 | if (/\Gin\s+/gc) { |
560 | my $expand; |
640 | my $expand; |
561 | |
641 | |
562 | if (/\G(inv|env|map)\b/gc) { |
642 | if (/\G(inv|env|map|arch|head)\b/gc) { |
563 | if ($1 eq "inv") { |
643 | if ($1 eq "inv") { |
564 | $expand = "map \$_->inv,"; |
644 | $expand = "map \$_->inv,"; |
565 | } elsif ($1 eq "env") { |
645 | } elsif ($1 eq "env") { |
566 | $expand = "map \$_->env // (),"; |
646 | $expand = "map \$_->env // (),"; |
|
|
647 | } elsif ($1 eq "head") { |
|
|
648 | $expand = "map \$_->head,"; |
|
|
649 | $deep = 0; # infinite loop otherwise |
|
|
650 | } elsif ($1 eq "arch") { |
|
|
651 | $expand = "map \$_->arch,"; |
|
|
652 | $deep = 0; # infinite loop otherwise |
567 | } elsif ($1 eq "map") { |
653 | } elsif ($1 eq "map") { |
568 | $expand = "map \$_->map->at (\$_->x, \$_->y),"; |
654 | $expand = "map \$_->map->at (\$_->x, \$_->y),"; |
|
|
655 | $deep = 0; # infinite loop otherwise |
569 | } |
656 | } |
570 | } else { |
657 | } else { |
571 | $expand = "map \$_->inv, grep { " . condition . " }"; |
658 | $expand = "map \$_->inv, grep { " . condition . " }"; |
572 | } |
659 | } |
573 | |
660 | |
… | |
… | |
581 | $res .= " (\@res, \@_)\n" |
668 | $res .= " (\@res, \@_)\n" |
582 | . "}"; |
669 | . "}"; |
583 | } else { |
670 | } else { |
584 | $res .= " $expand"; |
671 | $res .= " $expand"; |
585 | } |
672 | } |
|
|
673 | } else { |
|
|
674 | |
586 | } elsif (/\Gof\s+(self|object|source|originator)\b/gc) { |
675 | if (/\Gof\s+(self|object|source|originator)\b/gc) { |
587 | $also || $deep |
676 | $also || $deep |
588 | and die "neither 'also' nor 'deep' can be used with 'of'\n"; |
677 | and die "neither 'also' nor 'deep' can be used with 'of'\n"; |
589 | |
678 | |
590 | if ($1 eq "self") { |
679 | if ($1 eq "self") { |
591 | return "$res \$self // ()"; |
680 | return "$res \$self // ()"; |
592 | } elsif ($1 eq "object") { |
681 | } elsif ($1 eq "object") { |
593 | return "$res \$object"; |
682 | return "$res \$object"; |
594 | } elsif ($1 eq "source") { |
683 | } elsif ($1 eq "source") { |
595 | return "$res \$source // ()"; |
684 | return "$res \$source // ()"; |
596 | } elsif ($1 eq "originator") { |
685 | } elsif ($1 eq "originator") { |
597 | return "$res \$originator // \$source // ()"; |
686 | return "$res \$originator // \$source // ()"; |
|
|
687 | } |
|
|
688 | } else { |
|
|
689 | return "$res $defctx"; |
598 | } |
690 | } |
599 | } else { |
|
|
600 | return "$res $default"; |
|
|
601 | } |
691 | } |
602 | } |
692 | } |
603 | } |
693 | } |
604 | } |
694 | } |
605 | |
695 | |
606 | sub parse($;$) { |
696 | sub parse($$) { # wantarray, matchexpr |
607 | local $_ = shift; |
|
|
608 | local $all = shift; |
|
|
609 | |
|
|
610 | my $res; |
697 | my $res; |
611 | |
698 | |
|
|
699 | local $_ = $_[1]; |
|
|
700 | |
612 | eval { |
701 | eval { |
613 | $res = cf::match::parser::match "\$object"; |
702 | $res = cf::match::parser::match $_[0], "\$object"; |
614 | |
703 | |
615 | /\G\s*$/gc |
704 | /\G$/gc |
616 | or die "unexpected trailing characters after match\n"; |
705 | or die "unexpected trailing characters after match\n"; |
617 | }; |
706 | }; |
618 | |
707 | |
619 | if ($@) { |
708 | if ($@) { |
620 | my $ctx = 20; |
709 | my $ctx = 20; |
… | |
… | |
627 | |
716 | |
628 | $res |
717 | $res |
629 | } |
718 | } |
630 | |
719 | |
631 | if (0) {#d# |
720 | if (0) {#d# |
632 | die parse 'applied', 1; |
721 | die parse 1, 'stats.pow'; |
633 | exit 0; |
722 | exit 0; |
634 | } |
723 | } |
635 | |
724 | |
636 | =item cf::match::match $match, $object[, $self[, $source[, $originator]]] |
|
|
637 | |
|
|
638 | Compiles (and caches) the C<$match> expression and matches it against |
|
|
639 | the C<$object>. C<$self> should be the object initiating the match (or |
|
|
640 | C<undef>), C<$source> should be the actor/source and C<$originator> the |
|
|
641 | object that initiated the action (such as the player). C<$originator> |
|
|
642 | defaults to C<$source> when not given. |
|
|
643 | |
|
|
644 | In list context it finds and returns all matching objects, in scalar |
|
|
645 | context only a true or false value. |
|
|
646 | |
|
|
647 | =cut |
|
|
648 | |
|
|
649 | our %CACHE; |
725 | our %CACHE; |
650 | |
726 | |
651 | sub compile($$) { |
727 | sub compile($$) { |
652 | my ($match, $all) = @_; |
728 | my ($wantarray, $match) = @_; |
653 | my $expr = parse $match, $all; |
729 | my $expr = parse $wantarray, $match; |
654 | warn "MATCH DEBUG $match,$all => $expr\n";#d# |
730 | warn "MATCH DEBUG $match,$wantarray => $expr\n";#d# |
655 | $expr = eval " |
731 | $expr = eval " |
656 | package cf::match::exec; |
732 | package cf::match::exec; |
657 | sub { |
733 | sub { |
658 | my (\$object, \$self, \$source, \$originator) = \@_; |
734 | my (\$object, \$self, \$source, \$originator) = \@_; |
659 | $expr |
735 | $expr |
… | |
… | |
662 | die if $@; |
738 | die if $@; |
663 | |
739 | |
664 | $expr |
740 | $expr |
665 | } |
741 | } |
666 | |
742 | |
|
|
743 | =item cf::match::match $match, $object[, $self[, $source[, $originator]]] |
|
|
744 | |
|
|
745 | Compiles (and caches) the C<$match> expression and matches it against |
|
|
746 | the C<$object>. C<$self> should be the object initiating the match (or |
|
|
747 | C<undef>), C<$source> should be the actor/source and C<$originator> the |
|
|
748 | object that initiated the action (such as the player). C<$originator> |
|
|
749 | defaults to C<$source> when not given. |
|
|
750 | |
|
|
751 | In list context it finds and returns all matching objects, in scalar |
|
|
752 | context only a true or false value. |
|
|
753 | |
|
|
754 | =cut |
|
|
755 | |
667 | sub match($$;$$$) { |
756 | sub match($$;$$$) { |
668 | my $match = shift; |
757 | my $match = shift; |
669 | my $all = wantarray+0; |
758 | my $wantarray = wantarray+0; |
670 | |
759 | |
671 | &{ |
760 | &{ |
672 | $CACHE{"$all$match"} ||= compile $match, $all |
761 | $CACHE{"$wantarray$match"} ||= compile $wantarray, $match |
673 | } |
762 | } |
674 | } |
763 | } |
675 | |
764 | |
|
|
765 | our $CACHE_CLEARER = AE::timer 3600, 3600, sub { |
|
|
766 | %CACHE = (); |
|
|
767 | }; |
|
|
768 | |
676 | #d# $::schmorp=cf::player::find "schmorp"& |
769 | #d# $::schmorp=cf::player::find "schmorp"& |
677 | #d# cf::match::match '', $::schmorp->ob |
770 | #d# cf::match::match '', $::schmorp->ob |
678 | |
771 | |
679 | |
772 | |
680 | =back |
773 | =back |