NAME
Language::Befunge::Storage::2D::Sparse - a 2D storage, using sparse hash
VERSION
version 5.000
SYNOPSIS
my $storage = Language::Befunge::Storage::2D::Sparse->new;
$storage->clear;
$storage->store(<<EOF);
12345
67890
EOF
DESCRIPTION
This class implements a storage as defined in LBS. It makes the assumption that we're in a 2D Funge space for efficiency reasons. Therefore, it's only suited for befunge programs.
This storage is sparse, using a private hash with keys such as "$x,$y". Any value of a non-existing key defaults to 32 (space), as defined by funge specs.
PUBLIC METHODS
Constructor
Storage update
- $storage->clear;
-
Clear the storage.
- my $size = $storage->store_binary( $code [, $position] );
-
Store the given
$code
at the specified$position
(defaulting to the origin coordinates).Return the size of the code inserted, as a vector.
The code is a string, representing a block of Funge code. This is binary insertion, that is, EOL sequences are stored in Funge-space instead of causing the dimension counters to be resetted and incremented.
- my $size = $storage->store( $code [, $position] );
-
Store the given $code at the specified $position (defaulting to the origin coordinates).
Return the size of the code inserted, as a vector.
The code is a string, representing a block of Funge code. Rows are separated by newlines.
- $storage->set_value( $offset, $value );
-
Write the supplied
$value
in the storage at the specified$offset
./!\ As in Befunge, code and data share the same playfield, the number stored can be either an instruction or raw data (or even both... Eh, that's Befunge! :o) ).
Data retrieval
- my $dims = $storage->get_dims;
-
Return the dimensionality of the storage. For this module, the value is always 2.
- my $vmin = $storage->min;
-
Return a LBV pointing to the lower bounds of the storage.
- my $vmax = $storage->max;
-
Return a LBV pointing to the upper bounds of the storage.
- my $val = $storage->get_value( $offset );
-
Return the number stored in the torus at the specified
$offset
. If the value hasn't yet been set, it defaults to the ordinal value of a space (ie, #32)./!\ As in Befunge, code and data share the same playfield, the number returned can be either an instruction or raw data (or even both... Eh, that's Befunge! :o) ).
- my $chr = $storage->get_char( $offset )
-
Return the character stored in the torus at the specified
$offset
. If the value is not between 0 and 255 (inclusive), get_char will return a string that looks like<np-0x4500>
./!\ As in Befunge, code and data share the same playfield, the character returned can be either an instruction or raw data. No guarantee is made that the return value is printable.
- my $str = $storage->rectangle( $pos, $size );
-
Return a string containing the data/code in the rectangle defined by the supplied vectors.
Miscellaneous methods
- my $href = $storage->labels_lookup;
-
Parse the storage to find sequences such as
;:(\w[^\s;])[^;]*;
and return a hash reference whose keys are the labels and the values an anonymous array with four values: a vector describing the absolute position of the character just after the trailing;
, and a vector describing the velocity that leads to this label.This method will only look in the four cardinal directions, and does wrap basically like befunge93 (however, this should not be a problem since we're only using cardinal directions)
This allow to define some labels in the source code, to be used by
Inline::Befunge
(and maybe some exstensions).
AUTHOR
Jerome Quelin
COPYRIGHT AND LICENSE
This software is copyright (c) 2003 by Jerome Quelin.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.