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

68 lines
1.5 KiB
Rust
Raw Normal View History

2024-03-17 17:36:31 +00:00
use std::cmp::Ordering;
use crate::{
context::Context,
error::{RuntimeError, ValidationError},
};
2024-02-25 18:49:26 +00:00
2024-03-25 04:16:55 +00:00
use super::{AbstractNode, Action, Statement, Type};
2024-02-25 18:49:26 +00:00
2024-03-17 17:36:31 +00:00
#[derive(Clone, Debug)]
2024-03-08 21:14:47 +00:00
pub struct Loop {
2024-03-25 04:16:55 +00:00
statements: Vec<Statement>,
2024-03-02 01:17:55 +00:00
}
2024-03-08 21:14:47 +00:00
impl Loop {
2024-03-25 04:16:55 +00:00
pub fn new(statements: Vec<Statement>) -> Self {
2024-03-02 01:17:55 +00:00
Self { statements }
}
2024-02-25 18:49:26 +00:00
}
impl AbstractNode for Loop {
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
2024-03-08 17:24:11 +00:00
Ok(Type::None)
}
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
2024-03-08 17:24:11 +00:00
for statement in &self.statements {
2024-03-25 04:16:55 +00:00
statement.validate(_context)?;
2024-03-08 17:24:11 +00:00
}
Ok(())
}
fn run(self, _context: &mut Context, _clear_variables: bool) -> Result<Action, RuntimeError> {
2024-03-08 17:24:11 +00:00
loop {
2024-03-17 17:36:31 +00:00
for statement in &self.statements {
let action = statement.clone().run(_context, _clear_variables)?;
2024-03-08 17:24:11 +00:00
2024-03-17 17:36:31 +00:00
match action {
Action::Return(_) => {}
Action::None => {}
Action::Break => return Ok(Action::Break),
}
2024-03-08 17:24:11 +00:00
}
}
}
2024-03-08 17:24:11 +00:00
}
2024-03-17 17:36:31 +00:00
impl Eq for Loop {}
impl PartialEq for Loop {
fn eq(&self, other: &Self) -> bool {
self.statements.eq(&other.statements)
}
}
impl PartialOrd for Loop {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Loop {
fn cmp(&self, other: &Self) -> Ordering {
self.statements.cmp(&other.statements)
}
}