From b5b317df95daf54d64bbe2b9bd3f0e5becd27522 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 11 Feb 2024 14:10:11 -0500 Subject: [PATCH] Implement block contexts --- src/abstract_tree/block.rs | 2 + src/context.rs | 89 +++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index acf4d0d..872554d 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -20,6 +20,8 @@ use crate::{ pub struct Block { is_async: bool, statements: Vec, + + #[serde(skip)] context: Context, } diff --git a/src/context.rs b/src/context.rs index 0e15e69..f09f283 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,11 +1,10 @@ use std::{ - collections::HashMap, + cmp::Ordering, + collections::BTreeMap, fmt::{self, Debug, Display, Formatter}, sync::{Arc, RwLock, RwLockReadGuard}, }; -use serde::{Deserialize, Serialize}; - use crate::{error::rw_lock_error::RwLockError, Type, Value}; #[derive(Clone)] @@ -23,7 +22,29 @@ impl Eq for ValueData {} impl PartialEq for ValueData { fn eq(&self, other: &Self) -> bool { - todo!() + match (self, other) { + ( + ValueData::Value { + inner: left_inner, + runtime_uses: left_runtime_uses, + }, + ValueData::Value { + inner: right_inner, + runtime_uses: right_runtime_uses, + }, + ) => { + if left_inner != right_inner { + return false; + } else { + *left_runtime_uses.read().unwrap() == *right_runtime_uses.read().unwrap() + } + } + ( + ValueData::ExpectedType { inner: left_inner }, + ValueData::ExpectedType { inner: right_inner }, + ) => left_inner == right_inner, + _ => false, + } } } @@ -34,29 +55,46 @@ impl PartialOrd for ValueData { } impl Ord for ValueData { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - todo!() + fn cmp(&self, other: &Self) -> Ordering { + use Ordering::*; + + match (self, other) { + ( + ValueData::Value { + inner: inner_left, .. + }, + ValueData::Value { + inner: inner_right, .. + }, + ) => inner_left.cmp(inner_right), + (ValueData::Value { .. }, _) => Greater, + ( + ValueData::ExpectedType { inner: inner_left }, + ValueData::ExpectedType { inner: inner_right }, + ) => inner_left.cmp(inner_right), + (ValueData::ExpectedType { .. }, _) => Less, + } } } #[derive(Clone)] pub struct Context { - inner: Arc>>, + inner: Arc>>, } impl Context { pub fn new() -> Self { Self { - inner: Arc::new(RwLock::new(HashMap::new())), + inner: Arc::new(RwLock::new(BTreeMap::new())), } } - pub fn inner(&self) -> Result>, RwLockError> { + pub fn inner(&self) -> Result>, RwLockError> { Ok(self.inner.read()?) } pub fn inherit_from(other: &Context) -> Result { - let mut new_variables = HashMap::new(); + let mut new_variables = BTreeMap::new(); for (identifier, value_data) in other.inner.read()?.iter() { new_variables.insert(identifier.clone(), value_data.clone()); @@ -111,6 +149,12 @@ impl Context { } } +impl Default for Context { + fn default() -> Self { + Context::new() + } +} + impl Eq for Context {} impl PartialEq for Context { @@ -141,26 +185,11 @@ impl PartialOrd for Context { } impl Ord for Context { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - todo!() - } -} + fn cmp(&self, other: &Self) -> Ordering { + let left = self.inner().unwrap(); + let right = other.inner().unwrap(); -impl<'de> Deserialize<'de> for Context { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - todo!() - } -} - -impl Serialize for Context { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - todo!() + left.cmp(&right) } } @@ -171,7 +200,7 @@ impl Debug for Context { } impl Display for Context { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { todo!() } }