Skip to content

Commit

Permalink
Assess that the type is correct
Browse files Browse the repository at this point in the history
  • Loading branch information
ricglz committed Apr 30, 2022
1 parent 4495a19 commit c77695f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ impl Memory {
}
}

fn get_index(&self, address: usize) -> usize {
fn get_index(&self, address: usize) -> (usize, Types) {
let (contextless_address, _, address_type) = get_address_info(address, self.base);
let type_index = contextless_address % THRESHOLD;
let pointer = match address_type {
Expand All @@ -299,16 +299,17 @@ impl Memory {
Types::BOOL => self.bool_pointer,
data_type => unreachable!("{:?}", data_type),
};
type_index + pointer
(type_index + pointer, address_type)
}

pub fn get(&self, address: usize) -> Option<VariableValue> {
let index = self.get_index(address);
let index = self.get_index(address).0;
self.space.get(index).unwrap().to_owned()
}

pub fn write(&mut self, address: usize, value: VariableValue) {
let index = self.get_index(address);
pub fn write(&mut self, address: usize, uncast: VariableValue) {
let (index, address_type) = self.get_index(address);
let value = uncast.cast_to(address_type);
*self.space.get_mut(index).unwrap() = Some(value);
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/dir_func/variable_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ impl VariableValue {
_ => false,
}
}

#[inline]
fn cast_to_bool(&self) -> VariableValue {
Self::Bool(bool::from(self))
}

#[inline]
fn cast_to_float(&self) -> VariableValue {
Self::Float(f64::from(self))
}

pub fn cast_to(&self, to: Types) -> VariableValue {
match to {
Types::BOOL => self.cast_to_bool(),
Types::FLOAT => self.cast_to_float(),
_ => self.clone(),
}
}
}

impl From<&VariableValue> for Types {
Expand Down Expand Up @@ -70,6 +88,12 @@ impl From<VariableValue> for bool {
}
}

impl From<&VariableValue> for bool {
fn from(v: &VariableValue) -> Self {
Self::from(v.to_owned())
}
}

impl fmt::Debug for VariableValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let value = match self {
Expand Down

0 comments on commit c77695f

Please sign in to comment.