Introduction
StackLanguage is a generic programming system that uses a Last-in-First-out stack to perform calculations. Instructios are kept in a string object and compiled into a tuple of instructions. Programs are written using Reverse Polish Notation.
You can download the StackLanguage package here.
Contents of StackLanguage
- The Stack class
- This class contains the basic LIFO stack
- The StackLanguage class
- This class is the workhorse class of StackLanguage. Class objects contain the rules for the language, compiles code, and runs it.
- The StackLanguageLibrary class
- This class allows you to create libraries of rules and class methods tha can be used by StackLanguage objects.
- SampleLibraries
- These are the basic libraries that are ready for import
- Errors
- This is a list and description of the errors that can be raised by StackLanguage.
Quick Tutorial
Rules
StackLanguage governs the way it compiles and runs code by a set of 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.
Registers
Registers hold values. The Resisters attribute is a dictionary object. The keys needs to be alpha characters. Storing a value to a register uses an apostrophe after the register name:
3 n'
Stores 3 into the register ‘N’ (names are capitalized automatically) and leave nothing on the stack.
Recalling a register only requires the register name on the stack.
3 n' 1 n plus
Leaves 4 on the stack.
StackLanguage programs are written using reverse polish notation. RPN notation puts the operation after the operands. Normally mathematical operations go inbetween the operands. For example, instead of writing “1 + 2” you write “1 2 +". As each instruction is read the item is pushed onto the stack or it’s an operation. An operation will pop, or remove, items from the stack and push something new onto the stack. For example
- Instruction: ‘1’ Resultant Stack: [‘1’]
- Instruction: ‘2’ Resultant Stack: [‘1’, ‘2’]
- Instruction: '+' Resultant Stack: [‘3’]
Some operations will only pop 1 item, others pop 3 and conceivably an operation will pop 4 or more items from the stack. With the rule example above, the instructions “1 2 add” or “1 2 plus” would do the exact same thing to the stack.
Unit Tests
When you download the StackLang package, a separate file of unit tests is included. This file contains 56 test items that I use to maintain and refactor code.