Skip to main content

pascal/utils/
block.rs

1//! Block construction utilities
2//!
3//! This module provides helper functions to reduce code duplication
4//! when constructing AST blocks.
5
6use crate::ast::Block;
7
8/// Create an empty block
9pub fn empty_block() -> Block {
10    Block::empty()
11}
12
13/// Create a block with a single statement
14pub fn block_with_statement(stmt: crate::ast::Stmt) -> Block {
15    Block::with_statements(vec![stmt])
16}
17
18/// Create a block with multiple statements
19pub fn block_with_statements(stmts: Vec<crate::ast::Stmt>) -> Block {
20    Block::with_statements(stmts)
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use crate::ast::{Expr, Stmt};
27
28    #[test]
29    fn test_empty_block() {
30        let block = empty_block();
31        assert!(block.statements.is_empty());
32        assert!(block.consts.is_empty());
33        assert!(block.types.is_empty());
34        assert!(block.vars.is_empty());
35    }
36
37    #[test]
38    fn test_block_with_statement() {
39        let stmt = Stmt::Assignment {
40            target: "x".to_string(),
41            value: Expr::Literal(crate::ast::Literal::Integer(42)),
42        };
43        let block = block_with_statement(stmt.clone());
44        assert_eq!(block.statements.len(), 1);
45        assert!(matches!(block.statements[0], Stmt::Assignment { .. }));
46    }
47}