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

140 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-16 07:12:04 +00:00
use super::{AbstractNode, Action, ExpectedType, Type, ValueExpression, 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-16 07:12:04 +00:00
collection: ValueExpression,
index: ValueExpression,
2024-03-18 01:07:03 +00:00
}
impl MapIndex {
2024-06-16 07:12:04 +00:00
pub fn new(left: ValueExpression, right: ValueExpression) -> Self {
Self {
collection: left,
index: right,
}
2024-03-18 01:07:03 +00:00
}
}
impl AbstractNode 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)
}
fn run(self, context: &mut Context, _manage_memory: bool) -> Result<Action, RuntimeError> {
let collection_position = self.collection.position();
let action = self.collection.run(context, _manage_memory)?;
let collection = if let Action::Return(value) = action {
value
} else {
return Err(RuntimeError::ValidationFailure(
ValidationError::InterpreterExpectedReturn(collection_position),
));
};
if let (ValueInner::Map(map), ValueExpression::Identifier(index)) =
(collection.inner().as_ref(), self.index)
{
let action = map
.get(&index.node)
.map(|value| Action::Return(value.clone()))
.unwrap_or(Action::None);
Ok(action)
} else {
Err(RuntimeError::ValidationFailure(
ValidationError::CannotIndex {
r#type: collection.r#type(context)?,
position: collection_position,
},
))
}
}
}
impl ExpectedType for MapIndex {
2024-04-22 12:25:20 +00:00
fn expected_type(&self, context: &mut Context) -> Result<Type, ValidationError> {
2024-06-16 07:12:04 +00:00
if let (ValueExpression::Identifier(collection), ValueExpression::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-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-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-16 07:12:04 +00:00
ValueExpression::Value(WithPosition {
node: ValueNode::Map(properties),
2024-03-25 04:16:55 +00:00
..
}),
2024-06-16 07:12:04 +00:00
ValueExpression::Identifier(index),
) = (&self.collection, &self.index)
2024-03-18 01:07:03 +00:00
{
2024-06-16 07:12:04 +00:00
for (property, type_option, expression) in properties {
if property == &index.node {
return if let Some(r#type) = type_option {
Ok(r#type.node.clone())
} else {
Ok(expression.expected_type(context)?)
};
}
}
return Ok(Type::None);
2024-03-18 01:07:03 +00:00
}
2024-03-24 21:34:36 +00:00
if let (
2024-06-16 07:12:04 +00:00
ValueExpression::Value(WithPosition {
node: ValueNode::Structure { fields, .. },
2024-03-25 04:16:55 +00:00
..
}),
2024-06-16 07:12:04 +00:00
ValueExpression::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-16 07:12:04 +00:00
if property == &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 {
Ok(Type::None)
};
}
Err(ValidationError::CannotIndex {
r#type: self.collection.expected_type(context)?,
position: self.collection.position(),
2024-03-18 01:07:03 +00:00
})
}
}