swamp_semantic/
intr.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use std::fmt;
6use std::fmt::Debug;
7use std::rc::Rc;
8use swamp_types::prelude::*;
9
10#[derive(Debug, Hash, Eq, PartialEq, Clone)]
11pub enum IntrinsicFunction {
12    // Byte
13    ByteToString,
14    ByteToInt,
15    ByteToFloat,
16    ByteToCodepoint,
17
18    // Char
19    CodepointToString,
20    CodepointToInt,
21
22    // Bool
23    BoolToString,
24
25    // Int
26    IntAbs,
27    IntRnd,
28    IntMax,
29    IntMin,
30    IntClamp,
31    IntToFloat,
32    IntToString,
33
34    // Float
35    FloatRound,
36    FloatFloor,
37    FloatSqrt,
38    FloatSign,
39    FloatAbs,
40    FloatRnd,
41    FloatCos,
42    FloatSin,
43    FloatAcos,
44    FloatAsin,
45    FloatAtan2,
46    FloatMin,
47    FloatMax,
48    FloatClamp,
49    FloatToString,
50
51    // String
52    StringLen,
53    StringToString,
54    // TODO: StringSubscript, StringConcat
55    StringStartsWith,
56    StringToInt,
57    StringToFloat,
58
59    // Range
60    RangeInit,
61
62    // Vec
63    VecPush,
64    VecPop,
65    VecRemoveIndex,
66    VecRemoveIndexGetValue,
67    VecRemoveFirstIndexGetValue,
68    VecClear,
69    VecSwap,
70    VecInsert,
71    VecFirst,
72    VecGet,
73    VecSlice,
74    VecLast,
75    VecLen,
76    VecCapacity,
77    VecIsEmpty,
78    VecExtend,
79
80    // Transformer
81    TransformerFor,
82    TransformerWhile,
83    TransformerFindMap,
84    TransformerAny,
85    TransformerAll,
86    TransformerMap,
87    TransformerFilter,
88    TransformerFilterMap,
89    TransformerFind,
90    TransformerFold,
91
92    // Map
93    MapIsEmpty,
94    MapHas,
95    MapRemove,
96    MapLen,
97    MapCapacity,
98
99    // Grid
100    GridSet,
101    GridGet,
102    GridWidth,
103    GridHeight,
104
105    // Sparse
106    SparseAdd,
107    SparseRemove,
108    SparseIsAlive,
109
110    Float2Magnitude,
111
112    RuntimePanic,
113    RuntimeHalt,
114    RuntimeStep,
115    EnumDiscriminant,
116    EnumFromDiscriminant,
117    PtrFromU32,
118    StringDuplicate,
119    ByteVectorToString,
120    ByteVectorToStringStorage,
121    VecCopy,
122}
123
124pub type IntrinsicFunctionDefinitionRef = Rc<IntrinsicFunctionDefinition>;
125
126#[derive(Clone, PartialEq, Eq, Debug)]
127pub struct IntrinsicFunctionDefinition {
128    pub name: String,
129    pub signature: Signature,
130    pub intrinsic: IntrinsicFunction,
131}
132
133impl fmt::Display for IntrinsicFunction {
134    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135        let name = match self {
136            //  Codepoint
137            Self::CodepointToString => "codepoint_to_string",
138            Self::CodepointToInt => "codepoint_to_int",
139
140            // Byte
141            Self::ByteToString => "byte_to_string",
142            Self::ByteToInt => "byte_to_int",
143            Self::ByteToFloat => "byte_to_float",
144            Self::ByteToCodepoint => "byte_to_char",
145
146            // Bool
147            Self::BoolToString => "bool_to_string",
148
149            // Float
150            Self::FloatRound => "float_round",
151            Self::FloatFloor => "float_floor",
152            Self::FloatSqrt => "float_sqrt",
153            Self::FloatSign => "float_sign",
154            Self::FloatAbs => "float_abs",
155            Self::FloatRnd => "float_rnd",
156            Self::FloatCos => "float_cos",
157            Self::FloatSin => "float_sin",
158            Self::FloatAcos => "float_acos",
159            Self::FloatAsin => "float_asin",
160            Self::FloatAtan2 => "float_atan2",
161            Self::FloatMin => "float_min",
162            Self::FloatMax => "float_max",
163            Self::FloatClamp => "float_clamp",
164            Self::FloatToString => "float_to_string",
165
166            // Int
167            Self::IntAbs => "int_abs",
168            Self::IntRnd => "int_rnd",
169            Self::IntMax => "int_max",
170            Self::IntMin => "int_min",
171            Self::IntToFloat => "int_to_float",
172            Self::IntClamp => "int_clamp",
173            Self::IntToString => "int_to_string",
174
175            // String
176            Self::StringLen => "string_len",
177            Self::StringToString => "string_to_string",
178            Self::StringStartsWith => "string_starts_with",
179            Self::StringToInt => "string_to_int",
180            Self::StringToFloat => "string_to_float",
181            Self::StringDuplicate => "string_dup",
182            Self::ByteVectorToString => "bytes_to_string",
183            Self::ByteVectorToStringStorage => "bytes_to_string_storage",
184
185            // Enum
186            Self::EnumDiscriminant => "enum_discriminant",
187            Self::EnumFromDiscriminant => "enum_from_discriminant",
188
189            // Vec
190            Self::VecPush => "vec_push",
191            Self::VecExtend => "vec_extend",
192            Self::VecPop => "vec_pop",
193            Self::VecSlice => "vec_slice",
194            Self::VecRemoveIndex => "vec_remove_index",
195            Self::VecRemoveIndexGetValue => "vec_remove_index_get_value",
196            Self::VecRemoveFirstIndexGetValue => "vec_remove_first_get_value",
197            Self::VecClear => "vec_clear",
198            Self::VecGet => "vec_get",
199            Self::TransformerFor => "vec_for",
200            Self::VecIsEmpty => "vec_is_empty",
201            Self::TransformerWhile => "transformer_while",
202            Self::TransformerFindMap => "transformer_find_map",
203            Self::VecLen => "vec_len",
204            Self::VecCapacity => "vec_capacity",
205            Self::TransformerAny => "vec_any",
206            Self::TransformerAll => "vec_all",
207            Self::TransformerMap => "vec_map",
208            Self::TransformerFilter => "vec_filter",
209            Self::TransformerFilterMap => "vec_filter_map",
210            Self::TransformerFind => "vec_find",
211            Self::TransformerFold => "vec_fold",
212            Self::VecSwap => "vec_swap",
213            Self::VecInsert => "vec_insert",
214            Self::VecFirst => "vec_first",
215            Self::VecLast => "vec_last",
216            Self::VecCopy => "vec_copy",
217
218            // Map
219            Self::MapHas => "map_has",
220            Self::MapRemove => "map_remove",
221            Self::MapLen => "map_len",
222            Self::MapCapacity => "map_len",
223            Self::MapIsEmpty => "map_is_empty",
224
225            // Grid
226            Self::GridSet => "grid_set",
227            Self::GridGet => "grid_get",
228            Self::GridWidth => "grid_width",
229            Self::GridHeight => "grid_height",
230
231            // Sparse
232            Self::SparseAdd => "sparse_add",
233            Self::SparseRemove => "sparse_remove",
234            Self::SparseIsAlive => "sparse_is_alive",
235
236            // Other
237            Self::Float2Magnitude => "float2_magnitude",
238            Self::RuntimePanic => "runtime_panic",
239            Self::RuntimeHalt => "runtime_halt",
240            Self::RuntimeStep => "runtime_step",
241            Self::RangeInit => "rinit",
242            Self::PtrFromU32 => "ptr_from_u32",
243        };
244
245        write!(f, "{name}")
246    }
247}