File Coverage

blib/lib/Data/Sah/Compiler/Prog/TH.pm
Criterion Covered Total %
statement 90 97 92.7
branch 5 8 62.5
condition 4 9 44.4
subroutine 19 23 82.6
pod 0 19 0.0
total 118 156 75.6


line stmt bran cond sub pod time code
1              
2             use 5.010;
3 22     22   10233 use strict;
  22         66  
4 22     22   96 use warnings;
  22         36  
  22         369  
5 22     22   97 #use Log::Any '$log';
  22         38  
  22         645  
6              
7             use Mo qw(build default);
8 22     22   108  
  22         48  
  22         96  
9             extends 'Data::Sah::Compiler::TH';
10              
11             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12             our $DATE = '2022-09-30'; # DATE
13             our $DIST = 'Data-Sah'; # DIST
14             our $VERSION = '0.913'; # VERSION
15              
16             # handled in compiler's before_all_clauses()
17              
18              
19       90 0   # handled in compiler's after_all_clauses()
20       54 0    
21       498 0    
22       54 0   my ($self, $cd) = @_;
23       6 0   $self->compiler->_ignore_clause_and_attrs($cd);
24             }
25              
26             my ($self, $cd) = @_;
27       0 0   $self->compiler->_ignore_clause_and_attrs($cd);
28             }
29              
30 27     27 0 57 my ($self, $cd) = @_;
31 27         85 $self->compiler->_ignore_clause_and_attrs($cd);
32             }
33              
34             my ($self, $cd) = @_;
35 27     27 0 65 $self->compiler->_ignore_clause($cd);
36 27         91 }
37              
38             my ($self, $cd) = @_;
39             $self->compiler->_ignore_clause($cd);
40 27     27 0 59 }
41 27         98  
42             my ($self, $cd) = @_;
43             $self->compiler->_ignore_clause($cd);
44             }
45 0     0 0 0  
46 0         0 my ($self, $cd) = @_;
47             $self->compiler->_ignore_clause($cd);
48             }
49              
50 27     27 0 68 my ($self, $cd) = @_;
51 27         83 $self->compiler->_ignore_clause($cd);
52             }
53              
54             my ($self, $cd) = @_;
55 27     27 0 60 $self->compiler->_ignore_clause($cd);
56 27         84 }
57              
58             # temporarily use temporary variable for referring to data (e.g. when converting
59             # non-number to number for checking in clauses, or prefiltering)
60 27     27 0 62 my ($self, $cd, $expr) = @_;
61 27         87 my $c = $self->compiler;
62             #$log->errorf("TMP: set_tmp_data_term");
63              
64             $expr //= $cd->{data_term};
65 0     0 0 0  
66 0         0 my $tdn = $cd->{args}{tmp_data_name};
67             my $tdt = $cd->{args}{tmp_data_term};
68             my $t = $c->expr_array_subscript($tdt, $cd->{_subdata_level});
69             unless ($cd->{_save_data_term}) {
70 0     0 0 0 $c->add_var($cd, $tdn, []);
71 0         0 $cd->{_save_data_term} = $cd->{data_term};
72             $cd->{data_term} = $t;
73             }
74             local $cd->{_debug_ccl_note} = 'set temporary data term';
75             $c->add_ccl($cd, "(".$c->expr_set($t, $expr). ", ".$c->true.")",
76             {err_msg => ''});
77 530     530 0 968 }
78 530         1010  
79             my ($self, $cd) = @_;
80             my $c = $self->compiler;
81 530   66     2033 #$log->errorf("TMP: restore_data_term");
82              
83 530         967 my $tdt = $cd->{args}{tmp_data_term};
84 530         813 if ($cd->{_save_data_term}) {
85 530         1680 $cd->{data_term} = delete($cd->{_save_data_term});
86 530 50       1111 local $cd->{_debug_ccl_note} = 'restore original data term';
87 530         1498 $c->add_ccl($cd, "(".$c->expr_pop($tdt). ", ".$c->true.")",
88 530         1038 {err_msg => ''});
89 530         925 }
90             }
91 530         1035  
92 530         1296 my ($self, $which, $cd) = @_;
93             my $c = $self->compiler;
94             my $cv = $cd->{cl_value};
95             my $dt = $cd->{data_term};
96              
97 526     526 0 965 my $jccl;
98 526         1031 {
99             local $cd->{ccls} = [];
100             for my $i (0..@$cv-1) {
101 526         1735 local $cd->{spath} = [@{ $cd->{spath} }, $i];
102 526 50       1083 my $sch = $cv->[$i];
103 526         974 my %iargs = %{$cd->{args}};
104 526         1274 $iargs{outer_cd} = $cd;
105 526         1511 $iargs{schema} = $sch;
106             $iargs{schema_is_normalized} = 0;
107             $iargs{cache} = $cd->{args}{cache};
108             $iargs{indent_level}++;
109             my $icd = $c->compile(%iargs);
110             my @code = (
111 27     27 0 52 $icd->{result},
112 27         72 );
113 27         109 $c->add_ccl($cd, join("", @code));
114 27         40 }
115             if ($which eq 'all') {
116 27         32 $jccl = $c->join_ccls(
117             $cd, $cd->{ccls}, {err_msg=>''});
118 27         32 } else {
  27         48  
119 27         62 $jccl = $c->join_ccls(
120 54         77 $cd, $cd->{ccls}, {err_msg=>'', op=>'or'});
  54         144  
121 54         88 }
122 54         73 }
  54         522  
123 54         132 $c->add_ccl($cd, $jccl);
124 54         79 }
125 54         73  
126 54         73 my ($self, $cd) = @_;
127 54         69 my $c = $self->compiler;
128 54         288 my $cv = $cd->{cl_value};
129              
130             my ($cond, $then, $else) = @$cv;
131 54         218  
132 54         176 unless (!ref($cond) && ref($then) eq 'ARRAY' && !$else) {
133             $c->_die($cd, "Sorry, for 'if' clause, I currently can only handle COND=str (expr), THEN=array (schema), and no ELSE");
134 27 100       62 }
135              
136 12         39 # COND
137             my $comp_cond = $c->expr($cd, $cond);
138              
139 15         73 # THEN
140             my $comp_then;
141             {
142 27         93 local $cd->{ccls} = [];
143             local $cd->{spath} = [@{ $cd->{spath} }, 'if'];
144             my $sch = $then;
145             my %iargs = %{$cd->{args}};
146 13     13 0 26 $iargs{outer_cd} = $cd;
147 13         35 $iargs{schema} = $sch;
148 13         52 $iargs{schema_is_normalized} = 0;
149             $iargs{cache} = $cd->{args}{cache};
150 13         23 $iargs{indent_level}++;
151             my $icd = $c->compile(%iargs);
152 13 50 33     61 my @code = (
      33        
153 0         0 $icd->{result},
154             );
155             $comp_then = join("", @code);
156             }
157 13         32  
158             $c->add_ccl(
159             $cd,
160 13         160082 $c->expr_ternary($comp_cond, $comp_then, $c->true, {err_msg=>''}),
161             );
162 13         23 }
  13         37  
163 13         19  
  13         38  
164 13         22 1;
165 13         16 # ABSTRACT: Base class for programming-language emiting compiler's type handlers
  13         158  
166 13         37  
167 13         22  
168 13         35 =pod
169 13         29  
170 13         17 =encoding UTF-8
171 13         81  
172             =head1 NAME
173              
174 13         63 Data::Sah::Compiler::Prog::TH - Base class for programming-language emiting compiler's type handlers
175 13         183  
176             =head1 VERSION
177              
178             This document describes version 0.913 of Data::Sah::Compiler::Prog::TH (from Perl distribution Data-Sah), released on 2022-09-30.
179 13         32  
180             =for Pod::Coverage ^(compiler|clause_.+|handle_.+|gen_.+|set_tmp_data_term|restore_data_term)$
181              
182             =head1 HOMEPAGE
183              
184             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah>.
185              
186             =head1 SOURCE
187              
188             Source repository is at L<https://github.com/perlancar/perl-Data-Sah>.
189              
190             =head1 AUTHOR
191              
192             perlancar <perlancar@cpan.org>
193              
194             =head1 CONTRIBUTING
195              
196              
197             To contribute, you can send patches by email/via RT, or send pull requests on
198             GitHub.
199              
200             Most of the time, you don't need to build the distribution yourself. You can
201             simply modify the code, then test via:
202              
203             % prove -l
204              
205             If you want to build the distribution (e.g. to try to install it locally on your
206             system), you can install L<Dist::Zilla>,
207             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
208             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
209             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
210             that are considered a bug and can be reported to me.
211              
212             =head1 COPYRIGHT AND LICENSE
213              
214             This software is copyright (c) 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012 by perlancar <perlancar@cpan.org>.
215              
216             This is free software; you can redistribute it and/or modify it under
217             the same terms as the Perl 5 programming language system itself.
218              
219             =head1 BUGS
220              
221             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah>
222              
223             When submitting a bug or request, please include a test-file or a
224             patch to an existing test-file that illustrates the bug or desired
225             feature.
226              
227             =cut