Basic befunge interpreter written in Python
This interpreter implements (to a reasonable degree of conformance) the Befunge 93 spec. The aim was to write in a fully-formed engineering style rather than code golf, though liberties were taken with using a functional style in places.
-
Python 2/3 compatible. Tested on Python 2.7.3, 2.7.4, 3.2.3, and 3.3.1, as well as 2.6.6 (though unit tests do not run on this version).
-
Unit and integration test suite, which can be run with
./runtests.sh
. -
Reasonably
pep8
compliant (modulo assorted minor warnings) andpyflakes
linted. -
Extensible syntax--adding new symbols is as straightforward as adding a new token in
befunge.syntax
and then writing an evaluation inbefunge.evaluation
. Metaprogramming plumbing will take care of the rest. -
Multiple backends for befunge grids are possible by extending the abstract
Board
class inbefunge.board
. The current implementation,BefungeBoard
, uses nested lists, but an additional backend might leveragenumpy
or other higher-performance alternatives.
There are several tradeoffs made to keep this implementation simple
relative to the C reference implementation of Befunge 93.
Specifically, the current board backend stores everything as plain
Python (yes, autopromoting and possibly negative) int
types.
Befunge programs that rely on C-style char/int behavior will probably
not work with befunge.py
.
Different from the reference implementation, befunge.py
simply fails
on division by zero instead of asking the user. Since Python has a
cleaner separation between chr
and int
, this implementation uses
an integer grid instead of a char grid, though there are still limits
as to the type of data that may be placed in the grid compared with
the C reference implementation.
Finally, some Befunge programs are not fully conformant with the spec,
and rely on non-standard grid sizes, non-terminating behavior when
non-instruction characters are encountered, or specific integer
specifications (see above). To accommodate different program sizes,
befunge.py
will resize the grid larger than the 80x25 specification
if needed to fit a large program, but otherwise makes 80x25 the
minimum for all programs run. Like the C reference implementation
(which can be toggled), befunge.py
will treat all non-instruction
symbols as noops, not just the space character.
Though not extensively compared, this implementation shares some aspects in common with befungee and the online JavaScript befunge interpreter. These implementations appear to make similar tradeoffs relative to the C reference implementation, and fail on similar programs.
Befunge code can be run from the command line with
./befunge.py code/hello.bf
To slow down the computation, the number of seconds of wait time per step can be specified as well, e.g.,
./befunge.py code/hello.bf 0.01
Since many steps may not produce output, small numbers for this wait time (0.01 to 0.001) are probably best for many programs.
To programmatically use the interpreter, simply import befunge
and
use the run
command, which can be called in a number of ways:
import befunge
# standalone
befunge.run(filename='code/hello.bf')
# to string instead of stdout
str_output = befunge.run(filename='code/hello.bf',display=False)
print(str_output)
# with a modifiable state variable
state = befunge.State(befunge.BefungeBoard('code/hello.bf'))
state.pos = (3,5)
befunge.run(state=state)
Examine both befunge.py
and tests/{unit,integration}.py
for more
examples of usage.
The befunge programs in code/
were taken from here and
here, as well as wikis and a few other assorted locations
scattered around the internet.
All those components that are not obtained elsewhere are Copyright 2013 Jarrell Waggoner. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY JARRELL WAGGONER ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JARRELL WAGGONER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jarrell Waggoner
/-/ malloc47.com