2024-03-18 01:07:03 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
value::ValueInner,
|
|
|
|
};
|
|
|
|
|
2024-03-20 04:28:28 +00:00
|
|
|
use super::{AbstractNode, Action, Expression, Type, ValueNode, WithPosition};
|
2024-03-18 01:07:03 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct MapIndex {
|
2024-03-25 04:16:55 +00:00
|
|
|
left: Expression,
|
|
|
|
right: Expression,
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MapIndex {
|
2024-03-25 04:16:55 +00:00
|
|
|
pub fn new(left: Expression, right: Expression) -> Self {
|
2024-03-18 01:07:03 +00:00
|
|
|
Self { left, right }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 04:28:28 +00:00
|
|
|
impl AbstractNode for MapIndex {
|
2024-03-19 22:31:52 +00:00
|
|
|
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError> {
|
2024-03-18 01:07:03 +00:00
|
|
|
if let (
|
|
|
|
Expression::Identifier(collection_identifier),
|
|
|
|
Expression::Identifier(index_identifier),
|
2024-03-25 04:16:55 +00:00
|
|
|
) = (&self.left, &self.right)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
2024-03-25 04:16:55 +00:00
|
|
|
let collection =
|
|
|
|
if let Some(collection) = context.get_value(&collection_identifier.node)? {
|
|
|
|
collection
|
|
|
|
} else {
|
|
|
|
return Err(ValidationError::VariableNotFound {
|
|
|
|
identifier: collection_identifier.node.clone(),
|
|
|
|
position: collection_identifier.position,
|
|
|
|
});
|
|
|
|
};
|
2024-03-18 01:07:03 +00:00
|
|
|
|
|
|
|
if let ValueInner::Map(map) = collection.inner().as_ref() {
|
2024-03-25 04:16:55 +00:00
|
|
|
return if let Some(value) = map.get(&index_identifier.node) {
|
2024-03-19 22:31:52 +00:00
|
|
|
Ok(value.r#type(context)?)
|
2024-03-18 01:07:03 +00:00
|
|
|
} else {
|
2024-03-18 09:39:09 +00:00
|
|
|
Err(ValidationError::PropertyNotFound {
|
2024-03-25 04:16:55 +00:00
|
|
|
identifier: index_identifier.node.clone(),
|
|
|
|
position: self.right.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 (
|
|
|
|
Expression::Value(WithPosition {
|
|
|
|
node: ValueNode::Map(properties),
|
|
|
|
..
|
|
|
|
}),
|
|
|
|
Expression::Identifier(identifier),
|
|
|
|
) = (&self.left, &self.right)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
|
|
|
return if let Some(type_result) =
|
|
|
|
properties
|
|
|
|
.iter()
|
|
|
|
.find_map(|(property, type_option, expression)| {
|
2024-03-25 04:16:55 +00:00
|
|
|
if property == &identifier.node {
|
2024-03-18 01:07:03 +00:00
|
|
|
if let Some(r#type) = type_option {
|
2024-03-19 22:31:52 +00:00
|
|
|
Some(r#type.node.expected_type(context))
|
2024-03-18 01:07:03 +00:00
|
|
|
} else {
|
2024-03-25 04:16:55 +00:00
|
|
|
Some(expression.expected_type(context))
|
2024-03-18 01:07:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
{
|
|
|
|
type_result
|
|
|
|
} else {
|
|
|
|
Ok(Type::None)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-24 21:34:36 +00:00
|
|
|
if let (
|
2024-03-25 04:16:55 +00:00
|
|
|
Expression::Value(WithPosition {
|
|
|
|
node: ValueNode::Structure { fields, .. },
|
|
|
|
..
|
|
|
|
}),
|
2024-03-24 21:34:36 +00:00
|
|
|
Expression::Identifier(identifier),
|
2024-03-25 04:16:55 +00:00
|
|
|
) = (&self.left, &self.right)
|
2024-03-24 21:34:36 +00:00
|
|
|
{
|
|
|
|
return if let Some(type_result) = fields.iter().find_map(|(property, expression)| {
|
2024-03-25 04:16:55 +00:00
|
|
|
if property == &identifier.node {
|
|
|
|
Some(expression.expected_type(context))
|
2024-03-24 21:34:36 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
type_result
|
|
|
|
} else {
|
|
|
|
Ok(Type::None)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-18 01:07:03 +00:00
|
|
|
Err(ValidationError::CannotIndexWith {
|
2024-03-25 04:16:55 +00:00
|
|
|
collection_type: self.left.expected_type(context)?,
|
|
|
|
collection_position: self.left.position(),
|
|
|
|
index_type: self.right.expected_type(context)?,
|
|
|
|
index_position: self.right.position(),
|
2024-03-18 01:07:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, context: &Context) -> Result<(), ValidationError> {
|
2024-03-25 04:16:55 +00:00
|
|
|
let left_type = self.left.expected_type(context)?;
|
2024-03-18 01:07:03 +00:00
|
|
|
|
2024-03-25 04:16:55 +00:00
|
|
|
if let (
|
|
|
|
Expression::Value(WithPosition {
|
|
|
|
node: ValueNode::Map(_),
|
|
|
|
..
|
|
|
|
}),
|
|
|
|
Expression::Identifier(_),
|
|
|
|
) = (&self.left, &self.right)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
|
|
|
Ok(())
|
|
|
|
} else if let (Expression::Identifier(_), Expression::Identifier(_)) =
|
2024-03-25 04:16:55 +00:00
|
|
|
(&self.left, &self.right)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ValidationError::CannotIndexWith {
|
|
|
|
collection_type: left_type,
|
2024-03-25 04:16:55 +00:00
|
|
|
collection_position: self.left.position(),
|
|
|
|
index_type: self.right.expected_type(context)?,
|
|
|
|
index_position: self.right.position(),
|
2024-03-18 01:07:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 22:31:52 +00:00
|
|
|
fn run(self, context: &Context) -> Result<Action, RuntimeError> {
|
2024-03-25 04:16:55 +00:00
|
|
|
let collection_position = self.left.position();
|
|
|
|
let action = self.left.run(context)?;
|
2024-03-18 09:39:09 +00:00
|
|
|
let collection = if let Action::Return(value) = action {
|
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
2024-03-25 04:16:55 +00:00
|
|
|
ValidationError::InterpreterExpectedReturn(collection_position),
|
2024-03-18 09:39:09 +00:00
|
|
|
));
|
|
|
|
};
|
2024-03-18 01:07:03 +00:00
|
|
|
|
|
|
|
if let (ValueInner::Map(map), Expression::Identifier(identifier)) =
|
2024-03-25 04:16:55 +00:00
|
|
|
(collection.inner().as_ref(), &self.right)
|
2024-03-18 01:07:03 +00:00
|
|
|
{
|
|
|
|
let action = map
|
2024-03-25 04:16:55 +00:00
|
|
|
.get(&identifier.node)
|
2024-03-18 01:07:03 +00:00
|
|
|
.map(|value| Action::Return(value.clone()))
|
|
|
|
.unwrap_or(Action::None);
|
|
|
|
|
|
|
|
Ok(action)
|
|
|
|
} else {
|
|
|
|
Err(RuntimeError::ValidationFailure(
|
|
|
|
ValidationError::CannotIndexWith {
|
2024-03-19 22:31:52 +00:00
|
|
|
collection_type: collection.r#type(context)?,
|
2024-03-25 04:16:55 +00:00
|
|
|
collection_position,
|
|
|
|
index_type: self.right.expected_type(context)?,
|
|
|
|
index_position: self.right.position(),
|
2024-03-18 01:07:03 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|