roan_ast/ast/
mod.rs

1use roan_error::TextSpan;
2
3pub mod expr;
4/// Modules that contain definitions and code for statements and expressions in the AST.
5pub mod statements;
6
7pub use expr::*;
8/// Makes items from `statements` and `expr` modules available for use with the AST.
9pub use statements::*;
10
11/// Represents the Abstract Syntax Tree (AST) for the language.
12///
13/// The AST is a structured view of the code, with each part of the code represented as a "node."
14/// It is used for understanding and analyzing the code, such as checking for errors or generating final output.
15///
16/// # Examples
17///
18/// ```rust
19/// use roan_ast::Ast;
20/// let mut ast = Ast::new();
21/// ```
22#[derive(Clone, Debug, PartialEq)]
23pub struct Ast {
24    /// A list of top-level statements (instructions) in the AST.
25    pub stmts: Vec<Stmt>,
26}
27
28impl Ast {
29    /// Creates an empty AST with no statements.
30    ///
31    /// # Returns
32    /// A new `Ast` with an empty list of statements.
33    pub fn new() -> Self {
34        Self { stmts: vec![] }
35    }
36
37    /// Adds a statement to the AST.
38    ///
39    /// # Arguments
40    /// * `stmt` - The statement to add.
41    pub fn add_stmt(&mut self, stmt: Stmt) {
42        self.stmts.push(stmt);
43    }
44
45    /// Returns all statements in the AST.
46    ///
47    /// # Returns
48    /// A reference to the list of statements.
49    pub fn statements(&self) -> &Vec<Stmt> {
50        &self.stmts
51    }
52}
53
54/// A trait to get the source code position (span) of a node in the AST.
55///
56/// `GetSpan` helps retrieve the part of the code (location) related to a node in the AST.
57/// This is useful for showing where errors happen or for debugging.
58pub trait GetSpan {
59    /// Returns the `TextSpan` that shows where this AST node is in the source code.
60    fn span(&self) -> TextSpan;
61}