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

111 lines
2.7 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-21 22:28:12 +00:00
use super::{Evaluation, ExpectedType, Run, Statement, Type, 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 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
}
2024-06-21 22:28:12 +00:00
impl Validate for Block {
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-06-21 22:28:12 +00:00
}
2024-06-21 22:28:12 +00:00
impl Run for Block {
fn run(
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> {
let mut previous = None;
2024-03-02 00:29:16 +00:00
for statement in self.statements {
2024-06-21 22:28:12 +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
}
}
2024-06-16 07:12:04 +00:00
impl ExpectedType for Block {
2024-06-17 14:10:06 +00:00
fn expected_type(&self, _context: &mut Context) -> Result<Type, ValidationError> {
2024-06-16 07:12:04 +00:00
self.last_statement().expected_type(_context)
}
}
2024-03-02 00:29:16 +00:00
#[cfg(test)]
mod tests {
2024-03-09 17:58:29 +00:00
use crate::{
2024-06-17 14:10:06 +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-06-17 19:47:07 +00:00
Statement::Expression(Expression::Value(
2024-03-25 04:16:55 +00:00
ValueNode::Integer(1).with_position((0, 0)),
)),
2024-06-17 19:47:07 +00:00
Statement::Expression(Expression::Value(
2024-03-25 04:16:55 +00:00
ValueNode::Integer(2).with_position((0, 0)),
)),
2024-06-17 19:47:07 +00:00
Statement::Expression(Expression::Value(
2024-03-25 04:16:55 +00:00
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-06-21 22:28:12 +00:00
block.run(&mut Context::new(None), true).unwrap(),
Some(Evaluation::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-06-17 19:47:07 +00:00
Statement::Expression(Expression::Value(
2024-03-25 04:16:55 +00:00
ValueNode::String("42".to_string()).with_position((0, 0)),
)),
2024-06-17 19:47:07 +00:00
Statement::Expression(Expression::Value(
2024-03-25 04:16:55 +00:00
ValueNode::Integer(42).with_position((0, 0)),
)),
2024-03-02 00:29:16 +00:00
]);
assert_eq!(
block.expected_type(&mut Context::new(None)),
Ok(Type::Integer)
)
2024-02-25 18:49:26 +00:00
}
}