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

145 lines
4.4 KiB
Rust
Raw Normal View History

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-06-22 00:59:38 +00:00
use super::{AbstractNode, Evaluation, Expression, Type, Validate, 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 {
Self {
collection: left,
index: right,
}
2024-03-18 01:07:03 +00:00
}
}
2024-06-21 22:28:12 +00:00
impl Validate for MapIndex {
2024-06-16 07:12:04 +00:00
fn validate(
&self,
_context: &mut Context,
_manage_memory: bool,
) -> Result<(), ValidationError> {
self.collection.validate(_context, _manage_memory)
}
2024-06-21 22:28:12 +00:00
}
2024-06-16 07:12:04 +00:00
2024-06-22 00:59:38 +00:00
impl AbstractNode for MapIndex {
2024-06-17 14:10:06 +00:00
fn evaluate(
self,
context: &mut Context,
_manage_memory: bool,
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-06-17 14:10:06 +00:00
let action = self.collection.evaluate(context, _manage_memory)?;
let collection = if let Evaluation::Return(value) = action {
2024-06-16 07:12:04 +00:00
value
} else {
return Err(RuntimeError::ValidationFailure(
ValidationError::InterpreterExpectedReturn(collection_position),
));
};
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 00:59:38 +00:00
let evaluation = map
2024-06-16 07:12:04 +00:00
.get(&index.node)
2024-06-22 00:59:38 +00:00
.map(|value| Some(Evaluation::Return(value.clone())));
2024-06-16 07:12:04 +00:00
2024-06-22 00:59:38 +00:00
Ok(evaluation)
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 00:59:38 +00:00
fn expected_type(&self, context: &mut Context) -> Result<Option<Type>, ValidationError> {
2024-06-17 14:10:06 +00:00
if let (Expression::Identifier(collection), Expression::Identifier(index)) =
(&self.collection, &self.index)
2024-03-18 01:07:03 +00:00
{
2024-06-16 07:12:04 +00:00
let collection = if let Some(collection) = context.get_value(&collection.node)? {
collection
} else {
return Err(ValidationError::VariableNotFound {
2024-06-16 07:12:04 +00:00
identifier: collection.node.clone(),
position: collection.position,
});
};
2024-03-18 01:07:03 +00:00
if let ValueInner::Map(map) = collection.inner().as_ref() {
2024-06-16 07:12:04 +00:00
return if let Some(value) = map.get(&index.node) {
2024-06-22 00:59:38 +00:00
Ok(Some(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-06-16 07:12:04 +00:00
identifier: index.node.clone(),
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),
) = (&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-21 22:30:16 +00:00
return Ok(Type::Void);
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),
) = (&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-21 22:30:16 +00:00
Ok(Type::Void)
2024-03-24 21:34:36 +00:00
};
}
Err(ValidationError::CannotIndex {
r#type: self.collection.expected_type(context)?,
position: self.collection.position(),
2024-03-18 01:07:03 +00:00
})
}
}