swamp_semantic/
intr.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/semantic
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    CodepointToByte,
22
23    // Bool
24    BoolToString,
25
26    // Int
27    IntAbs,
28    IntRnd,
29    IntMax,
30    IntMin,
31    IntClamp,
32    IntToFloat,
33    IntToString,
34
35    // Float
36    FloatRound,
37    FloatFloor,
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    TransformerFilterInPlace,
89    TransformerFilterMap,
90    TransformerFind,
91    TransformerFold,
92
93    // Map
94    MapIsEmpty,
95    MapHas,
96    MapRemove,
97    MapLen,
98    MapCapacity,
99
100    // Grid
101    GridSet,
102    GridGet,
103    GridWidth,
104    GridHeight,
105
106    // Sparse
107    SparseAdd,
108    SparseRemove,
109    SparseIsAlive,
110
111    Float2Magnitude,
112
113    RuntimePanic,
114    RuntimeHalt,
115    RuntimeStep,
116    EnumDiscriminant,
117    EnumFromDiscriminant,
118    PtrFromU32,
119    StringDuplicate,
120    ByteVectorToString,
121    ByteVectorToStringStorage,
122    VecCopy,
123    PtrWriteU16,
124    PtrReadU16,
125    PtrWriteU32,
126    PtrWriteU8,
127}
128
129pub type IntrinsicFunctionDefinitionRef = Rc<IntrinsicFunctionDefinition>;
130
131#[derive(Clone, PartialEq, Eq, Debug)]
132pub struct IntrinsicFunctionDefinition {
133    pub name: String,
134    pub signature: Signature,
135    pub intrinsic: IntrinsicFunction,
136}
137
138impl fmt::Display for IntrinsicFunction {
139    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140        let name = match self {
141            //  Codepoint
142            Self::CodepointToString => "codepoint_to_string",
143            Self::CodepointToInt => "codepoint_to_int",
144            Self::CodepointToByte => "codepoint_to_byte",
145
146            // Byte
147            Self::ByteToString => "byte_to_string",
148            Self::ByteToInt => "byte_to_int",
149            Self::ByteToFloat => "byte_to_float",
150            Self::ByteToCodepoint => "byte_to_char",
151
152            // Bool
153            Self::BoolToString => "bool_to_string",
154
155            // Float
156            Self::FloatRound => "float_round",
157            Self::FloatFloor => "float_floor",
158            Self::FloatSign => "float_sign",
159            Self::FloatAbs => "float_abs",
160            Self::FloatRnd => "float_rnd",
161            Self::FloatCos => "float_cos",
162            Self::FloatSin => "float_sin",
163            Self::FloatAcos => "float_acos",
164            Self::FloatAsin => "float_asin",
165            Self::FloatAtan2 => "float_atan2",
166            Self::FloatMin => "float_min",
167            Self::FloatMax => "float_max",
168            Self::FloatClamp => "float_clamp",
169            Self::FloatToString => "float_to_string",
170
171            // Int
172            Self::IntAbs => "int_abs",
173            Self::IntRnd => "int_rnd",
174            Self::IntMax => "int_max",
175            Self::IntMin => "int_min",
176            Self::IntToFloat => "int_to_float",
177            Self::IntClamp => "int_clamp",
178            Self::IntToString => "int_to_string",
179
180            // String
181            Self::StringLen => "string_len",
182            Self::StringToString => "string_to_string",
183            Self::StringStartsWith => "string_starts_with",
184            Self::StringToInt => "string_to_int",
185            Self::StringToFloat => "string_to_float",
186            Self::StringDuplicate => "string_dup",
187            Self::ByteVectorToString => "bytes_to_string",
188            Self::ByteVectorToStringStorage => "bytes_to_string_storage",
189
190            // Enum
191            Self::EnumDiscriminant => "enum_discriminant",
192            Self::EnumFromDiscriminant => "enum_from_discriminant",
193
194            // Vec
195            Self::VecPush => "vec_push",
196            Self::VecExtend => "vec_extend",
197            Self::VecPop => "vec_pop",
198            Self::VecSlice => "vec_slice",
199            Self::VecRemoveIndex => "vec_remove_index",
200            Self::VecRemoveIndexGetValue => "vec_remove_index_get_value",
201            Self::VecRemoveFirstIndexGetValue => "vec_remove_first_get_value",
202            Self::VecClear => "vec_clear",
203            Self::VecGet => "vec_get",
204            Self::TransformerFor => "vec_for",
205            Self::VecIsEmpty => "vec_is_empty",
206            Self::TransformerWhile => "transformer_while",
207            Self::TransformerFindMap => "transformer_find_map",
208            Self::VecLen => "vec_len",
209            Self::VecCapacity => "vec_capacity",
210            Self::TransformerAny => "vec_any",
211            Self::TransformerAll => "vec_all",
212            Self::TransformerMap => "vec_map",
213            Self::TransformerFilter => "vec_filter",
214            Self::TransformerFilterInPlace => "vec_filter_mut",
215            Self::TransformerFilterMap => "vec_filter_map",
216            Self::TransformerFind => "vec_find",
217            Self::TransformerFold => "vec_fold",
218            Self::VecSwap => "vec_swap",
219            Self::VecInsert => "vec_insert",
220            Self::VecFirst => "vec_first",
221            Self::VecLast => "vec_last",
222            Self::VecCopy => "vec_copy",
223
224            // Map
225            Self::MapHas => "map_has",
226            Self::MapRemove => "map_remove",
227            Self::MapLen => "map_len",
228            Self::MapCapacity => "map_len",
229            Self::MapIsEmpty => "map_is_empty",
230
231            // Grid
232            Self::GridSet => "grid_set",
233            Self::GridGet => "grid_get",
234            Self::GridWidth => "grid_width",
235            Self::GridHeight => "grid_height",
236
237            // Sparse
238            Self::SparseAdd => "sparse_add",
239            Self::SparseRemove => "sparse_remove",
240            Self::SparseIsAlive => "sparse_is_alive",
241
242            // Other
243            Self::Float2Magnitude => "float2_magnitude",
244            Self::RuntimePanic => "runtime_panic",
245            Self::RuntimeHalt => "runtime_halt",
246            Self::RuntimeStep => "runtime_step",
247            Self::RangeInit => "rinit",
248
249            // Pointer
250            Self::PtrFromU32 => "ptr_from_u32",
251            Self::PtrWriteU32 => "ptr_write_u32",
252            Self::PtrWriteU16 => "ptr_write_u16",
253            Self::PtrWriteU8 => "ptr_write_u8",
254            Self::PtrReadU16 => "ptr_read_u16",
255        };
256
257        write!(f, "{name}")
258    }
259}