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-03-20 04:28:28 +00:00
|
|
|
use super::{AbstractNode, Action, 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-03-25 04:16:55 +00:00
|
|
|
left: Expression,
|
|
|
|
right: Expression,
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 01:07:03 +00:00
|
|
|
impl ListIndex {
|
2024-03-25 04:16:55 +00:00
|
|
|
pub fn new(left: Expression, right: 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-04-22 12:25:20 +00:00
|
|
|
fn expected_type(&self, _context: &mut Context) -> Result<Type, ValidationError> {
|
2024-03-25 04:16:55 +00:00
|
|
|
let left_type = self.left.expected_type(_context)?;
|
2024-03-07 17:29:07 +00:00
|
|
|
|
|
|
|
if let (
|
2024-03-25 04:16:55 +00:00
|
|
|
Expression::Value(WithPosition {
|
2024-04-22 07:41:21 +00:00
|
|
|
item: ValueNode::List(expression_list),
|
2024-03-25 04:16:55 +00:00
|
|
|
..
|
|
|
|
}),
|
|
|
|
Expression::Value(WithPosition {
|
2024-04-22 07:41:21 +00:00
|
|
|
item: ValueNode::Integer(index),
|
2024-03-25 04:16:55 +00:00
|
|
|
..
|
|
|
|
}),
|
|
|
|
) = (&self.left, &self.right)
|
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-25 04:16:55 +00:00
|
|
|
expression.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,
|
2024-03-25 04:16:55 +00:00
|
|
|
position: self.left.position(),
|
2024-03-17 22:03:43 +00:00
|
|
|
})
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-22 12:25:20 +00:00
|
|
|
fn validate(&self, context: &mut Context, _manage_memory: bool) -> Result<(), ValidationError> {
|
2024-04-22 11:56:03 +00:00
|
|
|
self.left.validate(context, _manage_memory)?;
|
|
|
|
self.right.validate(context, _manage_memory)?;
|
2024-04-22 09:50:26 +00:00
|
|
|
|
2024-03-25 04:16:55 +00:00
|
|
|
let left_type = self.left.expected_type(context)?;
|
2024-03-07 17:29:07 +00:00
|
|
|
|
|
|
|
match left_type {
|
|
|
|
Type::List => todo!(),
|
|
|
|
Type::ListOf(_) => todo!(),
|
|
|
|
Type::ListExact(_) => {
|
2024-03-25 04:16:55 +00:00
|
|
|
let right_type = self.right.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,
|
2024-03-25 04:16:55 +00:00
|
|
|
collection_position: self.left.position(),
|
2024-03-18 07:24:41 +00:00
|
|
|
index_type: right_type,
|
2024-03-25 04:16:55 +00:00
|
|
|
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,
|
2024-03-25 04:16:55 +00:00
|
|
|
position: self.left.position(),
|
2024-03-17 22:03:43 +00:00
|
|
|
}),
|
2024-03-07 17:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-22 05:51:34 +00:00
|
|
|
fn run(self, context: &mut Context, _clear_variables: bool) -> Result<Action, RuntimeError> {
|
2024-03-25 04:16:55 +00:00
|
|
|
let left_position = self.left.position();
|
2024-04-22 05:51:34 +00:00
|
|
|
let left_action = self.left.run(context, _clear_variables)?;
|
2024-03-18 09:39:09 +00:00
|
|
|
let left_value = if let Action::Return(value) = left_action {
|
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
2024-03-25 04:16:55 +00:00
|
|
|
ValidationError::InterpreterExpectedReturn(left_position),
|
2024-03-18 09:39:09 +00:00
|
|
|
));
|
|
|
|
};
|
2024-03-25 04:16:55 +00:00
|
|
|
let right_position = self.right.position();
|
2024-04-22 05:51:34 +00:00
|
|
|
let right_action = self.right.run(context, _clear_variables)?;
|
2024-03-18 09:39:09 +00:00
|
|
|
let right_value = if let Action::Return(value) = right_action {
|
|
|
|
value
|
|
|
|
} else {
|
|
|
|
return Err(RuntimeError::ValidationFailure(
|
2024-03-25 04:16:55 +00:00
|
|
|
ValidationError::InterpreterExpectedReturn(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-04-22 07:41:21 +00:00
|
|
|
Ok(Action::Return(item.item.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-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
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|