App/financeta/language.pm

filter: Statementstmt 88.8% 327 / 368 Branchbran 58.9% 79 / 134 Conditioncond 42.8% 15 / 35 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 93.7% 60 / 64 Podpod 0.0% 0 / 3 totaltotal 77.5% 484 / 624 CC 84 SCAR 22.1
CC
84
Cov
78%
_generate_pdl_custom
28.0
got_comparison_state
23.6
got_boolean
12.0
CRAP 9.1
« auto/share/dist/App-financeta/testpdljs.pl ↑ Summary App/financeta/data.pm »
1 package App::financeta::language::grammar;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.16';
7 3 $VERSION = eval $VERSION;
8 3 ▶use Pegex::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 extends 'Pegex::Grammar';
10 3 ▶use App::financeta::utils qw(log_filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Log::Any '$log', filter => \&App::financeta::utils::log_filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use constant text => <<GRAMMAR;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 %grammar financeta
15 %version 0.16
16  
17 program: statement* end-of-program
18 statement: comment | instruction | declaration
19  
20 end-of-program: - EOS
21 comment: /- HASH ANY* EOL/ | blank-line
22 blank-line: /- EOL/
23  
24 _: / BLANK* EOL?/
25 __: / BLANK+ EOL?/
26 line-ending: /- SEMI - EOL?/
27  
28 declaration: - /((i:'allow'|'no'))/ - /((i:'long'|'short'))/ - /(i:'trades')/ - line-ending
29 instruction: order - /(i:'when'|'if')/ - conditions - line-ending
30 conditions: single-condition | nested-condition
31  
32 nested-condition: start-nested single-condition end-nested
33 single-condition: any-condition-expr+ % logic-op
34 any-condition-expr: single-condition-expr | nested-condition-expr
35 nested-condition-expr: start-nested single-condition-expr end-nested
36 single-condition-expr: comparison | complement
37 comparison: comparison-state | comparison-basic
38 comparison-state: - variable - state-op-pre - state - state-op-post? -
39 comparison-basic: - value-expression - compare-op - value-expression -
40  
41 complement: - not-op - value-expression
42 value-expression: complement | value
43 state: /((i:'positive' | 'negative' | 'zero'))/ | value
44 value: variable | number
45 state-op-pre: - /((i: 'becomes' | 'crosses' ))/ -
46 state-op-post: - /(i: 'from') - ((i: 'above' | 'below'))/ -
47 compare-op: /((i:'is' | 'equals'))/ |
48 /([ BANG EQUAL LANGLE RANGLE] EQUAL | (: LANGLE | RANGLE ))/
49 not-op: /((i:'not') | BANG)/
50 logic-op: /((i:'and' | 'or'))/ | /([ AMP PIPE ]{2})/
51  
52 # instruction-task
53 order: buy-sell quantity? - /(i:'at')/ - price -
54 buy-sell: - /((i:'buy' | 'sell'))/ -
55 quantity: number
56 price: - variable | number -
57 variable: - DOLLAR identifier -
58  
59 # basic tokens
60 start-nested: /- LPAREN -/
61 end-nested: /- RPAREN -/
62 identifier: /(! keyword)( ALPHA [ WORDS ]*)/
63 keyword: /(i:
64 'buy' | 'sell' | 'at' | 'equals' | 'true' | 'false' | 'if' |
65 'when' | 'and' | 'or' | 'not' | 'above' | 'is' |
66 'becomes' | 'crosses' | 'below' | 'from' | 'to' |
67 'positive' | 'negative' | 'zero' | 'over' | 'into' |
68 'allow' | 'trades' | 'short' | 'long' | 'no'
69 )/
70 number: real-number | integer | boolean
71 real-number: /('-'? DIGIT* '.' DIGIT+)/
72 integer: /('-'? DIGIT+)/
73 boolean: /((i:'true'|'false'))/
74  
75 GRAMMAR
76  
77 1 ▶sub get_regexes {
Subroutine get_regexes: called CC=1 SCAR=0.0
78 1 my $conditions = [qw(
79 at equals if when and or not is becomes crosses from to
80 over into trades
81 )];
82 1 my $keywords = [qw(
83 buy sell negative positive zero below above long short
84 )];
85 1 my $booleans = [qw(allow no true false)];
86 1 my $variables = '(\$\w+)';
87 return {
88 1 conditions => $conditions,
89 keywords => $keywords,
90 booleans => $booleans,
91 variables => $variables,
92 };
93 }
94  
95 3 1;
96  
97 package App::financeta::language::receiver;
98 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 3 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 3 our $VERSION = '0.16';
103 3 $VERSION = eval $VERSION;
104 3 ▶use App::financeta::utils qw(log_filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
105 3 ▶use Log::Any '$log', filter => \&App::financeta::utils::log_filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 3 ▶use Perl::Tidy;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 3 ▶use Pegex::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 3 extends 'Pegex::Tree';
109  
110 3 has debug => 0;
111  
112 3 has preset_vars => [];
113  
114 3 has preset_vars_hash => undef;
115  
116 3 has const_vars => {
117 positive => sprintf("%0.06f", 1e-6),
118 negative => sprintf("%0.06f", -1e-6),
119 zero => 0,
120 lookback => 1,
121 };
122  
123 3 has local_vars => {};
124  
125 3 has index_var_count => 0;
126  
127 3 has PnL => { long => 1, short => 0 };
128  
129 78 ▶sub got_comment {} # strip the comments out
Subroutine got_comment: called CC=1 SCAR=0.0
130  
131 0 ▶sub got_boolean {
Subroutine got_boolean: not called CC=3 SCAR=24.8
132 0 my ($self, $got) = @_;
133 0 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 0 0 0
134 0 $self->flatten($got);
135 0 $got = shift @$got;
136 }
137 0 ▶ return ($got eq 'true') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$got eq 'true' ? : 0 0 0
138 }
139  
140 40 ▶sub got_variable {
Subroutine got_variable: called CC=5 SCAR=16.9
141 40 my ($self, $got) = @_;
142 40 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 40 0 40
143 40 $self->flatten($got);
144 40 $got = shift @$got;
145 }
146 40 $got = lc $got; # case-insensitive
147 40 ▶ unless (defined $self->preset_vars_hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->preset_vars_hash) 6 34 40
148 6 my $arr = $self->preset_vars;
149 6 my %pvars = map { $_ => 1 } @$arr;
150 6 $self->preset_vars_hash(\%pvars);
151 }
152 40 ▶ if (exists $self->preset_vars_hash->{$got} or
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->preset_vars_hash->{$got} or exists $self->local_vars->{$got}) { } 40 0 40
Condition33%1/3
exists $self->preset_vars_hash->{$got} or exists $self->local_vars->{$got}
l!l&&r!l&&!r
4000
MC/DC0%0/2
exists $self->preset_vars_hash->{$got} or exists $self->local_vars->{$got}
exists $self->preset_vars_hash->{$got} exists $self->local_vars->{$got}
Truth table33%1/3
A or B
A= exists $self->preset_vars_hash->{$got}B= exists $self->local_vars->{$got}
ABresult
1X1
011
000
153 exists $self->local_vars->{$got}) {
154 # do nothing
155 } else {
156 0 $self->parser->throw_error( "Variable $got does not exist");
157 0 return;
158 # $self->local_vars->{$got} = 1;
159 }
160 40 return '$' . $got;
161 }
162  
163 0 ▶sub got_quantity {
Subroutine got_quantity: not called CC=2 SCAR=17.9
164 0 my ($self, $got) = @_;
165 0 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 0 0 0
166 0 $self->flatten($got);
167 0 $got = shift @$got;
168 }
169 0 return { quantity => $got };
170 }
171  
172 10 ▶sub got_price {
Subroutine got_price: called CC=2 SCAR=7.0
173 10 my ($self, $got) = @_;
174 10 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 10 0 10
175 10 $self->flatten($got);
176 10 $got = shift @$got;
177 }
178 10 return { price => $got };
179 }
180  
181 12 ▶sub got_value {
Subroutine got_value: called CC=2 SCAR=8.4
182 12 my ($self, $got) = @_;
183 12 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 0 12 12
184 0 $self->flatten($got);
185 0 XXX {value => $got};
186 }
187 12 return $got;
188 }
189  
190 10 ▶sub got_buy_sell {
Subroutine got_buy_sell: called CC=2 SCAR=7.0
191 10 my ($self, $got) = @_;
192 10 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 10 0 10
193 10 $self->flatten($got);
194 10 $got = shift @$got;
195 }
196 10 return { trigger => lc $got };
197 }
198  
199 1 ▶sub got_compare_op {
Subroutine got_compare_op: called CC=2 SCAR=7.1
200 1 my ($self, $got) = @_;
201 1 ▶ $got = '==' if ($got =~ /is|equals/i);
Branch: 1 branch
BranchTrueFalseTotal
if $got =~ /is|equals/i 0 1 1
202 1 return { compare => $got};
203 }
204  
205 0 ▶sub got_not_op {
Subroutine got_not_op: not called CC=2 SCAR=17.9
206 0 my ($self, $got) = @_;
207 0 $got = lc $got;
208 0 ▶ $got = '!' if $got eq 'not';
Branch: 1 branch
BranchTrueFalseTotal
if $got eq "not" 0 0 0
209 0 return { complement => $got };
210 }
211  
212 9 ▶sub got_logic_op {
Subroutine got_logic_op: called CC=3 SCAR=11.3
213 9 my ($self, $got) = @_;
214 9 $got = lc $got;
215 9 ▶ $got = '&' if $got eq 'and';
Branch: 1 branch
BranchTrueFalseTotal
if $got eq "and" 9 0 9
216 9 ▶ $got = '|' if $got eq 'or';
Branch: 1 branch
BranchTrueFalseTotal
if $got eq "or" 0 9 9
217 9 return { logic => $got };
218 }
219  
220 18 ▶sub got_state_op_pre {
Subroutine got_state_op_pre: called CC=2 SCAR=7.0
221 18 my ($self, $got) = @_;
222 18 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 18 0 18
223 18 $self->flatten($got);
224 18 $got = shift @$got;
225 }
226 18 return 'ACT::' . lc $got;
227 }
228  
229 10 ▶sub got_state_op_post {
Subroutine got_state_op_post: called CC=2 SCAR=7.0
230 10 my ($self, $got) = @_;
231 10 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 10 0 10
232 10 $self->flatten($got);
233 10 $got = shift @$got;
234 }
235 10 return 'DIRXN::' . lc $got;
236 }
237  
238 18 ▶sub got_state {
Subroutine got_state: called CC=5 SCAR=17.5
239 18 my ($self, $got) = @_;
240 18 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 0 18 18
241 0 $self->flatten($got);
242 0 XXX {state => $got};
243 }
244 18 ▶ return $got if $got =~ /^\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $got =~ /^\$/ 10 8 18
245 8 ▶ $got = 0 if $got eq 'zero';
Branch: 1 branch
BranchTrueFalseTotal
if $got eq "zero" 0 8 8
246 # if it is a number
247 8 ▶ return "STATE::$got" if $got =~ /^[\d\.\+\-]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $got =~ /^[\d\.\+\-]+$/ 0 8 8
248 8 return 'STATE::' . lc $got;
249 }
250  
251 18 ▶sub got_comparison_state {
Subroutine got_comparison_state: called CC=12 SCAR=31.6
252 18 my ($self, $got) = @_;
253 18 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq 'ARRAY') { } 18 0 18
254 18 $self->flatten($got);
255 } else {
256 0 XXX {comparison_state => $got};
257 }
258 18 my ($var, $act, $state, $dirxn) = @$got;
259 18 my $fn;
260 18 ▶ if ($act eq 'ACT::becomes') {
Branch: 2 branches
BranchTrueFalseTotal
if ($act eq 'ACT::becomes') { } 8 10 18
elsif ($act eq 'ACT::crosses') { } 10 0 10
261 8 ▶ if ($state =~ /^\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /^\$/) { } 0 8 8
262 # state is a variable
263 0 $fn = 'merge';
264 } else {
265 8 $state =~ s/^STATE:://;
266 # use the const_var values
267 8 ▶ $state = $self->const_vars->{$state} if $state =~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /\w/ 8 0 8
268 8 $fn = 'become';
269 }
270 } elsif ($act eq 'ACT::crosses') {
271 10 ▶ $dirxn = 'DIRXN::below' unless defined $dirxn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dirxn 0 10 10
272 10 ▶ $fn = "x$1" if $dirxn =~ /DIRXN::(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $dirxn =~ /DIRXN::(.*)/ 10 0 10
273 10 ▶ if ($state =~ /STATE::(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /STATE::(.*)/) 0 10 10
274 0 $state = $1;
275 # use the const_var values
276 $state = $self->const_vars->{$state} if ($state =~ /\w/ and
277 0 ▶ defined $self->const_vars->{$state});
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /\w/ and defined $self->const_vars->{$state} 0 0 0
Condition0%0/3
$state =~ /\w/ and defined $self->const_vars->{$state}
!ll&&!rl&&r
000
MC/DC0%0/2
$state =~ /\w/ and defined $self->const_vars->{$state}
$state =~ /\w/ defined $self->const_vars->{$state}
Truth table0%0/3
A and B
A= $state =~ /\w/B= defined $self->const_vars->{$state}
ABresult
0X0
100
111
278 }
279 } else {
280 0 XXX {comparison_state => $got};
281 }
282 18 ▶ unless (defined $fn) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fn) 0 18 18
283 0 XXX {comparison_state => $got};
284 }
285 18 return { $fn => [ $var, $state ] };
286 }
287  
288 0 ▶sub got_complement {
Subroutine got_complement: not called CC=1 SCAR=6.9
289 0 my ($self, $got) = @_;
290 0 XXX { complement => $got };
291 }
292  
293 2 ▶sub got_value_expression {
Subroutine got_value_expression: called CC=3 SCAR=14.2
294 2 my ($self, $got) = @_;
295 2 ▶ if (ref $got eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $got eq 'ARRAY') { } 0 2 2
elsif (ref $got eq 'HASH') { } 0 2 2
296 0 $self->flatten($got);
297 } elsif (ref $got eq 'HASH') {
298 0 XXX { value_expr => $got };
299 } else {
300 # single values like variables and numbers
301 2 return $got;
302 }
303 0 XXX { value_expr => $got };
304 }
305  
306 1 ▶sub _got_comparison_basic_3 {
Subroutine _got_comparison_basic_3: called CC=4 SCAR=17.9
307 1 my ($self, $lhs, $op, $rhs) = @_;
308 1 ▶ if (ref $lhs ne 'HASH' and ref $rhs ne 'HASH' and ref $op eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lhs ne 'HASH' and ref $rhs ne 'HASH' and ref $op eq 'HASH') { } 1 0 1
Condition33%2/6
ref $lhs ne "HASH" and ref $rhs ne "HASH" and ref $op eq "HASH"
!ll&&!rl&&r
001
ref $lhs ne "HASH" and ref $rhs ne "HASH"
!ll&&!rl&&r
001
MC/DC0%0/3
ref $lhs ne "HASH" and ref $rhs ne "HASH" and ref $op eq "HASH"
ref $lhs ne "HASH" ref $rhs ne "HASH" ref $op eq "HASH"
Truth table25%1/4
A and B and C
A= ref $lhs ne "HASH"B= ref $rhs ne "HASH"C= ref $op eq "HASH"
ABCresult
0XX0
10X0
1100
1111
309 1 return { compare => [ $lhs, $rhs, $op->{compare} ] };
310 } else {
311 0 XXX { comparison_basic => [$lhs, $op, $rhs] };
312 }
313 }
314  
315 1 ▶sub got_comparison_basic {
Subroutine got_comparison_basic: called CC=3 SCAR=12.7
316 1 my ($self, $got) = @_;
317 1 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq 'ARRAY') { } 1 0 1
318 1 $self->flatten($got);
319 } else {
320 0 XXX {comparison_basic => $got};
321 }
322 1 ▶ return $self->_got_comparison_basic_3(@$got) if scalar(@$got) eq 3;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$got eq 3 1 0 1
323 0 XXX {comparison_basic => $got};
324 }
325  
326 10 ▶sub got_order {
Subroutine got_order: called CC=4 SCAR=14.3
327 10 my ($self, $got) = @_;
328 10 ▶ $self->flatten($got) if ref $got eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $got eq "ARRAY" 10 0 10
329 10 my $res = {};
330 # merge the order trigger details into one hash
331 10 foreach (@$got) {
332 20 ▶ if (ref $_ eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 20 0 20
333 20 $res = { %$res, %{$_} };
334 } else {
335 0 XXX {order => $got};
336 }
337 }
338 10 return { order => $res };
339 }
340  
341 10 ▶sub got_conditions {
Subroutine got_conditions: called CC=1 SCAR=0.0
342 10 my ($self, $got) = @_;
343 # conditions have to be in the order that the user asked them
344 10 return { conditions => $got };
345 }
346  
347 10 ▶sub got_instruction {
Subroutine got_instruction: called CC=3 SCAR=11.2
348 10 my ($self, $got) = @_;
349 10 my $res = {};
350 # merge the order trigger details into one hash
351 10 foreach (@$got) {
352 20 ▶ if (ref $_ eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 20 0 20
353 20 $res = { %$res, %{$_} };
354 } else {
355 0 XXX { instruction => $got };
356 }
357 }
358 10 return $res;
359 }
360  
361 1 ▶sub got_declaration {
Subroutine got_declaration: called CC=6 SCAR=18.7
362 1 my ($self, $got) = @_;
363 1 ▶ if (ref $got eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $got eq "ARRAY") 1 0 1
364 1 $self->flatten($got);
365 }
366 1 ▶ return unless scalar @$got >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$got >= 2 0 1 1
367 1 my $short = 0;
368 1 my $long = 0;
369 1 ▶ my $val = 1 if $got->[0] eq 'allow';
Branch: 1 branch
BranchTrueFalseTotal
if $got->[0] eq "allow" 0 1 1
370 1 ▶ $val = 0 if $got->[0] eq 'no';
Branch: 1 branch
BranchTrueFalseTotal
if $got->[0] eq "no" 1 0 1
371 1 my $type = $got->[1];
372 1 ▶ $self->PnL->{$type} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 1 0 1
373 1 return; # don't return anything
374 }
375  
376 6 ▶sub _generate_pdl_begin {
Subroutine _generate_pdl_begin: called CC=2 SCAR=6.9
377 6 my $self = shift;
378 6 my $lookback = $self->const_vars->{lookback};
379 6 my $pvars = $self->preset_vars;
380 6 my @decls = ();
381 6 foreach (@$pvars) {
382 49 push @decls, 'my $' . $_ . ' = shift;';
383 }
384 6 my @exprs = (
385 'use PDL;',
386 'use PDL::NiceSlice;',
387 'sub {', # an anonymous sub
388 @decls,
389 'my $buys = zeroes($close->dims);',
390 'my $sells = zeroes($close->dims);',
391 'my $lookback = ' . $lookback . ';',
392 );
393 6 return join("\n", @exprs);
394 }
395  
396 6 ▶sub _generate_pdl_end {
Subroutine _generate_pdl_end: called CC=3 SCAR=11.1
397 6 my $self = shift;
398 6 my $lookback = $self->const_vars->{lookback};
399 6 my $long = $self->PnL->{long};
400 6 my $short = $self->PnL->{short};
401 6 ▶ $long = 1 unless defined $long;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $long 0 6 6
402 6 ▶ $short = 0 unless defined $short;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $short 0 6 6
403 6 my @exprs = (
404 "return { buys => \$buys, sells => \$sells, long => $long, short => $short };",
405 '}', # end of subroutine
406 );
407 6 return join("\n", @exprs);
408 }
409  
410 10 ▶sub _generate_pdl_custom {
Subroutine _generate_pdl_custom: called CC=25 SCAR=33.3
411 10 my ($self, $ins) = @_;
412 #YYY { instruction => $ins };
413 10 ▶ if (ref $ins ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ins ne "HASH") 0 10 10
414 0 XXX $ins;
415 }
416 10 my $order = $ins->{order};
417 10 my $conds = $ins->{conditions};
418 10 ▶ if ((defined $order and ref $order ne 'HASH') or
Branch: 1 branch
BranchTrueFalseTotal
if (defined $order and ref $order ne "HASH" or defined $conds and ref $conds ne "ARRAY") 0 10 10
Condition33%3/9
defined $order and ref $order ne "HASH" or defined $conds and ref $conds ne "ARRAY"
l!l&&r!l&&!r
0010
defined $order and ref $order ne "HASH"
!ll&&!rl&&r
0100
defined $conds and ref $conds ne "ARRAY"
!ll&&!rl&&r
0100
MC/DC0%0/4
defined $order and ref $order ne "HASH" or defined $conds and ref $conds ne "ARRAY"
defined $order ref $order ne "HASH" defined $conds ref $conds ne "ARRAY"
Truth table14%1/7
A and B or C and D
A= defined $orderB= ref $order ne "HASH"C= defined $condsD= ref $conds ne "ARRAY"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
419 (defined $conds and ref $conds ne 'ARRAY')) {
420 0 XXX $ins;
421 }
422 # conds is a stack of hashes
423 10 my @indexes = ();
424 10 my @expressions = ();
425 10 ▶ while (@$conds) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$conds) 28 10 38
426 28 my $c = shift @$conds;
427 28 ▶ next unless ref $c eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "HASH" 0 28 28
428 28 ▶ if (defined $c->{become}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{"become"}) 8 20 28
429 8 my ($state1, $state2) = @{$c->{become}};
430 8 my $expr;
431 8 my $index = $self->index_var_count;
432 8 my $idxvar = '$idx_' . $index;
433 8 push @indexes, "my $idxvar = xvals($state1" .
434 '->dims) - $lookback; ';
435 8 push @indexes, "$idxvar = $idxvar" .
436 "->setbadif($idxvar < 0)->setbadtoval(0);";
437 8 $self->index_var_count($index + 1);
438 # state is not a var but a number for become
439 # masks use bitwise & instead of logical &&
440 8 ▶ if ($state2 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state2 >= 0) { } 4 4 8
441 4 $expr = "($state1 >= $state2) & ($state1" .
442 "->index($idxvar) < $state2)";
443 } else {
444 4 $expr = "($state1 <= $state2) & ($state1" .
445 "->index($idxvar) > $state2)";
446 }
447 8 push @expressions, $expr;
448 }
449 28 ▶ if (defined $c->{xbelow} or defined $c->{xabove}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{"xbelow"} or defined $c->{"xabove"}) 10 18 28
Condition100%3/3
defined $c->{"xbelow"} or defined $c->{"xabove"}
l!l&&r!l&&!r
5518
MC/DC100%2/2
defined $c->{"xbelow"} or defined $c->{"xabove"}
defined $c->{"xbelow"} defined $c->{"xabove"}
Truth table100%3/3
A or B
A= defined $c->{"xbelow"}B= defined $c->{"xabove"}
ABresult
1X1
011
000
450 10 ▶ my $dirxn = 'xabove' if defined $c->{xabove};
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->{"xabove"} 5 5 10
451 10 ▶ $dirxn = 'xbelow' if defined $c->{xbelow};
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->{"xbelow"} 5 5 10
452 10 my ($state1, $state2) = @{$c->{$dirxn}};
453 10 my $index = $self->index_var_count;
454 #TODO: whatif the state1 and state2 have different dims ?
455 10 my $idxvar = '$idx_' . $index;
456 10 push @indexes, "my $idxvar = xvals($state1" .
457 '->dims) - $lookback; ';
458 10 push @indexes, "$idxvar = $idxvar" .
459 "->setbadif($idxvar < 0)->setbadtoval(0);";
460 10 $self->index_var_count($index + 1);
461 # state2 can be var or number
462 10 my $expr;
463 10 ▶ my $s1 = '<' if $dirxn eq 'xbelow';
Branch: 1 branch
BranchTrueFalseTotal
if $dirxn eq "xbelow" 5 5 10
464 10 ▶ $s1 = '>' if $dirxn eq 'xabove';
Branch: 1 branch
BranchTrueFalseTotal
if $dirxn eq "xabove" 5 5 10
465 10 ▶ my $s2 = '>' if $dirxn eq 'xbelow';
Branch: 1 branch
BranchTrueFalseTotal
if $dirxn eq "xbelow" 5 5 10
466 10 ▶ $s2 = '<' if $dirxn eq 'xabove';
Branch: 1 branch
BranchTrueFalseTotal
if $dirxn eq "xabove" 5 5 10
467 # masks use bitwise & instead of logical &&
468 10 ▶ if ($state2 =~ /^\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state2 =~ /^\$/) { } 10 0 10
469 10 $expr = "($state1" . "->index($idxvar) $s1 $state2" .
470 "->index($idxvar)) & ($state1 $s2 $state2)";
471 } else {
472 0 $expr = "($state1" . "->index($idxvar) $s1 $state2) "
473 . "& ($state1 $s2 $state2)";
474 }
475 10 push @expressions, $expr;
476 }
477 28 ▶ if (defined $c->{logic}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{"logic"}) 9 19 28
478 9 push @expressions, $c->{logic};
479 }
480 28 ▶ if (defined $c->{compare}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{"compare"}) 1 27 28
481 1 my ($lhs, $rhs, $op) = @{$c->{compare}};
482 1 my $expr = "($lhs $op $rhs)";
483 1 push @expressions, $expr;
484 }
485 }
486 #YYY { expressions => \@expressions, indexes => \@indexes };
487 10 ▶ if (defined $order->{trigger} and defined $order->{price}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $order->{'trigger'} and defined $order->{'price'}) { } 10 0 10
Condition33%1/3
defined $order->{"trigger"} and defined $order->{"price"}
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $order->{"trigger"} and defined $order->{"price"}
defined $order->{"trigger"} defined $order->{"price"}
Truth table33%1/3
A and B
A= defined $order->{"trigger"}B= defined $order->{"price"}
ABresult
0X0
100
111
488 10 my $trig = $order->{trigger};
489 10 my $px = $order->{price};
490 10 ▶ my $qty = $order->{quantity} || 100;
Condition50%1/2
$order->{'quantity'} || 100
l!l
010
MC/DC0%0/1
$order->{'quantity'} || 100
$order->{'quantity'}
Truth table50%1/2
A
A= $order->{'quantity'}
Aresult
11
00
491 # px can be a variable or number
492 10 my $tvar = '$' . $trig . 's';
493 10 my $index = $self->index_var_count;
494 10 my $idxvar = '$idx_' . $index;
495 10 push @indexes, "my $idxvar = which(" .
496 join(' ', @expressions) . ');';
497 10 ▶ if ($px =~ /^\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($px =~ /^\$/) { } 10 0 10
498 10 push @indexes, $tvar . "->index($idxvar) .= $px" .
499 "->index($idxvar);";
500 } else {
501 0 push @indexes, $tvar . "->index($idxvar) .= $px;";
502 }
503 10 $self->index_var_count($index + 1);
504 } else {
505 0 XXX $order;
506 }
507 10 return join("\n", @indexes);
508 }
509  
510 7 ▶sub final {
Subroutine final: called CC=4 SCAR=13.9
511 7 my ($self, $got) = @_;
512 7 ▶ $self->flatten($got) if ref $got eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $got eq "ARRAY" 7 0 7
513 7 my @code = ();
514 7 foreach (@$got) {
515 10 push @code, $self->_generate_pdl_custom($_);
516 }
517 7 ▶ return unless scalar @code;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @code 1 6 7
518 6 my $final_code = join("\n", $self->_generate_pdl_begin(), @code,
519 $self->_generate_pdl_end());
520 6 my $tidy_code;
521 6 Perl::Tidy::perltidy(source => \$final_code, destination => \$tidy_code);
522 6 $log->debug("Tidy Code:\n$tidy_code");
523 6 return $tidy_code;
524 }
525  
526 3 1;
527  
528 package App::financeta::language;
529 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
530 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
531 3 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
532  
533 3 our $VERSION = '0.16';
534 3 $VERSION = eval $VERSION;
535 3 ▶use Pegex::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
536 3 ▶use App::financeta::mo;
Subroutine BEGIN: called CC=1 SCAR=0.0
537 3 ▶use App::financeta::utils qw(log_filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
538 3 ▶use Log::Any '$log', filter => \&App::financeta::utils::log_filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
539  
540 3 $| = 1;
541 3 has debug => 0;
542  
543 3 has preset_vars => [];
544  
545 2 ▶has grammar => (default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
546 2 return App::financeta::language::grammar->new;
547 3 });
548  
549 1 ▶sub get_grammar_regexes {
Subroutine get_grammar_regexes: called CC=1 SCAR=0.0
Pod undocumented
550 1 my $self = shift;
551 1 return $self->grammar->get_regexes;
552 }
553  
554 3 has receiver => (builder => '_build_receiver');
555  
556 2 ▶sub _build_receiver {
Subroutine _build_receiver: called CC=1 SCAR=0.0
557 2 my $self = shift;
558 2 return App::financeta::language::receiver->new(
559 debug => $self->debug,
560 preset_vars => $self->preset_vars,
561 );
562 }
563  
564 3 has parser => (builder => '_build_parser');
565  
566 2 ▶sub _build_parser {
Subroutine _build_parser: called CC=1 SCAR=0.0
567 2 my $self = shift;
568 2 return Pegex::Parser->new(
569 grammar => $self->grammar,
570 receiver => $self->receiver,
571 debug => $self->debug,
572 throw_on_error => 0,
573 );
574 }
575  
576 8 ▶sub compile {
Subroutine compile: called CC=4 SCAR=14.0
Pod undocumented
577 8 my ($self, $text, $presets) = @_;
578 8 ▶ return unless (defined $text and length $text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 1 7 8
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
017
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
579 # update the debug flag to keep it dynamic
580 7 $self->receiver->debug($self->debug);
581 7 $self->receiver->index_var_count(0); # reset for each compilation
582 # update the preset vars if necessary
583 7 ▶ $self->receiver->preset_vars($presets || $self->preset_vars);
Condition66%2/3
$presets || $self->preset_vars
l!l&&r!l&&!r
610
MC/DC0%0/2
$presets || $self->preset_vars
$presets $self->preset_vars
Truth table66%2/3
A || B
A= $presetsB= $self->preset_vars
ABresult
1X1
011
000
584 7 $self->receiver->preset_vars_hash(undef);
585 7 return $self->parser->parse($text);
586 }
587  
588 6 ▶sub generate_coderef {
Subroutine generate_coderef: called CC=3 SCAR=11.0
Pod undocumented
589 6 my ($self, $code) = @_;
590 6 ▶ return unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 6 6
591 6 ▶ my $coderef = eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
592 6 ▶ $log->error("Unable to compile into a code-ref: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
593 6 return $coderef;
594 }
595  
596 3 1;
597  
598 __END__
« auto/share/dist/App-financeta/testpdljs.pl ↑ Summary App/financeta/data.pm »