File Coverage

blib/lib/Language/Befunge/lib/STRN.pm
Criterion Covered Total %
statement 9 94 9.5
branch 0 18 0.0
condition 0 6 0.0
subroutine 3 17 17.6
pod 14 14 100.0
total 26 149 17.4


line stmt bran cond sub pod time code
1             #
2             # This file is part of Language::Befunge.
3             # Copyright (c) 2001-2009 Jerome Quelin, all rights reserved.
4             #
5             # This program is free software; you can redistribute it and/or modify
6             # it under the same terms as Perl itself.
7             #
8             #
9              
10             package Language::Befunge::lib::STRN;
11              
12 1     1   6002 use 5.010;
  1         3  
  1         38  
13 1     1   6 use strict;
  1         2  
  1         37  
14 1     1   5 use warnings;
  1         2  
  1         1101  
15              
16 0     0 1   sub new { return bless {}, shift; }
17              
18             sub A {
19 0     0 1   my ($self, $interp) = @_;
20 0           my $ip = $interp->get_curip;
21 0           my $b = $ip->spop_gnirts;
22 0           my $a = $ip->spop_gnirts;
23 0           $ip->spush_args( $b . $a );
24             }
25              
26             sub C {
27 0     0 1   my ($self, $interp) = @_;
28 0           my $ip = $interp->get_curip;
29 0           my $a = $ip->spop_gnirts;
30 0           my $b = $ip->spop_gnirts;
31 0           $ip->spush_args( $a cmp $b );
32             }
33              
34             sub D {
35 0     0 1   my ($self, $interp) = @_;
36 0           my $ip = $interp->get_curip;
37 0           my $a = $ip->spop_gnirts;
38 0           print $a;
39             }
40              
41             sub F {
42 0     0 1   my ($self, $interp) = @_;
43 0           my $ip = $interp->get_curip;
44 0           my $b = $ip->spop_gnirts;
45 0           my $a = $ip->spop_gnirts;
46 0           my $i = index $b, $a;
47 0 0         $ip->spush_args( $i==-1 ? '' : substr $b, $i );
48             }
49              
50             sub G {
51 0     0 1   my ($self, $interp) = @_;
52 0           my $ip = $interp->get_curip;
53 0           my $storage = $interp->get_storage;
54              
55             # pop vector
56 0           my $pos = $ip->spop_vec + $ip->get_storage;
57              
58             # create virtual ip to walk the storage
59 0           my $myip = Language::Befunge::IP->new( $pos->get_dims );
60 0           $myip->set_position($pos);
61              
62             # really walk the storage
63 0           my $str;
64 0           my $val = $storage->get_value( $pos );
65 0           my %seen = ( $pos => 1 );
66 0           while ( $val != 0 ) {
67 0           $str .= chr $val;
68             # let's move the virtual ip
69 0           $interp->_move_ip_once($myip);
70 0           $pos = $myip->get_position;
71 0 0         return $ip->dir_reverse if $seen{$pos}++;
72 0           $val = $storage->get_value($pos);
73             }
74 0           $ip->spush_args( $str );
75             }
76              
77              
78             sub I {
79 0     0 1   my ($self, $lbi) = @_;
80 0           my $ip = $lbi->get_curip;
81 0           my $in = $lbi->get_input;
82 0 0         return $ip->dir_reverse unless defined $in;
83              
84 0           while ( $in ne "\n" ) {
85 0           $ip->spush( ord $in );
86 0           $in = $lbi->get_input;
87 0 0         return $ip->dir_reverse unless defined $in;
88             }
89             }
90              
91             sub L {
92 0     0 1   my ($self, $interp) = @_;
93 0           my $ip = $interp->get_curip;
94 0           my $n = $ip->spop;
95 0           my $a = $ip->spop_gnirts;
96 0 0         return $ip->dir_reverse if $n < 0;
97 0 0         return $ip->spush( $a ) if $n > length $a;
98 0           $ip->spush_args( substr( $a, 0, $n ) );
99             }
100              
101             sub M {
102 0     0 1   my ($self, $interp) = @_;
103 0           my $ip = $interp->get_curip;
104 0           my $n = $ip->spop;
105 0           my $m = $ip->spop;
106 0           my $a = $ip->spop_gnirts;
107 0 0 0       return $ip->dir_reverse if ($m < 0 || $m > length($a) || $n < 0);
      0        
108 0           $ip->spush_args( substr($a, $m, $n) );
109             }
110              
111             sub N {
112 0     0 1   my ($self, $interp) = @_;
113 0           my $ip = $interp->get_curip;
114 0           my $a = $ip->spop_gnirts;
115 0           $ip->spush_args( $a, length $a );
116             }
117              
118              
119             sub P {
120 0     0 1   my ($self, $interp) = @_;
121 0           my $ip = $interp->get_curip;
122 0           my $storage = $interp->get_storage;
123              
124             # pop arguments
125 0           my $pos = $ip->spop_vec + $ip->get_storage;
126 0           my $str = $ip->spop_gnirts;
127 0           $storage->store_binary( $str . chr(0), $pos);
128             }
129              
130             sub R {
131 0     0 1   my ($self, $interp) = @_;
132 0           my $ip = $interp->get_curip;
133 0           my $n = $ip->spop;
134 0           my $a = $ip->spop_gnirts;
135 0 0         return $ip->dir_reverse if $n < 0;
136 0 0         return $ip->spush( $a ) if $n > length $a;
137 0           $ip->spush_args( substr($a, -$n) );
138             }
139              
140             sub S {
141 0     0 1   my ($self, $interp) = @_;
142 0           my $ip = $interp->get_curip;
143 0           my $n = $ip->spop;
144 0           $ip->spush( $_ ) for reverse map {ord} split //, $n.chr(0); # force string.
  0            
145             }
146              
147             sub V {
148 0     0 1   my ($self, $interp) = @_;
149 0           my $ip = $interp->get_curip;
150 0           my $n = $ip->spop_gnirts;
151 0           $ip->spush( 0+$n );
152             }
153              
154              
155             1;
156              
157             __END__