2024-06-04 18:47:15 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-03-07 17:29:07 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
};
|
|
|
|
|
2024-06-22 03:37:25 +00:00
|
|
|
use super::{AbstractNode, Evaluation, Expression, Type, ValueNode, WithPosition};
|
2024-03-07 17:29:07 +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 ListIndex {
|
2024-06-17 14:10:06 +00:00
|
|
|
collection: Expression,
|
|
|
|
index: Expression,
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 01:07:03 +00:00
|
|
|
impl ListIndex {
|
2024-06-17 14:10:06 +00:00
|
|
|
pub fn new(left: Expression, right: Expression) -> Self {
|
|
|
|
Self {
|
|
|
|
collection: left,
|
|
|
|
index: right,
|
|
|
|
}
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 03:37:25 +00:00
|
|
|
impl AbstractNode for ListIndex {
|
|
|
|
fn define_types(&self, context: &Context) -> Result<(), ValidationError> {
|
|
|
|
self.collection.define_types(context)?;
|
|
|
|
self.index.define_types(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, context: &Context, _manage_memory: bool) -> Result<(), ValidationError> {
|
2024-06-17 14:10:06 +00:00
|
|
|
self.collection.validate(context, _manage_memory)?;
|
|
|
|
self.index.validate(context, _manage_memory)?;
|
2024-03-07 17:29:07 +00:00
|
|
|
|
2024-06-22 17:55:43 +00:00
|
|
|
let collection_type = if let Some(r#type) = self.collection.expected_type(context)? {
|
2024-06-22 04:58:30 +00:00
|
|
|
r#type
|
|
|
|
} else {
|
|
|
|
return Err(ValidationError::ExpectedExpression(
|
|
|
|
self.collection.position(),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
let index_type = if let Some(r#type) = self.index.expected_type(context)? {
|
|
|
|
r#type
|
|
|
|
} else {
|
|
|
|
return Err(ValidationError::ExpectedExpression(self.index.position()));
|
|
|
|
};
|
2024-06-22 00:59:38 +00:00
|
|
|
|
2024-06-17 14:10:06 +00:00
|
|
|
match collection_type {
|
|
|
|
Type::List {
|
|
|
|
length: _,
|
|
|
|
item_type: _,
|
|
|
|
} => {
|
2024-06-22 04:58:30 +00:00
|
|
|
if index_type == Type::Integer {
|
2024-03-07 17:29:07 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-03-18 01:07:03 +00:00
|
|
|
Err(ValidationError::CannotIndexWith {
|
2024-06-17 14:10:06 +00:00
|
|
|
collection_type,
|
|
|
|
collection_position: self.collection.position(),
|
|
|
|
index_type,
|
|
|
|
index_position: self.index.position(),
|
2024-03-18 01:07:03 +00:00
|
|
|
})
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-17 14:10:06 +00:00
|
|
|
Type::ListOf(_) => todo!(),
|
2024-03-17 22:03:43 +00:00
|
|
|
_ => Err(ValidationError::CannotIndex {
|
2024-06-17 14:10:06 +00:00
|
|
|
r#type: collection_type,
|
|
|
|
position: self.collection.position(),
|
2024-03-17 22:03:43 +00:00
|
|
|
}),
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 14:10:06 +00:00
|
|
|
fn evaluate(
|
|
|
|
self,
|
2024-06-22 03:37:25 +00:00
|
|
|
context: &Context,
|
2024-06-17 14:10:06 +00:00
|
|
|
_clear_variables: bool,
|
2024-06-22 00:59:38 +00:00
|
|
|
) -> Result<Option<Evaluation>, RuntimeError> {
|
2024-06-17 14:10:06 +00:00
|
|
|
let left_position = self.collection.position();
|
2024-06-22 04:58:30 +00:00
|
|
|
let left_evaluation = self.collection.evaluate(context, _clear_variables)?;
|
|
|
|
let left_value = if let Some(Evaluation::Return(value)) = left_evaluation {
|
2024-03-18 09:39:09 +00:00
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
2024-06-22 03:37:25 +00:00
|
|
|
ValidationError::ExpectedExpression(left_position),
|
2024-03-18 09:39:09 +00:00
|
|
|
));
|
|
|
|
};
|
2024-06-17 14:10:06 +00:00
|
|
|
let right_position = self.index.position();
|
2024-06-22 04:58:30 +00:00
|
|
|
let right_evaluation = self.index.evaluate(context, _clear_variables)?;
|
|
|
|
let right_value = if let Some(Evaluation::Return(value)) = right_evaluation {
|
2024-03-18 09:39:09 +00:00
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
2024-06-22 03:37:25 +00:00
|
|
|
ValidationError::ExpectedExpression(right_position),
|
2024-03-18 09:39:09 +00:00
|
|
|
));
|
|
|
|
};
|
2024-03-07 17:29:07 +00:00
|
|
|
|
|
|
|
if let (Some(list), Some(index)) = (left_value.as_list(), right_value.as_integer()) {
|
2024-03-12 01:57:27 +00:00
|
|
|
let found_item = list.get(index as usize);
|
|
|
|
|
|
|
|
if let Some(item) = found_item {
|
2024-06-22 00:59:38 +00:00
|
|
|
Ok(Some(Evaluation::Return(item.clone())))
|
2024-03-12 01:57:27 +00:00
|
|
|
} else {
|
2024-06-22 04:58:30 +00:00
|
|
|
Ok(None)
|
2024-03-12 01:57:27 +00:00
|
|
|
}
|
2024-03-07 17:29:07 +00:00
|
|
|
} else {
|
|
|
|
Err(RuntimeError::ValidationFailure(
|
2024-03-18 01:07:03 +00:00
|
|
|
ValidationError::CannotIndexWith {
|
2024-03-19 22:31:52 +00:00
|
|
|
collection_type: left_value.r#type(context)?,
|
2024-03-25 04:16:55 +00:00
|
|
|
collection_position: left_position,
|
2024-03-19 22:31:52 +00:00
|
|
|
index_type: right_value.r#type(context)?,
|
2024-03-25 04:16:55 +00:00
|
|
|
index_position: right_position,
|
2024-03-18 01:07:03 +00:00
|
|
|
},
|
2024-03-07 17:29:07 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2024-06-16 07:12:04 +00:00
|
|
|
|
2024-06-22 03:37:25 +00:00
|
|
|
fn expected_type(&self, _context: &Context) -> Result<Option<Type>, ValidationError> {
|
2024-06-22 04:58:30 +00:00
|
|
|
let left_type = if let Some(r#type) = self.collection.expected_type(_context)? {
|
|
|
|
r#type
|
|
|
|
} else {
|
|
|
|
return Err(ValidationError::ExpectedExpression(
|
|
|
|
self.collection.position(),
|
|
|
|
));
|
|
|
|
};
|
2024-06-16 07:12:04 +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::List(expression_list),
|
|
|
|
..
|
|
|
|
}),
|
2024-06-17 14:10:06 +00:00
|
|
|
Expression::Value(WithPosition {
|
2024-06-16 07:12:04 +00:00
|
|
|
node: ValueNode::Integer(index),
|
|
|
|
..
|
|
|
|
}),
|
2024-06-17 14:10:06 +00:00
|
|
|
) = (&self.collection, &self.index)
|
2024-06-16 07:12:04 +00:00
|
|
|
{
|
|
|
|
let expression = if let Some(expression) = expression_list.get(*index as usize) {
|
|
|
|
expression
|
|
|
|
} else {
|
2024-06-22 04:58:30 +00:00
|
|
|
return Ok(None);
|
2024-06-16 07:12:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
expression.expected_type(_context)
|
|
|
|
} else {
|
|
|
|
Err(ValidationError::CannotIndex {
|
|
|
|
r#type: left_type,
|
2024-06-17 14:10:06 +00:00
|
|
|
position: self.collection.position(),
|
2024-06-16 07:12:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|