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

125 lines
3.1 KiB
Rust
Raw Normal View History

use std::fmt::{self, Display, Formatter};
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-22 00:59:38 +00:00
use super::{AbstractNode, Evaluation, Statement, Type};
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-22 00:59:38 +00:00
impl AbstractNode for Block {
fn define_types(&self, _context: &Context) -> Result<(), ValidationError> {
for statement in &self.statements {
statement.define_types(_context)?;
}
Ok(())
}
fn validate(&self, _context: &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-22 00:59:38 +00:00
fn evaluate(
2024-06-17 14:10:06 +00:00
self,
2024-06-22 00:59:38 +00:00
_context: &Context,
2024-06-17 14:10:06 +00:00
_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-22 03:37:25 +00:00
previous = statement.evaluate(_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-22 00:59:38 +00:00
fn expected_type(&self, _context: &Context) -> Result<Option<Type>, ValidationError> {
2024-06-16 07:12:04 +00:00
self.last_statement().expected_type(_context)
}
}
impl Display for Block {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2024-06-26 21:35:30 +00:00
write!(f, "{{ ")?;
for statement in &self.statements {
2024-06-26 21:35:30 +00:00
write!(f, "{statement} ")?;
}
write!(f, "}}")
}
}
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-22 03:37:25 +00:00
block.evaluate(&Context::new(None), true).unwrap(),
2024-06-21 22:28:12 +00:00
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!(
2024-06-22 03:37:25 +00:00
block.expected_type(&Context::new(None)),
Ok(Some(Type::Integer))
)
2024-02-25 18:49:26 +00:00
}
}