| Stability | stable |
|---|---|
| Safe Haskell | None |
| Language | Haskell2010 |
VM.Instruction.Stack
Description
Handles instructions that modify the stack structure itself (PUSH, POP, DUP).
This module relies on BytecodeReader to decode operands of varying sizes.
Synopsis
- instPush :: VirtualMachine ()
- instPop :: VirtualMachine ()
- instDup :: VirtualMachine ()
- instSwap :: VirtualMachine ()
- instCast :: VirtualMachine ()
- instTypeOf :: VirtualMachine ()
- instSizeOf :: VirtualMachine ()
Documentation
instPush :: VirtualMachine () Source #
Implements the PUSH instruction (Opcode 0x01).
@details
This is the entry point for the PUSH operation. It performs the following steps:
1. Reads the **TypeID** byte from the bytecode stream.
2. Delegates the reading of the value and the stack push to stackPushValue.
This separation allows for a cleaner implementation and easier extension of supported types in the future.
instPop :: VirtualMachine () Source #
Implements the POP instruction (Opcode 0x02).
@details Removes the top element of the stack and discards it. Commonly used to clean up the stack after an expression evaluation where the result is not needed (e.g., `func();` where return is ignored).
@throws Error "Stack Underflow" if the stack is empty.
instDup :: VirtualMachine () Source #
Implements the DUP instruction (Opcode 0x03).
@details Duplicates the top element of the stack. Useful for operations that consume a value but need to preserve it for a subsequent operation. State change: `[..., A] -> [..., A, A]`
@throws Error "Stack Underflow" if the stack is empty.
instSwap :: VirtualMachine () Source #
Implements the SWAP instruction (Opcode 0x04).
@details Swaps the top two elements of the stack. Stack: [..., A, B] -> [..., B, A]
instCast :: VirtualMachine () Source #
Implements CAST (Opcode 0x80).
@details Reads a TypeID and tries to convert the top of stack to that type.
instTypeOf :: VirtualMachine () Source #
Implements TYPEOF (Opcode 0x81).
@details Pops a value from the stack, determines its runtime type, and pushes a string representation (e.g., "int", "list").
Stack: [value] -> ["type_name"]
instSizeOf :: VirtualMachine () Source #
Implements SIZEOF (Opcode 0x82).
@details Pops a value and pushes its length. - List: Number of elements. - Struct: Number of fields. - Others: 0 (or error, depending on strictness choice).
@throws Error if the value is not a container (List/Struct).