Polyglot - a little language interpreter
# THIS IS ALPHA SOFTWARE use Polyglot; my $interpreter = Polyglot->new(); $polyglot->add_action( ... ); $interpreter->run();
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.
Creates a new Polyglot object and returns it.
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.
Returns the string used to mark a directive that affects the program state.
Returns the string used to mark a directive that performs an action.
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.
Returns the value for DIRECTIVE, or sets it if you specify VALUE.
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.
Like add(), but without TYPE and CODEREF which is automatically filled in. Use this for a directive that can set a value.
Like add(), but without TYPE and CODEREF which is automatically filled in. Use this for a value that can be either "on" or "off".
Returns a help message: you want to override this.
Returns a list of directives.
At the moment you are stuck with the examples and examining the source.
The Polyglot module provides some basic directives.
Load a Perl package.
Displays the value of DIRECTIVE
Displays all of the "state" DIRECTIVES with their values
Displays the Polyglot object
Displays the help message for DIRECTIVE
* i should really make all of these methods class methods that access a Singleton object stored as class data.
The source is in GitHub:
brian d foy,
Copyright © 2002-2017, brian d foy <firstname.lastname@example.org>. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.