2024-08-09 01:59:09 +00:00
|
|
|
//! Dust value representation
|
2024-02-25 18:49:26 +00:00
|
|
|
use std::{
|
2024-02-25 19:26:22 +00:00
|
|
|
cmp::Ordering,
|
2024-08-17 08:06:13 +00:00
|
|
|
collections::HashMap,
|
2024-08-09 00:58:56 +00:00
|
|
|
error::Error,
|
2024-03-06 20:36:58 +00:00
|
|
|
fmt::{self, Display, Formatter},
|
2024-08-16 15:21:20 +00:00
|
|
|
ops::{Range, RangeInclusive},
|
2024-08-17 02:43:29 +00:00
|
|
|
sync::{Arc, RwLock},
|
2024-02-25 18:49:26 +00:00
|
|
|
};
|
|
|
|
|
2024-06-19 07:32:51 +00:00
|
|
|
use serde::{
|
2024-08-17 02:43:29 +00:00
|
|
|
de::{self, MapAccess, Visitor},
|
|
|
|
ser::{SerializeStruct, SerializeStructVariant},
|
2024-08-14 05:03:46 +00:00
|
|
|
Deserialize, Deserializer, Serialize, Serializer,
|
2024-06-19 07:32:51 +00:00
|
|
|
};
|
2024-06-04 18:47:15 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
use crate::{
|
2024-08-17 09:32:18 +00:00
|
|
|
AbstractSyntaxTree, Context, EnumType, FieldsStructType, FunctionType, Identifier,
|
|
|
|
RuntimeError, StructType, TupleType, Type, Vm,
|
2024-08-16 21:07:49 +00:00
|
|
|
};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
2024-08-07 23:12:40 +00:00
|
|
|
/// Dust value representation
|
|
|
|
///
|
|
|
|
/// Each type of value has a corresponding constructor, here are some simple examples:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use dust_lang::Value;
|
|
|
|
/// let boolean = Value::boolean(true);
|
|
|
|
/// let float = Value::float(3.14);
|
|
|
|
/// let integer = Value::integer(42);
|
|
|
|
/// let string = Value::string("Hello, world!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Values can be combined into more complex values:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use dust_lang::Value;
|
|
|
|
/// let list = Value::list(vec![
|
|
|
|
/// Value::integer(1),
|
|
|
|
/// Value::integer(2),
|
|
|
|
/// Value::integer(3),
|
|
|
|
/// ]);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Values have a type, which can be retrieved using the `type` method:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use std::collections::HashMap;
|
2024-08-10 08:32:27 +00:00
|
|
|
/// # use dust_lang::*;
|
2024-08-07 23:12:40 +00:00
|
|
|
/// let value = Value::integer(42);
|
|
|
|
///
|
2024-08-13 16:23:25 +00:00
|
|
|
/// assert_eq!(value.r#type(), Type::Integer);
|
2024-08-07 23:12:40 +00:00
|
|
|
/// ```
|
2024-08-14 05:03:46 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Value {
|
2024-08-16 21:07:49 +00:00
|
|
|
Boolean(bool),
|
|
|
|
Byte(u8),
|
|
|
|
Character(char),
|
|
|
|
Enum { name: Identifier, r#type: EnumType },
|
|
|
|
Float(f64),
|
2024-08-17 02:43:29 +00:00
|
|
|
Function(Function),
|
2024-08-16 21:07:49 +00:00
|
|
|
Integer(i64),
|
|
|
|
List(Vec<Value>),
|
|
|
|
Mutable(Arc<RwLock<Value>>),
|
|
|
|
Range(Range<Rangeable>),
|
|
|
|
RangeInclusive(RangeInclusive<Rangeable>),
|
|
|
|
String(String),
|
|
|
|
Struct(Struct),
|
|
|
|
Tuple(Vec<Value>),
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
impl Value {
|
2024-08-17 02:43:29 +00:00
|
|
|
pub fn mutable(value: Value) -> Value {
|
|
|
|
Value::Mutable(Arc::new(RwLock::new(value)))
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
pub fn byte_range(start: u8, end: u8) -> Value {
|
|
|
|
Value::Range(Rangeable::Byte(start)..Rangeable::Byte(end))
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
pub fn byte_range_inclusive(start: u8, end: u8) -> Value {
|
|
|
|
Value::RangeInclusive(Rangeable::Byte(start)..=Rangeable::Byte(end))
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
pub fn character_range(start: char, end: char) -> Value {
|
|
|
|
Value::Range(Rangeable::Character(start)..Rangeable::Character(end))
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
pub fn character_range_inclusive(start: char, end: char) -> Value {
|
|
|
|
Value::RangeInclusive(Rangeable::Character(start)..=Rangeable::Character(end))
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
pub fn float_range(start: f64, end: f64) -> Value {
|
|
|
|
Value::Range(Rangeable::Float(start)..Rangeable::Float(end))
|
2024-08-13 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
pub fn float_range_inclusive(start: f64, end: f64) -> Value {
|
|
|
|
Value::RangeInclusive(Rangeable::Float(start)..=Rangeable::Float(end))
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
pub fn integer_range(start: i64, end: i64) -> Value {
|
|
|
|
Value::Range(Rangeable::Integer(start)..Rangeable::Integer(end))
|
2024-08-14 05:13:43 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
pub fn integer_range_inclusive(start: i64, end: i64) -> Value {
|
|
|
|
Value::RangeInclusive(Rangeable::Integer(start)..=Rangeable::Integer(end))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn string<T: ToString>(to_string: T) -> Value {
|
|
|
|
Value::String(to_string.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_boolean(&self) -> Option<bool> {
|
2024-08-16 21:07:49 +00:00
|
|
|
match self {
|
2024-08-17 02:43:29 +00:00
|
|
|
Value::Boolean(value) => Some(*value),
|
|
|
|
Value::Mutable(locked) => locked.read().unwrap().as_boolean(),
|
|
|
|
_ => None,
|
2024-08-16 21:07:49 +00:00
|
|
|
}
|
2024-08-14 08:59:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
pub fn as_byte(&self) -> Option<u8> {
|
|
|
|
match self {
|
|
|
|
Value::Byte(value) => Some(*value),
|
|
|
|
Value::Mutable(locked) => locked.read().unwrap().as_byte(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_character(&self) -> Option<char> {
|
|
|
|
match self {
|
|
|
|
Value::Character(value) => Some(*value),
|
|
|
|
Value::Mutable(locked) => locked.read().unwrap().as_character(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_float(&self) -> Option<f64> {
|
|
|
|
match self {
|
|
|
|
Value::Float(value) => Some(*value),
|
|
|
|
Value::Mutable(locked) => locked.read().unwrap().as_float(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_integer(&self) -> Option<i64> {
|
|
|
|
match self {
|
|
|
|
Value::Integer(value) => Some(*value),
|
|
|
|
Value::Mutable(locked) => locked.read().unwrap().as_integer(),
|
|
|
|
_ => None,
|
|
|
|
}
|
2024-08-14 08:59:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
pub fn as_mutable(&self) -> Result<&Arc<RwLock<Value>>, ValueError> {
|
|
|
|
match self {
|
|
|
|
Value::Mutable(inner) => Ok(inner),
|
|
|
|
_ => Err(ValueError::CannotMutate(self.clone())),
|
2024-08-14 07:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
pub fn into_mutable(self) -> Value {
|
|
|
|
match self {
|
|
|
|
Value::Mutable(_) => self,
|
|
|
|
immutable => Value::Mutable(Arc::new(RwLock::new(immutable))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_mutable(&self) -> bool {
|
|
|
|
matches!(self, Value::Mutable(_))
|
|
|
|
}
|
|
|
|
|
2024-08-16 10:43:29 +00:00
|
|
|
pub fn mutate(&self, other: Value) -> Result<(), ValueError> {
|
2024-08-16 21:07:49 +00:00
|
|
|
match self {
|
|
|
|
Value::Mutable(inner) => *inner.write().unwrap() = other,
|
|
|
|
_ => return Err(ValueError::CannotMutate(self.clone())),
|
2024-08-14 07:53:15 +00:00
|
|
|
};
|
|
|
|
|
2024-08-16 10:43:29 +00:00
|
|
|
Ok(())
|
2024-08-14 07:53:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 16:23:25 +00:00
|
|
|
pub fn r#type(&self) -> Type {
|
2024-08-14 05:03:46 +00:00
|
|
|
match self {
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Boolean(_) => Type::Boolean,
|
|
|
|
Value::Byte(_) => Type::Byte,
|
|
|
|
Value::Character(_) => Type::Character,
|
|
|
|
Value::Enum { r#type, .. } => Type::Enum(r#type.clone()),
|
|
|
|
Value::Float(_) => Type::Float,
|
|
|
|
Value::Function(function) => Type::Function(function.r#type.clone()),
|
|
|
|
Value::Integer(_) => Type::Integer,
|
|
|
|
Value::List(values) => {
|
|
|
|
let item_type = values.first().unwrap().r#type();
|
2024-03-07 17:29:07 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
Type::List {
|
|
|
|
item_type: Box::new(item_type),
|
|
|
|
length: values.len(),
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Mutable(locked) => locked.read().unwrap().r#type(),
|
|
|
|
Value::Range(_) => Type::Range,
|
|
|
|
Value::RangeInclusive(_) => Type::Range,
|
|
|
|
Value::String(_) => Type::String,
|
|
|
|
Value::Struct(r#struct) => match r#struct {
|
2024-08-17 08:06:13 +00:00
|
|
|
Struct::Unit { .. } => Type::Struct(StructType::Unit),
|
|
|
|
Struct::Tuple { fields, .. } => {
|
|
|
|
let types = fields.iter().map(|field| field.r#type()).collect();
|
|
|
|
|
|
|
|
Type::Struct(StructType::Tuple(TupleType { fields: types }))
|
|
|
|
}
|
|
|
|
Struct::Fields { fields, .. } => {
|
|
|
|
let types = fields
|
|
|
|
.iter()
|
|
|
|
.map(|(identifier, value)| (identifier.clone(), value.r#type()))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Type::Struct(StructType::Fields(FieldsStructType { fields: types }))
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
},
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Tuple(values) => {
|
2024-08-17 08:06:13 +00:00
|
|
|
let fields = values.iter().map(|value| value.r#type()).collect();
|
2024-08-05 02:15:31 +00:00
|
|
|
|
2024-08-17 08:06:13 +00:00
|
|
|
Type::Tuple(TupleType { fields })
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
2024-08-12 14:43:18 +00:00
|
|
|
}
|
2024-08-16 03:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_field(&self, field: &Identifier) -> Option<Value> {
|
2024-08-16 09:14:00 +00:00
|
|
|
match self {
|
2024-08-17 02:43:29 +00:00
|
|
|
Value::Mutable(inner) => inner.read().unwrap().get_field(field),
|
2024-08-17 08:06:13 +00:00
|
|
|
Value::Struct(Struct::Fields { fields, .. }) => fields.get(field).cloned(),
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => None,
|
2024-08-16 09:14:00 +00:00
|
|
|
}
|
2024-08-16 03:17:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_index(&self, index: usize) -> Option<Value> {
|
2024-08-16 09:14:00 +00:00
|
|
|
match self {
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::List(values) => values.get(index).cloned(),
|
2024-08-16 03:17:49 +00:00
|
|
|
Value::Mutable(inner) => inner.read().unwrap().get_index(index),
|
2024-08-17 08:06:13 +00:00
|
|
|
Value::Struct(Struct::Tuple { fields, .. }) => fields.get(index).cloned(),
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => None,
|
2024-08-16 09:14:00 +00:00
|
|
|
}
|
2024-08-16 03:17:49 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 02:15:31 +00:00
|
|
|
pub fn add(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Float(left + right)),
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
Ok(Value::Integer(left.saturating_add(*right)))
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::String(left), Value::String(right)) => {
|
|
|
|
Ok(Value::String(format!("{}{}", left, right)))
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.add(&right)
|
2024-08-09 04:49:17 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotAdd(self.clone(), other.clone())),
|
2024-08-05 02:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-09 03:28:47 +00:00
|
|
|
|
2024-08-16 13:22:36 +00:00
|
|
|
pub fn add_assign(&self, other: &Value) -> Result<(), ValueError> {
|
2024-08-14 08:59:27 +00:00
|
|
|
match (self, other) {
|
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
|
|
|
match (&mut *left.write().unwrap(), &*right.read().unwrap()) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
2024-08-14 08:59:27 +00:00
|
|
|
*left += right;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
2024-08-14 08:59:27 +00:00
|
|
|
*left = left.saturating_add(*right);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::String(left), Value::String(right)) => {
|
|
|
|
(*left).push_str(right);
|
2024-08-14 08:59:27 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Mutable(left), right) => match (&mut *left.write().unwrap(), right) {
|
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
|
|
|
*left += right;
|
|
|
|
return Ok(());
|
2024-08-14 08:59:27 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
*left = left.saturating_add(*right);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
(Value::String(left), Value::String(right)) => {
|
|
|
|
left.push_str(right);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
_ => {}
|
2024-08-14 08:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Err(ValueError::CannotAdd(self.clone(), other.clone()))
|
|
|
|
}
|
|
|
|
|
2024-08-09 03:28:47 +00:00
|
|
|
pub fn subtract(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Float(left - right)),
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
Ok(Value::Integer(left.saturating_sub(*right)))
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.subtract(&right)
|
2024-08-09 03:28:47 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotSubtract(self.clone(), other.clone())),
|
2024-08-09 03:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 13:22:36 +00:00
|
|
|
pub fn subtract_assign(&self, other: &Value) -> Result<(), ValueError> {
|
2024-08-14 18:28:39 +00:00
|
|
|
match (self, other) {
|
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
|
|
|
match (&mut *left.write().unwrap(), &*right.read().unwrap()) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
2024-08-14 18:28:39 +00:00
|
|
|
*left -= right;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
2024-08-14 18:28:39 +00:00
|
|
|
*left = left.saturating_sub(*right);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Mutable(left), right) => match (&mut *left.write().unwrap(), right) {
|
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
|
|
|
*left -= right;
|
|
|
|
return Ok(());
|
2024-08-14 18:28:39 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
*left = left.saturating_sub(*right);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
_ => {}
|
2024-08-14 18:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Err(ValueError::CannotSubtract(self.clone(), other.clone()))
|
|
|
|
}
|
|
|
|
|
2024-08-09 03:28:47 +00:00
|
|
|
pub fn multiply(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Float(left * right)),
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
Ok(Value::Integer(left.saturating_mul(*right)))
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.multiply(&right)
|
2024-08-09 03:28:47 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotMultiply(self.clone(), other.clone())),
|
2024-08-09 03:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-09 07:00:48 +00:00
|
|
|
|
2024-08-16 13:22:36 +00:00
|
|
|
pub fn multiply_assign(&self, other: &Value) -> Result<(), ValueError> {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
|
|
|
match (&mut *left.write().unwrap(), &*right.read().unwrap()) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
2024-08-16 13:22:36 +00:00
|
|
|
*left *= right;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
2024-08-16 13:22:36 +00:00
|
|
|
*left = left.saturating_mul(*right);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Mutable(left), right) => match (&mut *left.write().unwrap(), right) {
|
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
|
|
|
*left *= right;
|
|
|
|
return Ok(());
|
2024-08-16 13:22:36 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
*left = left.saturating_mul(*right);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
_ => {}
|
2024-08-16 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Err(ValueError::CannotMultiply(self.clone(), other.clone()))
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:46:24 +00:00
|
|
|
pub fn divide(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Float(left / right)),
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
2024-08-17 02:43:29 +00:00
|
|
|
Ok(Value::Integer(left.saturating_div(*right)))
|
2024-08-09 10:46:24 +00:00
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.divide(&right)
|
2024-08-09 10:46:24 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotDivide(self.clone(), other.clone())),
|
2024-08-09 10:46:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 13:22:36 +00:00
|
|
|
pub fn divide_assign(&self, other: &Value) -> Result<(), ValueError> {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
|
|
|
match (&mut *left.write().unwrap(), &*right.read().unwrap()) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
2024-08-16 13:22:36 +00:00
|
|
|
*left /= right;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
*left = (*left as f64 / *right as f64) as i64;
|
2024-08-16 13:22:36 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Mutable(left), right) => match (&mut *left.write().unwrap(), right) {
|
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
|
|
|
*left /= right;
|
|
|
|
return Ok(());
|
2024-08-16 13:22:36 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
*left = (*left as f64 / *right as f64) as i64;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
_ => {}
|
2024-08-16 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Err(ValueError::CannotDivide(self.clone(), other.clone()))
|
|
|
|
}
|
|
|
|
|
2024-08-09 11:02:55 +00:00
|
|
|
pub fn modulo(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Float(left % right)),
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => Ok(Value::Integer(left % right)),
|
2024-08-14 05:03:46 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.modulo(&right)
|
2024-08-09 11:02:55 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotModulo(self.clone(), other.clone())),
|
2024-08-09 11:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 13:22:36 +00:00
|
|
|
pub fn modulo_assign(&self, other: &Value) -> Result<(), ValueError> {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
|
|
|
match (&mut *left.write().unwrap(), &*right.read().unwrap()) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
2024-08-16 13:22:36 +00:00
|
|
|
*left %= right;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
2024-08-16 13:22:36 +00:00
|
|
|
*left %= right;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Mutable(left), right) => match (&mut *left.write().unwrap(), right) {
|
|
|
|
(Value::Float(left), Value::Float(right)) => {
|
|
|
|
*left %= right;
|
|
|
|
return Ok(());
|
2024-08-16 13:22:36 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Integer(left), Value::Integer(right)) => {
|
|
|
|
*left %= right;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
_ => {}
|
2024-08-16 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Err(ValueError::CannotModulo(self.clone(), other.clone()))
|
|
|
|
}
|
|
|
|
|
2024-08-16 10:43:29 +00:00
|
|
|
pub fn equal(&self, other: &Value) -> Value {
|
|
|
|
let is_equal = match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Boolean(left), Value::Boolean(right)) => left == right,
|
|
|
|
(Value::Byte(left), Value::Byte(right)) => left == right,
|
|
|
|
(Value::Character(left), Value::Character(right)) => left == right,
|
|
|
|
(Value::Float(left), Value::Float(right)) => left == right,
|
|
|
|
(Value::Function(left), Value::Function(right)) => left == right,
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => left == right,
|
|
|
|
(Value::List(left), Value::List(right)) => {
|
|
|
|
if left.len() != right.len() {
|
|
|
|
return Value::Boolean(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (left, right) in left.iter().zip(right.iter()) {
|
|
|
|
if let Value::Boolean(false) = left.equal(right) {
|
|
|
|
return Value::Boolean(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
(Value::Range(left), Value::Range(right)) => {
|
|
|
|
left.start == right.start && left.end == right.end
|
|
|
|
}
|
|
|
|
(Value::RangeInclusive(left), Value::RangeInclusive(right)) => {
|
|
|
|
left.start() == right.start() && left.end() == right.end()
|
|
|
|
}
|
|
|
|
(Value::String(left), Value::String(right)) => left == right,
|
|
|
|
(Value::Struct(left), Value::Struct(right)) => left == right,
|
2024-08-16 10:43:29 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
return left.equal(&right);
|
2024-08-16 10:43:29 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Mutable(locked), immutable) | (immutable, Value::Mutable(locked)) => {
|
|
|
|
let locked = locked.read().unwrap();
|
|
|
|
|
|
|
|
return locked.equal(immutable);
|
2024-08-16 10:43:29 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => false,
|
2024-08-16 10:43:29 +00:00
|
|
|
};
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Boolean(is_equal)
|
2024-08-16 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn not_equal(&self, other: &Value) -> Value {
|
2024-08-16 21:07:49 +00:00
|
|
|
if let Value::Boolean(is_equal) = self.equal(other) {
|
|
|
|
Value::Boolean(!is_equal)
|
|
|
|
} else {
|
|
|
|
Value::Boolean(true)
|
|
|
|
}
|
2024-08-16 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 07:00:48 +00:00
|
|
|
pub fn less_than(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Boolean(left < right)),
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => Ok(Value::Boolean(left < right)),
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Float(left), Value::Integer(right)) => {
|
|
|
|
Ok(Value::Boolean(*left < *right as f64))
|
|
|
|
}
|
|
|
|
(Value::Integer(left), Value::Float(right)) => {
|
|
|
|
Ok(Value::Boolean((*left as f64) < *right))
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.less_than(&right)
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Mutable(left), right) => {
|
|
|
|
let left = left.read().unwrap();
|
|
|
|
|
|
|
|
left.less_than(right)
|
|
|
|
}
|
|
|
|
(left, Value::Mutable(right)) => {
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.less_than(&right)
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotLessThan(self.clone(), other.clone())),
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn less_than_or_equal(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Boolean(left <= right)),
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => Ok(Value::Boolean(left <= right)),
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Float(left), Value::Integer(right)) => {
|
|
|
|
Ok(Value::Boolean(*left <= *right as f64))
|
|
|
|
}
|
|
|
|
(Value::Integer(left), Value::Float(right)) => {
|
|
|
|
Ok(Value::Boolean(*left as f64 <= *right))
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.less_than_or_equal(&right)
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Mutable(left), right) => {
|
|
|
|
let left = left.read().unwrap();
|
|
|
|
|
|
|
|
left.less_than_or_equal(right)
|
|
|
|
}
|
|
|
|
(left, Value::Mutable(right)) => {
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.less_than_or_equal(&right)
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotLessThanOrEqual(
|
|
|
|
self.clone(),
|
|
|
|
other.clone(),
|
|
|
|
)),
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn greater_than(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Boolean(left > right)),
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => Ok(Value::Boolean(left > right)),
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Float(left), Value::Integer(right)) => {
|
|
|
|
Ok(Value::Boolean(*left > *right as f64))
|
|
|
|
}
|
|
|
|
(Value::Integer(left), Value::Float(right)) => {
|
|
|
|
Ok(Value::Boolean(*left as f64 > *right))
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.greater_than(&right)
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Mutable(left), right) => {
|
|
|
|
let left = left.read().unwrap();
|
|
|
|
|
|
|
|
left.greater_than(right)
|
|
|
|
}
|
|
|
|
(left, Value::Mutable(right)) => {
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.greater_than(&right)
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotGreaterThan(self.clone(), other.clone())),
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn greater_than_or_equal(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-14 05:03:46 +00:00
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Float(left), Value::Float(right)) => Ok(Value::Boolean(left >= right)),
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => Ok(Value::Boolean(left >= right)),
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Float(left), Value::Integer(right)) => {
|
|
|
|
Ok(Value::Boolean(*left >= *right as f64))
|
|
|
|
}
|
|
|
|
(Value::Integer(left), Value::Float(right)) => {
|
|
|
|
Ok(Value::Boolean(*left as f64 >= *right))
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
2024-08-16 21:07:49 +00:00
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.greater_than_or_equal(&right)
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
2024-08-17 02:43:29 +00:00
|
|
|
(Value::Mutable(left), right) => {
|
|
|
|
let left = left.read().unwrap();
|
|
|
|
|
|
|
|
left.greater_than_or_equal(right)
|
|
|
|
}
|
|
|
|
(left, Value::Mutable(right)) => {
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.greater_than_or_equal(&right)
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotGreaterThanOrEqual(
|
|
|
|
self.clone(),
|
|
|
|
other.clone(),
|
|
|
|
)),
|
2024-08-09 07:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-09 08:56:24 +00:00
|
|
|
|
|
|
|
pub fn and(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-17 02:43:29 +00:00
|
|
|
match (self.as_boolean(), other.as_boolean()) {
|
|
|
|
(Some(left), Some(right)) => Ok(Value::Boolean(left && right)),
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotAnd(self.clone(), other.clone())),
|
2024-08-09 08:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn or(&self, other: &Value) -> Result<Value, ValueError> {
|
2024-08-17 02:43:29 +00:00
|
|
|
match (self.as_boolean(), other.as_boolean()) {
|
|
|
|
(Some(left), Some(right)) => Ok(Value::Boolean(left || right)),
|
2024-08-16 21:07:49 +00:00
|
|
|
_ => Err(ValueError::CannotOr(self.clone(), other.clone())),
|
2024-08-09 08:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-06 20:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-08-14 05:03:46 +00:00
|
|
|
match self {
|
|
|
|
Value::Mutable(inner_locked) => {
|
|
|
|
let inner = inner_locked.read().unwrap();
|
|
|
|
|
|
|
|
write!(f, "{inner}")
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Boolean(boolean) => write!(f, "{boolean}"),
|
|
|
|
Value::Byte(byte) => write!(f, "{byte}"),
|
|
|
|
Value::Character(character) => write!(f, "{character}"),
|
|
|
|
Value::Enum { name, r#type } => write!(f, "{name}::{type}"),
|
|
|
|
Value::Float(float) => write!(f, "{float}"),
|
|
|
|
Value::Function(function) => write!(f, "{function}"),
|
|
|
|
Value::Integer(integer) => write!(f, "{integer}"),
|
|
|
|
Value::List(list) => {
|
|
|
|
write!(f, "[")?;
|
2024-08-14 05:03:46 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
for (index, value) in list.iter().enumerate() {
|
|
|
|
write!(f, "{}", value)?;
|
2024-08-14 05:03:46 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
if index < list.len() - 1 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, "]")
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Range(Range { start, end }) => {
|
|
|
|
write!(f, "{start}..{end}")
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::RangeInclusive(inclusive) => {
|
|
|
|
let start = inclusive.start();
|
|
|
|
let end = inclusive.end();
|
2024-08-14 05:03:46 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, "{start}..={end}")
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::String(string) => write!(f, "{string}"),
|
|
|
|
Value::Struct(structure) => write!(f, "{structure}"),
|
|
|
|
Value::Tuple(fields) => {
|
|
|
|
write!(f, "(")?;
|
2024-08-14 05:03:46 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
for (index, field) in fields.iter().enumerate() {
|
|
|
|
write!(f, "{}", field)?;
|
2024-08-14 05:03:46 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
if index < fields.len() - 1 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
2024-08-16 03:17:49 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, ")")
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl Eq for Value {}
|
2024-03-07 21:19:24 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl PartialEq for Value {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::Boolean(left), Value::Boolean(right)) => left == right,
|
|
|
|
(Value::Byte(left), Value::Byte(right)) => left == right,
|
|
|
|
(Value::Character(left), Value::Character(right)) => left == right,
|
|
|
|
(Value::Float(left), Value::Float(right)) => left == right,
|
|
|
|
(Value::Function(left), Value::Function(right)) => left == right,
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => left == right,
|
|
|
|
(Value::List(left), Value::List(right)) => left == right,
|
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
|
|
|
let left = &*left.read().unwrap();
|
|
|
|
let right = &*right.read().unwrap();
|
2024-03-07 21:19:24 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
left == right
|
2024-03-07 21:19:24 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Range(left), Value::Range(right)) => left == right,
|
|
|
|
(Value::RangeInclusive(left), Value::RangeInclusive(right)) => left == right,
|
|
|
|
(Value::String(left), Value::String(right)) => left == right,
|
|
|
|
(Value::Struct(left), Value::Struct(right)) => left == right,
|
|
|
|
(Value::Tuple(left), Value::Tuple(right)) => left == right,
|
|
|
|
_ => false,
|
2024-03-06 20:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-25 19:26:22 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl PartialOrd for Value {
|
2024-08-14 05:03:46 +00:00
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
2024-02-25 19:26:22 +00:00
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl Ord for Value {
|
2024-08-14 05:03:46 +00:00
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Boolean(left), Value::Boolean(right)) => left.cmp(right),
|
|
|
|
(Value::Boolean(_), _) => Ordering::Greater,
|
|
|
|
(Value::Byte(left), Value::Byte(right)) => left.cmp(right),
|
|
|
|
(Value::Byte(_), _) => Ordering::Greater,
|
|
|
|
(Value::Character(left), Value::Character(right)) => left.cmp(right),
|
|
|
|
(Value::Character(_), _) => Ordering::Greater,
|
|
|
|
(Value::Float(left), Value::Float(right)) => left.partial_cmp(right).unwrap(),
|
|
|
|
(Value::Float(_), _) => Ordering::Greater,
|
|
|
|
(Value::Function(left), Value::Function(right)) => left.cmp(right),
|
|
|
|
(Value::Function(_), _) => Ordering::Greater,
|
|
|
|
(Value::Integer(left), Value::Integer(right)) => left.cmp(right),
|
|
|
|
(Value::Integer(_), _) => Ordering::Greater,
|
|
|
|
(Value::List(left), Value::List(right)) => left.cmp(right),
|
|
|
|
(Value::List(_), _) => Ordering::Greater,
|
|
|
|
(Value::Mutable(left), Value::Mutable(right)) => {
|
|
|
|
let left = left.read().unwrap();
|
|
|
|
let right = right.read().unwrap();
|
|
|
|
|
|
|
|
left.cmp(&right)
|
|
|
|
}
|
|
|
|
(Value::Mutable(_), _) => Ordering::Greater,
|
|
|
|
(Value::Range(left), Value::Range(right)) => {
|
2024-08-14 05:03:46 +00:00
|
|
|
let start_cmp = left.start.cmp(&right.start);
|
|
|
|
|
|
|
|
if start_cmp.is_eq() {
|
|
|
|
left.end.cmp(&right.end)
|
|
|
|
} else {
|
|
|
|
start_cmp
|
|
|
|
}
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::Range(_), _) => Ordering::Greater,
|
|
|
|
(Value::RangeInclusive(left), Value::RangeInclusive(right)) => {
|
2024-08-16 15:21:20 +00:00
|
|
|
let start_cmp = left.start().cmp(right.start());
|
|
|
|
|
|
|
|
if start_cmp.is_eq() {
|
|
|
|
left.end().cmp(right.end())
|
|
|
|
} else {
|
|
|
|
start_cmp
|
|
|
|
}
|
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Value::RangeInclusive(_), _) => Ordering::Greater,
|
|
|
|
(Value::String(left), Value::String(right)) => left.cmp(right),
|
|
|
|
(Value::String(_), _) => Ordering::Greater,
|
|
|
|
(Value::Struct(left), Value::Struct(right)) => left.cmp(right),
|
|
|
|
(Value::Struct(_), _) => Ordering::Greater,
|
|
|
|
(Value::Tuple(left), Value::Tuple(right)) => left.cmp(right),
|
|
|
|
_ => Ordering::Greater,
|
2024-08-14 05:03:46 +00:00
|
|
|
}
|
2024-02-25 19:26:22 +00:00
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl Serialize for Value {
|
2024-06-19 07:32:51 +00:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2024-06-04 18:47:15 +00:00
|
|
|
where
|
2024-08-14 05:03:46 +00:00
|
|
|
S: Serializer,
|
2024-06-19 07:32:51 +00:00
|
|
|
{
|
2024-08-14 05:03:46 +00:00
|
|
|
match self {
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Mutable(inner_locked) => {
|
|
|
|
let inner = inner_locked.read().unwrap();
|
2024-06-19 07:32:51 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
inner.serialize(serializer)
|
2024-06-19 07:32:51 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Boolean(boolean) => serializer.serialize_bool(*boolean),
|
|
|
|
Value::Byte(byte) => serializer.serialize_u8(*byte),
|
|
|
|
Value::Character(character) => serializer.serialize_char(*character),
|
|
|
|
Value::Enum { name, r#type } => {
|
|
|
|
let mut ser = serializer.serialize_struct_variant("Value", 4, "Enum", 2)?;
|
2024-08-16 15:21:20 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
ser.serialize_field("name", name)?;
|
|
|
|
ser.serialize_field("type", r#type)?;
|
2024-08-16 15:21:20 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
ser.end()
|
2024-08-16 15:21:20 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
Value::Float(float) => serializer.serialize_f64(*float),
|
|
|
|
Value::Function(function) => function.serialize(serializer),
|
|
|
|
Value::Integer(integer) => serializer.serialize_i64(*integer),
|
|
|
|
Value::List(list) => list.serialize(serializer),
|
|
|
|
Value::Range(range) => range.serialize(serializer),
|
|
|
|
Value::RangeInclusive(inclusive) => inclusive.serialize(serializer),
|
|
|
|
Value::String(string) => serializer.serialize_str(string),
|
|
|
|
Value::Struct(r#struct) => r#struct.serialize(serializer),
|
|
|
|
Value::Tuple(tuple) => tuple.serialize(serializer),
|
2024-06-19 07:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl<'de> Deserialize<'de> for Value {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Value, D::Error>
|
2024-06-19 07:32:51 +00:00
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
2024-08-16 21:07:49 +00:00
|
|
|
struct ValueVisitor;
|
2024-06-19 07:32:51 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl<'de> Visitor<'de> for ValueVisitor {
|
|
|
|
type Value = Value;
|
2024-06-19 07:32:51 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a value")
|
|
|
|
}
|
2024-06-19 08:56:56 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
fn visit_bool<E>(self, value: bool) -> Result<Value, E> {
|
|
|
|
Ok(Value::Boolean(value))
|
|
|
|
}
|
2024-06-19 08:56:56 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
fn visit_u8<E>(self, value: u8) -> Result<Value, E> {
|
|
|
|
Ok(Value::Byte(value))
|
|
|
|
}
|
2024-06-19 07:32:51 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
fn visit_char<E>(self, value: char) -> Result<Value, E> {
|
|
|
|
Ok(Value::Character(value))
|
|
|
|
}
|
2024-06-19 08:56:56 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
fn visit_f64<E>(self, value: f64) -> Result<Value, E> {
|
|
|
|
Ok(Value::Float(value))
|
|
|
|
}
|
2024-06-19 08:56:56 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
fn visit_i64<E>(self, value: i64) -> Result<Value, E> {
|
|
|
|
Ok(Value::Integer(value))
|
|
|
|
}
|
2024-06-19 07:32:51 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Value, E> {
|
|
|
|
Ok(Value::String(value.to_string()))
|
|
|
|
}
|
|
|
|
}
|
2024-06-04 18:47:15 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
deserializer.deserialize_any(ValueVisitor)
|
2024-06-22 17:55:43 +00:00
|
|
|
}
|
2024-03-09 13:10:54 +00:00
|
|
|
}
|
2024-08-05 02:15:31 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
2024-08-07 14:03:33 +00:00
|
|
|
pub struct Function {
|
|
|
|
pub name: Identifier,
|
2024-08-16 21:07:49 +00:00
|
|
|
pub r#type: FunctionType,
|
|
|
|
pub body: Arc<AbstractSyntaxTree>,
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
2024-08-07 22:24:25 +00:00
|
|
|
pub fn call(
|
2024-08-16 03:17:49 +00:00
|
|
|
&self,
|
2024-08-07 22:46:40 +00:00
|
|
|
_type_arguments: Option<Vec<Type>>,
|
2024-08-07 22:24:25 +00:00
|
|
|
value_arguments: Option<Vec<Value>>,
|
2024-08-12 12:54:21 +00:00
|
|
|
context: &Context,
|
2024-08-17 09:32:18 +00:00
|
|
|
) -> Result<Option<Value>, RuntimeError> {
|
2024-08-12 12:54:21 +00:00
|
|
|
let new_context = Context::with_variables_from(context);
|
2024-08-07 22:24:25 +00:00
|
|
|
|
|
|
|
if let (Some(value_parameters), Some(value_arguments)) =
|
2024-08-16 21:07:49 +00:00
|
|
|
(&self.r#type.value_parameters, value_arguments)
|
2024-08-07 22:24:25 +00:00
|
|
|
{
|
2024-08-16 15:21:20 +00:00
|
|
|
for ((identifier, _), value) in value_parameters.iter().zip(value_arguments) {
|
2024-08-16 03:17:49 +00:00
|
|
|
new_context.set_value(identifier.clone(), value);
|
2024-08-07 22:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
let mut vm = Vm::new(self.body.as_ref().clone(), new_context);
|
2024-08-07 22:24:25 +00:00
|
|
|
|
2024-08-12 12:54:21 +00:00
|
|
|
vm.run()
|
2024-08-07 22:24:25 +00:00
|
|
|
}
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Function {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, "fn {}", self.name)?;
|
2024-08-07 14:03:33 +00:00
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
if let Some(type_parameters) = &self.r#type.type_parameters {
|
2024-08-07 14:03:33 +00:00
|
|
|
write!(f, "<")?;
|
|
|
|
|
|
|
|
for (index, type_parameter) in type_parameters.iter().enumerate() {
|
|
|
|
if index > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{}", type_parameter)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ">")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "(")?;
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
if let Some(value_paramers) = &self.r#type.value_parameters {
|
2024-08-07 14:03:33 +00:00
|
|
|
for (index, (identifier, r#type)) in value_paramers.iter().enumerate() {
|
|
|
|
if index > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{identifier}: {type}")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ") {{")?;
|
|
|
|
|
2024-08-14 18:28:39 +00:00
|
|
|
for statement in &self.body.statements {
|
2024-08-07 14:03:33 +00:00
|
|
|
write!(f, "{}", statement)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "}}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl Serialize for Function {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let mut ser = serializer.serialize_struct("Function", 3)?;
|
|
|
|
|
|
|
|
ser.serialize_field("name", &self.name)?;
|
|
|
|
ser.serialize_field("type", &self.r#type)?;
|
|
|
|
ser.serialize_field("body", self.body.as_ref())?;
|
|
|
|
|
|
|
|
ser.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Function {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
struct FunctionVisitor;
|
|
|
|
|
|
|
|
impl<'de> Visitor<'de> for FunctionVisitor {
|
|
|
|
type Value = Function;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a function")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map<A>(self, mut map: A) -> Result<Function, A::Error>
|
|
|
|
where
|
|
|
|
A: MapAccess<'de>,
|
|
|
|
{
|
|
|
|
let mut name = None;
|
|
|
|
let mut r#type = None;
|
|
|
|
let mut body = None;
|
|
|
|
|
|
|
|
while let Some(key) = map.next_key()? {
|
|
|
|
match key {
|
|
|
|
"name" => {
|
|
|
|
if name.is_some() {
|
|
|
|
return Err(de::Error::duplicate_field("name"));
|
|
|
|
}
|
|
|
|
|
|
|
|
name = Some(map.next_value()?);
|
|
|
|
}
|
|
|
|
"type" => {
|
|
|
|
if r#type.is_some() {
|
|
|
|
return Err(de::Error::duplicate_field("type"));
|
|
|
|
}
|
|
|
|
|
|
|
|
r#type = Some(map.next_value()?);
|
|
|
|
}
|
|
|
|
"body" => {
|
|
|
|
if body.is_some() {
|
|
|
|
return Err(de::Error::duplicate_field("body"));
|
|
|
|
}
|
|
|
|
|
|
|
|
body = Some(map.next_value().map(|ast| Arc::new(ast))?);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(de::Error::unknown_field(key, &["name", "type", "body"]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = name.ok_or_else(|| de::Error::missing_field("name"))?;
|
|
|
|
let r#type = r#type.ok_or_else(|| de::Error::missing_field("type"))?;
|
|
|
|
let body = body.ok_or_else(|| de::Error::missing_field("body"))?;
|
|
|
|
|
|
|
|
Ok(Function { name, r#type, body })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_struct("Function", &["name", "type", "body"], FunctionVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 08:06:13 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
2024-08-12 19:02:04 +00:00
|
|
|
pub enum Struct {
|
|
|
|
Unit {
|
2024-08-17 08:06:13 +00:00
|
|
|
name: Identifier,
|
2024-08-12 19:02:04 +00:00
|
|
|
},
|
|
|
|
Tuple {
|
2024-08-17 08:06:13 +00:00
|
|
|
name: Identifier,
|
2024-08-12 19:02:04 +00:00
|
|
|
fields: Vec<Value>,
|
|
|
|
},
|
|
|
|
Fields {
|
2024-08-17 08:06:13 +00:00
|
|
|
name: Identifier,
|
|
|
|
fields: HashMap<Identifier, Value>,
|
2024-08-12 19:02:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-08-17 08:06:13 +00:00
|
|
|
impl PartialOrd for Struct {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Struct {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
|
|
|
(Struct::Unit { name: left }, Struct::Unit { name: right }) => left.cmp(right),
|
|
|
|
(Struct::Unit { .. }, _) => Ordering::Greater,
|
|
|
|
(
|
|
|
|
Struct::Tuple {
|
|
|
|
name: left_name,
|
|
|
|
fields: left_fields,
|
|
|
|
},
|
|
|
|
Struct::Tuple {
|
|
|
|
name: right_name,
|
|
|
|
fields: right_fields,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
let type_cmp = left_name.cmp(right_name);
|
|
|
|
|
|
|
|
if type_cmp != Ordering::Equal {
|
|
|
|
return type_cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
left_fields.cmp(right_fields)
|
|
|
|
}
|
|
|
|
(Struct::Tuple { .. }, _) => Ordering::Greater,
|
|
|
|
(
|
|
|
|
Struct::Fields {
|
|
|
|
name: left_name,
|
|
|
|
fields: left_fields,
|
|
|
|
},
|
|
|
|
Struct::Fields {
|
|
|
|
name: right_name,
|
|
|
|
fields: right_fields,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
let type_cmp = left_name.cmp(right_name);
|
|
|
|
|
|
|
|
if type_cmp != Ordering::Equal {
|
|
|
|
return type_cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
left_fields.into_iter().cmp(right_fields.into_iter())
|
|
|
|
}
|
|
|
|
(Struct::Fields { .. }, _) => Ordering::Greater,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 19:02:04 +00:00
|
|
|
impl Display for Struct {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2024-08-16 21:07:49 +00:00
|
|
|
Struct::Unit { .. } => write!(f, "()"),
|
|
|
|
Struct::Tuple { fields, .. } => {
|
|
|
|
write!(f, "(")?;
|
2024-08-12 19:02:04 +00:00
|
|
|
|
|
|
|
for (index, field) in fields.iter().enumerate() {
|
|
|
|
if index > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{}", field)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ")")
|
|
|
|
}
|
2024-08-17 08:06:13 +00:00
|
|
|
Struct::Fields { fields, .. } => {
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, "{{ ")?;
|
2024-08-12 19:02:04 +00:00
|
|
|
|
2024-08-17 08:06:13 +00:00
|
|
|
for (index, (identifier, value)) in fields.iter().enumerate() {
|
2024-08-12 19:02:04 +00:00
|
|
|
if index > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
write!(f, "{}: {}", identifier, value)?;
|
2024-08-12 19:02:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
write!(f, " }}")
|
|
|
|
}
|
2024-08-17 02:43:29 +00:00
|
|
|
_ => Ok(()),
|
2024-08-16 21:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2024-08-17 08:06:13 +00:00
|
|
|
pub enum Rangeable {
|
2024-08-16 21:07:49 +00:00
|
|
|
Byte(u8),
|
|
|
|
Character(char),
|
|
|
|
Float(f64),
|
|
|
|
Integer(i64),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Rangeable {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Rangeable::Byte(byte) => write!(f, "{byte}"),
|
|
|
|
Rangeable::Character(character) => write!(f, "{character}"),
|
|
|
|
Rangeable::Float(float) => write!(f, "{float}"),
|
|
|
|
Rangeable::Integer(integer) => write!(f, "{integer}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Rangeable {}
|
|
|
|
|
2024-08-17 02:43:29 +00:00
|
|
|
impl PartialOrd for Rangeable {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:07:49 +00:00
|
|
|
impl Ord for Rangeable {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
match (self, other) {
|
|
|
|
(Rangeable::Byte(left), Rangeable::Byte(right)) => left.cmp(right),
|
|
|
|
(Rangeable::Character(left), Rangeable::Character(right)) => left.cmp(right),
|
|
|
|
(Rangeable::Float(left), Rangeable::Float(right)) => {
|
|
|
|
left.to_bits().cmp(&right.to_bits())
|
2024-08-12 19:02:04 +00:00
|
|
|
}
|
2024-08-16 21:07:49 +00:00
|
|
|
(Rangeable::Integer(left), Rangeable::Integer(right)) => left.cmp(right),
|
|
|
|
_ => unreachable!(),
|
2024-08-12 19:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 14:03:33 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2024-08-05 02:15:31 +00:00
|
|
|
pub enum ValueError {
|
|
|
|
CannotAdd(Value, Value),
|
2024-08-09 08:56:24 +00:00
|
|
|
CannotAnd(Value, Value),
|
2024-08-09 10:46:24 +00:00
|
|
|
CannotDivide(Value, Value),
|
2024-08-09 07:00:48 +00:00
|
|
|
CannotGreaterThan(Value, Value),
|
|
|
|
CannotGreaterThanOrEqual(Value, Value),
|
2024-08-16 15:21:20 +00:00
|
|
|
CannotIndex { value: Value, index: Value },
|
2024-08-09 08:56:24 +00:00
|
|
|
CannotLessThan(Value, Value),
|
|
|
|
CannotLessThanOrEqual(Value, Value),
|
2024-08-16 13:22:36 +00:00
|
|
|
CannotMakeMutable,
|
2024-08-09 11:02:55 +00:00
|
|
|
CannotModulo(Value, Value),
|
2024-08-09 08:56:24 +00:00
|
|
|
CannotMultiply(Value, Value),
|
2024-08-14 05:13:43 +00:00
|
|
|
CannotMutate(Value),
|
2024-08-09 08:56:24 +00:00
|
|
|
CannotSubtract(Value, Value),
|
|
|
|
CannotOr(Value, Value),
|
2024-08-09 10:46:24 +00:00
|
|
|
DivisionByZero,
|
2024-08-05 18:58:58 +00:00
|
|
|
ExpectedList(Value),
|
2024-08-09 07:00:48 +00:00
|
|
|
IndexOutOfBounds { value: Value, index: i64 },
|
2024-08-05 02:15:31 +00:00
|
|
|
}
|
2024-08-09 00:58:56 +00:00
|
|
|
|
|
|
|
impl Error for ValueError {}
|
|
|
|
|
|
|
|
impl Display for ValueError {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
ValueError::CannotAdd(left, right) => write!(f, "Cannot add {} and {}", left, right),
|
2024-08-09 08:56:24 +00:00
|
|
|
ValueError::CannotAnd(left, right) => write!(
|
|
|
|
f,
|
|
|
|
"Cannot use logical and operation on {} and {}",
|
|
|
|
left, right
|
|
|
|
),
|
2024-08-09 10:46:24 +00:00
|
|
|
ValueError::CannotDivide(left, right) => {
|
|
|
|
write!(f, "Cannot divide {} by {}", left, right)
|
|
|
|
}
|
2024-08-16 15:21:20 +00:00
|
|
|
ValueError::CannotIndex { value, index } => {
|
|
|
|
write!(f, "Cannot index {} with {}", value, index)
|
|
|
|
}
|
2024-08-09 11:02:55 +00:00
|
|
|
ValueError::CannotModulo(left, right) => {
|
|
|
|
write!(f, "Cannot modulo {} by {}", left, right)
|
|
|
|
}
|
2024-08-09 03:28:47 +00:00
|
|
|
ValueError::CannotMultiply(left, right) => {
|
|
|
|
write!(f, "Cannot multiply {} and {}", left, right)
|
|
|
|
}
|
2024-08-16 13:22:36 +00:00
|
|
|
ValueError::CannotMakeMutable => write!(
|
|
|
|
f,
|
|
|
|
"Failed to make mutable value because the value has an immutable reference to it"
|
|
|
|
),
|
2024-08-14 05:13:43 +00:00
|
|
|
ValueError::CannotMutate(value) => write!(f, "Cannot mutate {}", value),
|
2024-08-09 03:28:47 +00:00
|
|
|
ValueError::CannotSubtract(left, right) => {
|
|
|
|
write!(f, "Cannot subtract {} and {}", left, right)
|
|
|
|
}
|
2024-08-09 07:00:48 +00:00
|
|
|
ValueError::CannotLessThan(left, right)
|
|
|
|
| ValueError::CannotLessThanOrEqual(left, right)
|
|
|
|
| ValueError::CannotGreaterThan(left, right)
|
|
|
|
| ValueError::CannotGreaterThanOrEqual(left, right) => {
|
|
|
|
write!(f, "Cannot compare {} and {}", left, right)
|
|
|
|
}
|
2024-08-09 08:56:24 +00:00
|
|
|
ValueError::CannotOr(left, right) => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Cannot use logical or operation on {} and {}",
|
|
|
|
left, right
|
|
|
|
)
|
2024-08-09 00:58:56 +00:00
|
|
|
}
|
2024-08-09 10:46:24 +00:00
|
|
|
ValueError::DivisionByZero => write!(f, "Division by zero"),
|
2024-08-09 00:58:56 +00:00
|
|
|
ValueError::IndexOutOfBounds { value, index } => {
|
|
|
|
write!(f, "{} does not have an index of {}", value, index)
|
|
|
|
}
|
|
|
|
ValueError::ExpectedList(value) => write!(f, "{} is not a list", value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|