2024-06-26 16:50:46 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
|
|
|
|
2024-06-04 18:47:15 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-03-18 01:07:03 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
value::ValueInner,
|
|
|
|
};
|
|
|
|
|
2024-07-04 18:39:45 +00:00
|
|
|
use super::{AbstractNode, Evaluation, Expression, SourcePosition, Type, ValueNode, WithPosition};
|
2024-03-18 01:07:03 +00:00
|
|
|
|
2024-06-04 18:47:15 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
|
2024-03-18 01:07:03 +00:00
|
|
|
pub struct MapIndex {
|
2024-06-17 14:10:06 +00:00
|
|
|
collection: Expression,
|
|
|
|
index: Expression,
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MapIndex {
|
2024-06-17 14:10:06 +00:00
|
|
|
pub fn new(left: Expression, right: Expression) -> Self {
|
2024-04-22 05:51:34 +00:00
|
|
|
Self {
|
|
|
|
collection: left,
|
|
|
|
index: right,
|
|
|
|
}
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 03:37:25 +00:00
|
|
|
impl AbstractNode for MapIndex {
|
2024-07-02 17:11:31 +00:00
|
|
|
fn define_and_validate(
|
|
|
|
&self,
|
|
|
|
context: &Context,
|
|
|
|
_manage_memory: bool,
|
2024-07-04 18:39:45 +00:00
|
|
|
scope: SourcePosition,
|
2024-07-02 17:11:31 +00:00
|
|
|
) -> Result<(), ValidationError> {
|
|
|
|
self.collection
|
2024-07-04 18:39:45 +00:00
|
|
|
.define_and_validate(context, _manage_memory, scope)?;
|
2024-06-24 08:02:44 +00:00
|
|
|
|
2024-06-24 09:26:49 +00:00
|
|
|
let collection_type = if let Some(r#type) = self.collection.expected_type(context)? {
|
|
|
|
r#type
|
|
|
|
} else {
|
2024-07-01 20:59:39 +00:00
|
|
|
return Err(ValidationError::ExpectedValueStatement(
|
2024-06-24 09:26:49 +00:00
|
|
|
self.collection.position(),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
if let (Type::Map(fields), Expression::Identifier(identifier)) =
|
|
|
|
(collection_type, &self.index)
|
|
|
|
{
|
|
|
|
if !fields.contains_key(&identifier.node) {
|
|
|
|
return Err(ValidationError::FieldNotFound {
|
|
|
|
identifier: identifier.node.clone(),
|
|
|
|
position: identifier.position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Expression::Identifier(_) = &self.index {
|
2024-06-24 08:02:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-07-04 18:39:45 +00:00
|
|
|
self.index
|
|
|
|
.define_and_validate(context, _manage_memory, scope)
|
2024-06-24 08:02:44 +00:00
|
|
|
}
|
2024-06-16 07:12:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 14:10:06 +00:00
|
|
|
fn evaluate(
|
|
|
|
self,
|
2024-06-22 03:37:25 +00:00
|
|
|
context: &Context,
|
2024-06-17 14:10:06 +00:00
|
|
|
_manage_memory: bool,
|
2024-07-04 18:39:45 +00:00
|
|
|
scope: SourcePosition,
|
2024-06-22 00:59:38 +00:00
|
|
|
) -> Result<Option<Evaluation>, RuntimeError> {
|
2024-06-16 07:12:04 +00:00
|
|
|
let collection_position = self.collection.position();
|
2024-07-04 18:39:45 +00:00
|
|
|
let evaluation = self.collection.evaluate(context, _manage_memory, scope)?;
|
2024-06-22 04:58:30 +00:00
|
|
|
let collection = if let Some(Evaluation::Return(value)) = evaluation {
|
2024-06-16 07:12:04 +00:00
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
2024-07-01 20:59:39 +00:00
|
|
|
ValidationError::ExpectedValueStatement(collection_position),
|
2024-06-16 07:12:04 +00:00
|
|
|
));
|
|
|
|
};
|
|
|
|
|
2024-06-17 14:10:06 +00:00
|
|
|
if let (ValueInner::Map(map), Expression::Identifier(index)) =
|
2024-06-16 07:12:04 +00:00
|
|
|
(collection.inner().as_ref(), self.index)
|
|
|
|
{
|
2024-06-22 04:58:30 +00:00
|
|
|
let eval_option = map
|
2024-06-16 07:12:04 +00:00
|
|
|
.get(&index.node)
|
2024-06-22 04:58:30 +00:00
|
|
|
.cloned()
|
|
|
|
.map(|value| Evaluation::Return(value));
|
2024-06-16 07:12:04 +00:00
|
|
|
|
2024-06-22 04:58:30 +00:00
|
|
|
Ok(eval_option)
|
2024-06-16 07:12:04 +00:00
|
|
|
} else {
|
|
|
|
Err(RuntimeError::ValidationFailure(
|
|
|
|
ValidationError::CannotIndex {
|
|
|
|
r#type: collection.r#type(context)?,
|
|
|
|
position: collection_position,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 03:37:25 +00:00
|
|
|
fn expected_type(&self, context: &Context) -> Result<Option<Type>, ValidationError> {
|
2024-06-17 14:10:06 +00:00
|
|
|
if let (Expression::Identifier(collection), Expression::Identifier(index)) =
|
2024-04-22 05:51:34 +00:00
|
|
|
(&self.collection, &self.index)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
2024-07-01 18:23:01 +00:00
|
|
|
let r#type = if let Some(r#type) = context.get_type(&collection.node)? {
|
|
|
|
r#type
|
2024-05-18 20:21:46 +00:00
|
|
|
} else {
|
|
|
|
return Err(ValidationError::VariableNotFound {
|
2024-06-16 07:12:04 +00:00
|
|
|
identifier: collection.node.clone(),
|
2024-05-18 20:21:46 +00:00
|
|
|
position: collection.position,
|
|
|
|
});
|
|
|
|
};
|
2024-03-18 01:07:03 +00:00
|
|
|
|
2024-07-01 18:23:01 +00:00
|
|
|
if let Type::Map(map) = r#type {
|
|
|
|
return if let Some(r#type) = map.get(&index.node) {
|
|
|
|
Ok(Some(r#type.clone()))
|
2024-03-18 01:07:03 +00:00
|
|
|
} else {
|
2024-06-24 09:26:49 +00:00
|
|
|
Err(ValidationError::FieldNotFound {
|
2024-06-16 07:12:04 +00:00
|
|
|
identifier: index.node.clone(),
|
2024-04-22 05:51:34 +00:00
|
|
|
position: index.position,
|
2024-03-18 09:39:09 +00:00
|
|
|
})
|
2024-03-18 01:07:03 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-25 04:16:55 +00:00
|
|
|
if let (
|
2024-06-17 14:10:06 +00:00
|
|
|
Expression::Value(WithPosition {
|
2024-06-16 07:12:04 +00:00
|
|
|
node: ValueNode::Map(properties),
|
2024-03-25 04:16:55 +00:00
|
|
|
..
|
|
|
|
}),
|
2024-06-17 14:10:06 +00:00
|
|
|
Expression::Identifier(index),
|
2024-04-22 05:51:34 +00:00
|
|
|
) = (&self.collection, &self.index)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
2024-06-17 14:10:06 +00:00
|
|
|
for (property, constructor_option, expression) in properties {
|
2024-06-16 07:12:04 +00:00
|
|
|
if property == &index.node {
|
2024-06-17 14:10:06 +00:00
|
|
|
return if let Some(constructor) = constructor_option {
|
2024-06-17 14:50:06 +00:00
|
|
|
let r#type = constructor.clone().construct(&context)?;
|
2024-06-17 14:10:06 +00:00
|
|
|
|
2024-06-22 00:59:38 +00:00
|
|
|
Ok(Some(r#type))
|
2024-06-16 07:12:04 +00:00
|
|
|
} else {
|
2024-06-22 00:59:38 +00:00
|
|
|
expression.expected_type(context)
|
2024-06-16 07:12:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 04:58:30 +00:00
|
|
|
return Ok(None);
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 21:34:36 +00:00
|
|
|
if let (
|
2024-06-17 14:10:06 +00:00
|
|
|
Expression::Value(WithPosition {
|
2024-06-16 07:12:04 +00:00
|
|
|
node: ValueNode::Structure { fields, .. },
|
2024-03-25 04:16:55 +00:00
|
|
|
..
|
|
|
|
}),
|
2024-06-17 14:10:06 +00:00
|
|
|
Expression::Identifier(index),
|
2024-04-22 05:51:34 +00:00
|
|
|
) = (&self.collection, &self.index)
|
2024-03-24 21:34:36 +00:00
|
|
|
{
|
|
|
|
return if let Some(type_result) = fields.iter().find_map(|(property, expression)| {
|
2024-06-17 14:10:06 +00:00
|
|
|
if property.node == index.node {
|
2024-03-25 04:16:55 +00:00
|
|
|
Some(expression.expected_type(context))
|
2024-03-24 21:34:36 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
type_result
|
|
|
|
} else {
|
2024-06-22 04:58:30 +00:00
|
|
|
Ok(None)
|
2024-03-24 21:34:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-06-22 04:58:30 +00:00
|
|
|
let collection_type = if let Some(r#type) = self.collection.expected_type(context)? {
|
|
|
|
r#type
|
|
|
|
} else {
|
2024-07-01 20:59:39 +00:00
|
|
|
return Err(ValidationError::ExpectedValueStatement(
|
2024-06-22 04:58:30 +00:00
|
|
|
self.collection.position(),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
Err(ValidationError::CannotIndex {
|
2024-06-22 04:58:30 +00:00
|
|
|
r#type: collection_type,
|
2024-04-22 05:51:34 +00:00
|
|
|
position: self.collection.position(),
|
2024-03-18 01:07:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-26 16:50:46 +00:00
|
|
|
|
|
|
|
impl Display for MapIndex {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
let MapIndex { collection, index } = self;
|
|
|
|
|
|
|
|
write!(f, "{collection}.{index}")
|
|
|
|
}
|
|
|
|
}
|