NAME
Language::Nouse - Perl interpreter for the nouse language
SYNOPSIS
use Language::Nouse;
# create a new interpreter
my $nouse = new Language::Nouse;
# load a linenoise formatted program
$nouse->load_linenoise( '#0<a>0:0#0>e>0:0#0>f>0>0:0#0^f>0:0#0+4>0:0#0#h>0:0#0^f>0:0#0<g>0:0#0>f' );
$nouse->load_linenoise( '>0:0#0<e>0:0#0?4>0:0#0^1>0:0#0>1>0:0^0' );
# display the loaded program in assembly mode, with 5 ops per line
print $nouse->get_assembly( 5 );
# clear the ring/stack
$nouse->clear();
# load an assembly formatted program
$nouse->load_assembly( 'read 0, write 6, swap 0, test 2, add 1' );
# run a single step of the program
$nouse->step();
# display the loaded program in linenoise mode
# NOTE: the previous step() call may have chaged the program in the ring!
print $nouse->get_linenoise();
# run the program to completion (until the ring is empty)
$nouse->run();
DESCRIPTION
The Language::Nouse module is an interpreter for the "nouse" language. It allows you to load and save nouse programs in both linenoise and assembly format, single step and run through code.
IO is handled via two callbacks. By default these simply "print" the output and "getc" for input, but can be configured to use custom routines.
The internals of the execution engine contain a whole bunch of microcode methods with are used to run the interpreter. The more adventurous can use these methods to finely control the engine and inspect the ring and stack.
METHODS
new()
-
Creates and returns a new interpreter. The ring and stack are both empty and the ring pointer is set to zero.
set_get( $code_ref )
-
Sets the IO get routine. This routine must return a character when called, or Undef for the end of available IO.
set_put( $code_ref )
-
Sets the IO put routine. This routine is passed a single argument containing a single char to output.
load_linenoise( $source_code )
-
Loads a program into the ring, formatted in linenoise mode. If the ring is not empty, the program is appended to the end of the ring (that is, in the position just *before* the ring pointer).
load_assembly( $source_code )
-
Loads a program into the ring, formatted in assemblye mode. If the ring is not empty, the program is appended to the end of the ring (that is, in the position just *before* the ring pointer).
clear()
-
Clears the ring and the stack and resets the ring pointer to zero. After a call to
clear()
, the interpreter is in the same state as when it was created. get_linenoise()
-
Returns a scalar containing the current program, formatted in linenoise mode.
get_assembly()
get_assembly( $ops_per_line )
-
Returns a scalar containing the current program, formatted in assembly mode. The optional argument specifies how many ops to show per line. The code is formatted with commas to seperate ops and, by default, 4 ops per line, which are seperated with newlines.
step()
-
Runs the program for a single step - that is, it executes the op currently under the ring pointer.
run()
-
Calls
step()
until the ring is empty.
AUTHOR
Copyright (C) 2003 Cal Henderson, <cal@iamcal.com>