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-07 14:03:33 +00:00
|
|
|
collections::{BTreeMap, HashMap},
|
2024-08-09 00:58:56 +00:00
|
|
|
error::Error,
|
2024-03-06 20:36:58 +00:00
|
|
|
fmt::{self, Display, Formatter},
|
2024-02-25 18:49:26 +00:00
|
|
|
ops::Range,
|
2024-03-23 12:15:48 +00:00
|
|
|
sync::Arc,
|
2024-02-25 18:49:26 +00:00
|
|
|
};
|
|
|
|
|
2024-06-19 07:32:51 +00:00
|
|
|
use serde::{
|
|
|
|
de::Visitor,
|
2024-08-04 00:23:52 +00:00
|
|
|
ser::{SerializeMap, SerializeSeq, SerializeTuple},
|
2024-06-19 07:32:51 +00:00
|
|
|
Deserialize, Deserializer, Serialize,
|
|
|
|
};
|
2024-06-04 18:47:15 +00:00
|
|
|
|
2024-08-07 23:12:40 +00:00
|
|
|
use crate::{identifier::Identifier, AbstractSyntaxTree, Type, Vm, VmError};
|
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;
|
|
|
|
/// # use dust_lang::{Type, Value};
|
|
|
|
/// let variables = HashMap::new();
|
|
|
|
/// let value = Value::integer(42);
|
|
|
|
///
|
|
|
|
/// assert_eq!(value.r#type(&variables), Type::Integer);
|
|
|
|
/// ```
|
2024-02-25 18:49:26 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub struct Value(Arc<ValueInner>);
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn inner(&self) -> &Arc<ValueInner> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn boolean(boolean: bool) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Boolean(boolean)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn float(float: f64) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Float(float)))
|
|
|
|
}
|
|
|
|
|
2024-08-07 22:24:25 +00:00
|
|
|
pub fn function(function: Function) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Function(function)))
|
|
|
|
}
|
|
|
|
|
2024-02-25 18:49:26 +00:00
|
|
|
pub fn integer(integer: i64) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Integer(integer)))
|
|
|
|
}
|
|
|
|
|
2024-06-19 08:56:56 +00:00
|
|
|
pub fn list(list: Vec<Value>) -> Self {
|
2024-02-25 18:49:26 +00:00
|
|
|
Value(Arc::new(ValueInner::List(list)))
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:36:47 +00:00
|
|
|
pub fn map(map: BTreeMap<Identifier, Value>) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Map(map)))
|
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
pub fn range(range: Range<i64>) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Range(range)))
|
|
|
|
}
|
|
|
|
|
2024-03-24 14:58:09 +00:00
|
|
|
pub fn string<T: ToString>(to_string: T) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::String(to_string.to_string())))
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-08-07 14:03:33 +00:00
|
|
|
pub fn r#type(&self, variables: &HashMap<Identifier, Value>) -> Type {
|
|
|
|
self.0.r#type(variables)
|
2024-03-06 22:32:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-17 20:59:52 +00:00
|
|
|
pub fn as_boolean(&self) -> Option<bool> {
|
2024-02-25 19:26:22 +00:00
|
|
|
if let ValueInner::Boolean(boolean) = self.0.as_ref() {
|
2024-03-17 20:59:52 +00:00
|
|
|
Some(*boolean)
|
|
|
|
} else {
|
|
|
|
None
|
2024-03-09 13:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 22:24:25 +00:00
|
|
|
pub fn as_function(&self) -> Option<&Function> {
|
|
|
|
if let ValueInner::Function(function) = self.0.as_ref() {
|
|
|
|
Some(function)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-19 08:56:56 +00:00
|
|
|
pub fn as_list(&self) -> Option<&Vec<Value>> {
|
2024-03-07 17:29:07 +00:00
|
|
|
if let ValueInner::List(list) = self.inner().as_ref() {
|
|
|
|
Some(list)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_integer(&self) -> Option<i64> {
|
|
|
|
if let ValueInner::Integer(integer) = self.inner().as_ref() {
|
|
|
|
Some(*integer)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2024-08-05 02:15:31 +00:00
|
|
|
|
|
|
|
pub fn add(&self, other: &Value) -> Result<Value, ValueError> {
|
|
|
|
match (self.inner().as_ref(), other.inner().as_ref()) {
|
|
|
|
(ValueInner::Float(left), ValueInner::Float(right)) => Ok(Value::float(left + right)),
|
|
|
|
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
|
2024-08-09 03:28:47 +00:00
|
|
|
Ok(Value::integer(left.saturating_add(*right)))
|
2024-08-05 02:15:31 +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> {
|
|
|
|
match (self.inner().as_ref(), other.inner().as_ref()) {
|
|
|
|
(ValueInner::Float(left), ValueInner::Float(right)) => Ok(Value::float(left - right)),
|
|
|
|
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
|
|
|
|
Ok(Value::integer(left.saturating_sub(*right)))
|
|
|
|
}
|
|
|
|
_ => Err(ValueError::CannotSubtract(self.clone(), other.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multiply(&self, other: &Value) -> Result<Value, ValueError> {
|
|
|
|
match (self.inner().as_ref(), other.inner().as_ref()) {
|
|
|
|
(ValueInner::Float(left), ValueInner::Float(right)) => Ok(Value::float(left * right)),
|
|
|
|
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
|
|
|
|
Ok(Value::integer(left * right))
|
|
|
|
}
|
|
|
|
_ => Err(ValueError::CannotMultiply(self.clone(), other.clone())),
|
|
|
|
}
|
|
|
|
}
|
2024-03-06 20:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self.inner().as_ref() {
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::Boolean(boolean) => write!(f, "{boolean}"),
|
2024-06-24 18:44:10 +00:00
|
|
|
ValueInner::Float(float) => {
|
2024-08-09 04:31:38 +00:00
|
|
|
if float == &f64::INFINITY {
|
|
|
|
return write!(f, "Infinity");
|
|
|
|
}
|
|
|
|
|
|
|
|
if float == &f64::NEG_INFINITY {
|
|
|
|
return write!(f, "-Infinity");
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:44:10 +00:00
|
|
|
write!(f, "{float}")?;
|
|
|
|
|
|
|
|
if &float.floor() == float {
|
|
|
|
write!(f, ".0")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-08-07 14:03:33 +00:00
|
|
|
ValueInner::Function(function) => write!(f, "{function}"),
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::Integer(integer) => write!(f, "{integer}"),
|
|
|
|
ValueInner::List(list) => {
|
2024-04-22 05:51:34 +00:00
|
|
|
write!(f, "[")?;
|
2024-03-08 03:20:59 +00:00
|
|
|
|
2024-07-12 14:20:52 +00:00
|
|
|
for (index, value) in list.iter().enumerate() {
|
2024-04-22 05:51:34 +00:00
|
|
|
if index == list.len() - 1 {
|
2024-06-19 08:56:56 +00:00
|
|
|
write!(f, "{}", value)?;
|
2024-04-22 05:51:34 +00:00
|
|
|
} else {
|
2024-06-19 08:56:56 +00:00
|
|
|
write!(f, "{}, ", value)?;
|
2024-04-22 05:51:34 +00:00
|
|
|
}
|
2024-03-08 03:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
write!(f, "]")
|
2024-03-08 03:20:59 +00:00
|
|
|
}
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::Map(map) => {
|
2024-06-24 08:16:05 +00:00
|
|
|
write!(f, "{{ ")?;
|
|
|
|
|
2024-07-12 14:20:52 +00:00
|
|
|
for (index, (key, value)) in map.iter().enumerate() {
|
2024-06-26 18:21:55 +00:00
|
|
|
write!(f, "{key} = {value}")?;
|
2024-03-07 21:19:24 +00:00
|
|
|
|
2024-06-24 08:16:05 +00:00
|
|
|
if index != map.len() - 1 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
2024-03-07 21:19:24 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:16:05 +00:00
|
|
|
write!(f, " }}")
|
2024-03-07 21:19:24 +00:00
|
|
|
}
|
2024-06-26 15:35:39 +00:00
|
|
|
ValueInner::Range(range) => write!(f, "{}..{}", range.start, range.end),
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::String(string) => write!(f, "{string}"),
|
2024-03-06 20:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-25 19:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Value {}
|
|
|
|
|
|
|
|
impl PartialOrd for Value {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Value {
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
self.0.as_ref().cmp(other.0.as_ref())
|
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 18:47:15 +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
|
|
|
|
S: serde::Serializer,
|
2024-06-19 07:32:51 +00:00
|
|
|
{
|
|
|
|
match self.0.as_ref() {
|
|
|
|
ValueInner::Boolean(boolean) => serializer.serialize_bool(*boolean),
|
|
|
|
ValueInner::Float(float) => serializer.serialize_f64(*float),
|
2024-08-07 14:03:33 +00:00
|
|
|
ValueInner::Function(function) => function.serialize(serializer),
|
2024-06-19 07:32:51 +00:00
|
|
|
ValueInner::Integer(integer) => serializer.serialize_i64(*integer),
|
|
|
|
ValueInner::List(list) => {
|
|
|
|
let mut list_ser = serializer.serialize_seq(Some(list.len()))?;
|
|
|
|
|
|
|
|
for item in list {
|
2024-06-19 08:56:56 +00:00
|
|
|
list_ser.serialize_element(&item)?;
|
2024-06-19 07:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list_ser.end()
|
|
|
|
}
|
|
|
|
ValueInner::Map(map) => {
|
|
|
|
let mut map_ser = serializer.serialize_map(Some(map.len()))?;
|
|
|
|
|
|
|
|
for (identifier, value) in map {
|
|
|
|
map_ser.serialize_entry(identifier, value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
map_ser.end()
|
|
|
|
}
|
|
|
|
ValueInner::Range(range) => {
|
|
|
|
let mut tuple_ser = serializer.serialize_tuple(2)?;
|
|
|
|
|
|
|
|
tuple_ser.serialize_element(&range.start)?;
|
|
|
|
tuple_ser.serialize_element(&range.end)?;
|
|
|
|
|
|
|
|
tuple_ser.end()
|
|
|
|
}
|
|
|
|
ValueInner::String(string) => serializer.serialize_str(string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ValueVisitor;
|
|
|
|
|
|
|
|
impl<'de> Visitor<'de> for ValueVisitor {
|
|
|
|
type Value = Value;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter
|
|
|
|
.write_str("a boolean, float, function, integer, list, map, range, string or structure")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2024-06-19 08:56:56 +00:00
|
|
|
Ok(Value::boolean(v))
|
2024-06-19 07:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_i64(v as i64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
Ok(Value::integer(v))
|
|
|
|
}
|
|
|
|
|
2024-06-19 08:56:56 +00:00
|
|
|
fn visit_i128<E>(self, _: i128) -> Result<Self::Value, E>
|
2024-06-19 07:32:51 +00:00
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_u64(v as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
Ok(Value::integer(v as i64))
|
|
|
|
}
|
|
|
|
|
2024-06-19 08:56:56 +00:00
|
|
|
fn visit_u128<E>(self, _: u128) -> Result<Self::Value, E>
|
2024-06-19 07:32:51 +00:00
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
2024-06-04 18:47:15 +00:00
|
|
|
{
|
|
|
|
todo!()
|
|
|
|
}
|
2024-06-19 07:32:51 +00:00
|
|
|
|
|
|
|
fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_f64(v as f64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2024-06-19 08:56:56 +00:00
|
|
|
Ok(Value::float(v))
|
2024-06-19 07:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(v.encode_utf8(&mut [0u8; 4]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
2024-06-19 08:56:56 +00:00
|
|
|
Ok(Value::string(v))
|
2024-06-19 07:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_str(&v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Bytes(v),
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_bytes(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
self.visit_bytes(&v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_none<E>(self) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Option,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let _ = deserializer;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Option,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_unit<E>(self) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: serde::de::Error,
|
|
|
|
{
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Unit,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let _ = deserializer;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::NewtypeStruct,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-06-19 08:56:56 +00:00
|
|
|
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
2024-06-19 07:32:51 +00:00
|
|
|
where
|
|
|
|
A: serde::de::SeqAccess<'de>,
|
|
|
|
{
|
2024-06-19 08:56:56 +00:00
|
|
|
let mut list = Vec::with_capacity(seq.size_hint().unwrap_or(10));
|
|
|
|
|
|
|
|
while let Some(element) = seq.next_element()? {
|
|
|
|
list.push(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::list(list))
|
2024-06-19 07:32:51 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 08:56:56 +00:00
|
|
|
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
2024-06-19 07:32:51 +00:00
|
|
|
where
|
|
|
|
A: serde::de::MapAccess<'de>,
|
|
|
|
{
|
2024-08-05 22:45:43 +00:00
|
|
|
let mut btree = BTreeMap::new();
|
2024-06-19 08:56:56 +00:00
|
|
|
|
|
|
|
while let Some((key, value)) = map.next_entry()? {
|
|
|
|
btree.insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Value::map(btree))
|
2024-06-19 07:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: serde::de::EnumAccess<'de>,
|
|
|
|
{
|
|
|
|
let _ = data;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Enum,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
2024-06-04 18:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Value {
|
2024-06-19 07:32:51 +00:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2024-06-04 18:47:15 +00:00
|
|
|
where
|
2024-06-19 07:32:51 +00:00
|
|
|
D: Deserializer<'de>,
|
2024-06-04 18:47:15 +00:00
|
|
|
{
|
2024-06-19 07:32:51 +00:00
|
|
|
deserializer.deserialize_any(ValueVisitor)
|
2024-06-04 18:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 17:55:43 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2024-02-25 18:49:26 +00:00
|
|
|
pub enum ValueInner {
|
|
|
|
Boolean(bool),
|
|
|
|
Float(f64),
|
2024-08-07 14:03:33 +00:00
|
|
|
Function(Function),
|
2024-02-25 18:49:26 +00:00
|
|
|
Integer(i64),
|
2024-06-19 08:56:56 +00:00
|
|
|
List(Vec<Value>),
|
2024-02-28 23:36:47 +00:00
|
|
|
Map(BTreeMap<Identifier, Value>),
|
2024-02-25 18:49:26 +00:00
|
|
|
Range(Range<i64>),
|
|
|
|
String(String),
|
|
|
|
}
|
|
|
|
|
2024-03-20 12:36:18 +00:00
|
|
|
impl ValueInner {
|
2024-08-07 14:03:33 +00:00
|
|
|
pub fn r#type(&self, variables: &HashMap<Identifier, Value>) -> Type {
|
2024-08-04 00:23:52 +00:00
|
|
|
match self {
|
2024-03-20 12:36:18 +00:00
|
|
|
ValueInner::Boolean(_) => Type::Boolean,
|
|
|
|
ValueInner::Float(_) => Type::Float,
|
2024-08-07 14:03:33 +00:00
|
|
|
ValueInner::Function(function) => Type::Function {
|
|
|
|
type_parameters: function.type_parameters.clone(),
|
|
|
|
value_parameters: function.value_parameters.clone(),
|
|
|
|
return_type: function.return_type(variables).map(Box::new),
|
|
|
|
},
|
2024-03-20 12:36:18 +00:00
|
|
|
ValueInner::Integer(_) => Type::Integer,
|
|
|
|
ValueInner::List(values) => {
|
2024-08-07 14:03:33 +00:00
|
|
|
let item_type = values.first().unwrap().r#type(variables);
|
2024-03-20 12:36:18 +00:00
|
|
|
|
2024-06-17 14:10:06 +00:00
|
|
|
Type::List {
|
|
|
|
length: values.len(),
|
|
|
|
item_type: Box::new(item_type),
|
2024-03-20 12:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-24 08:02:44 +00:00
|
|
|
ValueInner::Map(value_map) => {
|
2024-08-05 22:45:43 +00:00
|
|
|
let mut type_map = BTreeMap::new();
|
2024-06-24 08:02:44 +00:00
|
|
|
|
|
|
|
for (identifier, value) in value_map {
|
2024-08-07 14:03:33 +00:00
|
|
|
let r#type = value.r#type(variables);
|
2024-06-24 08:02:44 +00:00
|
|
|
|
|
|
|
type_map.insert(identifier.clone(), r#type);
|
|
|
|
}
|
|
|
|
|
|
|
|
Type::Map(type_map)
|
|
|
|
}
|
2024-03-20 12:36:18 +00:00
|
|
|
ValueInner::Range(_) => Type::Range,
|
|
|
|
ValueInner::String(_) => Type::String,
|
2024-08-04 00:23:52 +00:00
|
|
|
}
|
2024-03-20 12:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 19:26:22 +00:00
|
|
|
impl Eq for ValueInner {}
|
|
|
|
|
|
|
|
impl PartialOrd for ValueInner {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for ValueInner {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
use ValueInner::*;
|
|
|
|
|
|
|
|
match (self, other) {
|
|
|
|
(Boolean(left), Boolean(right)) => left.cmp(right),
|
|
|
|
(Boolean(_), _) => Ordering::Greater,
|
|
|
|
(Float(left), Float(right)) => left.total_cmp(right),
|
|
|
|
(Float(_), _) => Ordering::Greater,
|
2024-08-07 14:03:33 +00:00
|
|
|
(Function(left), Function(right)) => left.cmp(right),
|
|
|
|
(Function(_), _) => Ordering::Greater,
|
2024-02-25 19:26:22 +00:00
|
|
|
(Integer(left), Integer(right)) => left.cmp(right),
|
|
|
|
(Integer(_), _) => Ordering::Greater,
|
|
|
|
(List(left), List(right)) => left.cmp(right),
|
|
|
|
(List(_), _) => Ordering::Greater,
|
2024-02-28 23:36:47 +00:00
|
|
|
(Map(left), Map(right)) => left.cmp(right),
|
|
|
|
(Map(_), _) => Ordering::Greater,
|
2024-02-25 19:26:22 +00:00
|
|
|
(Range(left), Range(right)) => {
|
|
|
|
let start_cmp = left.start.cmp(&right.start);
|
|
|
|
|
|
|
|
if start_cmp.is_eq() {
|
|
|
|
left.end.cmp(&right.end)
|
|
|
|
} else {
|
|
|
|
start_cmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Range(_), _) => Ordering::Greater,
|
|
|
|
(String(left), String(right)) => left.cmp(right),
|
|
|
|
(String(_), _) => Ordering::Greater,
|
2024-07-12 14:20:52 +00:00
|
|
|
}
|
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-07 14:03:33 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
|
|
pub struct Function {
|
|
|
|
pub name: Identifier,
|
|
|
|
pub type_parameters: Option<Vec<Type>>,
|
|
|
|
pub value_parameters: Option<Vec<(Identifier, Type)>>,
|
2024-08-07 23:03:50 +00:00
|
|
|
pub body: AbstractSyntaxTree,
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
2024-08-07 22:24:25 +00:00
|
|
|
pub fn call(
|
|
|
|
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>>,
|
|
|
|
variables: &HashMap<Identifier, Value>,
|
2024-08-07 23:03:50 +00:00
|
|
|
) -> Result<Option<Value>, VmError> {
|
2024-08-07 22:24:25 +00:00
|
|
|
let mut new_variables = variables.clone();
|
|
|
|
|
|
|
|
if let (Some(value_parameters), Some(value_arguments)) =
|
|
|
|
(self.value_parameters, value_arguments)
|
|
|
|
{
|
|
|
|
for ((identifier, _), value) in value_parameters.into_iter().zip(value_arguments) {
|
|
|
|
new_variables.insert(identifier, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut vm = Vm::new(self.body);
|
|
|
|
|
|
|
|
vm.run(&mut new_variables)
|
|
|
|
}
|
|
|
|
|
2024-08-07 14:03:33 +00:00
|
|
|
pub fn return_type(&self, variables: &HashMap<Identifier, Value>) -> Option<Type> {
|
2024-08-07 22:24:25 +00:00
|
|
|
self.body
|
|
|
|
.nodes
|
|
|
|
.iter()
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.statement
|
|
|
|
.expected_type(variables)
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Function {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.name)?;
|
|
|
|
|
|
|
|
if let Some(type_parameters) = &self.type_parameters {
|
|
|
|
write!(f, "<")?;
|
|
|
|
|
|
|
|
for (index, type_parameter) in type_parameters.iter().enumerate() {
|
|
|
|
if index > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{}", type_parameter)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ">")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "(")?;
|
|
|
|
|
|
|
|
if let Some(value_paramers) = &self.value_parameters {
|
|
|
|
for (index, (identifier, r#type)) in value_paramers.iter().enumerate() {
|
|
|
|
if index > 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{identifier}: {type}")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ") {{")?;
|
|
|
|
|
2024-08-07 22:24:25 +00:00
|
|
|
for statement in &self.body.nodes {
|
2024-08-07 14:03:33 +00:00
|
|
|
write!(f, "{}", statement)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "}}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2024-08-05 02:15:31 +00:00
|
|
|
pub enum ValueError {
|
|
|
|
CannotAdd(Value, Value),
|
2024-08-09 03:28:47 +00:00
|
|
|
CannotMultiply(Value, Value),
|
|
|
|
CannotSubtract(Value, Value),
|
2024-08-05 18:31:08 +00:00
|
|
|
PropertyNotFound { value: Value, property: Identifier },
|
2024-08-05 18:58:58 +00:00
|
|
|
IndexOutOfBounds { value: Value, index: i64 },
|
|
|
|
ExpectedList(Value),
|
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 03:28:47 +00:00
|
|
|
ValueError::CannotMultiply(left, right) => {
|
|
|
|
write!(f, "Cannot multiply {} and {}", left, right)
|
|
|
|
}
|
|
|
|
ValueError::CannotSubtract(left, right) => {
|
|
|
|
write!(f, "Cannot subtract {} and {}", left, right)
|
|
|
|
}
|
2024-08-09 00:58:56 +00:00
|
|
|
ValueError::PropertyNotFound { value, property } => {
|
|
|
|
write!(f, "{} does not have a property named {}", value, property)
|
|
|
|
}
|
|
|
|
ValueError::IndexOutOfBounds { value, index } => {
|
|
|
|
write!(f, "{} does not have an index of {}", value, index)
|
|
|
|
}
|
|
|
|
ValueError::ExpectedList(value) => write!(f, "{} is not a list", value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|