swamp_script_semantic/
ns.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/script
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use crate::symtbl::{SymbolTable, SymbolTableRef};
6use crate::Type;
7use std::fmt::Debug;
8use std::rc::Rc;
9
10#[derive(Debug, Clone)]
11pub struct ModulePathStr(pub Vec<String>);
12
13pub type NamespacePath = Vec<String>;
14
15#[derive(Debug)]
16pub struct Namespace {
17    /*
18    #[allow(unused)]
19    structs: SeqMap<String, StructTypeRef>,
20
21    constants: SeqMap<String, ConstantRef>,
22
23    #[allow(unused)]
24    build_in_rust_types: SeqMap<String, RustTypeRef>,
25
26    #[allow(unused)]
27    enum_types: SeqMap<String, EnumTypeRef>,
28    //enum_variant_types: SeqMap<String, EnumVariantTypeRef>,
29    internal_functions: SeqMap<String, InternalFunctionDefinitionRef>,
30    external_function_declarations: SeqMap<String, ExternalFunctionDefinitionRef>,
31
32     */
33    pub symbol_table: SymbolTableRef,
34
35    pub path: Vec<String>,
36}
37
38pub struct UtilParameter {
39    pub name: String, // Not used,
40    pub is_mutable: bool,
41    pub resolved_type: Type,
42}
43
44impl Namespace {
45    #[must_use]
46    pub fn new(path: NamespacePath, symbol_table: SymbolTable) -> Self {
47        Self {
48            path,
49            symbol_table: Rc::new(symbol_table),
50        }
51    }
52
53    /*
54       pub fn structs(&self) -> &SeqMap<String, StructTypeRef> {
55           &self.structs
56       }
57
58       pub fn add_constant_ref(
59           &mut self,
60           constant_ref: ConstantRef,
61       ) -> Result<ConstantRef, SemanticError> {
62           let name = constant_ref.assigned_name.clone();
63
64           self.constants
65               .insert(name.to_string(), constant_ref.clone())
66               .map_err(|_| SemanticError::DuplicateConstName(name.to_string()))?;
67
68           Ok(constant_ref)
69       }
70
71       pub fn add_struct(
72           &mut self,
73           struct_type: StructType,
74       ) -> Result<StructTypeRef, SemanticError> {
75           let name = struct_type.assigned_name.clone();
76           let struct_ref = Rc::new(RefCell::new(struct_type));
77           self.structs
78               .insert(name.clone(), struct_ref.clone())
79               .map_err(|_| SemanticError::DuplicateStructName(name))?;
80
81           Ok(struct_ref)
82       }
83
84       pub fn add_struct_ref(
85           &mut self,
86           struct_type_ref: StructTypeRef,
87       ) -> Result<(), SemanticError> {
88           let name = struct_type_ref.borrow().assigned_name.clone();
89           self.structs
90               .insert(name.clone(), struct_type_ref)
91               .map_err(|_| SemanticError::DuplicateStructName(name))?;
92           Ok(())
93       }
94
95       pub fn add_generated_struct(
96           &mut self,
97           name: &str,
98           fields: &[(&str, Type)],
99       ) -> Result<StructTypeRef, SemanticError> {
100           let mut resolved_fields = SeqMap::new();
101
102           for (_index, (field_name, field_type)) in fields.iter().enumerate() {
103               let af = AnonymousStructFieldType {
104                   identifier: None,
105                   field_type: field_type.clone(),
106               };
107
108               resolved_fields
109                   .insert(field_name.to_string(), af)
110                   .map_err(|_| SemanticError::DuplicateFieldName(field_name.to_string()))?;
111           }
112
113           let anon_struct_type = AnonymousStructType {
114               defined_fields: resolved_fields,
115           };
116
117           let resolved_struct_type = StructType {
118               name: Node::default(),
119               assigned_name: name.to_string(),
120               anon_struct_type,
121               functions: SeqMap::default(),
122           };
123
124           self.add_struct(resolved_struct_type)
125       }
126
127       pub fn add_built_in_rust_type(
128           &mut self,
129           rust_type: RustType,
130       ) -> Result<RustTypeRef, SemanticError> {
131           let rust_type_ref = Rc::new(rust_type);
132           self.build_in_rust_types
133               .insert(rust_type_ref.type_name.clone(), rust_type_ref.clone())
134               .map_err(|_| {
135                   SemanticError::DuplicateRustType(rust_type_ref.clone().type_name.clone())
136               })?;
137
138           Ok(rust_type_ref)
139       }
140
141       pub fn add_enum_type(
142           &mut self,
143           enum_type: EnumType,
144       ) -> Result<EnumTypeRef, SemanticError> {
145           let enum_type_ref = Rc::new(RefCell::new(enum_type));
146
147           assert!(!enum_type_ref.borrow().module_path.is_empty());
148           self.enum_types
149               .insert(
150                   enum_type_ref.borrow().assigned_name.clone(),
151                   enum_type_ref.clone(),
152               )
153               .map_err(|_| {
154                   SemanticError::DuplicateEnumType(enum_type_ref.borrow().assigned_name.clone())
155               })?;
156
157           Ok(enum_type_ref)
158       }
159
160       pub fn add_enum_variant(
161           &mut self,
162           enum_type_name: EnumTypeRef,
163           enum_variant: EnumVariantType,
164       ) -> Result<EnumVariantTypeRef, SemanticError> {
165           let enum_variant_ref = Rc::new(enum_variant);
166           enum_type_name
167               .borrow_mut()
168               .variants
169               .insert(
170                   enum_variant_ref.common().assigned_name.clone(),
171                   enum_variant_ref.clone(),
172               )
173               .map_err(|_err| {
174                   SemanticError::DuplicateEnumVariantType(
175                       enum_type_name.borrow().assigned_name.clone(),
176                       enum_variant_ref.common().assigned_name.clone(),
177                   )
178               })?;
179
180           Ok(enum_variant_ref)
181       }
182
183       pub fn add_internal_function(
184           &mut self,
185           name: &str,
186           function: InternalFunctionDefinition,
187       ) -> Result<InternalFunctionDefinitionRef, SemanticError> {
188           let function_ref = Rc::new(function);
189           self.internal_functions
190               .insert(name.to_string(), function_ref.clone())
191               .expect("todo: add seqmap error handling");
192           Ok(function_ref)
193       }
194
195       pub fn add_internal_function_link(
196           &mut self,
197           name: &str,
198           function_ref: InternalFunctionDefinitionRef,
199       ) -> Result<(), SemanticError> {
200           self.internal_functions
201               .insert(name.to_string(), function_ref.clone())
202               .expect("todo: add seqmap error handling");
203           Ok(())
204       }
205
206       pub fn get_struct(&self, name: &str) -> Option<&StructTypeRef> {
207           self.structs.get(&name.to_string())
208       }
209
210       pub fn get_enum(&self, name: &str) -> Option<&EnumTypeRef> {
211           self.enum_types.get(&name.to_string())
212       }
213
214       pub fn get_constant(&self, name: &str) -> Option<&ConstantRef> {
215           self.constants.get(&name.to_string())
216       }
217
218       pub fn get_rust_type(&self, name: &str) -> Option<&RustTypeRef> {
219           self.build_in_rust_types.get(&name.to_string())
220       }
221
222       #[must_use]
223       pub fn get_internal_function(
224           &self,
225           name: &str,
226       ) -> Option<&InternalFunctionDefinitionRef> {
227           self.internal_functions.get(&name.to_string())
228       }
229
230       #[must_use]
231       pub fn get_external_function_declaration(
232           &self,
233           name: &str,
234       ) -> Option<&ExternalFunctionDefinitionRef> {
235           self.external_function_declarations.get(&name.to_string())
236       }
237
238       pub fn add_external_function_declaration(
239           &mut self,
240           name: &str,
241           declaration: ExternalFunctionDefinition,
242       ) -> Result<ExternalFunctionDefinitionRef, SemanticError> {
243           let decl_ref = Rc::new(declaration);
244           self.external_function_declarations
245               .insert(name.to_string(), decl_ref.clone())
246               .map_err(|_| SemanticError::DuplicateExternalFunction(name.to_string()))?;
247           Ok(decl_ref)
248       }
249
250       pub fn add_external_function_declaration_link(
251           &mut self,
252           name: &str,
253           decl_ref: ExternalFunctionDefinitionRef,
254       ) -> Result<(), SemanticError> {
255           self.external_function_declarations
256               .insert(name.to_string(), decl_ref.clone())
257               .map_err(|_| SemanticError::DuplicateExternalFunction(name.to_string()))?;
258           Ok(())
259       }
260
261    */
262}