File Coverage

blib/lib/Language/l33t/Operators.pm
Criterion Covered Total %
statement 98 114 85.9
branch 16 26 61.5
condition 6 15 40.0
subroutine 17 18 94.4
pod 0 1 0.0
total 137 174 78.7


line stmt bran cond sub pod time code
1             package Language::l33t::Operators;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Implementation of the l33t language operators
4             $Language::l33t::Operators::VERSION = '1.1.0';
5 4     4   170982 use Moo::Role;
  4         19730  
  4         38  
6              
7 4     4   5223 use Const::Fast;
  4         8198  
  4         41  
8 4     4   388 use Carp;
  4         10  
  4         293  
9              
10 4     4   624 use experimental 'signatures';
  4         1955  
  4         42  
11              
12             requires qw/ _incr_op_ptr _incr_mem_ptr _incr_mem /;
13              
14             const our $NOP => 0;
15             const our $WRT => 1;
16             const our $RD => 2;
17             const our $IF => 3;
18             const our $EIF => 4;
19             const our $FWD => 5;
20             const our $BAK => 6;
21             const our $INC => 7;
22             const our $DEC => 8;
23             const our $CON => 9;
24             const our $END => 10;
25              
26             our @op_codes;
27              
28             $op_codes[$NOP] = \&_nop;
29             $op_codes[$WRT] = \&_wrt;
30             $op_codes[$RD] = \&_rd;
31             $op_codes[$IF] = \&_if;
32             $op_codes[$EIF] = \&_eif;
33             $op_codes[$FWD] = \&_fwd;
34             $op_codes[$BAK] = \&_bak;
35             $op_codes[$INC] = \&_inc;
36             $op_codes[$DEC] = \&_dec;
37             $op_codes[$CON] = \&_con;
38             $op_codes[$END] = \&_end;
39              
40 477     477 0 777 sub opcode($self,$index) {
  477         913  
  477         767  
  477         662  
41 477 100 66     2222 if ( $index > $#op_codes or $index < 0 ) {
42 1         17 warn "j00 4r3 teh 5ux0r\n";
43 1         8 $index = $NOP;
44             }
45 477         1296 return $op_codes[ $index ]->($self);
46             }
47              
48              
49 127     127   180 sub _inc($self,$sign=1) {
  127         197  
  127         224  
  127         175  
50 127         398 $self->_incr_op_ptr;
51 127         9748 $self->_incr_mem( $sign * ( 1 + $self->memory_cell( $self->op_ptr ) ) );
52 127         17233 $self->_incr_op_ptr;
53 127         7337 return 1;
54             }
55              
56 80     80   126 sub _dec($self) {
  80         140  
  80         115  
57 80         231 return $self->_inc( -1 );
58             }
59              
60 184     184   326 sub _nop($self) {
  184         342  
  184         310  
61 184         615 $self->_incr_op_ptr;
62 184         10843 return 1;
63             }
64              
65             sub _end {
66 11     11   32 return 0;
67             }
68              
69              
70 1     1   2 sub _con($self) {
  1         2  
  1         2  
71             my $ip = join '.', map {
72 1         3 my $x = $self->_get_current_mem;
  4         8  
73 4         177 $self->_incr_mem_ptr;
74 4 100       13 $x || 0;
75             } 1..4;
76              
77 1   50     4 my $port = ( $self->_get_current_mem() || 0 ) << 8;
78 1         32 $self->_incr_mem_ptr;
79             {
80 4     4   5338 no warnings qw/ uninitialized /;
  4         10  
  4         2045  
  1         2  
81 1         2 $port += $self->_get_current_mem;
82             }
83              
84 1         30 $self->_incr_mem_ptr( -5 );
85              
86 1 50       11 warn "trying to connect at $ip:$port\n"
87             if $self->debug;
88              
89 1 50       9 if ( "$ip:$port" eq '0.0.0.0:0' ) {
90 0         0 $self->set_socket( undef );
91             }
92             else {
93 1 50       18 if ( my $sock = IO::Socket::INET->new( "$ip:$port" ) ) {
94 0         0 $self->set_socket( $sock );
95             }
96             else {
97 1         939 warn "h0s7 5uXz0r5! c4N'7 c0Nn3<7 101010101 l4m3R !!!\n";
98             }
99             }
100              
101 1         11 $self->_incr_op_ptr;
102 1         40 return 1;
103             }
104              
105              
106             sub _fwd {
107 98     98   175 my $self = shift;
108 98   100     314 my $direction = shift || 1;
109 98         331 $self->_incr_op_ptr;
110 98         5989 $self->_incr_mem_ptr( $direction * ( 1 + $self->_current_op ) );
111 98         370 $self->_incr_op_ptr;
112              
113 98         5738 return 1;
114             }
115              
116 49     49   146 sub _bak { return $_[0]->_fwd( -1 ); }
117              
118 18     18   31 sub _wrt($self) {
  18         30  
  18         28  
119 18 50 33     484 if ( my $io = $self->socket || $self->stdout ) {
120 4     4   37 no warnings qw/ uninitialized /;
  4         9  
  4         3137  
121 18         258 print {$io} chr $self->_get_current_mem;
  18         83  
122             }
123             else {
124 0         0 print chr $self->_get_current_mem;
125             }
126 18         1183 $self->_incr_op_ptr;
127              
128 18         941 return 1;
129             }
130              
131 0     0   0 sub _rd($self) {
  0         0  
  0         0  
132 0         0 my $chr;
133              
134 0 0 0     0 if ( my $io = $self->socket || $self->stdin ) {
135 0         0 read $io, $chr, 1;
136             }
137             else {
138 0         0 read STDIN, $chr, 1;
139             }
140              
141 0         0 $self->_set_current_mem( ord $chr );
142 0         0 $self->_incr_op_ptr;
143              
144 0         0 return 1;
145             }
146              
147              
148 44     44   88 sub _if($self) {
  44         79  
  44         95  
149 44 100       185 if ( $self->_get_current_mem ) {
150 43         2931 $self->_nop;
151             }
152             else {
153 1         32 my $nest_level = 0;
154 1         14 my $max_iterations = $self->memory_size;
155              
156             SCAN:
157 1         34 while (1) {
158 3         7 $self->_incr_op_ptr;
159 3         87 $max_iterations--;
160              
161 3 50 0     6 $nest_level++ and redo if $self->_current_op == $IF;
162              
163 3 50       239 if ( $self->_current_op == $EIF ) {
164 0 0       0 if ( $nest_level ) {
165 0         0 $nest_level--;
166             }
167             else {
168 0         0 break SCAN;
169             }
170             }
171              
172 3 100       429 croak "dud3, wh3r3's my EIF?" unless $max_iterations;
173             }
174             }
175              
176 43         178 return 1;
177             }
178              
179 43     43   81 sub _eif($self) {
  43         68  
  43         63  
180 43 100       133 if ( ! $self->_get_current_mem ) {
181 6         418 $self->_nop;
182             }
183             else {
184 37         2324 $self->_incr_op_ptr( -1 ) until $self->_current_op == 3;
185             };
186              
187 43         3387 return 1;
188             }
189              
190              
191             1;
192              
193             __END__
194              
195             =pod
196              
197             =encoding UTF-8
198              
199             =head1 NAME
200              
201             Language::l33t::Operators - Implementation of the l33t language operators
202              
203             =head1 VERSION
204              
205             version 1.1.0
206              
207             =head1 AUTHOR
208              
209             Yanick Champoux <yanick@cpan.org>
210              
211             =head1 COPYRIGHT AND LICENSE
212              
213             This software is copyright (c) 2026, 2008 by Yanick Champoux.
214              
215             This is free software; you can redistribute it and/or modify it under
216             the same terms as the Perl 5 programming language system itself.
217              
218             =cut