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   9079 use strict;
  22         66  
4 22     22   90 use warnings;
  22         38  
  22         373  
5 22     22   97 #use Log::Any '$log';
  22         49  
  22         596  
6              
7             use Mo qw(build default);
8 22     22   107  
  22         52  
  22         89  
9             extends 'Data::Sah::Compiler::TH';
10              
11             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12             our $DATE = '2022-10-19'; # DATE
13             our $DIST = 'Data-Sah'; # DIST
14             our $VERSION = '0.914'; # 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 69 my ($self, $cd) = @_;
31 27         83 $self->compiler->_ignore_clause_and_attrs($cd);
32             }
33              
34             my ($self, $cd) = @_;
35 27     27 0 60 $self->compiler->_ignore_clause($cd);
36 27         75 }
37              
38             my ($self, $cd) = @_;
39             $self->compiler->_ignore_clause($cd);
40 27     27 0 61 }
41 27         82  
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 73 my ($self, $cd) = @_;
51 27         73 $self->compiler->_ignore_clause($cd);
52             }
53              
54             my ($self, $cd) = @_;
55 27     27 0 64 $self->compiler->_ignore_clause($cd);
56 27         73 }
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 63 my ($self, $cd, $expr) = @_;
61 27         95 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 1082 }
78 530         954  
79             my ($self, $cd) = @_;
80             my $c = $self->compiler;
81 530   66     2118 #$log->errorf("TMP: restore_data_term");
82              
83 530         973 my $tdt = $cd->{args}{tmp_data_term};
84 530         866 if ($cd->{_save_data_term}) {
85 530         1377 $cd->{data_term} = delete($cd->{_save_data_term});
86 530 50       1057 local $cd->{_debug_ccl_note} = 'restore original data term';
87 530         1823 $c->add_ccl($cd, "(".$c->expr_pop($tdt). ", ".$c->true.")",
88 530         989 {err_msg => ''});
89 530         834 }
90             }
91 530         1089  
92 530         1260 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 1024 my $jccl;
98 526         1126 {
99             local $cd->{ccls} = [];
100             for my $i (0..@$cv-1) {
101 526         1746 local $cd->{spath} = [@{ $cd->{spath} }, $i];
102 526 50       1454 my $sch = $cv->[$i];
103 526         958 my %iargs = %{$cd->{args}};
104 526         1142 $iargs{outer_cd} = $cd;
105 526         1687 $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         70 );
113 27         113 $c->add_ccl($cd, join("", @code));
114 27         47 }
115             if ($which eq 'all') {
116 27         33 $jccl = $c->join_ccls(
117             $cd, $cd->{ccls}, {err_msg=>''});
118 27         34 } else {
  27         55  
119 27         73 $jccl = $c->join_ccls(
120 54         71 $cd, $cd->{ccls}, {err_msg=>'', op=>'or'});
  54         144  
121 54         93 }
122 54         68 }
  54         491  
123 54         147 $c->add_ccl($cd, $jccl);
124 54         77 }
125 54         100  
126 54         93 my ($self, $cd) = @_;
127 54         75 my $c = $self->compiler;
128 54         247 my $cv = $cd->{cl_value};
129              
130             my ($cond, $then, $else) = @$cv;
131 54         247  
132 54         188 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       68 }
135              
136 12         42 # COND
137             my $comp_cond = $c->expr($cd, $cond);
138              
139 15         61 # THEN
140             my $comp_then;
141             {
142 27         97 local $cd->{ccls} = [];
143             local $cd->{spath} = [@{ $cd->{spath} }, 'if'];
144             my $sch = $then;
145             my %iargs = %{$cd->{args}};
146 13     13 0 25 $iargs{outer_cd} = $cd;
147 13         30 $iargs{schema} = $sch;
148 13         52 $iargs{schema_is_normalized} = 0;
149             $iargs{cache} = $cd->{args}{cache};
150 13         25 $iargs{indent_level}++;
151             my $icd = $c->compile(%iargs);
152 13 50 33     67 my @code = (
      33        
153 0         0 $icd->{result},
154             );
155             $comp_then = join("", @code);
156             }
157 13         35  
158             $c->add_ccl(
159             $cd,
160 13         156904 $c->expr_ternary($comp_cond, $comp_then, $c->true, {err_msg=>''}),
161             );
162 13         25 }
  13         34  
163 13         19  
  13         42  
164 13         20 1;
165 13         21 # ABSTRACT: Base class for programming-language emiting compiler's type handlers
  13         190  
166 13         39  
167 13         24  
168 13         17 =pod
169 13         28  
170 13         22 =encoding UTF-8
171 13         74  
172             =head1 NAME
173              
174 13         60 Data::Sah::Compiler::Prog::TH - Base class for programming-language emiting compiler's type handlers
175 13         177  
176             =head1 VERSION
177              
178             This document describes version 0.914 of Data::Sah::Compiler::Prog::TH (from Perl distribution Data-Sah), released on 2022-10-19.
179 13         31  
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