2024-02-25 18:49:26 +00:00
|
|
|
use std::{
|
2024-02-25 19:26:22 +00:00
|
|
|
cmp::Ordering,
|
2024-02-28 23:36:47 +00:00
|
|
|
collections::BTreeMap,
|
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,
|
|
|
|
ser::{SerializeMap, SerializeSeq, SerializeStruct, SerializeTuple},
|
|
|
|
Deserialize, Deserializer, Serialize,
|
|
|
|
};
|
2024-06-04 18:47:15 +00:00
|
|
|
|
2024-03-06 22:32:31 +00:00
|
|
|
use crate::{
|
2024-06-17 14:10:06 +00:00
|
|
|
abstract_tree::{AbstractNode, Block, Evaluation, Type, WithPosition},
|
2024-03-09 03:34:17 +00:00
|
|
|
context::Context,
|
2024-03-18 12:15:30 +00:00
|
|
|
error::{RuntimeError, ValidationError},
|
2024-03-25 04:16:55 +00:00
|
|
|
identifier::Identifier,
|
2024-03-06 22:32:31 +00:00
|
|
|
};
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn integer(integer: i64) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Integer(integer)))
|
|
|
|
}
|
|
|
|
|
2024-03-24 16:21:08 +00:00
|
|
|
pub fn list(list: Vec<WithPosition<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-03-17 05:26:05 +00:00
|
|
|
pub fn function(
|
2024-06-17 14:10:06 +00:00
|
|
|
type_parameters: Option<Vec<Identifier>>,
|
|
|
|
value_parameters: Vec<(Identifier, Type)>,
|
|
|
|
return_type: Type,
|
|
|
|
body: Block,
|
2024-03-17 05:26:05 +00:00
|
|
|
) -> Self {
|
2024-04-21 21:00:08 +00:00
|
|
|
Value(Arc::new(ValueInner::Function(Function {
|
2024-06-17 14:10:06 +00:00
|
|
|
type_parameters,
|
|
|
|
value_parameters,
|
2024-04-21 21:00:08 +00:00
|
|
|
return_type,
|
|
|
|
body,
|
|
|
|
})))
|
2024-03-08 21:14:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 04:16:55 +00:00
|
|
|
pub fn structure(name: WithPosition<Identifier>, fields: Vec<(Identifier, Value)>) -> Self {
|
2024-03-19 23:16:33 +00:00
|
|
|
Value(Arc::new(ValueInner::Structure { name, fields }))
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:31:52 +00:00
|
|
|
pub fn r#type(&self, context: &Context) -> Result<Type, ValidationError> {
|
2024-03-20 12:36:18 +00:00
|
|
|
self.0.r#type(context)
|
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-03-24 16:21:08 +00:00
|
|
|
pub fn as_list(&self) -> Option<&Vec<WithPosition<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-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}"),
|
|
|
|
ValueInner::Float(float) => write!(f, "{float}"),
|
|
|
|
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-04-22 05:51:34 +00:00
|
|
|
for (index, value) in list.into_iter().enumerate() {
|
|
|
|
if index == list.len() - 1 {
|
2024-06-16 07:12:04 +00:00
|
|
|
write!(f, "{}", value.node)?;
|
2024-04-22 05:51:34 +00:00
|
|
|
} else {
|
2024-06-16 07:12:04 +00:00
|
|
|
write!(f, "{}, ", value.node)?;
|
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-04-22 05:51:34 +00:00
|
|
|
write!(f, "[")?;
|
2024-03-07 21:19:24 +00:00
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
for (key, value) in map {
|
|
|
|
writeln!(f, "{key} = {value},")?;
|
2024-03-07 21:19:24 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
write!(f, "]")
|
2024-03-07 21:19:24 +00:00
|
|
|
}
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::Range(_) => todo!(),
|
|
|
|
ValueInner::String(string) => write!(f, "{string}"),
|
2024-04-21 21:00:08 +00:00
|
|
|
ValueInner::Function(Function {
|
2024-06-17 14:10:06 +00:00
|
|
|
type_parameters,
|
|
|
|
value_parameters: parameters,
|
2024-03-08 21:14:47 +00:00
|
|
|
return_type,
|
|
|
|
body,
|
2024-04-21 21:00:08 +00:00
|
|
|
}) => {
|
2024-06-17 14:10:06 +00:00
|
|
|
if let Some(type_parameters) = type_parameters {
|
2024-03-24 13:10:49 +00:00
|
|
|
write!(f, "(")?;
|
|
|
|
|
2024-06-17 14:10:06 +00:00
|
|
|
for (index, r#type) in type_parameters.into_iter().enumerate() {
|
|
|
|
if index == type_parameters.len() - 1 {
|
|
|
|
write!(f, "{}", r#type)?;
|
2024-03-24 13:10:49 +00:00
|
|
|
} else {
|
2024-06-17 14:10:06 +00:00
|
|
|
write!(f, "{} ", r#type)?;
|
2024-03-24 13:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, ")")?;
|
|
|
|
}
|
|
|
|
|
2024-03-08 21:14:47 +00:00
|
|
|
write!(f, "(")?;
|
|
|
|
|
|
|
|
for (identifier, r#type) in parameters {
|
2024-06-17 14:10:06 +00:00
|
|
|
write!(f, "{identifier}: {}", r#type)?;
|
2024-03-08 21:14:47 +00:00
|
|
|
}
|
2024-03-08 03:20:59 +00:00
|
|
|
|
2024-06-17 14:10:06 +00:00
|
|
|
write!(f, "): {} {:?}", return_type, body)
|
2024-03-08 21:14:47 +00:00
|
|
|
}
|
2024-03-19 23:33:02 +00:00
|
|
|
ValueInner::Structure { name, fields } => {
|
2024-06-16 07:12:04 +00:00
|
|
|
write!(f, "{}\n{{", name.node)?;
|
2024-03-19 23:33:02 +00:00
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
for (key, value) in fields {
|
|
|
|
writeln!(f, "{key} = {value},")?;
|
2024-03-19 23:33:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
write!(f, "}}")
|
2024-03-19 23:33:02 +00:00
|
|
|
}
|
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),
|
|
|
|
ValueInner::Function(Function {
|
|
|
|
type_parameters,
|
|
|
|
value_parameters,
|
|
|
|
return_type,
|
|
|
|
body,
|
|
|
|
}) => {
|
|
|
|
let mut struct_ser = serializer.serialize_struct("Function", 4)?;
|
|
|
|
|
|
|
|
struct_ser.serialize_field("type_parameters", type_parameters)?;
|
|
|
|
struct_ser.serialize_field("value_parameters", value_parameters)?;
|
|
|
|
struct_ser.serialize_field("return_type", return_type)?;
|
|
|
|
struct_ser.serialize_field("body", body)?;
|
|
|
|
|
|
|
|
struct_ser.end()
|
|
|
|
}
|
|
|
|
ValueInner::Integer(integer) => serializer.serialize_i64(*integer),
|
|
|
|
ValueInner::List(list) => {
|
|
|
|
let mut list_ser = serializer.serialize_seq(Some(list.len()))?;
|
|
|
|
|
|
|
|
for item in list {
|
|
|
|
list_ser.serialize_element(&item.node)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
ValueInner::Structure { name, fields } => {
|
|
|
|
let mut struct_ser = serializer.serialize_struct("Structure", 2)?;
|
|
|
|
|
|
|
|
struct_ser.serialize_field("name", name)?;
|
|
|
|
struct_ser.serialize_field("fields", fields)?;
|
|
|
|
|
|
|
|
struct_ser.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Bool(v),
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Float(v),
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
{
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Str(v),
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: serde::de::SeqAccess<'de>,
|
|
|
|
{
|
|
|
|
let _ = seq;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Seq,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
|
|
|
|
where
|
|
|
|
A: serde::de::MapAccess<'de>,
|
|
|
|
{
|
|
|
|
let _ = map;
|
|
|
|
Err(serde::de::Error::invalid_type(
|
|
|
|
serde::de::Unexpected::Map,
|
|
|
|
&self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2024-02-25 18:49:26 +00:00
|
|
|
pub enum ValueInner {
|
|
|
|
Boolean(bool),
|
|
|
|
Float(f64),
|
2024-03-08 21:14:47 +00:00
|
|
|
Function(Function),
|
2024-02-25 18:49:26 +00:00
|
|
|
Integer(i64),
|
2024-03-24 16:21:08 +00:00
|
|
|
List(Vec<WithPosition<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-19 22:31:52 +00:00
|
|
|
Structure {
|
2024-03-25 04:16:55 +00:00
|
|
|
name: WithPosition<Identifier>,
|
2024-03-19 23:16:33 +00:00
|
|
|
fields: Vec<(Identifier, Value)>,
|
2024-03-19 22:31:52 +00:00
|
|
|
},
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 12:36:18 +00:00
|
|
|
impl ValueInner {
|
|
|
|
pub fn r#type(&self, context: &Context) -> Result<Type, ValidationError> {
|
|
|
|
let r#type = match self {
|
|
|
|
ValueInner::Boolean(_) => Type::Boolean,
|
|
|
|
ValueInner::Float(_) => Type::Float,
|
|
|
|
ValueInner::Integer(_) => Type::Integer,
|
|
|
|
ValueInner::List(values) => {
|
2024-06-17 14:10:06 +00:00
|
|
|
let item_type = values.first().unwrap().node.r#type(context)?;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
ValueInner::Map(_) => Type::Map,
|
|
|
|
ValueInner::Range(_) => Type::Range,
|
|
|
|
ValueInner::String(_) => Type::String,
|
2024-06-19 02:01:18 +00:00
|
|
|
ValueInner::Function(function) => {
|
|
|
|
let value_parameters = function
|
|
|
|
.value_parameters()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, r#type)| r#type)
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Type::Function {
|
|
|
|
type_parameters: function.type_parameters().clone(),
|
|
|
|
value_parameters,
|
|
|
|
return_type: Box::new(function.return_type.clone()),
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 12:36:18 +00:00
|
|
|
ValueInner::Structure { name, .. } => {
|
2024-06-16 07:12:04 +00:00
|
|
|
if let Some(r#type) = context.get_type(&name.node)? {
|
2024-03-20 12:36:18 +00:00
|
|
|
r#type
|
|
|
|
} else {
|
2024-03-25 04:16:55 +00:00
|
|
|
return Err(ValidationError::VariableNotFound {
|
2024-06-16 07:12:04 +00:00
|
|
|
identifier: name.node.clone(),
|
2024-03-25 04:16:55 +00:00
|
|
|
position: name.position,
|
|
|
|
});
|
2024-03-20 12:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(r#type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
(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-03-08 21:14:47 +00:00
|
|
|
(Function(left), Function(right)) => left.cmp(right),
|
|
|
|
(Function(_), _) => Ordering::Greater,
|
2024-03-19 22:31:52 +00:00
|
|
|
(
|
|
|
|
Structure {
|
|
|
|
name: left_name,
|
|
|
|
fields: left_fields,
|
|
|
|
},
|
|
|
|
Structure {
|
|
|
|
name: right_name,
|
|
|
|
fields: right_fields,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
let name_cmp = left_name.cmp(right_name);
|
|
|
|
|
|
|
|
if name_cmp.is_eq() {
|
|
|
|
left_fields.cmp(right_fields)
|
|
|
|
} else {
|
|
|
|
name_cmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Structure { .. }, _) => Ordering::Greater,
|
2024-02-25 19:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 21:14:47 +00:00
|
|
|
|
2024-06-04 18:47:15 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-04-21 21:00:08 +00:00
|
|
|
pub struct Function {
|
2024-06-17 14:10:06 +00:00
|
|
|
type_parameters: Option<Vec<Identifier>>,
|
|
|
|
value_parameters: Vec<(Identifier, Type)>,
|
|
|
|
return_type: Type,
|
|
|
|
body: Block,
|
2024-03-09 03:34:17 +00:00
|
|
|
}
|
|
|
|
|
2024-03-09 13:10:54 +00:00
|
|
|
impl Function {
|
2024-06-17 14:10:06 +00:00
|
|
|
pub fn type_parameters(&self) -> &Option<Vec<Identifier>> {
|
2024-04-21 21:00:08 +00:00
|
|
|
&self.type_parameters
|
|
|
|
}
|
2024-03-09 13:10:54 +00:00
|
|
|
|
2024-06-19 02:01:18 +00:00
|
|
|
pub fn value_parameters(&self) -> &Vec<(Identifier, Type)> {
|
|
|
|
&self.value_parameters
|
|
|
|
}
|
|
|
|
|
2024-06-19 06:32:17 +00:00
|
|
|
pub fn body(&self) -> &Block {
|
|
|
|
&self.body
|
|
|
|
}
|
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
pub fn call(
|
|
|
|
self,
|
|
|
|
arguments: Vec<Value>,
|
|
|
|
context: &mut Context,
|
2024-04-22 07:41:21 +00:00
|
|
|
clear_variables: bool,
|
2024-06-17 14:10:06 +00:00
|
|
|
) -> Result<Evaluation, RuntimeError> {
|
|
|
|
for ((identifier, _), value) in self.value_parameters.into_iter().zip(arguments.into_iter())
|
|
|
|
{
|
2024-04-21 21:00:08 +00:00
|
|
|
context.set_value(identifier.clone(), value)?;
|
|
|
|
}
|
2024-03-09 13:10:54 +00:00
|
|
|
|
2024-06-17 14:10:06 +00:00
|
|
|
self.body.evaluate(context, clear_variables)
|
2024-03-09 13:10:54 +00:00
|
|
|
}
|
|
|
|
}
|