1use crate::modules::ModuleRef;
6use seq_map::SeqMap;
7use std::fmt::Debug;
8use std::rc::Rc;
9use swamp_script_node::Node;
10use swamp_script_semantic::prelude::*;
11use swamp_script_types::ParameterizedTypeBlueprint;
12use swamp_script_types::prelude::*;
13use tiny_ver::TinyVersion;
14
15#[derive(Debug, Clone)]
16pub enum FuncDef {
17 Internal(InternalFunctionDefinitionRef),
18 Intrinsic(IntrinsicFunctionDefinitionRef),
19 External(ExternalFunctionDefinitionRef),
20}
21
22#[derive(Clone, Eq, PartialEq, Debug)]
23pub struct TypeParameterName {
24 pub resolved_node: Node,
25 pub assigned_name: String,
26}
27
28#[derive(Debug)]
29pub struct TypeParameter {
30 pub ty: Type,
31 pub debug_name: String,
32}
33
34#[derive(Clone, Debug)]
35pub enum TypeGeneratorKind {
36 Slice,
37 SlicePair,
38}
39
40#[derive(Clone, Debug)]
41pub struct TypeGenerator {
42 pub arity: usize,
43 pub kind: TypeGeneratorKind,
44}
45
46#[derive(Clone, Debug)]
47pub enum Symbol {
48 Type(Type),
49 Module(ModuleRef),
50 PackageVersion(TinyVersion),
51 Constant(ConstantRef),
52 FunctionDefinition(FuncDef),
53 Alias(AliasType),
54 Blueprint(ParameterizedTypeBlueprint),
55 TypeGenerator(TypeGenerator),
56}
57
58impl Symbol {
59 #[must_use]
60 pub const fn is_basic_type(&self) -> bool {
61 matches!(self, Self::Type(..) | Self::Alias(..))
62 }
63
64 pub const fn is_alias_type(&self) -> bool {
65 matches!(self, Self::Alias(..))
66 }
67}
68
69#[derive(Debug, Clone)]
70pub struct SymbolTable {
71 symbols: SeqMap<String, Symbol>,
72 module_path: Vec<String>,
73}
74
75impl SymbolTable {
76 pub fn internal_functions(&self) -> Vec<InternalFunctionDefinitionRef> {
77 let mut v = Vec::new();
78
79 for (_name, sym) in &self.symbols {
80 if let Symbol::FunctionDefinition(func_def) = sym {
81 if let FuncDef::Internal(internal) = func_def {
82 v.push(internal.clone())
83 }
84 }
85 }
86
87 v
88 }
89}
90
91impl SymbolTable {
92 pub fn module_path(&self) -> Vec<String> {
93 self.module_path.clone()
94 }
95}
96
97pub type SymbolTableRef = Rc<SymbolTable>;
98
99impl SymbolTable {
100 #[must_use]
101 pub fn new(module_path: &[String]) -> Self {
102 Self {
103 symbols: SeqMap::default(),
104 module_path: module_path.to_vec(),
105 }
106 }
107
108 #[must_use]
109 pub fn is_empty(&self) -> bool {
110 self.symbols.is_empty()
111 }
112
113 #[must_use]
114 pub const fn symbols(&self) -> &SeqMap<String, Symbol> {
115 &self.symbols
116 }
117
118 pub fn structs(&self) -> SeqMap<String, NamedStructType> {
119 let mut structs = SeqMap::new();
120
121 for (name, symbol) in &self.symbols {
122 if let Symbol::Type(ty) = symbol {
123 if let Type::NamedStruct(struct_ref) = ty {
124 structs
125 .insert(name.to_string(), struct_ref.clone())
126 .unwrap();
127 }
128 }
129 }
130
131 structs
132 }
133
134 pub fn extend_from(&mut self, symbol_table: &Self) -> Result<(), SemanticError> {
137 for (name, symbol) in symbol_table.symbols() {
138 self.add_symbol(name, symbol.clone())?;
139 }
140 Ok(())
141 }
142
143 pub fn extend_basic_from(&mut self, symbol_table: &Self) -> Result<(), SemanticError> {
146 for (name, symbol) in symbol_table.symbols() {
147 if symbol.is_basic_type() {
148 self.add_symbol(name, symbol.clone())?;
149 }
150 }
151 Ok(())
152 }
153
154 pub fn extend_alias_from(&mut self, symbol_table: &Self) -> Result<(), SemanticError> {
157 for (name, symbol) in symbol_table.symbols() {
158 if symbol.is_alias_type() {
159 self.add_symbol(name, symbol.clone())?;
160 }
161 }
162 Ok(())
163 }
164
165 pub fn extend_intrinsic_functions_from(
166 &mut self,
167 symbol_table: &Self,
168 ) -> Result<(), SemanticError> {
169 for (name, symbol) in symbol_table.symbols() {
170 if let Symbol::FunctionDefinition(func_def) = symbol {
171 if let FuncDef::Intrinsic(_intrinsic_def) = func_def {
172 self.add_symbol(name, symbol.clone())?;
173 }
174 }
175 }
176 Ok(())
177 }
178
179 #[must_use]
180 pub fn get_package_version(&self, name: &str) -> Option<String> {
181 match self.get_symbol(name)? {
182 Symbol::PackageVersion(name) => Some(name.to_string()),
183 _ => None,
184 }
185 }
186
187 pub fn add_constant(&mut self, constant: Constant) -> Result<ConstantRef, SemanticError> {
190 let constant_ref = Rc::new(constant);
191
192 self.add_constant_link(constant_ref.clone())?;
193
194 Ok(constant_ref)
195 }
196
197 pub fn add_constant_link(&mut self, constant_ref: ConstantRef) -> Result<(), SemanticError> {
200 let name = constant_ref.assigned_name.clone();
201
202 self.symbols
203 .insert(name.to_string(), Symbol::Constant(constant_ref))
204 .map_err(|_| SemanticError::DuplicateConstName(name.to_string()))?;
205
206 Ok(())
207 }
208
209 pub fn add_alias(&mut self, alias_type: AliasType) -> Result<AliasType, SemanticError> {
212 self.add_alias_link(alias_type.clone())?;
213 Ok(alias_type)
214 }
215
216 pub fn add_alias_link(&mut self, alias_type_ref: AliasType) -> Result<(), SemanticError> {
219 let name = alias_type_ref.assigned_name.clone();
220 self.symbols
221 .insert(name.clone(), Symbol::Alias(alias_type_ref.clone()))
222 .map_err(|_| SemanticError::DuplicateStructName(name))?;
223
224 Ok(())
225 }
226 pub fn add_blueprint(
229 &mut self,
230 blueprint: ParameterizedTypeBlueprint,
231 ) -> Result<ParameterizedTypeBlueprint, SemanticError> {
232 self.add_blueprint_link(blueprint.clone())?;
233 Ok(blueprint)
234 }
235
236 pub fn add_blueprint_link(
239 &mut self,
240 blueprint_ref: ParameterizedTypeBlueprint,
241 ) -> Result<(), SemanticError> {
242 let name = blueprint_ref.name().clone();
243 self.symbols
244 .insert(name.clone(), Symbol::Blueprint(blueprint_ref))
245 .map_err(|_| SemanticError::DuplicateStructName(name))?;
246 Ok(())
247 }
248
249 #[must_use]
250 pub fn get_blueprint(&self, name: &str) -> Option<&ParameterizedTypeBlueprint> {
251 if let Some(found_symbol) = self.get_symbol(name) {
252 if let Symbol::Blueprint(type_ref) = found_symbol {
253 return Some(type_ref);
254 }
255 }
256
257 None
258 }
259
260 pub fn add_type_generator(
263 &mut self,
264 name: &str,
265 type_generator: TypeGenerator,
266 ) -> Result<TypeGenerator, SemanticError> {
267 self.add_type_generator_link(name, type_generator.clone())?;
268 Ok(type_generator)
269 }
270
271 pub fn add_type_generator_link(
274 &mut self,
275 name: &str,
276 type_generator: TypeGenerator,
277 ) -> Result<(), SemanticError> {
278 self.symbols
279 .insert(name.to_string(), Symbol::TypeGenerator(type_generator))
280 .map_err(|_| SemanticError::DuplicateStructName(name.to_string()))?;
281 Ok(())
282 }
283
284 #[must_use]
285 pub fn get_type_generator(&self, name: &str) -> Option<&TypeGenerator> {
286 if let Some(found_symbol) = self.get_symbol(name) {
287 if let Symbol::TypeGenerator(type_gen) = found_symbol {
288 return Some(type_gen);
289 }
290 }
291
292 None
293 }
294
295 pub fn add_external_type(
296 &mut self,
297 external: ExternalType,
298 ) -> Result<ExternalType, SemanticError> {
299 self.add_external_type_link(external.clone())?;
300 Ok(external)
301 }
302
303 pub fn add_external_type_link(
306 &mut self,
307 external_type_ref: ExternalType,
308 ) -> Result<(), SemanticError> {
309 let name = external_type_ref.type_name.clone();
310 self.symbols
311 .insert(
312 name.clone(),
313 Symbol::Type(Type::External(external_type_ref)),
314 )
315 .map_err(|_| SemanticError::DuplicateStructName(name))?;
316 Ok(())
317 }
318
319 pub fn add_struct(
322 &mut self,
323 struct_type: NamedStructType,
324 ) -> Result<NamedStructType, SemanticError> {
325 self.add_struct_link(struct_type.clone())?;
326 Ok(struct_type)
327 }
328
329 pub fn add_generated_struct(
332 &mut self,
333 name: &str,
334 fields: &[(&str, Type)],
335 ) -> Result<NamedStructType, SemanticError> {
336 let mut defined_fields = SeqMap::new();
337 for (name, field_type) in fields {
338 defined_fields
339 .insert(
340 name.to_string(),
341 StructTypeField {
342 identifier: None,
343 field_type: field_type.clone(),
344 },
345 )
346 .unwrap();
347 }
348
349 let struct_type = NamedStructType {
350 name: Node::default(),
351 assigned_name: name.to_string(),
352 anon_struct_type: AnonymousStructType::new(defined_fields),
353 module_path: self.module_path.clone(),
354 instantiated_type_parameters: Vec::default(),
355 };
356
357 self.add_struct_link(struct_type.clone())?;
358
359 Ok(struct_type)
360 }
361
362 pub fn add_struct_link(
365 &mut self,
366 struct_type_ref: NamedStructType,
367 ) -> Result<(), SemanticError> {
368 let name = struct_type_ref.assigned_name.clone();
369 self.symbols
370 .insert(
371 name.clone(),
372 Symbol::Type(Type::NamedStruct(struct_type_ref)),
373 )
374 .map_err(|_| SemanticError::DuplicateStructName(name))?;
375 Ok(())
376 }
377
378 pub fn add_enum_type(&mut self, enum_type: EnumType) -> Result<(), SemanticError> {
379 self.add_enum_type_link(enum_type.clone())?;
380 Ok(())
381 }
382
383 pub fn add_enum_type_link(&mut self, enum_type_ref: EnumType) -> Result<(), SemanticError> {
384 let ty = Type::Enum(enum_type_ref.clone());
385 self.symbols
386 .insert(enum_type_ref.assigned_name.clone(), Symbol::Type(ty))
387 .map_err(|_| SemanticError::DuplicateEnumType(enum_type_ref.assigned_name.clone()))?;
388
389 Ok(())
390 }
391
392 pub fn add_internal_function(
393 &mut self,
394 name: &str,
395 function: InternalFunctionDefinition,
396 ) -> Result<InternalFunctionDefinitionRef, SemanticError> {
397 let function_ref = Rc::new(function);
398 self.symbols
399 .insert(
400 name.to_string(),
401 Symbol::FunctionDefinition(FuncDef::Internal(function_ref.clone())),
402 )
403 .expect("todo: add seqmap error handling");
404 Ok(function_ref)
405 }
406
407 pub fn add_internal_function_link(
408 &mut self,
409 name: &str,
410 function_ref: InternalFunctionDefinitionRef,
411 ) -> Result<(), SemanticError> {
412 self.symbols
413 .insert(
414 name.to_string(),
415 Symbol::FunctionDefinition(FuncDef::Internal(function_ref.clone())),
416 )
417 .expect("todo: add seqmap error handling");
418 Ok(())
419 }
420
421 pub fn get_symbol(&self, name: &str) -> Option<&Symbol> {
422 self.symbols.get(&name.to_string())
423 }
424
425 pub fn add_symbol(&mut self, name: &str, symbol: Symbol) -> Result<(), SemanticError> {
426 self.symbols
427 .insert(name.to_string(), symbol)
428 .map_err(|_| SemanticError::DuplicateSymbolName(name.to_string()))
429 }
430
431 pub fn get_type(&self, name: &str) -> Option<&Type> {
432 if let Some(found_symbol) = self.get_symbol(name) {
433 if let Symbol::Type(type_ref) = found_symbol {
434 return Some(type_ref);
435 }
436 }
437
438 None
439 }
440
441 pub fn get_struct(&self, name: &str) -> Option<&NamedStructType> {
442 match self.get_type(name)? {
443 Type::NamedStruct(struct_ref) => Some(struct_ref),
444 _ => None,
445 }
446 }
447
448 pub fn get_enum(&self, name: &str) -> Option<&EnumType> {
449 match self.get_type(name)? {
450 Type::Enum(enum_type) => Some(enum_type),
451 _ => None,
452 }
453 }
454
455 #[must_use]
456 pub fn get_enum_variant_type(
457 &self,
458 enum_type_name: &str,
459 variant_name: &str,
460 ) -> Option<EnumVariantType> {
461 self.get_enum(enum_type_name).as_ref().map_or_else(
462 || None,
463 |found_enum| found_enum.variants.get(&variant_name.to_string()).cloned(),
464 )
465 }
466
467 pub fn get_constant(&self, name: &str) -> Option<&ConstantRef> {
468 match self.get_symbol(name)? {
469 Symbol::Constant(constant) => Some(constant),
470 _ => None,
471 }
472 }
473
474 #[must_use]
477 pub fn get_function(&self, name: &str) -> Option<&FuncDef> {
478 match self.get_symbol(name)? {
479 Symbol::FunctionDefinition(func_def) => Some(func_def),
480 _ => None,
481 }
482 }
483
484 #[must_use]
485 pub fn get_internal_function(&self, name: &str) -> Option<&InternalFunctionDefinitionRef> {
486 match self.get_function(name)? {
487 FuncDef::Internal(internal_fn) => Some(internal_fn),
488 FuncDef::External(_) => None,
489 FuncDef::Intrinsic(_) => None,
490 }
491 }
492
493 #[must_use]
494 pub fn get_intrinsic_function(&self, name: &str) -> Option<&IntrinsicFunctionDefinitionRef> {
495 match self.get_function(name)? {
496 FuncDef::Intrinsic(intrinsic_fn) => Some(intrinsic_fn),
497 _ => None,
498 }
499 }
500
501 #[must_use]
502 pub fn get_external_function_declaration(
503 &self,
504 name: &str,
505 ) -> Option<&ExternalFunctionDefinitionRef> {
506 match self.get_function(name)? {
507 FuncDef::External(external_def) => Some(external_def),
508 _ => None,
509 }
510 }
511
512 pub fn get_external_type(&self, name: &str) -> Option<&ExternalType> {
513 match self.get_type(name)? {
514 Type::External(ext_type) => Some(ext_type),
515 _ => None,
516 }
517 }
518
519 fn insert_symbol(&mut self, name: &str, symbol: Symbol) -> Result<(), SemanticError> {
520 self.symbols
521 .insert(name.to_string(), symbol)
522 .map_err(|_| SemanticError::DuplicateSymbolName(name.to_string()))
523 }
524
525 pub fn add_external_function_declaration(
526 &mut self,
527 decl: ExternalFunctionDefinition,
528 ) -> Result<ExternalFunctionDefinitionRef, SemanticError> {
529 let decl_ref = Rc::new(decl);
530
531 self.add_external_function_declaration_link(decl_ref.clone())?;
532
533 Ok(decl_ref)
534 }
535
536 pub fn add_external_function_declaration_link(
537 &mut self,
538 decl_ref: ExternalFunctionDefinitionRef,
539 ) -> Result<(), SemanticError> {
540 self.insert_symbol(
541 &decl_ref.assigned_name,
542 Symbol::FunctionDefinition(FuncDef::External(decl_ref.clone())),
543 )
544 .map_err(|_| {
545 SemanticError::DuplicateExternalFunction(decl_ref.assigned_name.to_string())
546 })?;
547 Ok(())
548 }
549
550 pub fn add_module_link(&mut self, name: &str, ns: ModuleRef) -> Result<(), SemanticError> {
551 self.insert_symbol(name, Symbol::Module(ns))
552 .map_err(|_| SemanticError::DuplicateNamespaceLink(name.to_string()))?;
553 Ok(())
554 }
555
556 #[must_use]
557 pub fn get_module_link(&self, name: &str) -> Option<&ModuleRef> {
558 match self.get_symbol(name)? {
559 Symbol::Module(module_ref) => Some(module_ref),
560 _ => None,
561 }
562 }
563
564 pub fn add_package_version(
565 &mut self,
566 name: &str,
567 version: TinyVersion,
568 ) -> Result<(), SemanticError> {
569 self.insert_symbol(name, Symbol::PackageVersion(version))
570 .map_err(|_| SemanticError::DuplicateNamespaceLink(name.to_string()))?;
571 Ok(())
572 }
573
574 pub fn add_intrinsic_function(
575 &mut self,
576 function: IntrinsicFunctionDefinition,
577 ) -> Result<IntrinsicFunctionDefinitionRef, SemanticError> {
578 let function_ref = Rc::new(function);
579 self.symbols
580 .insert(
581 function_ref.name.clone(),
582 Symbol::FunctionDefinition(FuncDef::Intrinsic(function_ref.clone())),
583 )
584 .expect("todo: add seqmap error handling");
585 Ok(function_ref)
586 }
587}