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