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

94 lines
2.4 KiB
Rust
Raw Normal View History

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-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-25 04:16:55 +00:00
statements: Vec<Statement>,
2024-02-25 18:49:26 +00:00
}
2024-03-08 21:14:47 +00:00
impl Block {
2024-03-25 04:16:55 +00:00
pub fn new(statements: Vec<Statement>) -> Self {
2024-02-25 18:49:26 +00:00
Self { statements }
}
2024-03-17 14:19:22 +00:00
2024-03-25 04:16:55 +00:00
pub fn last_statement(&self) -> &Statement {
2024-03-17 14:19:22 +00:00
self.statements.last().unwrap()
}
2024-02-25 18:49:26 +00:00
}
impl AbstractNode for Block {
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-25 04:16:55 +00:00
statement.expected_type(_context)
2024-03-09 17:58:29 +00:00
} else {
Ok(Type::None)
}
}
fn validate(&self, _context: &Context) -> Result<(), ValidationError> {
2024-03-02 00:29:16 +00:00
for statement in &self.statements {
2024-03-25 04:16:55 +00:00
statement.validate(_context)?;
2024-03-02 00:29:16 +00:00
}
Ok(())
}
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-25 04:16:55 +00:00
previous = statement.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::{
2024-03-24 16:21:08 +00:00
abstract_tree::{Expression, ValueNode, WithPos},
2024-03-09 17:58:29 +00:00
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-25 04:16:55 +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-25 04:16:55 +00:00
Statement::Expression(Expression::Value(
ValueNode::String("42".to_string()).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
}
}