2024-03-07 17:29:07 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
};
|
|
|
|
|
2024-03-20 04:28:28 +00:00
|
|
|
use super::{AbstractNode, 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-20 04:28:28 +00:00
|
|
|
impl AbstractNode 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-19 22:31:52 +00:00
|
|
|
fn run(self, context: &Context) -> Result<Action, RuntimeError> {
|
|
|
|
let left_action = self.left.node.run(context)?;
|
2024-03-18 09:39:09 +00:00
|
|
|
let left_value = if let Action::Return(value) = left_action {
|
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
|
|
|
ValidationError::InterpreterExpectedReturn(self.left.position),
|
|
|
|
));
|
|
|
|
};
|
2024-03-19 22:31:52 +00:00
|
|
|
let right_action = self.right.node.run(context)?;
|
2024-03-18 09:39:09 +00:00
|
|
|
let right_value = if let Action::Return(value) = right_action {
|
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
|
|
|
ValidationError::InterpreterExpectedReturn(self.left.position),
|
|
|
|
));
|
|
|
|
};
|
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-03-24 16:21:08 +00:00
|
|
|
Ok(Action::Return(item.node.clone()))
|
2024-03-12 01:57:27 +00:00
|
|
|
} 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-19 22:31:52 +00:00
|
|
|
collection_type: left_value.r#type(context)?,
|
2024-03-18 07:24:41 +00:00
|
|
|
collection_position: self.left.position,
|
2024-03-19 22:31:52 +00:00
|
|
|
index_type: right_value.r#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-07 17:29:07 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|