Acme-AsciiArtinator
view release on metacpan or search on metacpan
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
our @EXPORT = qw(asciiartinate);
$| = 1;
my $DEBUG = 0;
#############################################################################
#
# run ASCII Artinization on a picture and a code string.
#
sub asciiartinate {
my %opts = @_;
if (@_ == 1 && ref $_[0] eq "HASH") {
%opts = @{$_[0]};
}
my ($PIC, $CODE, $OUTPUT);
if (defined $opts{"debug"} && $opts{"debug"}) {
$DEBUG = 1;
}
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
if ($ipad >= $max_tries) {
croak "The ASCII Artinator was unable to embed your code in the picture ",
"after $max_tries tries.\n";
}
}
#
# run a file containing Perl code for a Perl compilation check
#
sub compile_check {
my ($file) = @_;
print "\n";
print "- " x 20, "\n";
print "Compile check for $file:\n";
print "- " x 20, "\n";
print `$^X -cw "$file"`;
print "- " x 20, "\n";
return $?;
}
sub tweak_padding {
my ($filler, $tref, $cref) = @_;
# TODO: if there are many consecutive characters of padding
# in the code, we can improve its appearance by
# inserting some quoted text in void context.
}
#############################################################################
#
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
# //= is an operator in perl 5.10, I believe
# // is usually a regular expression, or a perl 5.10 operator
my %sigil = qw($ 1 @ 2 % 3 & 4 & 0);
#
# does the current string begin with an "operator keyword"?
# if so, return it
#
sub find_token_keyword {
my ($q) = @_;
foreach my $k (@token_keywords) {
if (substr($q,0,length($k)) eq $k) {
return $k;
}
}
return;
}
#
# find position of a scalar in an array.
#
sub STRPOS {
my ($word, @array) = @_;
my $pos = -1;
for (my $i=0; $i<@array; $i++) {
$pos = $i if $array[$i] =~ /$word/;
}
return $pos;
}
#
# what does the "/" token that we just encountered mean?
# this is a hard game to play.
# see http://www.perlmonks.org/index.pl?node_id=44722
#
sub regex_or_divide {
my ($tokenref, $contextref) = @_;
my @tokens = @$tokenref;
my @contexts = @$contextref;
# regex is expected following an operator,
# at the beginning of a statement
# divide is expected following a scalar,
# or any token that could complete an expression
my $c = $#contexts;
$c-- while $contexts[$c] eq "whitespace";
return "regex" if $contexts[$c] eq "operator";
return "regex" if $tokens[$c] eq ";" && $tokens[$c-1] ne "SIGIL";
return "divide";
}
sub tokenize_code {
my ($INPUT) = @_;
local $" = '';
my @INPUT = grep { /[^\n]/ } split //, $INPUT;
# tokens are:
# quotes strings
# numeric literals
# regular expression specifications
# except with //x and s///x
# alphanumeric strings
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
print "- " x 20,"\n";
print "Total token count: ", scalar @tokens, "\n";
}
@asciiartinate::contexts = @contexts;
@asciiartinate::tokens = @tokens;
@tokens;
}
sub asciiindex_code {
my ($X) = @_;
my $endpos = index($X,"\n__END__\n");
if ($endpos >= 0) {
substr($X,$endpos) = "\n";
}
$X =~ s/\n\s*#[^\n]*\n/\n/g;
$X =~ s/\n\s*#[^\n]*\n/\n/g;
&tokenize_code($X);
}
#############################################################################
sub tokenize_art {
my ($INPUT) = @_;
my @INPUT = split //, $INPUT;
my $white = 1;
my $block_size = 0;
my @blocks = ();
foreach my $char (@INPUT) {
if ($char eq " " || $char eq "\n" || $char eq "\t") {
if ($block_size > 0) {
push @blocks, $block_size;
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
} else {
++$block_size;
}
}
if ($block_size > 0) {
push @blocks, $block_size;
}
return @blocks;
}
sub asciiindex_art {
my ($X) = @_;
&tokenize_art($X);
}
#
# replace darkspace on the pic with characters from the code
#
sub print_code_to_pic {
my ($pic, @tokens) = @_;
local $" = '';
my $code = "@tokens";
my @code = split //, $code;
$pic =~ s/(\S)/@code==0?"#":shift @code/ge;
print $pic;
}
#
# find misalignment between multi-character tokens and blocks
# and report position where additional padding is needed for
# alignment
#
sub padding_needed {
my @tokens = @{$_[0]};
my @contexts = @{$_[1]};
my @blocks = @{$_[2]};
my $ib = 0;
my $tc = 0;
my $bc = $blocks[$ib++];
my $it = 0;
while ($bc == 0) {
$bc = $blocks[$ib++];
if ($ib > @blocks) {
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
$it++;
}
return;
}
#
# choose a random number between 0 and n-1,
# with the distribution heavily weighted toward
# the high end of the range
#
sub hi_weighted_rand {
my $n = shift;
my (@p, $r, $p);
for ($r = 1; $r <= $n; $r++) {
push @p, $p += $r * $r * $r;
}
$p = int(rand() * $p);
for ($r = 1; $r <= @p; $r++) {
return $r if $p[$r-1] >= $p;
}
return $n;
}
#
# look for opportunity to insert padding into the
# code at the specified location
#
sub try_to_pad {
my ($pos, $npad, $tref, $cref) = @_;
# padding techniques:
# X SIGIL name ---> SIGIL { name }
# XXX ---> ( XXX )
# for XXX in (numeric literal,quoted string)
# XXX ; ---> XXX ;;
# for XXX in (quoted string,numeric literal,regular expression
# <> operator, ")"
# X } ---> ; } for } that ends a code BLOCK
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
}
return 0;
}
#
# find all misalignments and insert padding into the code
# until all code is aligned or until the padded code is
# too large for the pic.
#
sub pad {
my @tokens = @{$_[0]};
my @contexts = @{$_[1]};
my @blocks = @{$_[2]};
my $nblocks = 0;
map { $nblocks += $_ } @blocks;
my ($needed, $where, $howmuch);
while ($needed = padding_needed(\@tokens,\@contexts,\@blocks)) {
($where,$howmuch) = @$needed;
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
=head1 EXAMPLE
Suppose we have a file called C<spider.pl> with
the following code:
&I();$N=<>;@o=(map{$z=${U}x($x=1+$N-$_);
' 'x$x.($".$F)x$_.($B.$z.$P.$z.$F).($B.$")x$_.$/}
0..$N);@o=(@o,($U.$F)x++$N.($"x3).($B.$U)x$N.$/);
print@o;
sub I{($B,$F,$P,$U)=qw(\\ / | _);}
while($_=pop@o){y'/\\'\/';@o||y#_# #;$t++||y#_ # _#;print}
What this code does is read one value from standard input
and draws a spider web of the given size:
$ echo 5 | perl spiders.pl
\______|______/
/\_____|_____/\
/ /\____|____/\ \
/ / /\___|___/\ \ \
lib/Acme/AsciiArtinator.pm view on Meta::CPAN
;; ;
;; ;; ;; ;
;; ;; ;; ;;
;; ;; ;@ o=
( map {$z =$
{U }x( $x= 1+
$N- $_) ;' 'x $x. ($".
$F)x$_ .($B.$z.$ P. $z.$F).
($B.$")x$_.$/}0..$N);@
o=(@o,($U.$F)x++$N.($"x3).($B.$U
)x$N.$/);;;;print@o;;;sub I{( $B,
$F, $P,$U)=qw(\\ /
| _);;}while($_=pop @o
){ y'/\\'\/';;;@o||y#_# #;; ;;;
;$ t++ ||y#_ # _#;print }# ##
## ## ################ ## ##
# ## ################ # #
# ## ################ ## #
# ## ############## ##
## ############ ##
## ######## ##
( run in 0.238 second using v1.01-cache-2.11-cpan-4d50c553e7e )