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

102 lines
2.6 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-05-20 21:15:05 +00:00
pub fn first_statement(&self) -> &Statement {
self.statements.first().unwrap()
}
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 {
2024-04-22 12:25:20 +00:00
fn expected_type(&self, _context: &mut 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)
}
}
2024-04-22 12:25:20 +00:00
fn validate(
&self,
_context: &mut Context,
_manage_memory: bool,
) -> Result<(), ValidationError> {
2024-03-02 00:29:16 +00:00
for statement in &self.statements {
2024-04-22 11:56:03 +00:00
statement.validate(_context, _manage_memory)?;
2024-03-02 00:29:16 +00:00
}
Ok(())
}
2024-04-22 11:56:03 +00:00
fn run(self, _context: &mut Context, _manage_memory: bool) -> 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-04-22 11:56:03 +00:00
previous = statement.run(_context, _manage_memory)?;
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!(
block.run(&mut Context::new(), true).unwrap(),
2024-03-17 21:39:39 +00:00
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
]);
2024-04-22 12:25:20 +00:00
assert_eq!(block.expected_type(&mut Context::new()), Ok(Type::Integer))
2024-02-25 18:49:26 +00:00
}
}