Introduction
The StackLanguage class sets rules, compiles programs, and runs ‘compiled’ code. When StackLanguage compiles code it returns a tuple of string objects that it can then process. It does not compile a program into machine-executable code.
StackLanguage objects compile and run programs according to rules. A rule is defined as a tuple of three items:
(['plus','add','+'],'do_add','+')
The first item is a list of keywords that, when encountered in running the program, trigger the procedure named by the second item. The third item is optional, but this will be passed to the procedure named by the second item. You can add a Rule-Tuple to a StackLanguage object with the AddRule method. The items of the list must be string objects, and the second and third items of the tuple must also be string objects.
Attributes
Stack
A Stack object to store the results of the program.
Rules
A list of rules (see above) that describe how the language works. Every language has one rule defined: ([‘END’],‘Terminate’,None) and has a Terminate method.
Methods
LanguageReset ()
Resets the rules and reserved words
AddRule ((tuple))
AddRule adds a rule tuple to the Rules list. If the tuple does not comply with the rules set out above, StackLanguage will raise exceptions that describe what it doesn’t like about the tuple.
PresetRegister (keyword arguments)
This sets Register values before compilation. This can be handy if you want to let the language run knowing a few things already.
Program ()
This returns the compiled program as a tuple whose elements are single string objects. Each item in the tuple is refered to as an ‘instruction’.
Feed (text)
Feeds text into the language object and automatically parses the new text. This will override the old program
Reset ()
The Reset method prepares the language object to run the program again. This does not reset the registers.
Run ()
This method runs the program until it terminates by coming to an ‘end’ instruction. programs that do not end will run forever.
Step ()
This method processes one instruction of the program.
ImportLibrary (lib)
This method adds rules and methods to the library. See the Libraries and Sample Library pages for examples.
Terminate ()
This method stops the program from running. Whatever is on the stack is the result of the program. Further calls to Step or Run will have no effect until the Reset method is called.
GoToInstruction(integer)
This sets the next instruction number to be processed. If passed an instruction number outside the range of the program it will not raise an error, but an error will be raised when that line is attempted to be processed by the Step or Run method.
Using StackLanguage
The StackLanguage class has little of it’s own to offer. You can read and write to registers, end the program, and add items to the stack. In order to add capabilities to the language, you need to subsclass the StackLanguage class and import a few libraries:
import StackLang class SlugFestLanguage(StackLang.StackLanguage): def __init__(self): StackLang.StackLanguage.__init__(self,'SlugFest',0.1) self.ImportLibrary(StackLang.FiveFunctionLibrary) self.ImportLibrary(StackLang.ComparisonsLibrary) self.ImportLibrary(StackLang.ControlOperationsLibrary) self.AddRule(['random'],'do_random') def do_random(self,caller): """adds a random number onto the stack""" from random import randint self.Stack.push(str(randint(0,99)))
This example is used in the Slugfest Tutorial . For information about the imported libraries, see the Sample Library page. This class also creates the ‘do_random’ function that can be called when the ‘RANDOM’ instruction is encountered in the program.
Future versions of StackLanguage may include the sample libraries natively and they wont be necessary to import. Currently the goal of StackLang is to allow the user to create their own customized RPN programming language.