v4.13
[language-befunge.git] / lib / Language / Befunge / lib / ORTH.pm
blob91f1a080b4b9a691c68e42540d56d40d8d32fcd6
2 # This file is part of Language::Befunge.
3 # Copyright (c) 2001-2009 Jerome Quelin, all rights reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
10 package Language::Befunge::lib::ORTH;
12 use strict;
13 use warnings;
15 use Language::Befunge::Vector;
17 sub new { return bless {}, shift; }
20 # -- bit operations
23 # $v = A( $a, $b )
25 # push $a & $b back onto the stack (bitwise AND)
27 sub A {
28 my ($self, $interp) = @_;
29 my $ip = $interp->get_curip();
31 # pop the values
32 my $b = $ip->spop;
33 my $a = $ip->spop;
35 # push the result
36 $ip->spush( $a&$b );
41 # $v = E( $a, $b )
43 # push $a ^ $b back onto the stack (bitwise XOR)
45 sub E {
46 my ($self, $interp) = @_;
47 my $ip = $interp->get_curip();
49 # pop the values
50 my $b = $ip->spop;
51 my $a = $ip->spop;
53 # push the result
54 $ip->spush( $a^$b );
59 # $v = O( $a, $b )
61 # push $a | $b back onto the stack (bitwise OR)
63 sub O {
64 my ($self, $interp) = @_;
65 my $ip = $interp->get_curip();
67 # pop the values
68 my $b = $ip->spop;
69 my $a = $ip->spop;
71 # push the result
72 $ip->spush( $a|$b );
76 # -- push / get
79 # $v = G( $y, $x )
81 # push back value stored at coords ($x, $y). note that befunge get is g($x,$y)
82 # (ie, the arguments are reversed).
84 sub G {
85 my ($self, $lbi) = @_;
86 my $ip = $lbi->get_curip;
88 my $x = $ip->spop;
89 my $y = $ip->spop;
90 my $v = Language::Befunge::Vector->new($x,$y);
91 my $val = $lbi->get_storage->get_value( $v );
92 $ip->spush( $val );
97 # P( $v, $y, $x )
99 # store value $v at coords ($x, $y). note that befunge put is p($v,$x,$y) (ie,
100 # the coordinates are reversed).
102 sub P {
103 my ($self, $lbi) = @_;
104 my $ip = $lbi->get_curip;
106 my $x = $ip->spop;
107 my $y = $ip->spop;
108 my $v = Language::Befunge::Vector->new($x,$y);
109 my $val = $ip->spop;
110 $lbi->get_storage->set_value( $v, $val );
114 # -- output
117 # S( 0gnirts )
119 # print popped 0gnirts on stdout.
121 sub S {
122 my ($self, $lbi) = @_;
123 print $lbi->get_curip->spop_gnirts;
127 # -- coordinates & velocity changes
130 # X( $x )
132 # Change X coordinate of IP to $x.
134 sub X {
135 my ($self, $lbi) = @_;
136 my $ip = $lbi->get_curip;
137 my $v = $ip->get_position;
138 my $x = $ip->spop;
139 $v->set_component(0,$x);
143 # Y( $y )
145 # Change Y coordinate of IP to $y.
147 sub Y {
148 my ($self, $lbi) = @_;
149 my $ip = $lbi->get_curip;
150 my $v = $ip->get_position;
151 my $y = $ip->spop;
152 $v->set_component(1,$y);
157 # V( $dx )
159 # Change X coordinate of IP velocity to $dx.
161 sub V {
162 my ($self, $lbi) = @_;
163 my $ip = $lbi->get_curip;
164 my $v = $ip->get_delta;
165 my $dx = $ip->spop;
166 $v->set_component(0,$dx);
171 # W( $dy )
173 # Change Y coordinate of IP velocity to $dy.
175 sub W {
176 my ($self, $lbi) = @_;
177 my $ip = $lbi->get_curip;
178 my $v = $ip->get_delta;
179 my $dy = $ip->spop;
180 $v->set_component(1,$dy);
184 # -- misc
187 # Z( $bool )
189 # Test the top stack element, and if zero, skip over the next cell (i.e., add
190 # the delta twice to the current position).
192 sub Z {
193 my ($self, $lbi) = @_;
194 my $ip = $lbi->get_curip;
195 my $v = $ip->spop;
196 $lbi->_move_ip_once($ip) if $v == 0;
202 __END__
205 =head1 NAME
207 Language::Befunge::IP::lib::ORTH - Orthogonal easement extension
211 =head1 DESCRIPTION
213 The ORTH fingerprint (0x4f525448) is designed to ease transition between the
214 Orthogonal programming language and Befunge-98 (or higher dimension Funges).
216 Even if transition from Orthogonal is not an issue, the ORTH library contains
217 some potentially interesting instructions not in standard Funge-98.
221 =head1 FUNCTIONS
223 =head2 new
225 Create a new ORTH instance.
228 =head2 Bit operations
230 =over 4
232 =item A( $a, $b )
234 Push back C<$a & $b> (bitwise AND).
237 =item O( $a, $b )
239 Push back C<$a | $b> (bitwise OR).
242 =item E( $a, $b )
244 Push back C<$a ^ $b> (bitwise XOR).
247 =back
251 =head2 Push & get
253 =over 4
255 =item G( $y, $x )
257 Push back value stored at coords ($x, $y). Note that Befunge get is C<g($x,$y)>
258 (ie, the arguments are reversed).
261 =item P( $v, $y, $x )
263 Store value C<$v> at coords ($x, $y). Note that Befunge put is C<p($v,$x,$y)> (ie,
264 the coordinates are reversed).
267 =back
271 =head2 Output
273 =over 4
275 =item S( 0gnirts )
277 Print popped 0gnirts on STDOUT.
279 =back
283 =head2 Coordinates & velocity changes
285 =over 4
287 =item X( $x )
289 Change X coordinate of IP to C<$x>.
292 =item Y( $y )
294 Change Y coordinate of IP to C<$y>.
297 =item V( $dx )
299 Change X coordinate of IP velocity to C<$dx>.
302 =item W( $dy )
304 Change Y coordinate of IP velocity to C<$dy>.
307 =back
310 =head2 Miscellaneous
312 =over 4
314 =item Z( $bool )
316 Test the top stack element, and if zero, skip over the next cell (i.e., add
317 the delta twice to the current position).
320 =back
323 =head1 SEE ALSO
325 L<Language::Befunge>, L<http://catseye.tc/projects/funge98/library/ORTH.html>,
326 and L<http://www.muppetlabs.com/~breadbox/orth/orth.html>.
330 =head1 AUTHOR
332 Jerome Quelin, C<< <jquelin@cpan.org> >>
335 =head1 COPYRIGHT & LICENSE
337 Copyright (c) 2001-2009 Jerome Quelin, all rights reserved.
339 This program is free software; you can redistribute it and/or modify
340 it under the same terms as Perl itself.
343 =cut