2024-02-29 02:04:38 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
Value,
|
|
|
|
};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
2024-03-08 17:24:11 +00:00
|
|
|
use super::{AbstractTree, Action, Statement, Type};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
2024-02-25 19:26:22 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
2024-03-08 21:14:47 +00:00
|
|
|
pub struct Block {
|
|
|
|
statements: Vec<Statement>,
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 21:14:47 +00:00
|
|
|
impl Block {
|
|
|
|
pub fn new(statements: Vec<Statement>) -> Self {
|
2024-02-25 18:49:26 +00:00
|
|
|
Self { statements }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 21:14:47 +00:00
|
|
|
impl AbstractTree for Block {
|
2024-02-29 02:04:38 +00:00
|
|
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
2024-03-02 00:29:16 +00:00
|
|
|
let final_statement = self.statements.last().unwrap();
|
|
|
|
|
|
|
|
final_statement.expected_type(_context)
|
2024-02-29 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
|
2024-03-02 00:29:16 +00:00
|
|
|
for statement in &self.statements {
|
|
|
|
statement.validate(_context)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2024-02-29 02:04:38 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:24:11 +00:00
|
|
|
fn run(self, _context: &Context) -> Result<Action, RuntimeError> {
|
2024-03-02 00:29:16 +00:00
|
|
|
let mut previous = Value::none();
|
|
|
|
|
|
|
|
for statement in self.statements {
|
2024-03-08 17:24:11 +00:00
|
|
|
let action = statement.run(_context)?;
|
|
|
|
previous = match action {
|
|
|
|
Action::Return(value) => value,
|
|
|
|
r#break => return Ok(r#break),
|
|
|
|
};
|
2024-03-02 00:29:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 17:24:11 +00:00
|
|
|
Ok(Action::Return(previous))
|
2024-03-02 00:29:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::abstract_tree::{Expression, ValueNode};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_returns_value_of_final_statement() {
|
|
|
|
let block = Block::new(vec![
|
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(1))),
|
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(2))),
|
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(42))),
|
|
|
|
]);
|
|
|
|
|
2024-03-08 17:24:11 +00:00
|
|
|
assert_eq!(
|
|
|
|
block.run(&Context::new()),
|
|
|
|
Ok(Action::Return(Value::integer(42)))
|
|
|
|
)
|
2024-03-02 00:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expected_type_returns_type_of_final_statement() {
|
|
|
|
let block = Block::new(vec![
|
2024-03-08 21:14:47 +00:00
|
|
|
Statement::Expression(Expression::Value(ValueNode::String("42".to_string()))),
|
2024-03-02 00:29:16 +00:00
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(42))),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(block.expected_type(&Context::new()), Ok(Type::Integer))
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
}
|