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

63 lines
1.4 KiB
Rust
Raw Normal View History

2024-06-04 18:47:15 +00:00
use serde::{Deserialize, Serialize};
use crate::{
context::Context,
error::{RuntimeError, ValidationError},
};
2024-02-25 18:49:26 +00:00
2024-06-22 00:59:38 +00:00
use super::{AbstractNode, Evaluation, Statement, Validate};
2024-02-25 18:49:26 +00:00
2024-06-04 18:47:15 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
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-06-19 16:03:25 +00:00
pub fn last_statement(&self) -> &Statement {
self.statements.last().unwrap()
}
2024-02-25 18:49:26 +00:00
}
2024-06-21 22:28:12 +00:00
impl Validate for Loop {
2024-04-22 12:25:20 +00:00
fn validate(
&self,
_context: &mut Context,
_manage_memory: bool,
) -> Result<(), ValidationError> {
2024-03-08 17:24:11 +00:00
for statement in &self.statements {
2024-04-22 11:56:03 +00:00
statement.validate(_context, false)?;
2024-03-08 17:24:11 +00:00
}
Ok(())
}
2024-06-21 22:28:12 +00:00
}
2024-03-08 17:24:11 +00:00
2024-06-22 00:59:38 +00:00
impl AbstractNode for Loop {
fn evaluate(
2024-06-17 14:10:06 +00:00
self,
_context: &mut Context,
_manage_memory: bool,
2024-06-21 22:28:12 +00:00
) -> Result<Option<Evaluation>, RuntimeError> {
2024-03-08 17:24:11 +00:00
loop {
2024-03-17 17:36:31 +00:00
for statement in &self.statements {
2024-06-21 22:28:12 +00:00
let run = statement.clone().run(_context, false)?;
2024-03-08 17:24:11 +00:00
2024-06-21 22:28:12 +00:00
if let Some(Evaluation::Break) = run {
return Ok(run);
2024-03-17 17:36:31 +00:00
}
2024-03-08 17:24:11 +00:00
}
}
}
2024-06-22 00:59:38 +00:00
fn expected_type(
&self,
_context: &mut Context,
) -> Result<Option<super::Type>, ValidationError> {
self.last_statement().expected_type(_context)
}
2024-03-08 17:24:11 +00:00
}