NAME
Inline::Befunge - write Perl subs in Befunge
SYNOPSIS
use Inline "Befunge";
print "9 + 16 = ", add(9, 16), "\n";
print "9 - 16 = ", substract(9, 16),"\n";
__END__
__Befunge__
BF-sub add
+q
BF-sub substract
-q
DESCRIPTION
The Inline::Befunge
module allows you to put Befunge source code directly "inline" in a Perl script or module.
This allows you to write cool stuff with all the power of Befunge!
USING Inline::Befunge
Using Inline::Befunge
will seem very similar to using a another Inline language, thanks to Inline's consistent look and feel.
This section will explain how to use Inline::Befunge
.
For more details on Inline
, see perldoc Inline
.
Feeding Inline with your code
The recommended way of using Inline is this:
use Inline Befunge;
...
__END__
__Befunge__
Befunge source code goes here.
But there's much more way to use Inline. You'll find them in perldoc Inline
.
Defining functions
As a befunge fan, you know that Befunge does not support named subroutines. So, I introduced a little hack in order for Inline to work: each subroutine definition should be prepended with BF-sub subname
, and the body of the function should be on the next lines.
Of course, one can write more than one subroutine, as long as you prepend them with their name.
So, here's a valid example:
use Inline "Befunge";
hello();
__END__
__Befunge__
BF-sub hello
<q_,#! #:<"Hello world!"a
BF-sub add
+q
In this example, I defined two functions: hello()
and add()
, and you can call them from within perl as if they were valid perl functions.
Passing arguments
In order to write useful functions, one should be able to pass arguments. This is possible, and one will find the arguments on the TOSS of the interpreter: this means the stack may not be empty at the beginning of the run. If you're a purist, then you may ignore this and call your functions without arguments, and the stack will be empty.
Strings are pushed as 0gnirts.
/!\ Remember, Befunge works with a stack: the first argument will be the first pushed, ie, the deeper in the stack.
For example, when calling a inlined Befunge function like this: foo( 'bar', 7, 'baz' );
, then the stack will be (bottom->top): (0 114 97 98 7 0 122 97 98)
.
Return values
Furthermore, one can return some values from the befunge subroutines... how exciting!
To do so, the Befunge semantics have been a little adapted:
- o
-
when an Instruction Pointer reaches a
q
instruction, one cell will be popped from its stack and will be returned. This works like a scalar return. - o
-
when an Instruction Pointer reaches a
@
instruction, it will be killed (just as in traditional Befunge). But when the last IP reaches a@
, then the sub is exited and will return the whole stack of the IP that just died. This works like a list return./!\ Be careful, that you will return a list of integer values. You are to decide what to do with those values (especially, you are to convert back to characters with the
chr
perl builtin function and join the chars if you're waiting for a string!).
PUBLIC METHODS
register( )
Register as an Inline Language Support Module.
validate( )
Check the params for the Befunge interpreter. Currently no options are supported.
build( )
Register the Befunge as a valid Inline extension.
load( )
This function actually fetches the Befunge code. It first splits it to find the functions.
info( )
Return a small report about the Foo code.
AUTHOR
Jerome Quelin, <jquelin@cpan.org>
COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.