File Coverage

blib/lib/Dallycot/AST/Assign.pm
Criterion Covered Total %
statement 18 52 34.6
branch 0 12 0.0
condition n/a
subroutine 7 13 53.8
pod 0 6 0.0
total 25 83 30.1


line stmt bran cond sub pod time code
1             package Dallycot::AST::Assign;
2             our $AUTHORITY = 'cpan:JSMITH';
3              
4             # ABSTRACT: Store result of expression in environment
5              
6 23     23   12111 use strict;
  23         36  
  23         719  
7 23     23   85 use warnings;
  23         32  
  23         491  
8              
9 23     23   84 use utf8;
  23         29  
  23         96  
10 23     23   504 use parent 'Dallycot::AST';
  23         39  
  23         98  
11              
12 23     23   1276 use Promises qw(deferred);
  23         48  
  23         126  
13              
14             sub to_string {
15 0     0 0 0 my ($self) = @_;
16              
17 0         0 return $self->[0] . " := " . $self->[1]->to_string;
18             }
19              
20 8     8 0 25 sub is_declarative { return 1 }
21              
22             sub identifier {
23 8     8 0 3155 my ($self) = @_;
24              
25 8         38 return $self->[0];
26             }
27              
28             sub simplify {
29 0     0 0   my ($self) = @_;
30              
31 0           return bless [ $self -> [0], $self -> [1] -> simplify ] => __PACKAGE__;
32             }
33              
34             sub to_rdf {
35 0     0 0   my($self, $model) = @_;
36              
37 0           my $bnode = $self->[1]->to_rdf($model);
38              
39 0           $model -> add_symbol($self->[0], $bnode);
40              
41 0           return $bnode;
42             }
43              
44             sub execute {
45 0     0 0   my ( $self, $engine ) = @_;
46              
47 0           my $d = deferred;
48              
49 0           my $registry = Dallycot::Registry->instance;
50              
51 0 0         if ( $registry->has_assignment( '', $self->[0] ) ) {
    0          
52 0           $d = $registry->get_assignment( '', $self->[0] );
53 0 0         if ( $d->is_resolved ) {
54 0           $d = deferred;
55 0           $d->reject('Core definitions may not be redefined.');
56 0           return $d->promise;
57             }
58             }
59             elsif ( $engine->has_assignment( $self->[0] ) ) {
60 0           $d = $engine->get_assignment( $self->[0] );
61 0 0         if ( $d->is_resolved ) {
62 0           $d = deferred;
63 0           $d->reject( 'Unable to redefine ' . $self->[0] );
64 0           return $d->promise;
65             }
66             }
67             else {
68 0           $d = $engine->add_assignment( $self->[0] );
69             }
70              
71             $engine->execute( $self->[1] )->done(
72             sub {
73 0     0     my ($result) = @_;
74 0           my $worked = eval {
75 0           $engine->add_assignment( $self->[0], $result );
76 0           1;
77             };
78 0 0         if ($@) {
    0          
79 0           $d->reject($@);
80             }
81             elsif ( !$worked ) {
82 0           $d->reject( "Unable to assign to " . $self->[0] );
83             }
84             else {
85 0           $d->resolve($result);
86             }
87             },
88             sub {
89 0     0     $d->reject(@_);
90             }
91 0           );
92              
93 0           return $d->promise;
94             }
95              
96             1;