1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
package Eval::TypeTiny::CodeAccumulator;
use 5.008001;
use strict;
use warnings;
BEGIN {
if ( $] < 5.010 ) { require Devel::TypeTiny::Perl58Compat }
}
BEGIN {
$Eval::TypeTiny::CodeAccumulator::AUTHORITY = 'cpan:TOBYINK';
$Eval::TypeTiny::CodeAccumulator::VERSION = '2.002001';
}
$Eval::TypeTiny::CodeAccumulator::VERSION =~ tr/_//d;
sub new {
my $class = shift;
my %self = @_ == 1 ? %{$_[0]} : @_;
$self{env} ||= {};
$self{code} ||= [];
$self{placeholders} ||= {};
$self{indent} ||= '';
bless \%self, $class;
}
sub code { join( "\n", @{ $_[0]{code} } ) }
sub description { $_[0]{description} }
sub env { $_[0]{env} }
sub add_line {
my $self = shift;
my $indent = $self->{indent};
push @{ $self->{code} }, map { $indent . $_ } map { split /\n/ } @_;
$self;
}
sub increase_indent {
$_[0]{indent} .= "\t";
$_[0];
}
sub decrease_indent {
$_[0]{indent} =~ s/\t$//;
$_[0];
}
sub add_gap {
push @{ $_[0]{code} }, '';
}
sub add_placeholder {
my ( $self, $for ) = ( shift, @_ );
my $indent = $self->{indent} || '';
$self->{placeholders}{$for} = [
scalar( @{ $self->{code} } ),
$self->{indent},
];
push @{ $self->{code} }, "$indent# placeholder [ $for ]";
if ( defined wantarray ) {
return sub { $self->fill_placeholder( $for, @_ ) };
}
}
sub fill_placeholder {
my ( $self, $for, @lines ) = ( shift, @_ );
my ( $line_number, $indent ) = @{ delete $self->{placeholders}{$for} or die };
my @indented_lines = map { $indent . $_ } map { split /\n/ } @lines;
splice( @{ $self->{code} }, $line_number, 1, @indented_lines );
$self;
}
sub add_variable {
my ( $self, $suggested_name, $reference ) = ( shift, @_ );
my $actual_name = $suggested_name;
my $i = 1;
while ( exists $self->{env}{$actual_name} ) {
$actual_name = sprintf '%s_%d', $suggested_name, ++$i;
}
$self->{env}{$actual_name} = $reference;
$actual_name;
}
sub finalize {
my $self = shift;
for my $p ( values %{ $self->{placeholders} } ) {
splice( @{ $self->{code} }, $p->[0], 1 );
}
$self;
}
sub compile {
my ( $self, %opts ) = ( shift, @_ );
$self->{finalized}++ or $self->finalize();
require Eval::TypeTiny;
return Eval::TypeTiny::eval_closure(
description => $self->description,
%opts,
source => $self->code,
environment => $self->env,
);
}
1;
__END__
=pod
=encoding utf-8
=for stopwords pragmas coderefs
=head1 NAME
Eval::TypeTiny::CodeAccumulator - alternative API for Eval::TypeTiny
=head1 SYNOPSIS
my $make_adder = 'Eval::TypeTiny::CodeAccumulator'->new(
description => 'adder',
);
my $n = 40;
my $varname = $make_adder->add_variable( '$addend' => \$n );
$make_adder->add_line( 'sub {' );
$make_adder->increase_indent;
$make_adder->add_line( 'my $other_addend = shift;' );
$make_adder->add_gap;
$make_adder->add_line( 'return ' . $varname . ' + $other_addend;' );
$make_adder->decrease_indent;
$make_adder->add_line( '}' );
my $adder = $make_adder->compile;
say $adder->( 2 ); ## ==> 42
=head1 STATUS
This module is covered by the
L<Type-Tiny stability policy|Type::Tiny::Manual::Policies/"STABILITY">.
=head1 DESCRIPTION
=head2 Constructor
=over
=item C<< new( %attrs ) >>
The only currently supported attribute is C<description>.
=back
=head2 Methods
=over
=item C<< env() >>
Returns the current compilation environment, a hashref of variables to close
over.
=item C<< code() >>
Returns the source code so far.
=item C<< description() >>
Returns the same description given to the constructor, if any.
=item C<< add_line( @lines_of_code ) >>
Adds the next line of code.
=item C<< add_gap() >>
Adds a blank line of code.
=item C<< increase_indent() >>
Increases the indentation level for subsequent lines of code.
=item C<< decrease_indent() >>
Decreases the indentation level for subsequent lines of code.
=item C<< add_variable( $varname, $reference_to_value ) >>
Adds a variable to the compilation environment so that the coderef being
generated can close over it.
If a variable already exists in the environment with that name, will instead
add a variable with a different name and return that name. You should always
continue to refer to the variable with that returned name, just in case.
=item C<< add_placeholder( $placeholder_name ) >>
Adds a line of code which is just a comment, but remembers its line number.
=item C<< fill_placeholder( $placeholder_name, @lines_of_code ) >>
Goes back to a previously inserted placeholder and replaces it with code.
As an alternative, C<add_placeholder> returns a coderef, which you can call
like C<< $callback->( @lines_of_code ) >>.
=item C<< compile( %opts ) >>
Compiles the code and returns it as a coderef.
Options are passed on to C<< eval_closure >> from L<Eval::TypeTiny>,
but cannot include C<code> or C<environment>. C<< alias => 1 >>
is probably the option most likely to be useful, but in general
you won't need to provide any options.
=item C<< finalize() >>
This method is called by C<compile> just before compiling the code. All it
does is remove unfilled placeholder comments. It is not intended for end
users to call, but is documented as it may be a useful hook if you are
subclassing this class.
=back
=head1 BUGS
Please report any bugs to
L<https://github.com/tobyink/p5-type-tiny/issues>.
=head1 SEE ALSO
L<Eval::TypeTiny>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2022-2023 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|