Struct ArithmeticHandler

Source
pub struct ArithmeticHandler;
Expand description

Handles arithmetic operations for the VM

Implementations§

Source§

impl ArithmeticHandler

Source

pub fn add<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Adds the top two values on the stack

Pops two values from the stack, adds them, and pushes the result. Supports numeric addition and string concatenation.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if insufficient operands
§Examples
let mut stack = MyStack::new();
stack.push(Value::Number(3.0));
stack.push(Value::Number(5.0));
ArithmeticHandler::add(&mut stack)?;
assert_eq!(stack.pop(), Some(Value::Number(8.0)));
Source

pub fn subtract<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Subtracts the second value from the first value on the stack

Pops two values from the stack, subtracts the second from the first, and pushes the result.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if insufficient operands
Source

pub fn multiply<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Multiplies the top two values on the stack

Pops two values from the stack, multiplies them, and pushes the result.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if insufficient operands
Source

pub fn divide<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Divides the first value by the second value on the stack

Pops two values from the stack, divides the first by the second, and pushes the result. Handles division by zero.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if insufficient operands
  • Err(ExecutionError::RuntimeError) on division by zero
Source

pub fn modulo<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Computes the remainder of division

Pops two values from the stack, computes a % b, and pushes the result.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if insufficient operands
  • Err(ExecutionError::RuntimeError) on division by zero
Source

pub fn power<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Computes the power of the first value raised to the second value

Pops two values from the stack, computes a^b, and pushes the result.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if insufficient operands
Source

pub fn negate<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Negates the top value on the stack

Pops one value from the stack, negates it, and pushes the result.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if stack is empty
Source

pub fn increment<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Increments the top value on the stack by 1

Pops one value from the stack, adds 1, and pushes the result.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if stack is empty
Source

pub fn decrement<S>(stack: &mut S) -> Result<(), ExecutionError>
where S: StackOperations,

Decrements the top value on the stack by 1

Pops one value from the stack, subtracts 1, and pushes the result.

§Arguments
  • stack - The stack to operate on
§Returns
  • Ok(()) on success
  • Err(ExecutionError::StackUnderflow) if stack is empty

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.