| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Affix::Infix2Postfix; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
780
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
43
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
1312
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
#use Data::Dumper; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
|
9
|
|
|
|
|
|
|
require AutoLoader; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
@ISA = qw(Exporter AutoLoader); |
|
12
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
|
13
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
|
14
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
|
15
|
|
|
|
|
|
|
@EXPORT = qw( |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
); |
|
18
|
|
|
|
|
|
|
$VERSION = '0.03'; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
|
22
|
1
|
|
|
1
|
0
|
64
|
my $class = shift; |
|
23
|
1
|
|
|
|
|
6
|
my %hsh=@_; |
|
24
|
1
|
|
|
|
|
2
|
my $self=\%hsh; |
|
25
|
1
|
|
|
|
|
2
|
my $op; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# add some check code here |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# create regular expressions |
|
30
|
|
|
|
|
|
|
# combined lists insert defaults etc. |
|
31
|
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
3
|
for $op (@{$self->{'ops'}}) { |
|
|
1
|
|
|
|
|
3
|
|
|
33
|
9
|
100
|
|
|
|
20
|
if (!exists( $op->{'type'} )) { $op->{'type'}='binary'; } |
|
|
7
|
|
|
|
|
11
|
|
|
34
|
9
|
50
|
|
|
|
20
|
if (!exists( $op->{'assoc'} )) { $op->{'assoc'}='left'; } |
|
|
9
|
|
|
|
|
13
|
|
|
35
|
9
|
100
|
|
|
|
21
|
if (!exists( $op->{'trans'} )) { $op->{'trans'}=$op->{'op'}; } |
|
|
8
|
|
|
|
|
22
|
|
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
2
|
@{$self->{'opr'}}=map { $_->{'op'} } @{$self->{'ops'}}; |
|
|
1
|
|
|
|
|
4
|
|
|
|
9
|
|
|
|
|
17
|
|
|
|
1
|
|
|
|
|
3
|
|
|
39
|
|
|
|
|
|
|
|
|
40
|
1
|
|
|
|
|
3
|
@{$self->{'tokens'}}=(@{$self->{'opr'}},@{$self->{'func'}},@{$self->{'vars'}},@{$self->{'grouping'}}); |
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
1
|
|
|
41
|
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
3
|
$self->{'varre'}=join('|',map { quotemeta($_) } @{$self->{'vars'}}); |
|
|
3
|
|
|
|
|
10
|
|
|
|
1
|
|
|
|
|
2
|
|
|
43
|
1
|
|
|
|
|
3
|
$self->{'funcre'}=join('|',map { quotemeta($_) } @{$self->{'func'}}); |
|
|
4
|
|
|
|
|
10
|
|
|
|
1
|
|
|
|
|
3
|
|
|
44
|
|
|
|
|
|
|
|
|
45
|
1
|
|
|
|
|
3
|
$self->{'numre'}='[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?'; |
|
46
|
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
3
|
$self->{'re'}=join('|',(map { quotemeta($_).'(?!'.quotemeta($_).')' } @{$self->{'tokens'}}),$self->{'numre'}); |
|
|
18
|
|
|
|
|
44
|
|
|
|
1
|
|
|
|
|
3
|
|
|
48
|
1
|
|
|
|
|
5
|
$self->{'ree'}=$self->{'re'}.'|.+?'; |
|
49
|
1
|
|
|
|
|
2
|
$self->{ERRSTR}=''; |
|
50
|
1
|
|
|
|
|
4
|
bless $self,$class; |
|
51
|
1
|
|
|
|
|
5
|
return $self; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub tokenize { |
|
55
|
1
|
|
|
1
|
0
|
8
|
my $self=shift; |
|
56
|
1
|
|
|
|
|
2
|
my $str=shift; |
|
57
|
1
|
|
|
|
|
11
|
my $ree=$self->{'ree'}; |
|
58
|
|
|
|
|
|
|
# print "ree: $ree\n"; |
|
59
|
1
|
|
|
|
|
158
|
return ( $str =~ m/($ree)/g ); # tokenize |
|
60
|
|
|
|
|
|
|
# return ( $str =~ m/($ree)/xg ); # tokenize |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Returns the indices of non recognized tokens |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub verify { |
|
66
|
1
|
|
|
1
|
0
|
4
|
my $self=shift; |
|
67
|
1
|
|
|
|
|
3
|
my $re=$self->{'re'}; |
|
68
|
1
|
|
|
|
|
4
|
my @matches=@_; |
|
69
|
1
|
|
|
|
|
5
|
return grep { $matches[$_] !~ /^$re$/ } 0..$#matches; |
|
|
7
|
|
|
|
|
126
|
|
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub translate { |
|
73
|
1
|
|
|
1
|
0
|
7
|
my $self=shift; |
|
74
|
1
|
|
|
|
|
2
|
my $str=shift; |
|
75
|
1
|
|
|
|
|
2
|
my (@matches,@errors,@res); |
|
76
|
|
|
|
|
|
|
|
|
77
|
1
|
|
|
|
|
4
|
@matches=$self->tokenize($str); |
|
78
|
1
|
|
|
|
|
11
|
@errors=$self->verify(@matches); |
|
79
|
|
|
|
|
|
|
|
|
80
|
1
|
50
|
|
|
|
4
|
if (@errors) { |
|
81
|
0
|
|
|
|
|
0
|
$self->{ERRSTR}='Bad tokens: '.join(' ',@matches[@errors]); |
|
82
|
0
|
|
|
|
|
0
|
return undef; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
1
|
|
|
|
|
7
|
@res=$self->elist(@matches); |
|
86
|
1
|
|
|
|
|
11
|
return @res; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub elist { |
|
91
|
7
|
|
|
7
|
0
|
10
|
my $self=shift; |
|
92
|
7
|
|
|
|
|
8
|
my (@poss,$i,$cop); # possible breaks |
|
93
|
7
|
|
|
|
|
9
|
my $b=0; |
|
94
|
7
|
|
|
|
|
14
|
my $numre=$self->{'numre'}; |
|
95
|
7
|
|
|
|
|
10
|
my $varre=$self->{'varre'}; |
|
96
|
7
|
|
|
|
|
7
|
my (%func,@func,@ops,$un,$fn,$as,$rop,$op,$bi,$bd,$las,@trlist); |
|
97
|
7
|
|
|
|
|
22
|
@func=@{$self->{'func'}}; |
|
|
7
|
|
|
|
|
19
|
|
|
98
|
7
|
|
|
|
|
25
|
@func{@func}=1..@func; |
|
99
|
7
|
|
|
|
|
11
|
@ops=@{$self->{'ops'}}; |
|
|
7
|
|
|
|
|
19
|
|
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# print Dumper(\%func); |
|
102
|
|
|
|
|
|
|
# print "elist: ",join(" ",map { "$_" } @_ ),"\n"; |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# the only single elements should be numbers or vars |
|
105
|
|
|
|
|
|
|
|
|
106
|
7
|
100
|
|
|
|
18
|
if ($#_ == 0) { |
|
107
|
4
|
50
|
|
|
|
64
|
if ( $_[0] =~ m/^($numre|$varre)$/ ) { |
|
108
|
4
|
|
|
|
|
38
|
return $_[0]; |
|
109
|
|
|
|
|
|
|
} else { |
|
110
|
0
|
|
|
|
|
0
|
die "Single element '$_[0]' wrong\n"; |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# All operators and functions |
|
115
|
|
|
|
|
|
|
|
|
116
|
3
|
|
|
|
|
5
|
for $cop(@ops) { |
|
117
|
12
|
100
|
|
|
|
26
|
$un=($cop->{'type'} eq 'unary') ? 1:0; |
|
118
|
12
|
50
|
|
|
|
22
|
$las=($cop->{'assoc'} eq 'left') ? 1:0; |
|
119
|
12
|
50
|
|
|
|
24
|
$fn=($cop->{'op'} eq 'func') ? 1:0; |
|
120
|
12
|
|
|
|
|
15
|
$op=$cop->{'op'}; |
|
121
|
12
|
|
|
|
|
14
|
$rop=$cop->{'trans'}; |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# print Dumper($cop); |
|
124
|
|
|
|
|
|
|
|
|
125
|
12
|
100
|
|
|
|
20
|
if ($un) { # unary operator |
|
126
|
1
|
50
|
|
|
|
5
|
if ($fn) { # magic type for functions |
|
127
|
0
|
0
|
|
|
|
0
|
if ($las) { # left associative |
|
128
|
0
|
0
|
|
|
|
0
|
if ($func{$_[0]}) { return ( $self->elist(@_[1..$#_]) , $_[0] ); } |
|
|
0
|
|
|
|
|
0
|
|
|
129
|
|
|
|
|
|
|
} else { # right associative |
|
130
|
0
|
0
|
|
|
|
0
|
if ($func{$_[-1]}) { return ( $self->elist(@_[0..$#_-1]) , $_[-1] ); } |
|
|
0
|
|
|
|
|
0
|
|
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
} else { |
|
133
|
|
|
|
|
|
|
# print "op: $op\n"; |
|
134
|
1
|
50
|
|
|
|
4
|
if ($las) { # left associative # normal unary ops |
|
135
|
1
|
50
|
|
|
|
4
|
if ($_[0] eq $op) { return ( $self->elist(@_[1..$#_]) , $rop ); } |
|
|
0
|
|
|
|
|
0
|
|
|
136
|
|
|
|
|
|
|
} else { # right associative |
|
137
|
0
|
0
|
|
|
|
0
|
if ($func{$_[-1]}) { return ( $self->elist(@_[0..$#_-1]) , $rop ); } |
|
|
0
|
|
|
|
|
0
|
|
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
} else { # binary operator |
|
141
|
11
|
50
|
|
|
|
17
|
$bi=$las ? ')':'('; |
|
142
|
11
|
50
|
|
|
|
23
|
$bd=$las ? '(':')'; |
|
143
|
|
|
|
|
|
|
# we only need to inspect the ones not at the since they could only be |
|
144
|
|
|
|
|
|
|
# unary ops |
|
145
|
11
|
50
|
|
|
|
33
|
@trlist=$las ? (reverse 0..$#_) : (0..$#_); |
|
146
|
|
|
|
|
|
|
|
|
147
|
11
|
|
|
|
|
13
|
$b=0; #brace count |
|
148
|
11
|
|
|
|
|
14
|
for $i(@trlist) { |
|
149
|
32
|
|
|
|
|
40
|
$_=$_[$i]; |
|
150
|
|
|
|
|
|
|
# print "item: ",$_,"\n"; |
|
151
|
32
|
50
|
|
|
|
55
|
($b++,next) if $_ eq $bi; |
|
152
|
32
|
50
|
|
|
|
48
|
($b--,next) if $_ eq $bd; |
|
153
|
32
|
50
|
|
|
|
67
|
if ($b < 0) { die "Too many ')'\n"; } |
|
|
0
|
|
|
|
|
0
|
|
|
154
|
32
|
50
|
|
|
|
53
|
next if $b; |
|
155
|
32
|
100
|
100
|
|
|
121
|
next if $i==0 or $i==$#_; |
|
156
|
|
|
|
|
|
|
# if we made it here we are outside of braces |
|
157
|
13
|
100
|
|
|
|
26
|
if ( $_ eq $op ) { return ( $self->elist(@_[(0..$i-1)]) , $self->elist(@_[$i+1..$#_]) ,$rop ); } # this is the magic line |
|
|
3
|
|
|
|
|
35
|
|
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# end of binary |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
# print Dumper($cop); |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# this is just for parens |
|
166
|
|
|
|
|
|
|
|
|
167
|
0
|
0
|
0
|
|
|
|
if ( $_[0] eq '(' and $_[$#_] eq ')' ) { |
|
168
|
0
|
0
|
|
|
|
|
if ( $#_<2 ) { die "Empty parens\n"; } |
|
|
0
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
return $self->elist(@_[1..$#_-1]); |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
die "error stack is: @_ error\n"; |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# Preloaded methods go here. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# Autoload methods go after =cut, and are processed by the autosplit program. |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1; |
|
180
|
|
|
|
|
|
|
__END__ |