line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ByteBeat::Compiler; |
2
|
1
|
|
|
1
|
|
3
|
use Mo; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
3
|
1
|
|
|
1
|
|
468
|
use Pegex::Parser; |
|
1
|
|
|
|
|
10764
|
|
|
1
|
|
|
|
|
25
|
|
4
|
1
|
|
|
1
|
|
389
|
use Pegex::Grammar; |
|
1
|
|
|
|
|
1504
|
|
|
1
|
|
|
|
|
5
|
|
5
|
1
|
|
|
1
|
|
326
|
use ByteBeat::RPN; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
120
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has code => (); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $grammar = <<'...'; |
10
|
|
|
|
|
|
|
# Operator Precedence, Lowest to Highest |
11
|
|
|
|
|
|
|
# , |
12
|
|
|
|
|
|
|
# ?: (right) |
13
|
|
|
|
|
|
|
# || |
14
|
|
|
|
|
|
|
# && |
15
|
|
|
|
|
|
|
# | ^ |
16
|
|
|
|
|
|
|
# & |
17
|
|
|
|
|
|
|
# == != |
18
|
|
|
|
|
|
|
# < > <= >= |
19
|
|
|
|
|
|
|
# << >> |
20
|
|
|
|
|
|
|
# + - |
21
|
|
|
|
|
|
|
# * / % |
22
|
|
|
|
|
|
|
# ! - (unary) (right) |
23
|
|
|
|
|
|
|
# ** (right) |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# ByteBeat Precedence-Climbing Grammar |
26
|
|
|
|
|
|
|
bytebeat: expr # -list - |
27
|
|
|
|
|
|
|
expr-list: expr+ %% /- ','/ |
28
|
|
|
|
|
|
|
expr: log-or-expr |
29
|
|
|
|
|
|
|
log-or-expr: log-and-expr+ % /- ( '||' )/ |
30
|
|
|
|
|
|
|
log-and-expr: or-expr+ % /- ( '&&' )/ |
31
|
|
|
|
|
|
|
or-expr: and-expr+ % /- ( '|' | '^' )/ |
32
|
|
|
|
|
|
|
and-expr: equal-expr+ % /- ( '&' )/ |
33
|
|
|
|
|
|
|
equal-expr: compare-expr+ % /- ( '==' | '!=' )/ |
34
|
|
|
|
|
|
|
compare-expr: shift-expr+ % /- ( '<' | '>' | '<=' | '>=' )/ |
35
|
|
|
|
|
|
|
shift-expr: add-sub-expr+ % /- ( '<<' | '>>' )/ |
36
|
|
|
|
|
|
|
add-sub-expr: mul-div-mod-expr+ % /- ( '+' | '-' )/ |
37
|
|
|
|
|
|
|
mul-div-mod-expr: power-expr+ % /- ( '*' | '/' | '%' )/ |
38
|
|
|
|
|
|
|
power-expr: token+ % /- ( '**' )/ |
39
|
|
|
|
|
|
|
token: |
40
|
|
|
|
|
|
|
| /- '('/ expr /- ')'/ |
41
|
|
|
|
|
|
|
| variable |
42
|
|
|
|
|
|
|
| integer |
43
|
|
|
|
|
|
|
variable: /- ( 't' )/ |
44
|
|
|
|
|
|
|
integer: /- ( DASH? DIGIT+ )/ |
45
|
|
|
|
|
|
|
... |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub compile { |
48
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
49
|
0
|
|
|
0
|
|
|
local $SIG{__WARN__} = sub { }; |
|
0
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $parser = Pegex::Parser->new( |
51
|
|
|
|
|
|
|
grammar => Pegex::Grammar->new( |
52
|
|
|
|
|
|
|
text => $grammar, |
53
|
|
|
|
|
|
|
), |
54
|
|
|
|
|
|
|
receiver => ByteBeat::RPN->new(), |
55
|
|
|
|
|
|
|
# debug => 1, |
56
|
|
|
|
|
|
|
); |
57
|
0
|
|
|
|
|
|
$parser->parse($self->code); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |