PGA - ProGram Algebra

MSP with eval/apply

MSPea is an extension of MSP with an eval and apply that enables execution of arbitrary code.

Additional instructions

compile extfocus
Compiles the string selected by extfcous into a molecule and assigns it to extfcous. It is capable of compiling strings representing programs in the primitive instruction set PGLA with MSP as basic instruction set.
Returns false when there are errors detected.
apply extfocus
If extfocus is a string, it is executed as a basic instruction and returns the value returned by the basic instruction. It fails otherwise.
eval extfocus
If extfocus is a string, it compiles the string into a molecule, after which the molecule is evaluated. If extfocus is an atom, it is evaluated. It fails otherwise.
The returned value of eval is the returned value of the last evaluated basic instruction.

Molecular representation of code

Compilation results in a molecule that represents a null-terminated list of instructions connected by next fields. The instructions are represented by an atom with additional fields. The possible instructions are:
end
This instruction consists of an atom with an end field, and stops the evaluation.
goto
Consists of an atom with a goto field which selects the next instruction to be evaluated.
test
Consists of an atom with the fields test, basic, T, and F. On the returned value of the basic instruction, selected by the basic field, either the T (true) or the F (false) field selects the next instruction to be evaluated.
basic
Consists of an atom with the field basic which selects a basic instructions to be evaluated. Evaluation continues with the next instruction in the list.
The basic instructions are represented by a string. For compilation the basic instruction may be anything, but for evaluation it has to be a MSP instruction. In evaluation, if a string is not recognized as a MSP instruction false is returned as the result of this instruction.

Example

The following program counts until 10 and then stops.
x = 0;
incr x;
+ x == 10;
!;
\\#3

This can be put in a string and assigned to a focus with the instruction

count = "x = 0; incr x; + x == 10; !; \\\\#3"

Compilation of count results in the molecule