lib/FP/AST/Perl.pm

filter: Statementstmt 96.3% 235 / 244 Branchbran 33.3% 2 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.4% 76 / 84 Podpod 0.0% 0 / 2 totaltotal 93.1% 313 / 336 CC 4 SCAR 2.3
CC
4
Cov
95%
is_lexvar_string
6.0
string
3.2
type_name
2.0
CRAP 1.3
« lib/FP/Interfaces.pm ↑ Summary lib/FP/Abstract/Interface.pm »
1 #
2 # Copyright (c) 2019-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::AST::Perl -- abstract syntax tree for representing Perl code
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::AST::Perl ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::List; use FP::Equal ":all"; use FP::Ops qw(regex_substitute);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 is Get(ScalarVar "foo")->string, '$foo';
20 is Get(ArrayVar "foo")->string, '@foo';
21 is Get(HashVar "foo::bar")->string, '%foo::bar';
22 is Ref(HashVar "foo::bar")->string, '\%foo::bar';
23  
24 my $codefoo = CodeVar("foo");
25 my $arrayfoo = ArrayVar("foo");
26 my $lexfoo = ScalarVar("foo");
27 is App(Get($codefoo), list())->string,
28 '&foo()';
29 is AppP(Get($codefoo), list())->string,
30 'foo()';
31 is AppP(Get($lexfoo), list(Ref($codefoo)))->string,
32 '$foo->(\&foo)';
33 is eval { App(Get(HashVar "foo"), list())->string } ||
34 regex_substitute(sub{s/ at .*//s}, $@),
35 'HASH var can\'t be called';
36 is AppP(Get($lexfoo), list(Get($lexfoo), Get($arrayfoo), Ref($arrayfoo)))->string,
37 '$foo->($foo, @foo, \@foo)';
38 is AppP(Get($codefoo), list(Get(ScalarVar 'foo'), Literal(Number 123)))->string,
39 'foo($foo, 123)';
40 is AppP(Get($codefoo), list(Get($lexfoo), Literal(String 123)))->string,
41 'foo($foo, \'123\')';
42  
43 # Semicolons are like a compile time (AST level) operator:
44 is Semicolon(Get(ScalarVar "a"), Get(ScalarVar "b"))->string, '$a; $b';
45 # to end with a semicolon you could use:
46 is Semicolon(Get(ScalarVar "a"), Noop)->string, '$a; ';
47  
48 # The n-ary `semicolons` function builds a Semicolon chain for
49 # you (right-associated):
50 is_equal semicolons(), Noop;
51 is_equal semicolons("x"), "x";
52 # ^ no `Semicolon` instantiation thus no type failure because
53 # of the string
54 my $sems = semicolons(map {Get ScalarVar $_} qw(a b c));
55 is_equal $sems,
56 Semicolon(Get(ScalarVar('a')),
57 Semicolon(Get(ScalarVar('b')),
58 Get(ScalarVar('c'))));
59 is $sems->string, '$a; $b; $c';
60  
61 is commas(map {Get ScalarVar $_} qw(a b c))->string,
62 '$a, $b, $c';
63  
64 is Let(list(ScalarVar("foo"), ScalarVar("bar")),
65 Get(ArrayVar "baz"),
66 semicolons(
67 AppP(Get(CodeVar "print"),
68 list Literal String "Hello"),
69 Get(ScalarVar("bar"))))->string,
70 'my ($foo, $bar) = @baz; print(\'Hello\'); $bar';
71 # Yes, how should print, map etc. be handled?
72  
73  
74 =head1 DESCRIPTION
75  
76 This is not a parser, and hence should be outside the scope of the
77 "can only parse Perl at runtime" issue.
78  
79 The longer term aim is to support all of Perl, and to support
80 conversion to and maybe from an op tree.
81  
82 =head1 SEE ALSO
83  
84 Implements: L<FP::Abstract::Pure>, L<FP::Abstract::Show>,
85 L<FP::Abstract::Equal>.
86  
87 =head1 NOTE
88  
89 This is alpha software! Read the status section in the package README
90 or on the L<website|http://functional-perl.org/>.
91  
92 =cut
93  
94 package FP::AST::Perl;
95 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 2 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 2 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 2 our @EXPORT = qw();
102 2 my @classes = qw(
103 ScalarVar CodeVar HashVar ArrayVar Glob
104 App AppP Get Ref
105 Number String
106 Literal
107 Semicolon Comma Noop Let);
108 2 our @EXPORT_OK = (
109 @classes, qw(
110 is_packvar_type
111 is_var
112 is_expr is_nonnoop_expr is_noop
113 semicolons commas)
114 );
115  
116 2 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
117  
118 2 ▶use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 #use Chj::TEST;
121 2 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 2 ▶use FP::Combinators2 qw(right_associate_);
Subroutine BEGIN: called CC=1 SCAR=0.0
123 2 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125 # import the constructors of the classes defined below
126 2 for my $name (@classes) {
127 32 "FP::AST::Perl::${name}::constructors"->import()
128 }
129  
130 2 package FP::AST::_::Perl {
131 2 ▶ use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 2 ▶ use FP::Struct [] => "FP::Abstract::Pure",
Subroutine BEGIN: called CC=1 SCAR=0.0
134 2 "FP::Struct::Equal", "FP::Struct::Show";
135  
136 2 ▶ sub string_proto($self) {
Subroutine string_proto: called CC=1 SCAR=0.0
137 2 __ "stringification when used as the callee in AppP";
138 2 $self->string
139 }
140  
141 2 _END_
142 }
143  
144 # Variables
145  
146 2 package FP::AST::Perl::Var {
147 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 2 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
150 2 [\&is_string, 'name'],
151  
152 # ^ XX is_package_name ? But isn't everything allowed? But
153 # then todo proper printing.
154 #FUTURE: [\&is_bool, 'is_lexical'] ?
155 2 ] => "FP::AST::_::Perl";
156  
157 27 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
158 27 $self->sigil . $self->name
159 }
160 1 ▶ sub callderef($self) { die $self->type_name . " var can't be called" }
Subroutine callderef: called CC=1 SCAR=0.0
161  
162 2 _END_
163 }
164  
165 2 *is_var = instance_of
166 "FP::AST::Perl::Var"; ## XX Are those not all auto generated MAN ???
167  
168 # XX move to FP::Predicates? Or FP::Parser::Perl ?
169 0 ▶sub is_lexvar_string {
Subroutine is_lexvar_string: not called CC=2 SCAR=17.9
Pod undocumented
170 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
171 0 my ($str) = @_;
172 0 $str =~ /^\w+\z/
173 }
174  
175 2 package FP::AST::Perl::ScalarVar {
176 2 ▶ use FP::Struct [] => "FP::AST::Perl::Var";
Subroutine BEGIN: called CC=1 SCAR=0.0
177 0 ▶ sub type_name($self) {"SCALAR"}
Subroutine type_name: not called CC=1 SCAR=6.9
178 18 ▶ sub sigil($self) {'$'}
Subroutine sigil: called CC=1 SCAR=0.0
179 2 ▶ sub callderef($self) {'->'}
Subroutine callderef: called CC=1 SCAR=0.0
180 2 _END_
181 }
182  
183 2 package FP::AST::Perl::CodeVar {
184 2 ▶ use FP::Struct [] => "FP::AST::Perl::Var";
Subroutine BEGIN: called CC=1 SCAR=0.0
185 0 ▶ sub type_name($self) {"CODE"}
Subroutine type_name: not called CC=1 SCAR=6.9
186 2 ▶ sub sigil($self) {'&'}
Subroutine sigil: called CC=1 SCAR=0.0
187 5 ▶ sub callderef($self) {''}
Subroutine callderef: called CC=1 SCAR=0.0
188  
189 4 ▶ sub string_proto($self) { $self->name }
Subroutine string_proto: called CC=1 SCAR=0.0
190 2 _END_
191 }
192  
193 2 package FP::AST::Perl::HashVar {
194 2 ▶ use FP::Struct [] => "FP::AST::Perl::Var";
Subroutine BEGIN: called CC=1 SCAR=0.0
195 1 ▶ sub type_name($self) {"HASH"}
Subroutine type_name: called CC=1 SCAR=0.0
196 3 ▶ sub sigil($self) {'%'}
Subroutine sigil: called CC=1 SCAR=0.0
197 2 _END_
198 }
199  
200 2 package FP::AST::Perl::ArrayVar {
201 2 ▶ use FP::Struct [] => "FP::AST::Perl::Var";
Subroutine BEGIN: called CC=1 SCAR=0.0
202 0 ▶ sub type_name($self) {"ARRAY"}
Subroutine type_name: not called CC=1 SCAR=6.9
203 4 ▶ sub sigil($self) {'@'}
Subroutine sigil: called CC=1 SCAR=0.0
204 2 _END_
205 }
206  
207 2 package FP::AST::Perl::Glob {
208 2 ▶ use FP::Struct [] => "FP::AST::Perl::Var"; # XX *?*
Subroutine BEGIN: called CC=1 SCAR=0.0
209 0 ▶ sub type_name($self) {"GLOB"}
Subroutine type_name: not called CC=1 SCAR=6.9
210 0 ▶ sub sigil($self) {'*'}
Subroutine sigil: not called CC=1 SCAR=6.9
211 2 _END_
212 }
213  
214 # Expressions
215  
216 2 package FP::AST::Perl::Expr {
217  
218 2 ▶ use FP::Struct [] => "FP::AST::_::Perl";
Subroutine BEGIN: called CC=1 SCAR=0.0
219  
220 2 _END_
221 }
222  
223 2 *is_expr = instance_of "FP::AST::Perl::Expr";
224 2 *is_nonnoop_expr = both \&is_expr, complement \&is_noop;
225  
226 # Do we need to distinguish context (list vs. scalar [vs. void]),
227 # really? No, since the *dynamic* context determines this!
228  
229 2 package FP::AST::Perl::Get {
230 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
231  
232 2 ▶ use FP::Struct [[\&FP::AST::Perl::is_var, 'var'],] => "FP::AST::Perl::Expr";
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 # XX problem is, for CODE vars, Get is only valid in App proc
235 # context! Otherwise, Ref must be used! How to type check?
236 # Different methods than just string I guess!!
237  
238 # XX BTW eliminate those delegates? Just call manually? OR: use
239 # Var directly in App proc ??
240  
241 20 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
242 20 $self->var->string
243 }
244  
245 6 ▶ sub string_proto($self) {
Subroutine string_proto: called CC=1 SCAR=0.0
246 6 $self->var->string_proto
247 }
248  
249 8 ▶ sub callderef($self) {
Subroutine callderef: called CC=1 SCAR=0.0
250 8 $self->var->callderef
251 }
252  
253 2 _END_
254 }
255  
256 # Take referenceS, 'instead of just Get-ing the valueS':
257  
258 2 package FP::AST::Perl::Ref {
259 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
260  
261 2 ▶ use FP::Struct [[\&FP::AST::Perl::is_var, 'var'],] => "FP::AST::Perl::Expr";
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 3 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
264 3 "\\" . $self->var->string
265 }
266  
267 # sub string_proto ($self) {
268 # # $self->var->string_proto
269 # #die "there is no prototype call with a ref expression"
270 # # ok? or just omit and let it fall back to ->string ? but
271 # # i DO think this call should never happen, right?
272 # }
273  
274 0 ▶ sub callderef($self) {
Subroutine callderef: not called CC=1 SCAR=6.9
275  
276 # taking a ref means the call will have to deref it, always
277 0 '->'
278 }
279  
280 2 _END_
281 }
282  
283 # *is_ref = instance_of "FP::AST::Perl::Ref";
284  
285 2 package FP::AST::Perl::App {
286 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
287 2 ▶ use FP::List ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
288 2 ▶ use FP::Ops ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
289  
290 2 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
291 2 [\&FP::AST::Perl::is_expr, 'proc'],
292 [list_of(\&FP::AST::Perl::is_expr), 'argexprs'],
293  
294 # ^ yes, proc is also an expr, but only yielding one (usable)
295 # value, as opposed to argexprs which may yield more used
296 # values than there are exprs (at least here; not
297 # (necessarily) in AppP).
298 2 ] => "FP::AST::Perl::Expr";
299  
300 8 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
301 8 $self->proc_string
302 . $self->proc->callderef . "("
303 . $self->argexprs->map(the_method "string")->strings_join(", ")
304 .
305  
306 # ^ XX are parens needed around arguments?
307 ")"
308 }
309  
310 2 ▶ sub proc_string($self) {
Subroutine proc_string: called CC=1 SCAR=0.0
311 2 $self->proc->string
312 }
313 2 _END_
314 }
315  
316 2 package FP::AST::Perl::AppP {
317  
318 # App with exposure to prototypes
319 2 ▶ use FP::Struct [] => "FP::AST::Perl::App";
Subroutine BEGIN: called CC=1 SCAR=0.0
320  
321 6 ▶ sub proc_string($self) {
Subroutine proc_string: called CC=1 SCAR=0.0
322 6 $self->proc->string_proto
323 }
324  
325 2 _END_
326 }
327  
328 # Literals
329  
330 2 package FP::AST::Perl::Value {
331 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
332  
333 2 ▶ use FP::Struct [] => "FP::AST::_::Perl";
Subroutine BEGIN: called CC=1 SCAR=0.0
334  
335 # missing string method XX interface
336  
337 2 _END_
338 }
339  
340 2 *is_value = instance_of "FP::AST::Perl::Value";
341  
342 2 package FP::AST::Perl::Number {
343 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
344 2 ▶ use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
345  
346 2 ▶ use FP::Struct [[\&looks_like_number, 'perlvalue'],] =>
Subroutine BEGIN: called CC=1 SCAR=0.0
347 2 "FP::AST::Perl::Value";
348  
349 1 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
350 1 $self->perlvalue # no quoting
351 }
352  
353 2 _END_
354 }
355  
356 2 package FP::AST::Perl::String {
357 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
358 2 ▶ use Chj::singlequote;
Subroutine BEGIN: called CC=1 SCAR=0.0
359  
360 2 ▶ use FP::Struct ['perlvalue',] => "FP::AST::Perl::Value";
Subroutine BEGIN: called CC=1 SCAR=0.0
361  
362 2 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
363 2 singlequote($self->perlvalue)
364 }
365  
366 2 _END_
367 }
368  
369 2 package FP::AST::Perl::Literal {
370 2 ▶ use FP::Predicates ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
371 2 ▶ use FP::List ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
372 2 ▶ use FP::Ops ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
373  
374 2 ▶ use FP::Struct [[\&FP::AST::Perl::is_value, 'value'],] =>
Subroutine BEGIN: called CC=1 SCAR=0.0
375 2 "FP::AST::Perl::Expr";
376  
377 3 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
378 3 $self->value->string
379 }
380  
381 2 _END_
382 }
383  
384 2 package FP::AST::Perl::Semicolon {
385 2 ▶ use FP::Struct [[\&FP::AST::Perl::is_expr, 'a'],
Subroutine BEGIN: called CC=1 SCAR=0.0
386 2 [\&FP::AST::Perl::is_expr, 'b'],] => "FP::AST::Perl::Expr";
387  
388 5 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
389 5 $self->a->string . "; " . $self->b->string
390 }
391 2 _END_
392 }
393  
394 # mostly-copy-paste of above
395 2 package FP::AST::Perl::Comma {
396 2 ▶ use FP::Struct [[\&FP::AST::Perl::is_expr, 'a'],
Subroutine BEGIN: called CC=1 SCAR=0.0
397 2 [\&FP::AST::Perl::is_expr, 'b'],] => "FP::AST::Perl::Expr";
398  
399 2 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
400 2 $self->a->string . ", " . $self->b->string
401 }
402 2 _END_
403 }
404  
405 2 package FP::AST::Perl::Noop {
406 2 ▶ use FP::Struct [] => "FP::AST::Perl::Expr";
Subroutine BEGIN: called CC=1 SCAR=0.0
407  
408 1 ▶ sub string($self) {
Subroutine string: called CC=1 SCAR=0.0
409 1 ""
410 }
411 2 _END_
412 }
413  
414 2 *is_noop = instance_of "FP::AST::Perl::Noop";
415  
416 2 *semicolons = right_associate_ \&Semicolon, Noop();
417 2 *commas = right_associate_ \&Comma, Noop();
418  
419 2 package FP::AST::Perl::Let {
420 2 ▶ use FP::Predicates;
Subroutine BEGIN: called CC=1 SCAR=0.0
421 2 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
422 2 ▶ use FP::Ops ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
423  
424 2 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
425 2 [list_of(\&FP::AST::Perl::is_var), 'vars'],
426 [\&FP::AST::Perl::is_nonnoop_expr, 'expr'],
427 [\&FP::AST::Perl::is_expr, 'body'],
428 2 ] => "FP::AST::Perl::Expr";
429  
430 1 ▶ sub string($self) {
Subroutine string: called CC=3 SCAR=11.7
431 1 my $vars = $self->vars;
432 1 my $multiple = $vars->length > 1;
433 1 ▶ "my "
Branch: 2 branches
BranchTrueFalseTotal
$multiple ? : 1 0 1
$multiple ? : 1 0 1
434 . ($multiple ? "(" : "")
435 . $vars->map(the_method "string")->strings_join(", ")
436 . ($multiple ? ")" : "") . " = "
437 . $self->expr->string
438 . "; " # XX like Semicolon! But not the same *?* Q
439 . $self->body->string
440 }
441 2 _END_
442 }
443  
444 sub String;
445 sub Literal;
446  
447 0 ▶sub t {
Subroutine t: not called CC=1 SCAR=0.0
Pod undocumented
448 }
449  
450 2 1
« lib/FP/Interfaces.pm ↑ Summary lib/FP/Abstract/Interface.pm »