Befunge-Interpreter
view release on metacpan or search on metacpan
These are explained in greater detail in the Commands section.
In order to push a number greater than 9 on the stack, calculations
must be done with numbers less than or equal to 9. In any other
language this would be a pain. In Befunge-93 it is a joy. For example,
to push '123' onto the stack, one might push 9, then 9, then multiply
(leaving 81), then push 7, then 6, then multiply (leaving 81 and 42,)
then add (leaving 123.) In Befunge, this would look something like :
99*76*+
This is, of course, assuming (correctly) that the PC starts at or
before the first '9' and is working towards the right.
NB. If the stack is be empty when you pop something off, be warned
that this will not generate an underflow! It will simply return '0' to
you. Hope you can live with it!
The Program Counter in Detail
There are 5 commands which directly control the PC direction: '>',
'<', 'v', '^', and '?'. '>' makes the PC travel to the right; '<' to
the left;'v' down; '^' up; and '?' in a random direction. So, the
following example is an infinite loop :
><
As is :
>v^<
As is :
>v>v >^v^ <
Note that ' ' (space) is a null command which does nothing.
Should the PC encounter the 'edge' of the program, such as if you were
to try to execute:
<
The PC will 'wrap around' to the other 'edge' of the program. This,
too, is an infinite loop.
Decision Making
The standard 'if' statement in Befunge is either '_' or '|', depending
on how you want to branch. Both pop a value off the stack and check to
see if it is true (non-zero,) and change the direction of the PC
accordingly.
'_' acts like '<' if it is true, and '>' if it is false.
'|' acts like '^' if it is true, and 'v' if it is false.
'While' loops can be made by sticking an 'if' in an infinite loop. For
example,
>_@
(This program fragment pops all of the non-zero values off the stack,
and the first zero value, then exits ['@' is the exit command.])
Input
The '&' command will get a numeric value from the standard input and
push it on the stack. '~' will get the next ASCII character from
standard input and push it on the stack.
eg.
&,
...prints out "A" if the user types "65 ", and...
~.
...prints out "65 " if the user types "A".
Output
The '.' command will pop a value off the stack and output it as an
integer, followed by a space. (somewhat like Forth.) ',' will pop
avalue and output as ASCII with no space.
eg.
665+*1-,
...prints out ASCII 65 ("A".), and...
665+*1-.
...prints out "65 ".
Special Commands
'#' is the 'bridge' command... it causes the next command which would
normally be executed to be skipped over, and not executed. For
example,
>123...@
would output "3 2 1 " but
>123#...@
would output "3 2 " with one of the '.''s being skipped. Judicious use
of '#' can make for very interesting code!
':' is the duplicating command. It makes a copy of the top element of
the stack. This is useful, as demonstrated in the program below.
v.<>:| @
This makes duplicates of each value on the stacked, which is checked,
and if non-zero, printed.
'$' pops a value off the stack, but does nothing with it. So,
123.$.@
( run in 2.151 seconds using v1.01-cache-2.11-cpan-364913b4093 )