File Coverage

blib/lib/Language/Befunge/Wrapping/LaheySpace.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 23 23 100.0


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::Wrapping::LaheySpace;
11              
12 72     72   47260 use strict;
  72         153  
  72         3001  
13 72     72   405 use warnings;
  72         145  
  72         2514  
14              
15 72     72   423 use base qw{ Language::Befunge::Wrapping };
  72         144  
  72         52218  
16              
17              
18             # -- PUBLIC METHODS
19              
20             #
21             # $wrapping->wrap( $storage, $ip );
22             #
23             # Wrap $ip in $storage according to this module wrapping algorithm. Note
24             # that $ip is already out of bounds, ie, it has been moved once by LBI.
25             # As a side effect, $ip will have its position changed.
26             #
27             # LBW::LaheySpace implements a wrapping as defined in befunge specs -
28             # ie, when hitting a bound of the storage, the ip reverses and
29             # backtraces until it bumps into another bound, and then it reverses one
30             # last time to keep its velocity.
31             #
32             sub wrap {
33 437     437 1 3886 my ($self, $storage, $ip) = @_;
34              
35             # fetch the current position/velocity of the ip.
36 437         788 my $v = $ip->get_position;
37 437         732 my $d = $ip->get_delta;
38              
39             # fetch the storage min / max
40 437         1219 my $min = $storage->min;
41 437         6685 my $max = $storage->max;
42              
43             # funge98 says we should walk our current trajectory in reverse,
44             # until we hit the other side of the storage, and then continue
45             # along the same path.
46 437         671 do {
47 4836         13284 $v -= $d;
48             } while ( $v->bounds_check($min, $max) );
49              
50             # now that we've hit the wall, walk back into the valid code range.
51 437         1085 $v += $d;
52              
53             # store new position.
54 437         2359 $ip->set_position($v);
55             }
56              
57             1;
58             __END__