| 1 | package Dallycot::AST::Sequence; | ||||||||||||||||||||
| 2 | 29 | our $AUTHORITY = 'cpan:JSMITH'; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | # ABSTRACT: Creates a new execution context for child nodes | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 29 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 29 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 29 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 29 | ▶ | use parent 'Dallycot::AST'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 29 | ▶ | use List::Util qw(any); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 29 | ▶ | use Promises qw(deferred); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 29 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | sub new { | ||||||||||||||||||||
| 17 | 45 | ▶ | my ( $class, @expressions ) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 45 | ▶ | my @declarations = grep { blessed($_) && $_->is_declarative } @expressions; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | 45 | ▶ | my @statements = grep { blessed($_) && !$_->is_declarative } @expressions; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 45 | my @assignment_names = grep {defined} map { $_->identifier } @declarations; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | my %namespace_prefixes | ||||||||||||||||||||
| 25 | 45 | = map { $_->prefix => $_->namespace } grep { $_->isa('Dallycot::AST::XmlnsDef') } @declarations; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 45 | @declarations = grep { !$_->isa('Dallycot::AST::XmlnsDef') } @declarations; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 45 | my @namespace_searches = map { $_->namespace } grep { $_->isa('Dallycot::AST::Uses') } @declarations; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 45 | @declarations = grep { !$_->isa('Dallycot::AST::Uses') } @declarations; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 45 | ▶ | $class = ref $class || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | return | ||||||||||||||||||||
| 36 | 45 | bless [ \@declarations, \@statements, \@assignment_names, \%namespace_prefixes, | |||||||||||||||||||
| 37 | \@namespace_searches ] => $class; | ||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub to_rdf { | ||||||||||||||||||||
| 41 | 2 | ▶ | my($self, $model) = @_; | ||||||||||||||||||
|
Subroutine
to_rdf: called CC=8 SCAR=23.4
Pod
undocumented
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 2 | my $bnode = $model -> bnode; | |||||||||||||||||||
| 44 | 2 | my $child_model = $model -> child_model; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 2 | ▶ | while(my($ns, $href) = each %{$self->[3]||{}}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 47 | 0 | ▶ | $child_model -> add_namespace_mapping( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | $ns => (blessed $href ? $href -> value : $href) | ||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 2 | ▶ | my @uses = @{$self -> [4]||[]}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 2 | $child_model -> add_search_path(@uses); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 2 | foreach my $decl (@{$self->[0]}) { | |||||||||||||||||||
| 56 | 2 | $decl -> to_rdf($child_model) | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # actually, we need to build out a lambda for each one and discard | ||||||||||||||||||||
| 60 | # its argument, something like: | ||||||||||||||||||||
| 61 | # { expression[n] }( { expression[n-1] }( { expression[n-2] }( ... ) ) ) | ||||||||||||||||||||
| 62 | # | ||||||||||||||||||||
| 63 | # run( a, b ) => b | ||||||||||||||||||||
| 64 | # run( run( expression[1] ), expression[0] ) | ||||||||||||||||||||
| 65 | # run( run( expression[2] ), expression[1] ), expression[0] ) | ||||||||||||||||||||
| 66 | # | ||||||||||||||||||||
| 67 | # last({ (#2)() }/2 << [ sequence of expressions ]) | ||||||||||||||||||||
| 68 | # | ||||||||||||||||||||
| 69 | # applying <last> to <foldl> applied to a list of expressions | ||||||||||||||||||||
| 70 | # with each expression being a closure over what's declared in this scope | ||||||||||||||||||||
| 71 | # and parent scopes | ||||||||||||||||||||
| 72 | # | ||||||||||||||||||||
| 73 | 2 | my @expressions = @{$self->[1]}; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 2 | ▶ | return $bnode unless @expressions; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 1 | ▶ | if(@expressions == 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 1 | return $expressions[0] -> to_rdf($child_model); | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | my $expression_list = $child_model -> model -> add_list( | ||||||||||||||||||||
| 82 | 0 | map { $_ -> to_rdf($child_model) } @expressions | |||||||||||||||||||
| 83 | ); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | $child_model -> apply( | |||||||||||||||||||
| 86 | $child_model -> meta_uri('loc:execute-list'), | ||||||||||||||||||||
| 87 | [ $expression_list ] | ||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | return $bnode; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | sub to_string { | ||||||||||||||||||||
| 94 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
to_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 95 | return join( "; ", | ||||||||||||||||||||
| 96 | 0 | ( map { 'uses "' . $_ . '"' } @{ $self->[4] } ), | |||||||||||||||||||
| 97 | 0 | ( map { "ns:$_ := \"" . $self->[3]->{$_} . "\"" } keys %{ $self->[3] } ), | |||||||||||||||||||
| 98 | 0 | map { $_->to_string } @{ $self->[0] }, | |||||||||||||||||||
| 99 | 0 | @{ $self->[1] } ); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | sub simplify { | ||||||||||||||||||||
| 103 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
simplify: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | return $self->new( map { $_->simplify } @{ $self->[0] }, @{ $self->[1] } ); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | sub check_for_common_mistakes { | ||||||||||||||||||||
| 109 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
check_for_common_mistakes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | my @warnings; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | # if(any { $_ -> isa('Dallycot::AST::Equality') } @{$self}[1][0..-2]) { | ||||||||||||||||||||
| 114 | # push @warnings, 'Did you mean to assign instead of test for equality?'; | ||||||||||||||||||||
| 115 | # } | ||||||||||||||||||||
| 116 | # if(any { !$_ -> isa('Dallycot::AST::Equality') && $_ -> isa('Dallycot::AST::ComparisonBase') } @{$self}[1][0..-2]) { | ||||||||||||||||||||
| 117 | # push @warnings, 'Result of comparison is not used.'; | ||||||||||||||||||||
| 118 | # } | ||||||||||||||||||||
| 119 | # push @warnings, map { $_ -> check_for_common_mistakes } @$self; | ||||||||||||||||||||
| 120 | 0 | return @warnings; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | sub execute { | ||||||||||||||||||||
| 124 | 82 | ▶ | my ( $self, $engine ) = @_; | ||||||||||||||||||
|
Subroutine
execute: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 82 | my $child_scope = $engine->with_child_scope(); | |||||||||||||||||||
| 127 | 82 | ▶ | my $var_scope = $engine->has_parent ? $child_scope : $engine; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 82 | foreach my $ident ( @{ $self->[2] } ) { | |||||||||||||||||||
| 130 | 92 | $var_scope->add_assignment($ident); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # wait for namespaces to load | ||||||||||||||||||||
| 134 | 82 | Dallycot::Registry->instance->register_used_namespaces( @{$self->[4]} )->then(sub { | |||||||||||||||||||
| 135 | 82 | ▶ | $var_scope->append_namespace_search_path( @{ $self->[4] } ); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 82 | for my $ns ( keys %{ $self->[3] } ) { | |||||||||||||||||||
| 138 | 0 | $var_scope->add_namespace( $ns, $self->[3]->{$ns} ); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 82 | my $assignments = $var_scope->collect( @{ $self->[0] } ); | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 82 | ▶ | if(@{$self->[1]}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | 80 | ▶ | $assignments->done(sub{}); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 145 | 80 | return $var_scope->execute( @{ $self->[1] } ); | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||
| 148 | return $assignments->then(sub { | ||||||||||||||||||||
| 149 | 2 | ▶ | my($last) = pop @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 150 | 2 | ▶ | if($last) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 2 | return $last; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | else { | ||||||||||||||||||||
| 154 | 0 | return $engine -> UNDEFINED; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | 2 | }); | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | 82 | }); | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | sub identifiers { | ||||||||||||||||||||
| 162 | 13 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
identifiers: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 13 | my @identifiers = map { $_->identifiers } $self->child_nodes; | |||||||||||||||||||
| 165 | 13 | my %assignments = map { $_ => 1 } @{ $self->[2] }; | |||||||||||||||||||
| 166 | 13 | return grep { !$assignments{$_} } @identifiers; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | sub child_nodes { | ||||||||||||||||||||
| 170 | 26 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
child_nodes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 26 | return ( @{ $self->[0] }, @{ $self->[1] } ); | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 29 | 1; | |||||||||||||||||||