line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ByteBeat::RPN; |
2
|
1
|
|
|
1
|
|
4
|
use Pegex::Base; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
5
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
extends 'Pegex::Tree'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
has rpn => (); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub gotrule { |
9
|
0
|
|
|
0
|
1
|
|
my ($self, $list) = @_; |
10
|
0
|
0
|
|
|
|
|
return $list unless ref $list; |
11
|
0
|
0
|
|
|
|
|
if ($self->rule eq 'power') { |
12
|
0
|
|
|
|
|
|
while (@$list > 1) { |
13
|
0
|
|
|
|
|
|
my ($a, $b) = splice(@$list, -2, 2); |
14
|
0
|
|
|
|
|
|
push @$list, [$a, $b, '**']; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
else { |
18
|
0
|
|
|
|
|
|
while (@$list > 1) { |
19
|
0
|
|
|
|
|
|
my ($a, $op, $b) = splice(@$list, 0, 3); |
20
|
0
|
|
|
|
|
|
unshift @$list, [$a, $b, $op]; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
} |
23
|
0
|
|
|
|
|
|
return $list; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub final { |
27
|
0
|
|
|
0
|
1
|
|
my ($self, $got) = @_; |
28
|
0
|
|
|
|
|
|
$self->rpn($self->flatten($got)); |
29
|
0
|
|
|
|
|
|
return $self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub run { |
33
|
0
|
|
|
0
|
0
|
|
my ($self, $t) = @_; |
34
|
0
|
0
|
|
|
|
|
my $rpn = [ map { /t/ ? $t : $_ } @{$self->{rpn}} ]; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
evaluate($rpn); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub evaluate { |
39
|
0
|
|
|
0
|
0
|
|
my ($rpn) = @_; |
40
|
0
|
0
|
|
|
|
|
return $rpn->[0] if @$rpn == 1; |
41
|
0
|
|
|
|
|
|
my $op = pop @$rpn; |
42
|
0
|
|
|
|
|
|
my $b = get_value($rpn); |
43
|
0
|
|
|
|
|
|
my $a = get_value($rpn); |
44
|
|
|
|
|
|
|
return |
45
|
0
|
0
|
|
|
|
|
$op eq '^' ? $a ^ $b : |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$op eq '|' ? $a | $b : |
47
|
|
|
|
|
|
|
$op eq '&' ? $a & $b : |
48
|
|
|
|
|
|
|
$op eq '>>' ? $a >> $b : |
49
|
|
|
|
|
|
|
$op eq '<<' ? $a << $b : |
50
|
|
|
|
|
|
|
$op eq '+' ? $a + $b : |
51
|
|
|
|
|
|
|
$op eq '-' ? $a - $b : |
52
|
|
|
|
|
|
|
$op eq '*' ? $a * $b : |
53
|
|
|
|
|
|
|
$op eq '/' ? $a / $b : |
54
|
|
|
|
|
|
|
$op eq '%' ? $a % $b : |
55
|
|
|
|
|
|
|
$op eq '**' ? $a ** $b : |
56
|
|
|
|
|
|
|
die "Unknown operator '$op'"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub get_value { |
60
|
0
|
|
|
0
|
0
|
|
my ($rpn) = @_; |
61
|
0
|
0
|
|
|
|
|
if (ref($rpn->[-1]) eq 'ARRAY') { |
|
|
0
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
evaluate(pop @$rpn); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
elsif ($rpn->[-1] !~ /^\d+$/) { |
65
|
0
|
|
|
|
|
|
evaluate($rpn); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
else { |
68
|
0
|
|
|
|
|
|
pop @$rpn; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |