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::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 import names to import definitions.
36 pub imports: IndexMap<Symbol, (Program, Span)>,
37 /// A map from program stub names to program stub scopes.
38 pub stubs: IndexMap<Symbol, Stub>,
39 /// A map from program names to program scopes.
40 pub program_scopes: IndexMap<Symbol, ProgramScope>,
41}
42
43impl fmt::Display for Program {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 for (id, _import) in self.imports.iter() {
46 writeln!(f, "import {id}.aleo;")?;
47 }
48 for (_, stub) in self.stubs.iter() {
49 writeln!(f, "{}", stub)?;
50 }
51 for (_, program_scope) in self.program_scopes.iter() {
52 writeln!(f, "{}", program_scope)?;
53 }
54 Ok(())
55 }
56}
57
58impl Default for Program {
59 /// Constructs an empty program node.
60 fn default() -> Self {
61 Self { imports: IndexMap::new(), stubs: IndexMap::new(), program_scopes: IndexMap::new() }
62 }
63}