roan_ast/ast/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use roan_error::TextSpan;

pub mod expr;
/// Modules that contain definitions and code for statements and expressions in the AST.
pub mod statements;

pub use expr::*;
/// Makes items from `statements` and `expr` modules available for use with the AST.
pub use statements::*;

/// Represents the Abstract Syntax Tree (AST) for the language.
///
/// The AST is a structured view of the code, with each part of the code represented as a "node."
/// It is used for understanding and analyzing the code, such as checking for errors or generating final output.
///
/// # Examples
///
/// ```rust
/// use roan_ast::Ast;
/// let mut ast = Ast::new();
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct Ast {
    /// A list of top-level statements (instructions) in the AST.
    pub stmts: Vec<Stmt>,
}

impl Ast {
    /// Creates an empty AST with no statements.
    ///
    /// # Returns
    /// A new `Ast` with an empty list of statements.
    pub fn new() -> Self {
        Self { stmts: vec![] }
    }

    /// Adds a statement to the AST.
    ///
    /// # Arguments
    /// * `stmt` - The statement to add.
    pub fn add_stmt(&mut self, stmt: Stmt) {
        self.stmts.push(stmt);
    }

    /// Returns all statements in the AST.
    ///
    /// # Returns
    /// A reference to the list of statements.
    pub fn statements(&self) -> &Vec<Stmt> {
        &self.stmts
    }
}

/// A trait to get the source code position (span) of a node in the AST.
///
/// `GetSpan` helps retrieve the part of the code (location) related to a node in the AST.
/// This is useful for showing where errors happen or for debugging.
pub trait GetSpan {
    /// Returns the `TextSpan` that shows where this AST node is in the source code.
    fn span(&self) -> TextSpan;
}