Skip to the content.

Executor

Extensible stack based virtual machine

Basic Usage

The code below executes instructions that compute the result of 20 multiplied by 20

GroupState groupState = new GroupState();
groupState.Group.Instructions = new[] {
  InstructionProvider<GroupState>.GetInstruction(InstructionCode.VR, new object[] { 20 }),
  InstructionProvider<GroupState>.GetInstruction(InstructionCode.CPHR),
  InstructionProvider<GroupState>.GetInstruction(InstructionCode.RMultiply)
};
InstructionExecutor<GroupState> executor = new InstructionExecutor<GroupState>();
executor.Execute(new ExecutionState<GroupState>(groupState)); 

Here’s a breakdown of some of the classes used above:
GroupState - Represents a unit of instructions and value pointers. One of the main ways OOP can be achieves in the engine.
InstructionCode - Enum representing the opcode of the instruction. See all Instruction Codes and usage.
InstructionProvider - Factory for creating Instructions for a specified InstructionCode and payload that the engine can execute.
ExecutionState - The execution context, providing access to the stack, register, and context specific interrupts.
InstructionExecutor - Executes instructions and provides access to hooks in order to extend or interop with the engine.

Modifying Value and Operating Handling

Although the engine is primarily built for dynamic typing, all values are converted to a typed representation so that operations can be performed on them. The classes located in Synfron.Staxe.Executor.Values provide the engine’s default value handling functionality. Implementations of IValueProvider can be registered with the InstructionExecutor. This is used by the engine to create IValuables depending on the type of the value. All operations between values (addition, multiplication, etc.) are implemented by the value type specific IValuable implementation.

Debugger Hooks

Debuggers can interop with the engine using Interrupts. Based on the condition used by the built-in or custom Interrupts, if an Instruction is marked as interruptable, InterruptHandlers set on the InstructionExecutor will be called before the Instruction is executed.