2024-03-07 17:29:07 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
};
|
|
|
|
|
2024-03-17 11:31:45 +00:00
|
|
|
use super::{AbstractTree, Action, Expression, Type, ValueNode, WithPosition};
|
2024-03-07 17:29:07 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
2024-03-18 01:07:03 +00:00
|
|
|
pub struct ListIndex {
|
2024-03-17 11:31:45 +00:00
|
|
|
left: WithPosition<Expression>,
|
|
|
|
right: WithPosition<Expression>,
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 01:07:03 +00:00
|
|
|
impl ListIndex {
|
2024-03-17 11:31:45 +00:00
|
|
|
pub fn new(left: WithPosition<Expression>, right: WithPosition<Expression>) -> Self {
|
2024-03-07 17:29:07 +00:00
|
|
|
Self { left, right }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 01:07:03 +00:00
|
|
|
impl AbstractTree for ListIndex {
|
2024-03-07 21:13:15 +00:00
|
|
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
2024-03-17 10:26:12 +00:00
|
|
|
let left_type = self.left.node.expected_type(_context)?;
|
2024-03-07 17:29:07 +00:00
|
|
|
|
|
|
|
if let (
|
|
|
|
Expression::Value(ValueNode::List(expression_list)),
|
|
|
|
Expression::Value(ValueNode::Integer(index)),
|
2024-03-17 10:26:12 +00:00
|
|
|
) = (&self.left.node, &self.right.node)
|
2024-03-07 17:29:07 +00:00
|
|
|
{
|
|
|
|
let expression = if let Some(expression) = expression_list.get(*index as usize) {
|
|
|
|
expression
|
|
|
|
} else {
|
|
|
|
return Ok(Type::None);
|
|
|
|
};
|
|
|
|
|
2024-03-17 10:26:12 +00:00
|
|
|
expression.node.expected_type(_context)
|
2024-03-07 17:29:07 +00:00
|
|
|
} else {
|
2024-03-17 22:03:43 +00:00
|
|
|
Err(ValidationError::CannotIndex {
|
|
|
|
r#type: left_type,
|
|
|
|
position: self.left.position,
|
|
|
|
})
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, context: &Context) -> Result<(), ValidationError> {
|
2024-03-17 10:26:12 +00:00
|
|
|
let left_type = self.left.node.expected_type(context)?;
|
2024-03-07 17:29:07 +00:00
|
|
|
|
|
|
|
match left_type {
|
|
|
|
Type::List => todo!(),
|
|
|
|
Type::ListOf(_) => todo!(),
|
|
|
|
Type::ListExact(_) => {
|
2024-03-17 10:26:12 +00:00
|
|
|
let right_type = self.right.node.expected_type(context)?;
|
2024-03-07 17:29:07 +00:00
|
|
|
|
|
|
|
if let Type::Integer = right_type {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-03-18 01:07:03 +00:00
|
|
|
Err(ValidationError::CannotIndexWith {
|
2024-03-18 07:24:41 +00:00
|
|
|
collection_type: left_type,
|
|
|
|
collection_position: self.left.position,
|
|
|
|
index_type: right_type,
|
|
|
|
index_position: self.right.position,
|
2024-03-18 01:07:03 +00:00
|
|
|
})
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-17 22:03:43 +00:00
|
|
|
_ => Err(ValidationError::CannotIndex {
|
|
|
|
r#type: left_type,
|
|
|
|
position: self.left.position,
|
|
|
|
}),
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:24:11 +00:00
|
|
|
fn run(self, _context: &Context) -> Result<Action, RuntimeError> {
|
2024-03-17 10:26:12 +00:00
|
|
|
let left_value = self.left.node.run(_context)?.as_return_value()?;
|
|
|
|
let right_value = self.right.node.run(_context)?.as_return_value()?;
|
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 {
|
|
|
|
Ok(Action::Return(item.clone()))
|
|
|
|
} else {
|
|
|
|
Ok(Action::None)
|
|
|
|
}
|
2024-03-07 17:29:07 +00:00
|
|
|
} else {
|
|
|
|
Err(RuntimeError::ValidationFailure(
|
2024-03-18 01:07:03 +00:00
|
|
|
ValidationError::CannotIndexWith {
|
2024-03-18 07:24:41 +00:00
|
|
|
collection_type: left_value.r#type(),
|
|
|
|
collection_position: self.left.position,
|
|
|
|
index_type: right_value.r#type(),
|
|
|
|
index_position: self.right.position,
|
2024-03-18 01:07:03 +00:00
|
|
|
},
|
2024-03-07 17:29:07 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|