/ compiler / ast / src / program / mod.rs
mod.rs
 1  // Copyright (C) 2019-2025 ADnet Contributors
 2  // This file is part of the ADL library.
 3  
 4  // The ADL 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 ADL 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 ADL library. If not, see <https://www.gnu.org/licenses/>.
16  
17  //! A ADL program consists of import statements and program scopes.
18  
19  mod program_id;
20  pub use program_id::*;
21  
22  mod program_scope;
23  pub use program_scope::*;
24  
25  use adl_span::{Span, Symbol};
26  
27  use crate::{Module, Stub};
28  use indexmap::IndexMap;
29  use serde::{Deserialize, Serialize};
30  use std::fmt;
31  
32  /// Stores the ADL program abstract syntax tree.
33  #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
34  pub 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, 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  
45  impl 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}.alpha;")?;
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  
63  impl 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  }