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-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 {
|
2024-03-17 11:31:45 +00:00
|
|
|
statements: Vec<WithPosition<Statement>>,
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 21:14:47 +00:00
|
|
|
impl Block {
|
2024-03-17 11:31:45 +00:00
|
|
|
pub fn new(statements: Vec<WithPosition<Statement>>) -> Self {
|
2024-02-25 18:49:26 +00:00
|
|
|
Self { statements }
|
|
|
|
}
|
2024-03-17 14:19:22 +00:00
|
|
|
|
|
|
|
pub fn last_statement(&self) -> &WithPosition<Statement> {
|
|
|
|
self.statements.last().unwrap()
|
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 04:28:28 +00:00
|
|
|
impl AbstractNode for Block {
|
2024-02-29 02:04:38 +00:00
|
|
|
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
|
2024-03-09 17:58:29 +00:00
|
|
|
if let Some(statement) = self.statements.last() {
|
2024-03-17 10:26:12 +00:00
|
|
|
statement.node.expected_type(_context)
|
2024-03-09 17:58:29 +00:00
|
|
|
} else {
|
|
|
|
Ok(Type::None)
|
|
|
|
}
|
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 {
|
2024-03-17 10:26:12 +00:00
|
|
|
statement.node.validate(_context)?;
|
2024-03-02 00:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-09 17:58:29 +00:00
|
|
|
let mut previous = Action::None;
|
2024-03-02 00:29:16 +00:00
|
|
|
|
|
|
|
for statement in self.statements {
|
2024-03-17 17:36:31 +00:00
|
|
|
previous = statement.node.run(_context)?;
|
2024-03-02 00:29:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-09 17:58:29 +00:00
|
|
|
Ok(previous)
|
2024-03-02 00:29:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-03-09 17:58:29 +00:00
|
|
|
use crate::{
|
|
|
|
abstract_tree::{Expression, ValueNode},
|
|
|
|
Value,
|
|
|
|
};
|
2024-03-02 00:29:16 +00:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_returns_value_of_final_statement() {
|
|
|
|
let block = Block::new(vec![
|
2024-03-17 11:48:06 +00:00
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(1))).with_position((0, 0)),
|
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(2))).with_position((0, 0)),
|
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(42))).with_position((0, 0)),
|
2024-03-02 00:29:16 +00:00
|
|
|
]);
|
|
|
|
|
2024-03-08 17:24:11 +00:00
|
|
|
assert_eq!(
|
2024-03-17 21:39:39 +00:00
|
|
|
block.run(&Context::new()).unwrap(),
|
|
|
|
Action::Return(Value::integer(42))
|
2024-03-08 17:24:11 +00:00
|
|
|
)
|
2024-03-02 00:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn expected_type_returns_type_of_final_statement() {
|
|
|
|
let block = Block::new(vec![
|
2024-03-17 11:31:45 +00:00
|
|
|
Statement::Expression(Expression::Value(ValueNode::String("42".to_string())))
|
2024-03-17 11:48:06 +00:00
|
|
|
.with_position((0, 0)),
|
|
|
|
Statement::Expression(Expression::Value(ValueNode::Integer(42))).with_position((0, 0)),
|
2024-03-02 00:29:16 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(block.expected_type(&Context::new()), Ok(Type::Integer))
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
}
|