NAME
Polyglot - a little language interpreter
SYNOPSIS
# THIS IS ALPHA SOFTWARE
use Polyglot;
my $interpreter = Polyglot->new();
$polyglot->add_action( ... );
$interpreter->run();
DESCRIPTION
This module implements a simple, little language interpreter to describe the language.
For this interpreter, a program is a series of lines with one directive perl line. The first group of non-whitespace characters in the line is a the directive and the remainder of the line becomes its arguments. The interpreter reads one line, does what it says, then moves to the next line until it reaches the end of the file. If the interpreter does not read from a file, it prompts for standard input.
A small program to control an CD player may look like:
VOLUME 5
PLAY
SLEEP 50
STOP
EJECT
The interpreter does not support loops, conditionals, or other fancy things, and I do not have plans to add those things.
The interpret provides a few commands, but I expect other people to create their own little languages specialized for their task. Most of the methods deal with creating the language description at the interpreter level. I plan on creating another layer above this to make the language description even more simple.
Methods
- new
-
Creates a new Polyglot object and returns it.
- run
-
Start the interpreter. It will read lines from the file names in @ARGV or from standard input using the diamond operator. It splits lines on whitespace and assumes the first element of that list is the directive name. If the directive does not exist, it prints a warning and continues.
This method uses the diamond operator and assumes that nothing else has mucked with it.
- state
-
Returns the string used to mark a directive that affects the program state.
- action
-
Returns the string used to mark a directive that performs an action.
- add( DIRECTIVE, TYPE, CODEREF, INITIAL_VALUE, HELP )
-
Adds DIRECTIVE to the little language with TYPE (state or action). The value of the directive (for those that represent program state) is INITIAL_VALUE or undef. The CODEREF is executed when the interpreter encounters the directive. The built-in HELP directive returns the HELP string for this DIRECTIVE.
- value( DIRECTIVE [, VALUE ] )
-
Returns the value for DIRECTIVE, or sets it if you specify VALUE.
- add_action( DIRECTIVE, CODEREF, INITIAL_VALUE, HELP )
-
Like add(), but without TYPE which is automatically filled in. Use this for a directive that does something other than setting a value.
The CODEREF can be anything. The first argument is always the interpreter object, and the rest of the arguments are from the current line.
- add_state( DIRECTIVE, INITIAL_VALUE, HELP )
-
Like add(), but without TYPE and CODEREF which is automatically filled in. Use this for a directive that can set a value.
- add_toggle( DIRECTIVE, INITIAL_VALUE, HELP )
-
Like add(), but without TYPE and CODEREF which is automatically filled in. Use this for a value that can be either "on" or "off".
- help
-
Returns a help message: you want to override this.
- directives
-
Returns a list of directives.
POLYGLOT LANGUAGES
At the moment you are stuck with the examples and examining the source.
Built in directives
The Polyglot module provides some basic directives.
- POLYGLOT PACKAGE
-
Load a Perl package.
- SHOW DIRECTIVE
-
Displays the value of DIRECTIVE
- DUMP
-
Displays all of the "state" DIRECTIVES with their values
- REFLECT
-
Displays the Polyglot object
- HELP DIRECTIVE
-
Displays the help message for DIRECTIVE
TO DO
* I should really make all of these methods class methods that access a Singleton object stored as class data.
SOURCE AVAILABILITY
The source is in GitHub:
https://github.com/briandfoy/polyglot
AUTHOR
brian d foy, <briandfoy@pobox.com>
.
COPYRIGHT AND LICENSE
Copyright © 2002-2024, brian d foy <briandfoy@pobox.com>. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.