use crate::{ context::Context, error::{RuntimeError, ValidationError}, }; use super::{AbstractNode, Action, Statement, Type}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub struct Loop { statements: Vec, } impl Loop { pub fn new(statements: Vec) -> Self { Self { statements } } } impl AbstractNode for Loop { fn expected_type(&self, _context: &Context) -> Result { Ok(Type::None) } fn validate(&self, _context: &Context, _manage_memory: bool) -> Result<(), ValidationError> { for statement in &self.statements { statement.validate(_context, false)?; } Ok(()) } fn run(self, _context: &mut Context, _manage_memory: bool) -> Result { loop { for statement in &self.statements { let action = statement.clone().run(_context, false)?; match action { Action::Return(_) => {} Action::None => {} Action::Break => return Ok(Action::Break), } } } } }