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             #
4             # This software is copyright (c) 2003 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 71     71   2898 use strict;
  71         79  
  71         1820  
10 71     71   224 use warnings;
  71         80  
  71         3037  
11              
12             package Language::Befunge::Wrapping::LaheySpace;
13             # ABSTRACT: a LaheySpace wrapping
14             $Language::Befunge::Wrapping::LaheySpace::VERSION = '5.000';
15              
16 71     71   248 use base qw{ Language::Befunge::Wrapping };
  71         65  
  71         24753  
17              
18              
19             # -- PUBLIC METHODS
20              
21             #
22             # $wrapping->wrap( $storage, $ip );
23             #
24             # Wrap $ip in $storage according to this module wrapping algorithm. Note
25             # that $ip is already out of bounds, ie, it has been moved once by LBI.
26             # As a side effect, $ip will have its position changed.
27             #
28             # LBW::LaheySpace implements a wrapping as defined in befunge specs -
29             # ie, when hitting a bound of the storage, the ip reverses and
30             # backtraces until it bumps into another bound, and then it reverses one
31             # last time to keep its velocity.
32             #
33             sub wrap {
34 437     437 1 379 my ($self, $storage, $ip) = @_;
35              
36             # fetch the current position/velocity of the ip.
37 437         425 my $v = $ip->get_position;
38 437         409 my $d = $ip->get_delta;
39              
40             # fetch the storage min / max
41 437         681 my $min = $storage->min;
42 437         666 my $max = $storage->max;
43              
44             # funge98 says we should walk our current trajectory in reverse,
45             # until we hit the other side of the storage, and then continue
46             # along the same path.
47 437         341 do {
48 4851         6030 $v -= $d;
49             } while ( $v->bounds_check($min, $max) );
50              
51             # now that we've hit the wall, walk back into the valid code range.
52 437         694 $v += $d;
53              
54             # store new position.
55 437         1317 $ip->set_position($v);
56             }
57              
58             1;
59              
60             __END__