leo_ast/program/
mod.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17//! A Leo program consists of import statements and program scopes.
18
19mod program_id;
20pub use program_id::*;
21
22mod program_scope;
23pub use program_scope::*;
24
25use leo_span::{Span, Symbol};
26
27use crate::{Module, Stub};
28use indexmap::IndexMap;
29use serde::{Deserialize, Serialize};
30use std::fmt;
31
32/// Stores the Leo program abstract syntax tree.
33#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
34pub struct Program {
35    /// A map from module paths to module definitions.
36    pub modules: IndexMap<Vec<Symbol>, Module>,
37    /// A map from import names to import definitions.
38    pub imports: IndexMap<Symbol, (Program, Span)>,
39    /// A map from program stub names to program stub scopes.
40    pub stubs: IndexMap<Symbol, Stub>,
41    /// A map from program names to program scopes.
42    pub program_scopes: IndexMap<Symbol, ProgramScope>,
43}
44
45impl fmt::Display for Program {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        for (_, module) in self.modules.iter() {
48            writeln!(f, "{module}")?;
49        }
50        for (id, _import) in self.imports.iter() {
51            writeln!(f, "import {id}.aleo;")?;
52        }
53        for (_, stub) in self.stubs.iter() {
54            writeln!(f, "{stub}")?;
55        }
56        for (_, program_scope) in self.program_scopes.iter() {
57            writeln!(f, "{program_scope}")?;
58        }
59        Ok(())
60    }
61}
62
63impl Default for Program {
64    /// Constructs an empty program node.
65    fn default() -> Self {
66        Self {
67            modules: IndexMap::new(),
68            imports: IndexMap::new(),
69            stubs: IndexMap::new(),
70            program_scopes: IndexMap::new(),
71        }
72    }
73}