glados-0.1.0.0
Stabilitystable
Safe HaskellNone
LanguageHaskell2010

VM.Instruction.Arithmetic

Description

Handles basic arithmetic. WARNING: Operations are performed on the top two stack elements. Since the stack is LIFO, the first popped element is the Right-Hand Operand.

Synopsis

Documentation

instAdd :: VirtualMachine () Source #

Implements Addition (Opcode 0x10).

Stack: [a, b] -> [a + b]

instSub :: VirtualMachine () Source #

Implements Subtraction (Opcode 0x11).

Stack: [a, b] -> [a - b] (Note: b is popped first)

instMul :: VirtualMachine () Source #

Implements Multiplication (Opcode 0x12).

Stack: [a, b] -> [a * b]

instDiv :: VirtualMachine () Source #

Implements Division (Opcode 0x13).

Stack: [a, b] -> [a / b]

instMod :: VirtualMachine () Source #

Implements Modulo (Opcode 0x14).

Stack: [a, b] -> [a % b]

binaryInstInt :: (Int -> Int -> Int) -> VirtualMachine () Source #

Helper function for binary integer operations.

@args - fn: The Haskell binary function to apply (e.g., (+), (-)).

@details 1. Pops the Right Operand (v2). 2. Pops the Left Operand (v1). 3. Checks if both are integers. 4. Applies fn and pushes the result.

@throws Error if operands are not VInt.

binaryInstIntZero :: (Int -> Int -> Int) -> VirtualMachine () Source #

Helper function for division and modulo with Zero Check.

@args - fn: The Haskell binary function (div or mod).

@throws Error "VM Error: Division by Zero" if the divisor (v2) is 0.