| 1 | package Language::FormulaEngine::Compiler; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | # This is at the top of the file to make sure the eval namespace is as clean as possible | ||||||||||||||||||||||
| 4 | # Need a second package to avoid getting clobbered by namespace::clean | ||||||||||||||||||||||
| 5 | 134 | ▶ | sub Language::FormulaEngine::Compiler::_CleanEval::_clean_eval { | ||||||||||||||||||||
|
Subroutine
_clean_eval: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 10 | ▶ | use strict; # these apply to the contents of the eval, too. | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 9 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | # Arguments are ($compiler, $perl_code) | ||||||||||||||||||||||
| 9 | 134 | my $default_namespace= $_[0]->namespace; # referred to by evals | |||||||||||||||||||||
| 10 | 134 | ▶ | eval $_[1]; | ||||||||||||||||||||
|
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
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
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
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
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
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
| |||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | 9 | ▶ | use Moo; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | 9 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 9 | ▶ | use Try::Tiny; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | 9 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 17 | 9 | ▶ | use Sub::Util 'subname', 'set_subname'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | 9 | ▶ | use namespace::clean; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | # ABSTRACT: Compile a parse tree into perl code | ||||||||||||||||||||||
| 21 | 7 | our $VERSION = '0.08'; # VERSION | |||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | 7 | *_clean_eval= *Language::FormulaEngine::Compiler::_CleanEval::_clean_eval; | |||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 7 | has namespace => ( is => 'rw', trigger => 1 ); | |||||||||||||||||||||
| 27 | 7 | has optimize_var_access => ( is => 'rw', trigger => 1 ); | |||||||||||||||||||||
| 28 | 7 | has output_api => ( is => 'rw', trigger => 1, default => 'function_of_vars' ); | |||||||||||||||||||||
| 29 | 7 | has _optimize_var_access => ( is => 'lazy', clearer => 1 ); # holds the effective value of optimize_var_access | |||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 7 | has variables_via_namespace => ( is => 'rw' ); # Deprecated | |||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | 7 | has error => ( is => 'rw' ); | |||||||||||||||||||||
| 34 | 7 | has code_body => ( is => 'rw' ); | |||||||||||||||||||||
| 35 | |||||||||||||||||||||||
| 36 | 10 | ▶ | has _perl_generator_cache => ( is => 'lazy', clearer => 1, default => sub { {} } ); | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 142 | ▶ | sub _trigger_namespace { | ||||||||||||||||||||
|
Subroutine
_trigger_namespace: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 39 | 142 | my ($self, $newval)= @_; | |||||||||||||||||||||
| 40 | 142 | ▶ | $self->_clear_perl_generator_cache if $newval ne ($self->{_cur_namespace}||''); | ||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->{'_cur_namespace'} || ''
MC/DC100%1/1
$self->{'_cur_namespace'} || ''
$self->{'_cur_namespace'}
Truth table100%2/2
A
A= $self->{'_cur_namespace'}
| |||||||||||||||||||||||
| 41 | 142 | $self->_clear_optimize_var_access; | |||||||||||||||||||||
| 42 | 142 | $self->{_cur_namespace}= $newval; | |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | 4 | ▶ | sub _trigger_optimize_var_access { | ||||||||||||||||||||
|
Subroutine
_trigger_optimize_var_access: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 46 | 4 | shift->_clear_optimize_var_access; | |||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 5 | ▶ | sub _trigger_output_api { | ||||||||||||||||||||
|
Subroutine
_trigger_output_api: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 50 | 5 | shift->_clear_optimize_var_access; | |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | 134 | ▶ | sub _build__optimize_var_access { | ||||||||||||||||||||
|
Subroutine
_build__optimize_var_access: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 54 | 134 | my $self= shift; | |||||||||||||||||||||
| 55 | 134 | ▶ | return $self->optimize_var_access if defined $self->optimize_var_access; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 56 | 133 | ▶ | return ($self->output_api =~ /^function_of_vars/) | ||||||||||||||||||||
|
Condition66%2/3
$self->output_api =~ /^function_of_vars/ && $self->namespace->can('get_value') == 'Language::FormulaEngine::Namespace'->can('get_value')
MC/DC50%1/2
$self->output_api =~ /^function_of_vars/ && $self->namespace->can('get_value') == 'Language::FormulaEngine::Namespace'->can('get_value')
$self->output_api =~ /^function_of_vars/
$self->namespace->can('get_value') == 'Language::FormulaEngine::Namespace'->can('get_value')
Truth table66%2/3
A && B
A= $self->output_api =~ /^function_of_vars/B= $self->namespace->can('get_value') == 'Language::FormulaEngine::Namespace'->can('get_value')
| |||||||||||||||||||||||
| 57 | && $self->namespace->can('get_value') == Language::FormulaEngine::Namespace->can('get_value'); | ||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | 10 | ▶ | sub BUILD { | ||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 61 | 10 | my ($self, $args)= @_; | |||||||||||||||||||||
| 62 | # Handle back-compat for initial broken version of this feature. | ||||||||||||||||||||||
| 63 | # There is no longer any reason to set variables_via_namespace to true, because true is the default. | ||||||||||||||||||||||
| 64 | # So if a user does that, they might be asking for the 'output_api => "function_of_namespace"' | ||||||||||||||||||||||
| 65 | 10 | ▶ | if (defined $self->variables_via_namespace) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 66 | 4 | carp "variables_via_namespace is deprecated. See 'output_api' and 'optimize_var_access'"; | |||||||||||||||||||||
| 67 | 4 | $self->output_api('function_of_namespace'); | |||||||||||||||||||||
| 68 | 4 | $self->optimize_var_access(0); | |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | 132 | ▶ | sub compile { | ||||||||||||||||||||
|
Subroutine
compile: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||
| 74 | 134 | my ($self, $parse_tree, $subname)= @_; | |||||||||||||||||||||
| 75 | 134 | my $ret; | |||||||||||||||||||||
| 76 | 134 | $self->reset; | |||||||||||||||||||||
| 77 | 132 | ▶ | try { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 78 | 132 | $self->code_body($self->perlgen($parse_tree)); | |||||||||||||||||||||
| 79 | 132 | $ret= $self->generate_coderef_wrapper($self->code_body, $subname); | |||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||
| 81 | 1 | ▶ | catch { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||
| 82 | 1 | ▶ | chomp unless ref $_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 83 | 1 | $self->error($_); | |||||||||||||||||||||
| 84 | 134 | }; | |||||||||||||||||||||
| 85 | 132 | return $ret; | |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | 132 | ▶ | sub reset { | ||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 90 | 132 | my $self= shift; | |||||||||||||||||||||
| 91 | 132 | $self->error(undef); | |||||||||||||||||||||
| 92 | 132 | $self->code_body(undef); | |||||||||||||||||||||
| 93 | 132 | $self; | |||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | 132 | ▶ | sub generate_coderef_wrapper { | ||||||||||||||||||||
|
Subroutine
generate_coderef_wrapper: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||
| 98 | 132 | my ($self, $perl, $subname)= @_; | |||||||||||||||||||||
| 99 | 132 | $self->error(undef); | |||||||||||||||||||||
| 100 | 132 | ▶ | my $wrapper_method= $self->can('_output_wrapper__'.$self->output_api) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 101 | or Carp::croak("Unsupported output_api='".$self->output_api."'"); | ||||||||||||||||||||||
| 102 | 132 | my $code= join "\n", $self->$wrapper_method(qq{# line 0 "compiled formula"\n$perl}); | |||||||||||||||||||||
| 103 | 132 | my $ret; | |||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||
| 105 | 132 | local $@= undef; | |||||||||||||||||||||
| 106 | 132 | ▶ | if (defined ($ret= $self->_clean_eval($code))) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 107 | 132 | ▶ | set_subname $subname, $ret if defined $subname; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||
| 109 | 1 | $self->error($@); | |||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||
| 112 | 132 | return $ret; | |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 131 | ▶ | sub _output_wrapper__function_of_vars { | ||||||||||||||||||||
|
Subroutine
_output_wrapper__function_of_vars: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 116 | 131 | my ($self, $code)= @_; | |||||||||||||||||||||
| 117 | 131 | ▶ | return $self->_optimize_var_access? ( | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 118 | '# line '.(__LINE__+1), | ||||||||||||||||||||||
| 119 | 'my $namespace= $default_namespace;', | ||||||||||||||||||||||
| 120 | 'sub {', | ||||||||||||||||||||||
| 121 | ' use warnings FATAL => qw( uninitialized numeric );', | ||||||||||||||||||||||
| 122 | ' my $vars= $namespace->variables;', | ||||||||||||||||||||||
| 123 | ' $vars= { %$vars, (@_ == 1? %{$_[0]} : @_) } if @_;', | ||||||||||||||||||||||
| 124 | $code, | ||||||||||||||||||||||
| 125 | '}' | ||||||||||||||||||||||
| 126 | ) : ( | ||||||||||||||||||||||
| 127 | '# line '.(__LINE__+1), | ||||||||||||||||||||||
| 128 | 'sub {', | ||||||||||||||||||||||
| 129 | ' use warnings FATAL => qw( uninitialized numeric );', | ||||||||||||||||||||||
| 130 | ' my $namespace= @_ == 0? $default_namespace', | ||||||||||||||||||||||
| 131 | ' : $default_namespace->clone_and_merge(variables => (@_ == 1 && ref $_[0] eq "HASH"? $_[0] : { @_ }));', | ||||||||||||||||||||||
| 132 | $code, | ||||||||||||||||||||||
| 133 | '}' | ||||||||||||||||||||||
| 134 | ) | ||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | 1 | ▶ | sub _output_wrapper__function_of_vars_no_default { | ||||||||||||||||||||
|
Subroutine
_output_wrapper__function_of_vars_no_default: called CC=2 SCAR=9.2
| |||||||||||||||||||||||
| 138 | 1 | my ($self, $code)= @_; | |||||||||||||||||||||
| 139 | 1 | ▶ | return $self->_optimize_var_access? ( | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 140 | '# line '.(__LINE__+1), | ||||||||||||||||||||||
| 141 | 'my $namespace= $default_namespace;', | ||||||||||||||||||||||
| 142 | 'sub {', | ||||||||||||||||||||||
| 143 | ' use warnings FATAL => qw( uninitialized numeric );', | ||||||||||||||||||||||
| 144 | ' my $vars= @_ == 1? $_[0] : { @_ };', | ||||||||||||||||||||||
| 145 | $code, | ||||||||||||||||||||||
| 146 | '}' | ||||||||||||||||||||||
| 147 | ) : ( | ||||||||||||||||||||||
| 148 | '# line '.(__LINE__+1), | ||||||||||||||||||||||
| 149 | 'sub {', | ||||||||||||||||||||||
| 150 | ' use warnings FATAL => qw( uninitialized numeric );', | ||||||||||||||||||||||
| 151 | ' my $namespace= ref($default_namespace)->new(variables => (@_ == 1? $_[0] : { @_ }));', | ||||||||||||||||||||||
| 152 | $code, | ||||||||||||||||||||||
| 153 | '}' | ||||||||||||||||||||||
| 154 | ) | ||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | 2 | ▶ | sub _output_wrapper__function_of_namespace { | ||||||||||||||||||||
|
Subroutine
_output_wrapper__function_of_namespace: called CC=2 SCAR=7.2
| |||||||||||||||||||||||
| 158 | 2 | my ($self, $code)= @_; | |||||||||||||||||||||
| 159 | return | ||||||||||||||||||||||
| 160 | 2 | ▶ | '# line '.(__LINE__+1), | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 161 | 'sub {', | ||||||||||||||||||||||
| 162 | ' use warnings FATAL => qw( uninitialized numeric );', | ||||||||||||||||||||||
| 163 | ' my $namespace= @_ == 0? $default_namespace', | ||||||||||||||||||||||
| 164 | ' : @_ == 1 && Scalar::Util::blessed($_[0])? $_[0]', | ||||||||||||||||||||||
| 165 | ' : $default_namespace->clone_and_merge(@_);', | ||||||||||||||||||||||
| 166 | ($self->_optimize_var_access? ' my $vars= $namespace->variables;' : ()), | ||||||||||||||||||||||
| 167 | $code, | ||||||||||||||||||||||
| 168 | '}' | ||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | |||||||||||||||||||||||
| 172 | 490 | ▶ | sub perlgen { | ||||||||||||||||||||
|
Subroutine
perlgen: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||
| 173 | 490 | my ($self, $node)= @_; | |||||||||||||||||||||
| 174 | 490 | ▶ | if ($node->can('function_name')) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 175 | 189 | my $name= $node->function_name; | |||||||||||||||||||||
| 176 | 189 | ▶ | my $gen= $self->_perl_generator_cache->{$name} ||= $self->_get_perl_generator($name); | ||||||||||||||||||||
|
Condition66%2/3
$self->_perl_generator_cache->{$name} ||= $self->_get_perl_generator($name)
MC/DC0%0/2
$self->_perl_generator_cache->{$name} ||= $self->_get_perl_generator($name)
$self->_perl_generator_cache->{$name}
$self->_get_perl_generator($name)
Truth table66%2/3
A ||= B
A= $self->_perl_generator_cache->{$name}B= $self->_get_perl_generator($name)
| |||||||||||||||||||||||
| 177 | 189 | return $gen->($self->namespace, $self, $node); | |||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||
| 179 | elsif ($node->can('symbol_name')) { | ||||||||||||||||||||||
| 180 | 81 | my $name= $node->symbol_name; | |||||||||||||||||||||
| 181 | 81 | my $x= $self->namespace->get_constant($name); | |||||||||||||||||||||
| 182 | 81 | ▶ | return defined $x? $self->perlgen_literal($x) : $self->perlgen_var_access($name); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||
| 184 | elsif ($node->can('string_value')) { | ||||||||||||||||||||||
| 185 | 57 | return $self->perlgen_string_literal($node->string_value); | |||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||
| 187 | elsif ($node->can('number_value')) { | ||||||||||||||||||||||
| 188 | 166 | return $node->number_value+0; | |||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||
| 190 | else { | ||||||||||||||||||||||
| 191 | 1 | die "Don't know how to compile node of type '".ref($node)."'\n"; | |||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||
| 194 | |||||||||||||||||||||||
| 195 | 71 | ▶ | sub _get_perl_generator { | ||||||||||||||||||||
|
Subroutine
_get_perl_generator: called CC=6 SCAR=18.5
| |||||||||||||||||||||||
| 196 | 71 | my ($self, $name)= @_; | |||||||||||||||||||||
| 197 | 71 | ▶ | my $info= $self->namespace->get_function($name) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 198 | or die "No such function '$name'\n"; | ||||||||||||||||||||||
| 199 | # If a generator is given, nothing else to do. | ||||||||||||||||||||||
| 200 | 71 | ▶ | return $info->{perl_generator} if $info->{perl_generator}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 201 | |||||||||||||||||||||||
| 202 | # Else need to create a generator around a native perl function | ||||||||||||||||||||||
| 203 | $info->{native} | ||||||||||||||||||||||
| 204 | 52 | ▶ | or die "Cannot compile function '$name'; no generator or native function given\n"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 205 | 52 | ▶ | my $fqn= subname($info->{native}) || ''; | ||||||||||||||||||||
|
Condition50%1/2
subname($info->{'native'}) || ''
MC/DC0%0/1
subname($info->{'native'}) || ''
subname($info->{'native'})
Truth table50%1/2
A
A= subname($info->{'native'})
| |||||||||||||||||||||||
| 206 | # For security, make reasonably sure that perl will parse the subname as a function name. | ||||||||||||||||||||||
| 207 | # This regex is more restrictive than perl's actual allowed identifier names. | ||||||||||||||||||||||
| 208 | 52 | ▶ | $fqn =~ /^[A-Za-z_][A-Za-z0-9_]*::([A-Za-z0-9_]+::)*\p{Word}+$/ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 209 | or die "Can't compile function '$name'; native function does not have a valid fully qualified name '$fqn'\n"; | ||||||||||||||||||||||
| 210 | # Create a generator that injects this function name | ||||||||||||||||||||||
| 211 | 135 | ▶ | return sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 212 | 135 | $fqn . '(' . join(',', map $_[1]->perlgen($_), @{ $_[2]->parameters }) . ')' | |||||||||||||||||||||
| 213 | 52 | }; | |||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||
| 215 | |||||||||||||||||||||||
| 216 | |||||||||||||||||||||||
| 217 | 81 | ▶ | sub perlgen_var_access { | ||||||||||||||||||||
|
Subroutine
perlgen_var_access: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 218 | 81 | my ($self, $varname)= @_; | |||||||||||||||||||||
| 219 | 81 | ▶ | return $self->_optimize_var_access | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 220 | ? '$vars->{'.$self->perlgen_string_literal(lc $varname).'}' | ||||||||||||||||||||||
| 221 | : '$namespace->get_value('.$self->perlgen_string_literal($varname).')'; | ||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||
| 223 | |||||||||||||||||||||||
| 224 | |||||||||||||||||||||||
| 225 | 137 | ▶ | sub perlgen_string_literal { | ||||||||||||||||||||
|
Subroutine
perlgen_string_literal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 226 | 137 | my ($self, $string)= @_; | |||||||||||||||||||||
| 227 | 137 | $string =~ s/([\0-\x1F\x7f"\@\$\%\\])/ sprintf("\\x%02x", ord $1) /gex; | |||||||||||||||||||||
| 228 | 137 | return qq{"$string"}; | |||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||
| 230 | |||||||||||||||||||||||
| 231 | |||||||||||||||||||||||
| 232 | 1 | ▶ | sub perlgen_literal { | ||||||||||||||||||||
|
Subroutine
perlgen_literal: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||
| 233 | 1 | my ($self, $string)= @_; | |||||||||||||||||||||
| 234 | 9 | ▶ | no warnings 'numeric'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 235 | 1 | ▶ | return ($string+0) eq $string? $string+0 : $self->perlgen_string_literal($string); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||
| 237 | |||||||||||||||||||||||
| 238 | 7 | 1; | |||||||||||||||||||||
| 239 | |||||||||||||||||||||||
| 240 | __END__ | ||||||||||||||||||||||