1
0
dust/src/abstract_tree/map_index.rs

134 lines
4.6 KiB
Rust
Raw Normal View History

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