swamp_script_core/
lib.rs

1use swamp_script_modules::modules::Module;
2use swamp_script_modules::symtbl::{SymbolTable, TypeGenerator, TypeGeneratorKind};
3use swamp_script_node::Node;
4use swamp_script_semantic::prelude::{IntrinsicFunction, IntrinsicFunctionDefinition};
5use swamp_script_types::{AliasType, Signature, Type, TypeForParameter};
6use tiny_ver::TinyVersion;
7
8pub const PACKAGE_NAME: &str = "core";
9fn add_intrinsic_types(core_ns: &mut SymbolTable) {
10    let int_alias = AliasType {
11        name: Node::default(),
12        assigned_name: "Int".to_string(),
13        referenced_type: Type::Int,
14    };
15    core_ns.add_alias(int_alias).unwrap();
16
17    let float_alias = AliasType {
18        name: Node::default(),
19        assigned_name: "Float".to_string(),
20        referenced_type: Type::Float,
21    };
22    core_ns.add_alias(float_alias).unwrap();
23
24    let string_alias = AliasType {
25        name: Node::default(),
26        assigned_name: "String".to_string(),
27        referenced_type: Type::String,
28    };
29    core_ns.add_alias(string_alias).unwrap();
30
31    let bool_alias = AliasType {
32        name: Node::default(),
33        assigned_name: "Bool".to_string(),
34        referenced_type: Type::Bool,
35    };
36    core_ns.add_alias(bool_alias).unwrap();
37}
38
39#[allow(clippy::too_many_lines)]
40fn add_intrinsic_functions(core_ns: &mut SymbolTable) {
41    add_intrinsic_float_functions(core_ns);
42    add_intrinsic_int_functions(core_ns);
43    add_intrinsic_string_functions(core_ns);
44    add_intrinsic_grid_functions(core_ns);
45    add_intrinsic_vec_functions(core_ns);
46    add_intrinsic_map_functions(core_ns);
47    add_intrinsic_map2_functions(core_ns);
48    add_intrinsic_sparse_functions(core_ns);
49}
50
51#[allow(clippy::too_many_lines)]
52fn add_intrinsic_sparse_functions(core_ns: &mut SymbolTable) {
53    let slice_to_self = Signature {
54        parameters: [TypeForParameter {
55            name: "slice".to_string(),
56            resolved_type: Type::Never,
57            is_mutable: false,
58            node: None,
59        }]
60        .into(),
61        return_type: Box::new(Type::Never),
62    };
63    let slice_to_self_functions = [IntrinsicFunction::SparseFromSlice];
64    for intrinsic_fn in slice_to_self_functions {
65        let name = intrinsic_fn.to_string();
66        core_ns
67            .add_intrinsic_function(IntrinsicFunctionDefinition {
68                name,
69                intrinsic: intrinsic_fn,
70                signature: slice_to_self.clone(),
71            })
72            .unwrap();
73    }
74
75    let unit_to_value = Signature {
76        parameters: [].into(),
77        return_type: Box::new(Type::Never),
78    };
79
80    let unit_to_value_functions = [IntrinsicFunction::SparseCreate];
81
82    for intrinsic_fn in unit_to_value_functions {
83        let name = intrinsic_fn.to_string();
84        core_ns
85            .add_intrinsic_function(IntrinsicFunctionDefinition {
86                name,
87                intrinsic: intrinsic_fn,
88                signature: unit_to_value.clone(),
89            })
90            .unwrap();
91    }
92
93    let self_to_value = Signature {
94        parameters: [TypeForParameter {
95            name: "self".to_string(),
96            resolved_type: Type::Never,
97            is_mutable: false,
98            node: None,
99        }]
100        .into(),
101        return_type: Box::new(Type::Never),
102    };
103
104    let self_to_value_functions = [
105        IntrinsicFunction::SparseIter,
106        IntrinsicFunction::SparseIterMut,
107    ];
108
109    for intrinsic_fn in self_to_value_functions {
110        let name = intrinsic_fn.to_string();
111        core_ns
112            .add_intrinsic_function(IntrinsicFunctionDefinition {
113                name,
114                intrinsic: intrinsic_fn,
115                signature: self_to_value.clone(),
116            })
117            .unwrap();
118    }
119
120    let self_value_to_value = Signature {
121        parameters: [
122            TypeForParameter {
123                name: "self".to_string(),
124                resolved_type: Type::Never,
125                is_mutable: false,
126                node: None,
127            },
128            TypeForParameter {
129                name: "i".to_string(),
130                resolved_type: Type::Never,
131                is_mutable: false,
132                node: None,
133            },
134        ]
135        .into(),
136        return_type: Box::new(Type::Never),
137    };
138
139    let self_value_to_value_functions = [
140        IntrinsicFunction::SparseSubscript,
141        IntrinsicFunction::SparseAdd,
142    ];
143
144    for intrinsic_fn in self_value_to_value_functions {
145        let name = intrinsic_fn.to_string();
146        core_ns
147            .add_intrinsic_function(IntrinsicFunctionDefinition {
148                name,
149                intrinsic: intrinsic_fn,
150                signature: self_value_to_value.clone(),
151            })
152            .unwrap();
153    }
154
155    let self_value_value_mut_to_unit = Signature {
156        parameters: [
157            TypeForParameter {
158                name: "self".to_string(),
159                resolved_type: Type::Never,
160                is_mutable: false,
161                node: None,
162            },
163            TypeForParameter {
164                name: "key".to_string(),
165                resolved_type: Type::Never,
166                is_mutable: false,
167                node: None,
168            },
169            TypeForParameter {
170                name: "value".to_string(),
171                resolved_type: Type::Never,
172                is_mutable: true,
173                node: None,
174            },
175        ]
176        .into(),
177        return_type: Box::new(Type::Never),
178    };
179
180    let self_value_value_mut_to_unit_functions = [IntrinsicFunction::SparseSubscriptMut];
181
182    for intrinsic_fn in self_value_value_mut_to_unit_functions {
183        let name = intrinsic_fn.to_string();
184        core_ns
185            .add_intrinsic_function(IntrinsicFunctionDefinition {
186                name,
187                intrinsic: intrinsic_fn,
188                signature: self_value_value_mut_to_unit.clone(),
189            })
190            .unwrap();
191    }
192
193    let self_value_to_bool = Signature {
194        parameters: [
195            TypeForParameter {
196                name: "self".to_string(),
197                resolved_type: Type::Never,
198                is_mutable: false,
199                node: None,
200            },
201            TypeForParameter {
202                name: "i".to_string(),
203                resolved_type: Type::Never,
204                is_mutable: false,
205                node: None,
206            },
207        ]
208        .into(),
209        return_type: Box::new(Type::Bool),
210    };
211
212    let self_value_to_bool_functions = [IntrinsicFunction::SparseHas];
213
214    for intrinsic_fn in self_value_to_bool_functions {
215        let name = intrinsic_fn.to_string();
216        core_ns
217            .add_intrinsic_function(IntrinsicFunctionDefinition {
218                name,
219                intrinsic: intrinsic_fn,
220                signature: self_value_to_bool.clone(),
221            })
222            .unwrap();
223    }
224
225    let self_value_to_option_value = Signature {
226        parameters: [
227            TypeForParameter {
228                name: "self".to_string(),
229                resolved_type: Type::Never,
230                is_mutable: false,
231                node: None,
232            },
233            TypeForParameter {
234                name: "key".to_string(),
235                resolved_type: Type::Never,
236                is_mutable: false,
237                node: None,
238            },
239        ]
240        .into(),
241        return_type: Box::new(Type::Optional(Box::new(Type::Never))),
242    };
243
244    let self_value_to_option_value_functions = [IntrinsicFunction::SparseRemove];
245
246    for intrinsic_fn in self_value_to_option_value_functions {
247        let name = intrinsic_fn.to_string();
248        core_ns
249            .add_intrinsic_function(IntrinsicFunctionDefinition {
250                name,
251                intrinsic: intrinsic_fn,
252                signature: self_value_to_option_value.clone(),
253            })
254            .unwrap();
255    }
256}
257
258#[allow(clippy::too_many_lines)]
259fn add_intrinsic_map2_functions(core_ns: &mut SymbolTable) {
260    let self_value_value_to_bool = Signature {
261        parameters: [
262            TypeForParameter {
263                name: "self".to_string(),
264                resolved_type: Type::Never,
265                is_mutable: false,
266                node: None,
267            },
268            TypeForParameter {
269                name: "x".to_string(),
270                resolved_type: Type::Never,
271                is_mutable: false,
272                node: None,
273            },
274            TypeForParameter {
275                name: "y".to_string(),
276                resolved_type: Type::Never,
277                is_mutable: false,
278                node: None,
279            },
280        ]
281        .into(),
282        return_type: Box::new(Type::Bool),
283    };
284    let self_value_to_bool_functions = [IntrinsicFunction::Map2Has];
285    for intrinsic_fn in self_value_to_bool_functions {
286        let name = intrinsic_fn.to_string();
287        core_ns
288            .add_intrinsic_function(IntrinsicFunctionDefinition {
289                name,
290                intrinsic: intrinsic_fn,
291                signature: self_value_value_to_bool.clone(),
292            })
293            .unwrap();
294    }
295
296    let self_value_to_vec = Signature {
297        parameters: [
298            TypeForParameter {
299                name: "self".to_string(),
300                resolved_type: Type::Never,
301                is_mutable: false,
302                node: None,
303            },
304            TypeForParameter {
305                name: "x".to_string(),
306                resolved_type: Type::Never,
307                is_mutable: false,
308                node: None,
309            },
310        ]
311        .into(),
312        return_type: Box::new(Type::Never),
313    };
314    let self_value_to_vec_functions = [IntrinsicFunction::Map2GetColumn];
315    for intrinsic_fn in self_value_to_vec_functions {
316        let name = intrinsic_fn.to_string();
317        core_ns
318            .add_intrinsic_function(IntrinsicFunctionDefinition {
319                name,
320                intrinsic: intrinsic_fn,
321                signature: self_value_to_vec.clone(),
322            })
323            .unwrap();
324    }
325
326    let self_value_value_to_value = Signature {
327        parameters: [
328            TypeForParameter {
329                name: "self".to_string(),
330                resolved_type: Type::Never,
331                is_mutable: false,
332                node: None,
333            },
334            TypeForParameter {
335                name: "x".to_string(),
336                resolved_type: Type::Never,
337                is_mutable: false,
338                node: None,
339            },
340            TypeForParameter {
341                name: "y".to_string(),
342                resolved_type: Type::Never,
343                is_mutable: false,
344                node: None,
345            },
346        ]
347        .into(),
348        return_type: Box::new(Type::Never),
349    };
350    let self_value_value_to_value_functions = [IntrinsicFunction::Map2Get];
351    for intrinsic_fn in self_value_value_to_value_functions {
352        let name = intrinsic_fn.to_string();
353        core_ns
354            .add_intrinsic_function(IntrinsicFunctionDefinition {
355                name,
356                intrinsic: intrinsic_fn,
357                signature: self_value_value_to_value.clone(),
358            })
359            .unwrap();
360    }
361
362    let self_value_y_to_vec = Signature {
363        parameters: [
364            TypeForParameter {
365                name: "self".to_string(),
366                resolved_type: Type::Never,
367                is_mutable: false,
368                node: None,
369            },
370            TypeForParameter {
371                name: "y".to_string(),
372                resolved_type: Type::Never,
373                is_mutable: false,
374                node: None,
375            },
376        ]
377        .into(),
378        return_type: Box::new(Type::Never),
379    };
380    let self_value_y_to_vec_functions = [IntrinsicFunction::Map2GetRow];
381    for intrinsic_fn in self_value_y_to_vec_functions {
382        let name = intrinsic_fn.to_string();
383        core_ns
384            .add_intrinsic_function(IntrinsicFunctionDefinition {
385                name,
386                intrinsic: intrinsic_fn,
387                signature: self_value_y_to_vec.clone(),
388            })
389            .unwrap();
390    }
391
392    let self_value_value_to_unit = Signature {
393        parameters: [
394            TypeForParameter {
395                name: "self".to_string(),
396                resolved_type: Type::Never,
397                is_mutable: true,
398                node: None,
399            },
400            TypeForParameter {
401                name: "x".to_string(),
402                resolved_type: Type::Never,
403                is_mutable: false,
404                node: None,
405            },
406            TypeForParameter {
407                name: "y".to_string(),
408                resolved_type: Type::Never,
409                is_mutable: false,
410                node: None,
411            },
412        ]
413        .into(),
414        return_type: Box::new(Type::Unit),
415    };
416    let self_value_value_to_unit_functions = [IntrinsicFunction::Map2Remove];
417    for intrinsic_fn in self_value_value_to_unit_functions {
418        let name = intrinsic_fn.to_string();
419        core_ns
420            .add_intrinsic_function(IntrinsicFunctionDefinition {
421                name,
422                intrinsic: intrinsic_fn,
423                signature: self_value_value_to_unit.clone(),
424            })
425            .unwrap();
426    }
427
428    let self_value_value_value_to_unit = Signature {
429        parameters: [
430            TypeForParameter {
431                name: "self".to_string(),
432                resolved_type: Type::Never,
433                is_mutable: true,
434                node: None,
435            },
436            TypeForParameter {
437                name: "x".to_string(),
438                resolved_type: Type::Never,
439                is_mutable: false,
440                node: None,
441            },
442            TypeForParameter {
443                name: "y".to_string(),
444                resolved_type: Type::Never,
445                is_mutable: false,
446                node: None,
447            },
448            TypeForParameter {
449                name: "v".to_string(),
450                resolved_type: Type::Never,
451                is_mutable: false,
452                node: None,
453            },
454        ]
455        .into(),
456        return_type: Box::new(Type::Unit),
457    };
458    let self_value_value_value_to_unit_functions = [IntrinsicFunction::Map2Insert];
459    for intrinsic_fn in self_value_value_value_to_unit_functions {
460        let name = intrinsic_fn.to_string();
461        core_ns
462            .add_intrinsic_function(IntrinsicFunctionDefinition {
463                name,
464                intrinsic: intrinsic_fn,
465                signature: self_value_value_value_to_unit.clone(),
466            })
467            .unwrap();
468    }
469
470    let to_self = Signature {
471        parameters: [].into(),
472        return_type: Box::new(Type::Never),
473    };
474    let to_self_functions = [IntrinsicFunction::Map2Create];
475    for intrinsic_fn in to_self_functions {
476        let name = intrinsic_fn.to_string();
477        core_ns
478            .add_intrinsic_function(IntrinsicFunctionDefinition {
479                name,
480                intrinsic: intrinsic_fn,
481                signature: to_self.clone(),
482            })
483            .unwrap();
484    }
485}
486
487#[allow(clippy::too_many_lines)]
488fn add_intrinsic_map_functions(core_ns: &mut SymbolTable) {
489    let slice_to_self = Signature {
490        parameters: [TypeForParameter {
491            name: "slice_pair".to_string(),
492            resolved_type: Type::Never,
493            is_mutable: false,
494            node: None,
495        }]
496        .into(),
497        return_type: Box::new(Type::Never),
498    };
499    let slice_to_self_functions = [IntrinsicFunction::MapFromSlicePair];
500    for intrinsic_fn in slice_to_self_functions {
501        let name = intrinsic_fn.to_string();
502        core_ns
503            .add_intrinsic_function(IntrinsicFunctionDefinition {
504                name,
505                intrinsic: intrinsic_fn,
506                signature: slice_to_self.clone(),
507            })
508            .unwrap();
509    }
510
511    let unit_to_value = Signature {
512        parameters: [].into(),
513        return_type: Box::new(Type::Never),
514    };
515
516    let unit_to_value_functions = [IntrinsicFunction::MapCreate];
517
518    for intrinsic_fn in unit_to_value_functions {
519        let name = intrinsic_fn.to_string();
520        core_ns
521            .add_intrinsic_function(IntrinsicFunctionDefinition {
522                name,
523                intrinsic: intrinsic_fn,
524                signature: unit_to_value.clone(),
525            })
526            .unwrap();
527    }
528
529    let self_to_value = Signature {
530        parameters: [TypeForParameter {
531            name: "self".to_string(),
532            resolved_type: Type::Never,
533            is_mutable: false,
534            node: None,
535        }]
536        .into(),
537        return_type: Box::new(Type::Never),
538    };
539
540    let self_to_value_functions = [IntrinsicFunction::MapIter, IntrinsicFunction::MapIterMut];
541
542    for intrinsic_fn in self_to_value_functions {
543        let name = intrinsic_fn.to_string();
544        core_ns
545            .add_intrinsic_function(IntrinsicFunctionDefinition {
546                name,
547                intrinsic: intrinsic_fn,
548                signature: self_to_value.clone(),
549            })
550            .unwrap();
551    }
552
553    let self_value_to_value = Signature {
554        parameters: [
555            TypeForParameter {
556                name: "self".to_string(),
557                resolved_type: Type::Never,
558                is_mutable: false,
559                node: None,
560            },
561            TypeForParameter {
562                name: "i".to_string(),
563                resolved_type: Type::Never,
564                is_mutable: false,
565                node: None,
566            },
567        ]
568        .into(),
569        return_type: Box::new(Type::Never),
570    };
571
572    let self_value_to_value_functions = [IntrinsicFunction::MapSubscript];
573
574    for intrinsic_fn in self_value_to_value_functions {
575        let name = intrinsic_fn.to_string();
576        core_ns
577            .add_intrinsic_function(IntrinsicFunctionDefinition {
578                name,
579                intrinsic: intrinsic_fn,
580                signature: self_value_to_value.clone(),
581            })
582            .unwrap();
583    }
584
585    let self_value_value_mut_to_unit = Signature {
586        parameters: [
587            TypeForParameter {
588                name: "self".to_string(),
589                resolved_type: Type::Never,
590                is_mutable: true,
591                node: None,
592            },
593            TypeForParameter {
594                name: "key".to_string(),
595                resolved_type: Type::Never,
596                is_mutable: false,
597                node: None,
598            },
599        ]
600        .into(),
601        return_type: Box::new(Type::Never),
602    };
603
604    let self_value_value_mut_to_unit_functions = [
605        IntrinsicFunction::MapSubscriptMut,
606        IntrinsicFunction::MapSubscriptMutCreateIfNeeded,
607    ];
608
609    for intrinsic_fn in self_value_value_mut_to_unit_functions {
610        let name = intrinsic_fn.to_string();
611        core_ns
612            .add_intrinsic_function(IntrinsicFunctionDefinition {
613                name,
614                intrinsic: intrinsic_fn,
615                signature: self_value_value_mut_to_unit.clone(),
616            })
617            .unwrap();
618    }
619
620    let self_value_to_bool = Signature {
621        parameters: [
622            TypeForParameter {
623                name: "self".to_string(),
624                resolved_type: Type::Never,
625                is_mutable: false,
626                node: None,
627            },
628            TypeForParameter {
629                name: "i".to_string(),
630                resolved_type: Type::Never,
631                is_mutable: false,
632                node: None,
633            },
634        ]
635        .into(),
636        return_type: Box::new(Type::Bool),
637    };
638
639    let self_value_to_bool_functions = [IntrinsicFunction::MapHas];
640
641    for intrinsic_fn in self_value_to_bool_functions {
642        let name = intrinsic_fn.to_string();
643        core_ns
644            .add_intrinsic_function(IntrinsicFunctionDefinition {
645                name,
646                intrinsic: intrinsic_fn,
647                signature: self_value_to_bool.clone(),
648            })
649            .unwrap();
650    }
651
652    let self_value_to_unit_value = Signature {
653        parameters: [
654            TypeForParameter {
655                name: "self".to_string(),
656                resolved_type: Type::Never,
657                is_mutable: true,
658                node: None,
659            },
660            TypeForParameter {
661                name: "key".to_string(),
662                resolved_type: Type::Never,
663                is_mutable: false,
664                node: None,
665            },
666        ]
667        .into(),
668        return_type: Box::new(Type::Unit),
669    };
670
671    let self_value_to_option_value_functions = [IntrinsicFunction::MapRemove];
672
673    for intrinsic_fn in self_value_to_option_value_functions {
674        let name = intrinsic_fn.to_string();
675        core_ns
676            .add_intrinsic_function(IntrinsicFunctionDefinition {
677                name,
678                intrinsic: intrinsic_fn,
679                signature: self_value_to_unit_value.clone(),
680            })
681            .unwrap();
682    }
683
684    let self_to_int = Signature {
685        parameters: [TypeForParameter {
686            name: "self".to_string(),
687            resolved_type: Type::Never,
688            is_mutable: false,
689            node: None,
690        }]
691        .into(),
692        return_type: Box::new(Type::Int),
693    };
694
695    let self_to_int_functions = [IntrinsicFunction::MapLen];
696
697    for intrinsic_fn in self_to_int_functions {
698        let name = intrinsic_fn.to_string();
699        core_ns
700            .add_intrinsic_function(IntrinsicFunctionDefinition {
701                name,
702                intrinsic: intrinsic_fn,
703                signature: self_to_int.clone(),
704            })
705            .unwrap();
706    }
707
708    let self_to_bool = Signature {
709        parameters: [TypeForParameter {
710            name: "self".to_string(),
711            resolved_type: Type::Never,
712            is_mutable: false,
713            node: None,
714        }]
715        .into(),
716        return_type: Box::new(Type::Bool),
717    };
718
719    let self_to_bool_functions = [IntrinsicFunction::MapIsEmpty];
720
721    for intrinsic_fn in self_to_bool_functions {
722        let name = intrinsic_fn.to_string();
723        core_ns
724            .add_intrinsic_function(IntrinsicFunctionDefinition {
725                name,
726                intrinsic: intrinsic_fn,
727                signature: self_to_bool.clone(),
728            })
729            .unwrap();
730    }
731}
732
733#[allow(clippy::too_many_lines)]
734fn add_intrinsic_grid_functions(core_ns: &mut SymbolTable) {
735    let int_int_value_to_self = Signature {
736        parameters: [
737            TypeForParameter {
738                name: "x".to_string(),
739                resolved_type: Type::Int,
740                is_mutable: false,
741                node: None,
742            },
743            TypeForParameter {
744                name: "y".to_string(),
745                resolved_type: Type::Int,
746                is_mutable: false,
747                node: None,
748            },
749            TypeForParameter {
750                name: "initial_value".to_string(),
751                resolved_type: Type::Never,
752                is_mutable: false,
753                node: None,
754            },
755        ]
756        .into(),
757        return_type: Box::new(Type::Never),
758    };
759    let int_int_value_to_self_functions = [IntrinsicFunction::GridCreate];
760    for intrinsic_fn in int_int_value_to_self_functions {
761        let name = intrinsic_fn.to_string();
762        core_ns
763            .add_intrinsic_function(IntrinsicFunctionDefinition {
764                name,
765                intrinsic: intrinsic_fn,
766                signature: int_int_value_to_self.clone(),
767            })
768            .unwrap();
769    }
770
771    let self_value_int_int_value_to_unit = Signature {
772        parameters: [
773            TypeForParameter {
774                name: "self".to_string(),
775                resolved_type: Type::Never,
776                is_mutable: true,
777                node: None,
778            },
779            TypeForParameter {
780                name: "x".to_string(),
781                resolved_type: Type::Int,
782                is_mutable: false,
783                node: None,
784            },
785            TypeForParameter {
786                name: "y".to_string(),
787                resolved_type: Type::Int,
788                is_mutable: false,
789                node: None,
790            },
791            TypeForParameter {
792                name: "v".to_string(),
793                resolved_type: Type::Never,
794                is_mutable: false,
795                node: None,
796            },
797        ]
798        .into(),
799        return_type: Box::new(Type::Unit),
800    };
801    let self_value_int_int_value_to_unit_functions = [IntrinsicFunction::GridSet];
802    for intrinsic_fn in self_value_int_int_value_to_unit_functions {
803        let name = intrinsic_fn.to_string();
804        core_ns
805            .add_intrinsic_function(IntrinsicFunctionDefinition {
806                name,
807                intrinsic: intrinsic_fn,
808                signature: self_value_int_int_value_to_unit.clone(),
809            })
810            .unwrap();
811    }
812
813    let self_int_int_to_value = Signature {
814        parameters: [
815            TypeForParameter {
816                name: "self".to_string(),
817                resolved_type: Type::Never,
818                is_mutable: false,
819                node: None,
820            },
821            TypeForParameter {
822                name: "x".to_string(),
823                resolved_type: Type::Int,
824                is_mutable: false,
825                node: None,
826            },
827            TypeForParameter {
828                name: "y".to_string(),
829                resolved_type: Type::Int,
830                is_mutable: false,
831                node: None,
832            },
833        ]
834        .into(),
835        return_type: Box::new(Type::Never),
836    };
837    let self_int_int_to_value_functions = [IntrinsicFunction::GridGet];
838    for intrinsic_fn in self_int_int_to_value_functions {
839        let name = intrinsic_fn.to_string();
840        core_ns
841            .add_intrinsic_function(IntrinsicFunctionDefinition {
842                name,
843                intrinsic: intrinsic_fn,
844                signature: self_int_int_to_value.clone(),
845            })
846            .unwrap();
847    }
848
849    let self_int_to_array_value = Signature {
850        parameters: [
851            TypeForParameter {
852                name: "self".to_string(),
853                resolved_type: Type::Never,
854                is_mutable: false,
855                node: None,
856            },
857            TypeForParameter {
858                name: "x".to_string(),
859                resolved_type: Type::Int,
860                is_mutable: false,
861                node: None,
862            },
863        ]
864        .into(),
865        return_type: Box::new(Type::Never),
866    };
867    let self_int_to_array_value_functions = [IntrinsicFunction::GridGetColumn];
868    for intrinsic_fn in self_int_to_array_value_functions {
869        let name = intrinsic_fn.to_string();
870        core_ns
871            .add_intrinsic_function(IntrinsicFunctionDefinition {
872                name,
873                intrinsic: intrinsic_fn,
874                signature: self_int_to_array_value.clone(),
875            })
876            .unwrap();
877    }
878}
879
880#[allow(clippy::too_many_lines)]
881fn add_intrinsic_vec_functions(core_ns: &mut SymbolTable) {
882    let unit_to_value = Signature {
883        parameters: [].into(),
884        return_type: Box::new(Type::Never),
885    };
886
887    let unit_to_value_functions = [IntrinsicFunction::VecCreate];
888
889    for intrinsic_fn in unit_to_value_functions {
890        let name = intrinsic_fn.to_string();
891        core_ns
892            .add_intrinsic_function(IntrinsicFunctionDefinition {
893                name,
894                intrinsic: intrinsic_fn,
895                signature: unit_to_value.clone(),
896            })
897            .unwrap();
898    }
899
900    let self_value_to_unit = Signature {
901        parameters: [
902            TypeForParameter {
903                name: "self".to_string(),
904                resolved_type: Type::Never,
905                is_mutable: true,
906                node: None,
907            },
908            TypeForParameter {
909                name: "v".to_string(),
910                resolved_type: Type::Never,
911                is_mutable: false,
912                node: None,
913            },
914        ]
915        .into(),
916        return_type: Box::new(Type::Unit),
917    };
918
919    let self_value_to_unit_functions = [IntrinsicFunction::VecPush];
920
921    for intrinsic_fn in self_value_to_unit_functions {
922        let name = intrinsic_fn.to_string();
923        core_ns
924            .add_intrinsic_function(IntrinsicFunctionDefinition {
925                name,
926                intrinsic: intrinsic_fn,
927                signature: self_value_to_unit.clone(),
928            })
929            .unwrap();
930    }
931
932    let self_value_to_value = Signature {
933        parameters: [TypeForParameter {
934            name: "self".to_string(),
935            resolved_type: Type::Never,
936            is_mutable: true,
937            node: None,
938        }]
939        .into(),
940        return_type: Box::new(Type::Never),
941    };
942    let self_value_to_option_functions = [IntrinsicFunction::VecPop];
943    for intrinsic_fn in self_value_to_option_functions {
944        let name = intrinsic_fn.to_string();
945        core_ns
946            .add_intrinsic_function(IntrinsicFunctionDefinition {
947                name,
948                intrinsic: intrinsic_fn,
949                signature: self_value_to_value.clone(),
950            })
951            .unwrap();
952    }
953
954    let self_int_to_value = Signature {
955        parameters: [
956            TypeForParameter {
957                name: "self".to_string(),
958                resolved_type: Type::Never,
959                is_mutable: false,
960                node: None,
961            },
962            TypeForParameter {
963                name: "i".to_string(),
964                resolved_type: Type::Int,
965                is_mutable: false,
966                node: None,
967            },
968        ]
969        .into(),
970        return_type: Box::new(Type::Never),
971    };
972    let self_int_to_value_functions = [IntrinsicFunction::VecSubscript];
973    for intrinsic_fn in self_int_to_value_functions {
974        let name = intrinsic_fn.to_string();
975        core_ns
976            .add_intrinsic_function(IntrinsicFunctionDefinition {
977                name,
978                intrinsic: intrinsic_fn,
979                signature: self_int_to_value.clone(),
980            })
981            .unwrap();
982    }
983
984    let self_range_to_self = Signature {
985        parameters: [
986            TypeForParameter {
987                name: "self".to_string(),
988                resolved_type: Type::Never,
989                is_mutable: false,
990                node: None,
991            },
992            TypeForParameter {
993                name: "range".to_string(),
994                resolved_type: Type::Never,
995                is_mutable: false,
996                node: None,
997            },
998        ]
999        .into(),
1000        return_type: Box::new(Type::Never),
1001    };
1002    let self_range_to_self_functions = [IntrinsicFunction::VecSubscriptRange];
1003    for intrinsic_fn in self_range_to_self_functions {
1004        let name = intrinsic_fn.to_string();
1005        core_ns
1006            .add_intrinsic_function(IntrinsicFunctionDefinition {
1007                name,
1008                intrinsic: intrinsic_fn,
1009                signature: self_range_to_self.clone(),
1010            })
1011            .unwrap();
1012    }
1013
1014    let self_int_value_mut_to_unit = Signature {
1015        parameters: [
1016            TypeForParameter {
1017                name: "self".to_string(),
1018                resolved_type: Type::Never,
1019                is_mutable: true,
1020                node: None,
1021            },
1022            TypeForParameter {
1023                name: "i".to_string(),
1024                resolved_type: Type::Int,
1025                is_mutable: false,
1026                node: None,
1027            },
1028        ]
1029        .into(),
1030        return_type: Box::new(Type::Never),
1031    };
1032
1033    let self_int_value_mut_to_unit_functions = [IntrinsicFunction::VecSubscriptMut];
1034
1035    for intrinsic_fn in self_int_value_mut_to_unit_functions {
1036        let name = intrinsic_fn.to_string();
1037        core_ns
1038            .add_intrinsic_function(IntrinsicFunctionDefinition {
1039                name,
1040                intrinsic: intrinsic_fn,
1041                signature: self_int_value_mut_to_unit.clone(),
1042            })
1043            .unwrap();
1044    }
1045
1046    let self_int_to_unit_value = Signature {
1047        parameters: [
1048            TypeForParameter {
1049                name: "self".to_string(),
1050                resolved_type: Type::Never,
1051                is_mutable: true,
1052                node: None,
1053            },
1054            TypeForParameter {
1055                name: "i".to_string(),
1056                resolved_type: Type::Int,
1057                is_mutable: false,
1058                node: None,
1059            },
1060        ]
1061        .into(),
1062        return_type: Box::new(Type::Unit),
1063    };
1064
1065    let self_int_to_optional_value_functions = [IntrinsicFunction::VecRemoveIndex];
1066
1067    for intrinsic_fn in self_int_to_optional_value_functions {
1068        let name = intrinsic_fn.to_string();
1069        core_ns
1070            .add_intrinsic_function(IntrinsicFunctionDefinition {
1071                name,
1072                intrinsic: intrinsic_fn,
1073                signature: self_int_to_unit_value.clone(),
1074            })
1075            .unwrap();
1076    }
1077
1078    let self_to_unit = Signature {
1079        parameters: [TypeForParameter {
1080            name: "self".to_string(),
1081            resolved_type: Type::Never,
1082            is_mutable: false,
1083            node: None,
1084        }]
1085        .into(),
1086        return_type: Box::new(Type::Unit),
1087    };
1088
1089    let self_to_unit_functions = [IntrinsicFunction::VecClear];
1090
1091    for intrinsic_fn in self_to_unit_functions {
1092        let name = intrinsic_fn.to_string();
1093        core_ns
1094            .add_intrinsic_function(IntrinsicFunctionDefinition {
1095                name,
1096                intrinsic: intrinsic_fn,
1097                signature: self_to_unit.clone(),
1098            })
1099            .unwrap();
1100    }
1101
1102    let self_to_value = Signature {
1103        parameters: [TypeForParameter {
1104            name: "self".to_string(),
1105            resolved_type: Type::Never,
1106            is_mutable: false,
1107            node: None,
1108        }]
1109        .into(),
1110        return_type: Box::new(Type::Never),
1111    };
1112    let self_to_value_functions = [IntrinsicFunction::VecIter, IntrinsicFunction::VecIterMut];
1113    for intrinsic_fn in self_to_value_functions {
1114        let name = intrinsic_fn.to_string();
1115        core_ns
1116            .add_intrinsic_function(IntrinsicFunctionDefinition {
1117                name,
1118                intrinsic: intrinsic_fn,
1119                signature: self_to_value.clone(),
1120            })
1121            .unwrap();
1122    }
1123
1124    let self_block_to_unit = Signature {
1125        parameters: [
1126            TypeForParameter {
1127                name: "self".to_string(),
1128                resolved_type: Type::Never,
1129                is_mutable: false,
1130                node: None,
1131            },
1132            TypeForParameter {
1133                name: "block".to_string(),
1134                resolved_type: Type::Never,
1135                is_mutable: false,
1136                node: None,
1137            },
1138        ]
1139        .into(),
1140        return_type: Box::new(Type::Unit),
1141    };
1142    let self_block_to_unit_functions = [IntrinsicFunction::VecFor, IntrinsicFunction::VecWhile];
1143    for intrinsic_fn in self_block_to_unit_functions {
1144        let name = intrinsic_fn.to_string();
1145        core_ns
1146            .add_intrinsic_function(IntrinsicFunctionDefinition {
1147                name,
1148                intrinsic: intrinsic_fn,
1149                signature: self_block_to_unit.clone(),
1150            })
1151            .unwrap();
1152    }
1153
1154    let self_block_to_generic = Signature {
1155        parameters: [
1156            TypeForParameter {
1157                name: "self".to_string(),
1158                resolved_type: Type::Never,
1159                is_mutable: false,
1160                node: None,
1161            },
1162            TypeForParameter {
1163                name: "block".to_string(),
1164                resolved_type: Type::Never,
1165                is_mutable: false,
1166                node: None,
1167            },
1168        ]
1169        .into(),
1170        return_type: Box::new(Type::Never),
1171    };
1172    let self_block_to_generic_functions = [IntrinsicFunction::VecFind];
1173    for intrinsic_fn in self_block_to_generic_functions {
1174        let name = intrinsic_fn.to_string();
1175        core_ns
1176            .add_intrinsic_function(IntrinsicFunctionDefinition {
1177                name,
1178                intrinsic: intrinsic_fn,
1179                signature: self_block_to_generic.clone(),
1180            })
1181            .unwrap();
1182    }
1183
1184    let slice_to_self = Signature {
1185        parameters: [TypeForParameter {
1186            name: "slice".to_string(),
1187            resolved_type: Type::Never, // TODO: Should have proper slice generic
1188            is_mutable: false,
1189            node: None,
1190        }]
1191        .into(),
1192        return_type: Box::new(Type::Never),
1193    };
1194    let slice_to_self_functions = [IntrinsicFunction::VecFromSlice];
1195    for intrinsic_fn in slice_to_self_functions {
1196        let name = intrinsic_fn.to_string();
1197        core_ns
1198            .add_intrinsic_function(IntrinsicFunctionDefinition {
1199                name,
1200                intrinsic: intrinsic_fn,
1201                signature: slice_to_self.clone(),
1202            })
1203            .unwrap();
1204    }
1205
1206    let self_to_int = Signature {
1207        parameters: [TypeForParameter {
1208            name: "self".to_string(),
1209            resolved_type: Type::Never,
1210            is_mutable: false,
1211            node: None,
1212        }]
1213        .into(),
1214        return_type: Box::new(Type::Int),
1215    };
1216
1217    let self_to_int_functions = [IntrinsicFunction::VecLen];
1218
1219    for intrinsic_fn in self_to_int_functions {
1220        let name = intrinsic_fn.to_string();
1221        core_ns
1222            .add_intrinsic_function(IntrinsicFunctionDefinition {
1223                name,
1224                intrinsic: intrinsic_fn,
1225                signature: self_to_int.clone(),
1226            })
1227            .unwrap();
1228    }
1229
1230    let self_to_bool = Signature {
1231        parameters: [TypeForParameter {
1232            name: "self".to_string(),
1233            resolved_type: Type::Never,
1234            is_mutable: false,
1235            node: None,
1236        }]
1237        .into(),
1238        return_type: Box::new(Type::Bool),
1239    };
1240
1241    let self_to_bool_functions = [IntrinsicFunction::VecIsEmpty];
1242
1243    for intrinsic_fn in self_to_bool_functions {
1244        let name = intrinsic_fn.to_string();
1245        core_ns
1246            .add_intrinsic_function(IntrinsicFunctionDefinition {
1247                name,
1248                intrinsic: intrinsic_fn,
1249                signature: self_to_bool.clone(),
1250            })
1251            .unwrap();
1252    }
1253}
1254
1255#[allow(clippy::too_many_lines)]
1256fn add_intrinsic_string_functions(core_ns: &mut SymbolTable) {
1257    let string_to_int = Signature {
1258        parameters: [TypeForParameter {
1259            name: "self".into(),
1260            resolved_type: Type::String,
1261            is_mutable: false,
1262            node: None,
1263        }]
1264        .into(),
1265        return_type: Box::new(Type::Int),
1266    };
1267
1268    let string_to_int_functions = [IntrinsicFunction::StringLen];
1269
1270    for intrinsic_fn in string_to_int_functions {
1271        let name = intrinsic_fn.to_string();
1272        core_ns
1273            .add_intrinsic_function(IntrinsicFunctionDefinition {
1274                name,
1275                intrinsic: intrinsic_fn,
1276                signature: string_to_int.clone(),
1277            })
1278            .unwrap();
1279    }
1280}
1281
1282#[allow(clippy::too_many_lines)]
1283fn add_intrinsic_int_functions(core_ns: &mut SymbolTable) {
1284    let int_to_int = Signature {
1285        parameters: [TypeForParameter {
1286            name: "self".into(),
1287            resolved_type: Type::Int,
1288            is_mutable: false,
1289            node: None,
1290        }]
1291        .into(),
1292        return_type: Box::new(Type::Int),
1293    };
1294
1295    let int_to_int_functions = [IntrinsicFunction::IntAbs, IntrinsicFunction::IntRnd];
1296
1297    for intrinsic_fn in int_to_int_functions {
1298        let name = intrinsic_fn.to_string();
1299        core_ns
1300            .add_intrinsic_function(IntrinsicFunctionDefinition {
1301                name,
1302                intrinsic: intrinsic_fn,
1303                signature: int_to_int.clone(),
1304            })
1305            .unwrap();
1306    }
1307
1308    let int_int_to_int = Signature {
1309        parameters: [
1310            TypeForParameter {
1311                name: "self".into(),
1312                resolved_type: Type::Int,
1313                is_mutable: false,
1314                node: None,
1315            },
1316            TypeForParameter {
1317                name: "b".into(),
1318                resolved_type: Type::Int,
1319                is_mutable: false,
1320                node: None,
1321            },
1322        ]
1323        .into(),
1324        return_type: Box::new(Type::Int),
1325    };
1326    let int_int_to_int_functions = [IntrinsicFunction::IntMax, IntrinsicFunction::IntMin];
1327
1328    for intrinsic_fn in int_int_to_int_functions {
1329        let name = intrinsic_fn.to_string();
1330        core_ns
1331            .add_intrinsic_function(IntrinsicFunctionDefinition {
1332                name,
1333                intrinsic: intrinsic_fn,
1334                signature: int_int_to_int.clone(),
1335            })
1336            .unwrap();
1337    }
1338
1339    let int_int_int_to_int = Signature {
1340        parameters: [
1341            TypeForParameter {
1342                name: "self".into(),
1343                resolved_type: Type::Int,
1344                is_mutable: false,
1345                node: None,
1346            },
1347            TypeForParameter {
1348                name: "a".into(),
1349                resolved_type: Type::Int,
1350                is_mutable: false,
1351                node: None,
1352            },
1353            TypeForParameter {
1354                name: "b".into(),
1355                resolved_type: Type::Int,
1356                is_mutable: false,
1357                node: None,
1358            },
1359        ]
1360        .into(),
1361        return_type: Box::new(Type::Int),
1362    };
1363    let int_int_int_to_int_functions = [IntrinsicFunction::IntClamp];
1364    for intrinsic_fn in int_int_int_to_int_functions {
1365        let name = intrinsic_fn.to_string();
1366        core_ns
1367            .add_intrinsic_function(IntrinsicFunctionDefinition {
1368                name,
1369                intrinsic: intrinsic_fn,
1370                signature: int_int_int_to_int.clone(),
1371            })
1372            .unwrap();
1373    }
1374
1375    let int_to_float = Signature {
1376        parameters: [TypeForParameter {
1377            name: "self".into(),
1378            resolved_type: Type::Int,
1379            is_mutable: false,
1380            node: None,
1381        }]
1382        .into(),
1383        return_type: Box::new(Type::Float),
1384    };
1385
1386    core_ns
1387        .add_intrinsic_function(IntrinsicFunctionDefinition {
1388            name: IntrinsicFunction::IntToFloat.to_string(),
1389            intrinsic: IntrinsicFunction::IntToFloat,
1390            signature: int_to_float.clone(),
1391        })
1392        .unwrap();
1393}
1394
1395#[allow(clippy::too_many_lines)]
1396fn add_intrinsic_float_functions(core_ns: &mut SymbolTable) {
1397    let float_to_float = Signature {
1398        parameters: [TypeForParameter {
1399            name: "self".into(),
1400            resolved_type: Type::Float,
1401            is_mutable: false,
1402            node: None,
1403        }]
1404        .into(),
1405        return_type: Box::new(Type::Float),
1406    };
1407
1408    let float_to_float_functions = [
1409        IntrinsicFunction::FloatSqrt,
1410        IntrinsicFunction::FloatSign,
1411        IntrinsicFunction::FloatAbs,
1412        IntrinsicFunction::FloatRnd,
1413        IntrinsicFunction::FloatCos,
1414        IntrinsicFunction::FloatSin,
1415        IntrinsicFunction::FloatAcos,
1416        IntrinsicFunction::FloatAsin,
1417    ];
1418    for intrinsic_fn in float_to_float_functions {
1419        let name = intrinsic_fn.to_string();
1420        core_ns
1421            .add_intrinsic_function(IntrinsicFunctionDefinition {
1422                name,
1423                intrinsic: intrinsic_fn,
1424                signature: float_to_float.clone(),
1425            })
1426            .unwrap();
1427    }
1428
1429    let float_to_int = Signature {
1430        parameters: [TypeForParameter {
1431            name: "self".into(),
1432            resolved_type: Type::Float,
1433            is_mutable: false,
1434            node: None,
1435        }]
1436        .into(),
1437        return_type: Box::new(Type::Int),
1438    };
1439
1440    let float_to_int_functions = [IntrinsicFunction::FloatRound, IntrinsicFunction::FloatFloor];
1441    for intrinsic_fn in float_to_int_functions {
1442        let name = intrinsic_fn.to_string();
1443        core_ns
1444            .add_intrinsic_function(IntrinsicFunctionDefinition {
1445                name,
1446                intrinsic: intrinsic_fn,
1447                signature: float_to_int.clone(),
1448            })
1449            .unwrap();
1450    }
1451
1452    let float_float_to_float = Signature {
1453        parameters: [
1454            TypeForParameter {
1455                name: "self".into(),
1456                resolved_type: Type::Float,
1457                is_mutable: false,
1458                node: None,
1459            },
1460            TypeForParameter {
1461                name: "other".into(),
1462                resolved_type: Type::Float,
1463                is_mutable: false,
1464                node: None,
1465            },
1466        ]
1467        .into(),
1468        return_type: Box::new(Type::Float),
1469    };
1470
1471    let float_float_to_float_functions = [
1472        IntrinsicFunction::FloatAtan2,
1473        IntrinsicFunction::FloatMin,
1474        IntrinsicFunction::FloatMax,
1475        IntrinsicFunction::Float2Magnitude,
1476    ];
1477    for intrinsic_fn in float_float_to_float_functions {
1478        let name = intrinsic_fn.to_string();
1479        core_ns
1480            .add_intrinsic_function(IntrinsicFunctionDefinition {
1481                name,
1482                intrinsic: intrinsic_fn,
1483                signature: float_float_to_float.clone(),
1484            })
1485            .unwrap();
1486    }
1487
1488    let float_float_float_to_float = Signature {
1489        parameters: [
1490            TypeForParameter {
1491                name: "self".into(),
1492                resolved_type: Type::Float,
1493                is_mutable: false,
1494                node: None,
1495            },
1496            TypeForParameter {
1497                name: "a".into(),
1498                resolved_type: Type::Float,
1499                is_mutable: false,
1500                node: None,
1501            },
1502            TypeForParameter {
1503                name: "b".into(),
1504                resolved_type: Type::Float,
1505                is_mutable: false,
1506                node: None,
1507            },
1508        ]
1509        .into(),
1510        return_type: Box::new(Type::Float),
1511    };
1512
1513    core_ns
1514        .add_intrinsic_function(IntrinsicFunctionDefinition {
1515            name: IntrinsicFunction::FloatClamp.to_string(),
1516            intrinsic: IntrinsicFunction::FloatClamp,
1517            signature: float_float_float_to_float.clone(),
1518        })
1519        .unwrap();
1520}
1521
1522/// # Panics
1523/// if `versioned_name` is wrong
1524#[must_use]
1525pub fn create_module(tiny_version: &TinyVersion) -> Module {
1526    let canonical_core_path = [tiny_version.versioned_name(PACKAGE_NAME).unwrap()];
1527    let mut intrinsic_types_symbol_table = SymbolTable::new(&canonical_core_path);
1528    add_intrinsic_types(&mut intrinsic_types_symbol_table);
1529    add_intrinsic_type_generator(&mut intrinsic_types_symbol_table);
1530    add_intrinsic_functions(&mut intrinsic_types_symbol_table);
1531
1532    Module::new(intrinsic_types_symbol_table, None)
1533}
1534
1535fn add_intrinsic_type_generator(core_ns: &mut SymbolTable) {
1536    core_ns
1537        .add_type_generator(
1538            "Slice",
1539            TypeGenerator {
1540                arity: 1,
1541                kind: TypeGeneratorKind::Slice,
1542            },
1543        )
1544        .unwrap();
1545
1546    core_ns
1547        .add_type_generator(
1548            "SlicePair",
1549            TypeGenerator {
1550                arity: 2,
1551                kind: TypeGeneratorKind::SlicePair,
1552            },
1553        )
1554        .unwrap();
1555}