Skip to content

Commit

Permalink
Merge pull request #33 from oqc-community/jd/pointer-ops
Browse files Browse the repository at this point in the history
Add operators for flexi-pointers
  • Loading branch information
chemix-lunacy authored Mar 20, 2024
2 parents ed958e4 + 3da65b1 commit eacf00c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/rasqal/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,15 +633,14 @@ impl QuantumRuntime {
let left = follow_reference(left, context);
let right = follow_reference(right, context);

// TODO: Make smart-pointers forward operators.
let result = Ptr::from(match op {
Operator::Multiply => left.deref() * right.deref(),
Operator::Divide => left.deref() / right.deref(),
Operator::Add => left.deref() + right.deref(),
Operator::Subtract => left.deref() - right.deref(),
Operator::Or => left.deref() | right.deref(),
Operator::And => left.deref() & right.deref(),
Operator::Xor => left.deref() ^ right.deref(),
Operator::Multiply => left * right,
Operator::Divide => left / right,
Operator::Add => left + right,
Operator::Subtract => left - right,
Operator::Or => left | right,
Operator::And => left & right,
Operator::Xor => left ^ right,
Operator::PowerOf => Value::from(left.as_int().pow(right.as_int() as u32))
});

Expand Down
91 changes: 90 additions & 1 deletion src/rasqal/src/smart_pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::cell::Cell;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::ops::{Add, BitAnd, BitOr, BitXor, Deref, DerefMut, Div, Index, Mul, Rem, Shl, Shr, Sub};
use std::panic::UnwindSafe;

pub type Ptr<T> = FlexiPtr<T>;
Expand Down Expand Up @@ -372,6 +372,95 @@ impl<T: ?Sized> From<&mut T> for FlexiPtr<T> {
fn from(value: &mut T) -> Self { FlexiPtr::Borrow(value) }
}

impl<T: Add> Add for FlexiPtr<T> where for<'a> &'a T: Add<&'a T, Output = T> {
type Output = T;

fn add(self, rhs: Self) -> Self::Output {
self.deref() + rhs.deref()
}
}


impl<T: Sub> Sub for FlexiPtr<T> where for<'a> &'a T: Sub<&'a T, Output = T> {
type Output = T;

fn sub(self, rhs: Self) -> Self::Output {
self.deref() - rhs.deref()
}
}

impl<T: Mul> Mul for FlexiPtr<T> where for<'a> &'a T: Mul<&'a T, Output = T> {
type Output = T;

fn mul(self, rhs: Self) -> Self::Output {
self.deref() * rhs.deref()
}
}

impl<T: Div> Div for FlexiPtr<T> where for<'a> &'a T: Div<&'a T, Output = T> {
type Output = T;

fn div(self, rhs: Self) -> Self::Output {
self.deref() / rhs.deref()
}
}

impl<T: BitOr> BitOr for FlexiPtr<T> where for<'a> &'a T: BitOr<&'a T, Output = T> {
type Output = T;

fn bitor(self, rhs: Self) -> Self::Output {
self.deref() | rhs.deref()
}
}

impl<T: BitAnd> BitAnd for FlexiPtr<T> where for<'a> &'a T: BitAnd<&'a T, Output = T> {
type Output = T;

fn bitand(self, rhs: Self) -> Self::Output {
self.deref() & rhs.deref()
}
}

impl<T: BitXor> BitXor for FlexiPtr<T> where for<'a> &'a T: BitXor<&'a T, Output = T> {
type Output = T;

fn bitxor(self, rhs: Self) -> Self::Output {
self.deref() ^ rhs.deref()
}
}

impl <T: Shl> Shl for FlexiPtr<T> where for<'a> &'a T: Shl<&'a T, Output = T> {
type Output = T;

fn shl(self, rhs: Self) -> Self::Output {
self.deref() << rhs.deref()
}
}

impl<T: Shr> Shr for FlexiPtr<T> where for<'a> &'a T: Shr<&'a T, Output = T> {
type Output = T;

fn shr(self, rhs: Self) -> Self::Output {
self.deref() >> rhs.deref()
}
}

impl<T: Rem> Rem for FlexiPtr<T> where for<'a> &'a T: Rem<&'a T, Output = T> {
type Output = T;

fn rem(self, rhs: Self) -> Self::Output {
self.deref() % rhs.deref()
}
}

impl<T, A: Index<A>> Index<A> for FlexiPtr<T> where T: Index<A> {
type Output = <T as Index<A>>::Output;

fn index(&self, index: A) -> &Self::Output {
&self.deref()[index]
}
}

#[cfg(test)]
mod tests {
use crate::smart_pointers::FlexiPtr;
Expand Down

0 comments on commit eacf00c

Please sign in to comment.