swamp_script_semantic/
intr.rs

1use crate::Signature;
2use std::fmt;
3use std::rc::Rc;
4
5#[derive(Debug, Hash, Clone)]
6pub enum IntrinsicFunction {
7    FloatRound,
8    FloatFloor,
9    FloatSqrt,
10    FloatSign,
11    FloatAbs,
12    FloatRnd,
13    FloatCos,
14    FloatSin,
15    FloatAcos,
16    FloatAsin,
17    FloatAtan2,
18    FloatMin,
19    FloatMax,
20    FloatClamp,
21
22    // Int
23    IntAbs,
24    IntRnd,
25    IntMax,
26    IntMin,
27    IntClamp,
28    IntToFloat,
29
30    // String
31    StringLen,
32
33    // Vec
34    VecFromSlice,
35    VecPush,
36    VecPop,
37    VecRemoveIndex,
38    VecClear,
39    VecCreate,
40    VecSubscript,
41    VecSubscriptMut,
42    VecIter,
43    VecIterMut,
44
45    VecSelfPush,
46    VecSelfExtend,
47
48    // Map
49    MapCreate,
50    MapFromSlicePair,
51    MapHas,
52    MapRemove,
53    MapIter,
54    MapIterMut,
55    MapSubscript,
56    MapSubscriptMut,
57
58    // Sparse
59    SparseCreate,
60    SparseFromSlice,
61    SparseIter,
62    SparseIterMut,
63    SparseSubscript,
64    SparseSubscriptMut,
65    SparseHas,
66    SparseRemove,
67
68    Float2Magnitude,
69    SparseAdd,
70    VecLen,
71    VecIsEmpty,
72}
73
74pub type IntrinsicFunctionDefinitionRef = Rc<IntrinsicFunctionDefinition>;
75#[derive(Clone, Debug)]
76pub struct IntrinsicFunctionDefinition {
77    pub name: String,
78    pub signature: Signature,
79    pub intrinsic: IntrinsicFunction,
80}
81
82impl fmt::Display for IntrinsicFunction {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        let name = match self {
85            // Float
86            Self::FloatRound => "float_round",
87            Self::FloatFloor => "float_floor",
88            Self::FloatSqrt => "float_sqrt",
89            Self::FloatSign => "float_sign",
90            Self::FloatAbs => "float_abs",
91            Self::FloatRnd => "float_rnd",
92            Self::FloatCos => "float_cos",
93            Self::FloatSin => "float_sin",
94            Self::FloatAcos => "float_acos",
95            Self::FloatAsin => "float_asin",
96            Self::FloatAtan2 => "float_atan2",
97            Self::FloatMin => "float_min",
98            Self::FloatMax => "float_max",
99            Self::FloatClamp => "float_clamp",
100
101            // Int
102            Self::IntAbs => "int_abs",
103            Self::IntRnd => "int_rnd",
104            Self::IntMax => "int_max",
105            Self::IntMin => "int_min",
106            Self::IntToFloat => "int_to_float",
107            Self::IntClamp => "int_clamp",
108
109            // String
110            Self::StringLen => "string_len",
111
112            // Vec
113            Self::VecFromSlice => "vec_from_slice",
114            Self::VecPush => "vec_push",
115            Self::VecPop => "vec_pop",
116            Self::VecRemoveIndex => "vec_remove",
117            Self::VecClear => "vec_clear",
118            Self::VecCreate => "vec_create",
119            Self::VecSubscriptMut => "vec_subscript_mut",
120            Self::VecSubscript => "vec_subscript",
121            Self::VecIter => "vec_iter",
122            Self::VecIterMut => "vec_iter_mut",
123            Self::VecLen => "vec_len",
124            Self::VecIsEmpty => "vec_is_empty",
125
126            Self::VecSelfPush => "vec_self_push",
127            Self::VecSelfExtend => "vec_self_extend",
128
129            // Map
130            Self::MapCreate => "map_create",
131            Self::MapFromSlicePair => "map_from_slice_pair",
132            Self::MapHas => "map_has",
133            Self::MapRemove => "map_remove",
134            Self::MapSubscriptMut => "map_subscript_mut",
135            Self::MapSubscript => "map_subscript",
136            Self::MapIter => "map_iter",
137            Self::MapIterMut => "map_iter_mut",
138
139            // Sparse
140            Self::SparseCreate => "sparse_create",
141            Self::SparseFromSlice => "sparse_from_slice",
142            Self::SparseAdd => "sparse_add",
143            Self::SparseHas => "sparse_has",
144            Self::SparseRemove => "sparse_remove",
145            Self::SparseSubscriptMut => "sparse_subscript_mut",
146            Self::SparseSubscript => "sparse_subscript",
147            Self::SparseIter => "sparse_iter",
148            Self::SparseIterMut => "sparse_iter_mut",
149
150            // Other
151            Self::Float2Magnitude => "float2_magnitude",
152        };
153
154        write!(f, "{name}")
155    }
156}