1
0
dust/dust-lang/src/abstract_tree/map_index.rs

150 lines
5.1 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::{AbstractNode, Action, Expression, Type, ValueNode, WithPosition};
2024-03-18 01:07:03 +00:00
#[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 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),
) = (&self.left.node, &self.right.node)
{
2024-03-19 22:31:52 +00:00
let collection = if let Some(collection) = context.get_value(collection_identifier)? {
2024-03-18 01:07:03 +00:00
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) {
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 {
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 {
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-19 22:31:52 +00:00
Some(expression.node.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 (
Expression::Value(ValueNode::Structure { fields, .. }),
Expression::Identifier(identifier),
) = (&self.left.node, &self.right.node)
{
return if let Some(type_result) = fields.iter().find_map(|(property, expression)| {
if property == identifier {
Some(expression.node.expected_type(context))
} else {
None
}
}) {
type_result
} else {
Ok(Type::None)
};
}
2024-03-18 01:07:03 +00:00
Err(ValidationError::CannotIndexWith {
2024-03-23 12:15:48 +00:00
collection_type: self.left.node.expected_type(context)?,
2024-03-18 09:39:09 +00:00
collection_position: self.left.position,
2024-03-19 22:31:52 +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
})
}
}
2024-03-19 22:31:52 +00:00
fn run(self, context: &Context) -> Result<Action, RuntimeError> {
let action = self.left.node.run(context)?;
2024-03-18 09:39:09 +00:00
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 {
2024-03-19 22:31:52 +00:00
collection_type: collection.r#type(context)?,
2024-03-18 09:39:09 +00:00
collection_position: self.left.position,
2024-03-19 22:31:52 +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
},
))
}
}
}