Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adding advent 2011 day 7
  • Loading branch information
dwarring committed Jun 6, 2014
1 parent 7db5a80 commit 9f36451
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions integration/advent2011-day07.t
@@ -0,0 +1,18 @@
#! http://perl6advent.wordpress.com/2011/12/07/grammarprofiler/
use v6;
use Test;
use lib 't/spec/packages';
use Advent::GrammarProfiler;
plan 3;

grammar MyGrammar {
rule TOP { <num> +% <op> }
token num { <[ 0..9 \. ]>+ }
token op { < + - * / = > }
}
ok MyGrammar.parse("37 + 10 - 5 = 42"), "parsed";

my %timing = get-timing();

ok %timing<MyGrammar><num><calls>, "num calls recorded";
ok %timing<MyGrammar><num><time>, "time recorded";
34 changes: 34 additions & 0 deletions packages/Advent/GrammarProfiler.pm
@@ -0,0 +1,34 @@
use v6;

our %timing;

my class ProfiledGrammarHOW is Metamodel::GrammarHOW is Mu {

method find_method($obj, $name) {
my $meth := callsame;
substr($name, 0, 1) eq '!' || $name eq any(<parse CREATE Bool defined MATCH perl>) ??
$meth !!
-> $c, |args {
my $grammar = $obj.WHAT.perl;
%timing{$grammar} //= {}; # Vivify grammar hash
%timing{$grammar}{$meth.name} //= {}; # Vivify method hash
my %t := %timing{$grammar}{$meth.name};
my $start = now; # get start time
my $result := $meth($obj, |args); # Call original method
%t<time> += now - $start; # accumulate execution time
%t<calls>++;
$result
}
}

method publish_method_cache($obj) {
# no caching, so we always hit find_method
}

}

sub get-timing is export { %timing }
sub reset-timing is export { %timing = {} }

my module EXPORTHOW { }
EXPORTHOW.WHO.<grammar> = ProfiledGrammarHOW;

0 comments on commit 9f36451

Please sign in to comment.