File Coverage

blib/lib/Language/l33t/Operators.pm
Criterion Covered Total %
statement 106 123 86.1
branch 34 64 53.1
condition 6 15 40.0
subroutine 17 18 94.4
pod 0 1 0.0
total 163 221 73.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.0.1';
5 3     3   1665 use Moo::Role;
  3         4  
  3         48  
6              
7 3     3   2357 use Const::Fast;
  3         2772  
  3         19  
8 3     3   225 use Carp;
  3         4  
  3         146  
9              
10 3     3   15 use experimental 'signatures';
  3         5  
  3         20  
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 50   477 0 1292 sub opcode($self,$index) {
  477 50       1129  
  477         705  
  477         620  
  477         568  
41 477 100 66     2859 if ( $index > $#op_codes or $index < 0 ) {
42 1         17 warn "j00 4r3 teh 5ux0r\n";
43 1         49 $index = $NOP;
44             }
45 477         1323 return $op_codes[ $index ]->( $_[0] );
46             }
47              
48              
49 127 50   127   319 sub _inc($self,$sign=1) {
  127 50       307  
  127 100       171  
  127         302  
  127         150  
50 127         383 $self->_incr_op_ptr;
51 127         10180 $self->_incr_mem( $sign * ( 1 + $self->memory_cell( $self->op_ptr ) ) );
52 127         18132 $self->_incr_op_ptr;
53 127         7153 return 1;
54             }
55              
56 80 50   80   261 sub _dec($self) {
  80 50       224  
  80         124  
  80         109  
57 80         237 return $self->_inc( -1 );
58             }
59              
60 184 50   184   500 sub _nop($self) {
  184 50       449  
  184         235  
  184         223  
61 184         616 $self->_incr_op_ptr;
62 184         11311 return 1;
63             }
64              
65             sub _end {
66 11     11   36 return 0;
67             }
68              
69              
70 1 50   1   4 sub _con($self) {
  1 50       3  
  1         2  
  1         1  
71             my $ip = join '.', map {
72 1         3 my $x = $self->_get_current_mem;
  4         7  
73 4         144 $self->_incr_mem_ptr;
74 4 100       17 $x || 0;
75             } 1..4;
76              
77 1   50     4 my $port = ( $self->_get_current_mem() || 0 ) << 8;
78 1         39 $self->_incr_mem_ptr;
79             {
80 3     3   2232 no warnings qw/ uninitialized /;
  3         13  
  3         991  
  1         1  
81 1         3 $port += $self->_get_current_mem;
82             }
83              
84 1         39 $self->_incr_mem_ptr( -5 );
85              
86 1 50       17 warn "trying to connect at $ip:$port\n"
87             if $self->debug;
88              
89 1 50       10 if ( "$ip:$port" eq '0.0.0.0:0' ) {
90 0         0 $self->set_socket( undef );
91             }
92             else {
93 1 50       14 if ( my $sock = IO::Socket::INET->new( "$ip:$port" ) ) {
94 0         0 $self->set_socket( $sock );
95             }
96             else {
97 1         703 warn "h0s7 5uXz0r5! c4N'7 c0Nn3<7 101010101 l4m3R !!!\n";
98             }
99             }
100              
101 1         46 $self->_incr_op_ptr;
102 1         44 return 1;
103             }
104              
105              
106             sub _fwd {
107 98     98   161 my $self = shift;
108 98   100     389 my $direction = shift || 1;
109 98         284 $self->_incr_op_ptr;
110 98         6075 $self->_incr_mem_ptr( $direction * ( 1 + $self->_current_op ) );
111 98         352 $self->_incr_op_ptr;
112              
113 98         5764 return 1;
114             }
115              
116 49     49   144 sub _bak { return $_[0]->_fwd( -1 ); }
117              
118 18 50   18   64 sub _wrt($self) {
  18 50       46  
  18         36  
  18         37  
119 18 50 33     569 if ( my $io = $self->socket || $self->stdout ) {
120 3     3   17 no warnings qw/ uninitialized /;
  3         7  
  3         1247  
121 18         1750 print {$io} chr $self->_get_current_mem;
  18         81  
122             }
123             else {
124 0         0 print chr $self->_get_current_mem;
125             }
126 18         1204 $self->_incr_op_ptr;
127              
128 18         927 return 1;
129             }
130              
131 0 0   0   0 sub _rd($self) {
  0 0       0  
  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 50   44   143 sub _if($self) {
  44 50       117  
  44         63  
  44         89  
149 44 100       174 if ( $self->_get_current_mem ) {
150 43         2636 $self->_nop;
151             }
152             else {
153 1         40 my $nest_level = 0;
154 1         3 my $max_iterations = $self->memory_size;
155              
156             SCAN:
157 1         441 while (1) {
158 3         9 $self->_incr_op_ptr;
159 3         135 $max_iterations--;
160              
161 3 50 0     8 $nest_level++ and redo if $self->_current_op == $IF;
162              
163 3 50       172 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       183 croak "dud3, wh3r3's my EIF?" unless $max_iterations;
173             }
174             }
175              
176 43         199 return 1;
177             }
178              
179 43 50   43   125 sub _eif($self) {
  43 50       114  
  43         70  
  43         52  
180 43 100       148 if ( ! $self->_get_current_mem ) {
181 6         365 $self->_nop;
182             }
183             else {
184 37         2581 $self->_incr_op_ptr( -1 ) until $self->_current_op == 3;
185             };
186              
187 43         3476 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.0.1
206              
207             =head1 AUTHOR
208              
209             Yanick Champoux <yanick@cpan.org>
210              
211             =head1 COPYRIGHT AND LICENSE
212              
213             This software is copyright (c) 2006 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