swamp_script_semantic/
intr.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 */
5
6use std::fmt;
7use std::fmt::Debug;
8use std::rc::Rc;
9use swamp_script_types::prelude::*;
10
11#[derive(Debug, Hash, Clone)]
12pub enum IntrinsicFunction {
13    FloatRound,
14    FloatFloor,
15    FloatSqrt,
16    FloatSign,
17    FloatAbs,
18    FloatRnd,
19    FloatCos,
20    FloatSin,
21    FloatAcos,
22    FloatAsin,
23    FloatAtan2,
24    FloatMin,
25    FloatMax,
26    FloatClamp,
27
28    // Int
29    IntAbs,
30    IntRnd,
31    IntMax,
32    IntMin,
33    IntClamp,
34    IntToFloat,
35
36    // String
37    StringLen,
38
39    // Vec
40    VecFromSlice,
41    VecPush,
42    VecPop,
43    VecRemoveIndex,
44    VecClear,
45    VecCreate,
46    VecSubscript,
47    VecSubscriptMut,
48    VecSubscriptRange,
49    VecIter,
50    VecIterMut,
51    VecFor,
52    VecWhile,
53    VecFind,
54
55    VecSelfPush,
56    VecSelfExtend,
57
58    // Map
59    MapCreate,
60    MapFromSlicePair,
61    MapHas,
62    MapRemove,
63    MapIter,
64    MapIterMut,
65    MapLen,
66    MapIsEmpty,
67    MapSubscript,
68    MapSubscriptSet,
69    MapSubscriptMut,
70    MapSubscriptMutCreateIfNeeded,
71
72    // Map2
73    Map2Create,
74    Map2Insert,
75    Map2Remove,
76    Map2Get,
77    Map2GetColumn,
78    Map2GetRow,
79    Map2Has,
80
81    // Sparse
82    SparseCreate,
83    SparseFromSlice,
84    SparseIter,
85    SparseIterMut,
86    SparseSubscript,
87    SparseSubscriptMut,
88    SparseHas,
89    SparseRemove,
90
91    // Grid
92    GridCreate,
93    GridFromSlice,
94    GridGetColumn,
95    //GridIter,
96    //GridIterMut,
97    GridSet,
98    GridGet,
99    //GridSubscriptMut,
100    Float2Magnitude,
101    SparseAdd,
102    VecLen,
103    VecIsEmpty,
104    SparseNew,
105}
106
107pub type IntrinsicFunctionDefinitionRef = Rc<IntrinsicFunctionDefinition>;
108#[derive(Clone, Debug)]
109pub struct IntrinsicFunctionDefinition {
110    pub name: String,
111    pub signature: Signature,
112    pub intrinsic: IntrinsicFunction,
113}
114
115impl fmt::Display for IntrinsicFunction {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        let name = match self {
118            // Float
119            Self::FloatRound => "float_round",
120            Self::FloatFloor => "float_floor",
121            Self::FloatSqrt => "float_sqrt",
122            Self::FloatSign => "float_sign",
123            Self::FloatAbs => "float_abs",
124            Self::FloatRnd => "float_rnd",
125            Self::FloatCos => "float_cos",
126            Self::FloatSin => "float_sin",
127            Self::FloatAcos => "float_acos",
128            Self::FloatAsin => "float_asin",
129            Self::FloatAtan2 => "float_atan2",
130            Self::FloatMin => "float_min",
131            Self::FloatMax => "float_max",
132            Self::FloatClamp => "float_clamp",
133
134            // Int
135            Self::IntAbs => "int_abs",
136            Self::IntRnd => "int_rnd",
137            Self::IntMax => "int_max",
138            Self::IntMin => "int_min",
139            Self::IntToFloat => "int_to_float",
140            Self::IntClamp => "int_clamp",
141
142            // String
143            Self::StringLen => "string_len",
144
145            // Vec
146            Self::VecFromSlice => "vec_from_slice",
147            Self::VecPush => "vec_push",
148            Self::VecPop => "vec_pop",
149            Self::VecRemoveIndex => "vec_remove",
150            Self::VecClear => "vec_clear",
151            Self::VecCreate => "vec_create",
152            Self::VecSubscriptMut => "vec_subscript_mut",
153            Self::VecSubscript => "vec_subscript",
154            Self::VecSubscriptRange => "vec_subscript_range",
155            Self::VecIter => "vec_iter",
156            Self::VecIterMut => "vec_iter_mut",
157            Self::VecFor => "vec_for",
158            Self::VecWhile => "vec_while",
159            Self::VecFind => "vec_find",
160            Self::VecLen => "vec_len",
161            Self::VecIsEmpty => "vec_is_empty",
162
163            Self::VecSelfPush => "vec_self_push",
164            Self::VecSelfExtend => "vec_self_extend",
165
166            // Map
167            Self::MapCreate => "map_create",
168            Self::MapFromSlicePair => "map_from_slice_pair",
169            Self::MapHas => "map_has",
170            Self::MapRemove => "map_remove",
171            Self::MapSubscriptMut => "map_subscript_mut",
172            Self::MapSubscriptMutCreateIfNeeded => "map_subscript_mut_create_if_needed",
173            Self::MapSubscript => "map_subscript",
174            Self::MapSubscriptSet => "map_subscript_set",
175            Self::MapIter => "map_iter",
176            Self::MapIterMut => "map_iter_mut",
177            Self::MapLen => "map_len",
178            Self::MapIsEmpty => "map_is_empty",
179
180            // Map2
181            Self::Map2Create => "map2_create",
182            Self::Map2Insert => "map2_insert",
183            Self::Map2Remove => "map2_remove",
184            Self::Map2Has => "map2_has",
185            Self::Map2GetColumn => "map2_get_column",
186            Self::Map2GetRow => "map2_get_row",
187            Self::Map2Get => "map2_get",
188
189            // Sparse
190            Self::SparseNew => "sparse_new",
191            Self::SparseCreate => "sparse_create",
192            Self::SparseFromSlice => "sparse_from_slice",
193            Self::SparseAdd => "sparse_add",
194            Self::SparseHas => "sparse_has",
195            Self::SparseRemove => "sparse_remove",
196            Self::SparseSubscriptMut => "sparse_subscript_mut",
197            Self::SparseSubscript => "sparse_subscript",
198            Self::SparseIter => "sparse_iter",
199            Self::SparseIterMut => "sparse_iter_mut",
200
201            // Grid
202            Self::GridCreate => "grid_new",
203            Self::GridSet => "grid_set",
204            Self::GridGet => "grid_get",
205            Self::GridFromSlice => "grid_from_slice",
206            Self::GridGetColumn => "grid_get_column",
207
208            // Other
209            Self::Float2Magnitude => "float2_magnitude",
210        };
211
212        write!(f, "{name}")
213    }
214}