2024-03-17 17:36:31 +00:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
2024-02-29 02:04:38 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
2024-03-20 04:28:28 +00:00
|
|
|
use super::{AbstractNode, Action, Statement, Type, WithPosition};
|
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-17 11:31:45 +00:00
|
|
|
statements: Vec<WithPosition<Statement>>,
|
2024-03-02 01:17:55 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 21:14:47 +00:00
|
|
|
impl Loop {
|
2024-03-17 11:31:45 +00:00
|
|
|
pub fn new(statements: Vec<WithPosition<Statement>>) -> Self {
|
2024-03-02 01:17:55 +00:00
|
|
|
Self { statements }
|
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 04:28:28 +00:00
|
|
|
impl AbstractNode for Loop {
|
2024-02-29 02:04:38 +00:00
|
|
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
2024-03-08 17:24:11 +00:00
|
|
|
Ok(Type::None)
|
2024-02-29 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
2024-03-08 17:24:11 +00:00
|
|
|
for statement in &self.statements {
|
2024-03-17 10:26:12 +00:00
|
|
|
statement.node.validate(_context)?;
|
2024-03-08 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(self, _context: &Context) -> Result<Action, RuntimeError> {
|
|
|
|
loop {
|
2024-03-17 17:36:31 +00:00
|
|
|
for statement in &self.statements {
|
|
|
|
let action = statement.node.clone().run(_context)?;
|
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-02-29 02:04:38 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:24:11 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-03-17 17:36:31 +00:00
|
|
|
use crate::{
|
|
|
|
abstract_tree::{
|
2024-03-24 16:21:08 +00:00
|
|
|
Assignment, AssignmentOperator, Block, Expression, Identifier, IfElse, Logic,
|
|
|
|
ValueNode, WithPos,
|
2024-03-17 17:36:31 +00:00
|
|
|
},
|
|
|
|
Value,
|
|
|
|
};
|
|
|
|
|
2024-03-08 17:24:11 +00:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_loop() {
|
2024-03-17 21:39:39 +00:00
|
|
|
let action = Loop::new(vec![Statement::Break.with_position((0, 0))])
|
|
|
|
.run(&Context::new())
|
|
|
|
.unwrap();
|
2024-02-29 02:04:38 +00:00
|
|
|
|
2024-03-17 21:39:39 +00:00
|
|
|
assert_eq!(action, Action::Break)
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
2024-03-17 17:36:31 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex_loop() {
|
2024-03-17 21:39:39 +00:00
|
|
|
let action = Block::new(vec![
|
2024-03-17 17:36:31 +00:00
|
|
|
Statement::Assignment(Assignment::new(
|
2024-03-18 09:39:09 +00:00
|
|
|
Identifier::new("i").with_position((0, 0)),
|
2024-03-17 17:36:31 +00:00
|
|
|
None,
|
|
|
|
AssignmentOperator::Assign,
|
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(1)))
|
|
|
|
.with_position((0, 0)),
|
|
|
|
))
|
|
|
|
.with_position((0, 0)),
|
|
|
|
Statement::Loop(Loop::new(vec![Statement::IfElse(IfElse::new(
|
|
|
|
Expression::Logic(Box::new(Logic::Greater(
|
2024-03-24 16:21:08 +00:00
|
|
|
Expression::Identifier(Identifier::new("i")).with_position((0, 0)),
|
|
|
|
Expression::Value(ValueNode::Integer(2)).with_position((0, 0)),
|
2024-03-17 17:36:31 +00:00
|
|
|
)))
|
2024-03-24 16:21:08 +00:00
|
|
|
.with_position((0, 0)),
|
|
|
|
Block::new(vec![Statement::Break.with_position((0, 0))]),
|
2024-03-17 17:36:31 +00:00
|
|
|
Some(Block::new(vec![Statement::Assignment(Assignment::new(
|
2024-03-18 09:39:09 +00:00
|
|
|
Identifier::new("i").with_position((0, 0)),
|
2024-03-17 17:36:31 +00:00
|
|
|
None,
|
|
|
|
AssignmentOperator::AddAssign,
|
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(1)))
|
2024-03-24 16:21:08 +00:00
|
|
|
.with_position((0, 0)),
|
2024-03-17 17:36:31 +00:00
|
|
|
))
|
2024-03-24 16:21:08 +00:00
|
|
|
.with_position((0, 0))])),
|
2024-03-17 17:36:31 +00:00
|
|
|
))
|
|
|
|
.with_position((0, 0))]))
|
|
|
|
.with_position((0, 0)),
|
|
|
|
Statement::Expression(Expression::Identifier(Identifier::new("i")))
|
|
|
|
.with_position((0, 0)),
|
|
|
|
])
|
2024-03-17 21:39:39 +00:00
|
|
|
.run(&Context::new())
|
|
|
|
.unwrap();
|
2024-03-17 17:36:31 +00:00
|
|
|
|
2024-03-17 21:39:39 +00:00
|
|
|
assert_eq!(action, Action::Return(Value::integer(3)))
|
2024-03-17 17:36:31 +00:00
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|