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

124 lines
4.1 KiB
Rust
Raw Normal View History

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-16 07:12:04 +00:00
use super::{AbstractNode, Action, ExpectedType, Type, ValueExpression, 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-16 07:12:04 +00:00
left: ValueExpression,
right: ValueExpression,
2024-03-07 17:29:07 +00:00
}
2024-03-18 01:07:03 +00:00
impl ListIndex {
2024-06-16 07:12:04 +00:00
pub fn new(left: ValueExpression, right: ValueExpression) -> Self {
2024-03-07 17:29:07 +00:00
Self { left, right }
}
}
impl AbstractNode for ListIndex {
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
}
}
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();
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();
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-06-16 07:12:04 +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-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
impl ExpectedType for ListIndex {
fn expected_type(&self, _context: &mut Context) -> Result<Type, ValidationError> {
let left_type = self.left.expected_type(_context)?;
if let (
ValueExpression::Value(WithPosition {
node: ValueNode::List(expression_list),
..
}),
ValueExpression::Value(WithPosition {
node: ValueNode::Integer(index),
..
}),
) = (&self.left, &self.right)
{
let expression = if let Some(expression) = expression_list.get(*index as usize) {
expression
} else {
return Ok(Type::None);
};
expression.expected_type(_context)
} else {
Err(ValidationError::CannotIndex {
r#type: left_type,
position: self.left.position(),
})
}
}
}