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}
118
119pub type IntrinsicFunctionDefinitionRef = Rc<IntrinsicFunctionDefinition>;
120
121#[derive(Clone, PartialEq, Eq, Debug)]
122pub struct IntrinsicFunctionDefinition {
123    pub name: String,
124    pub signature: Signature,
125    pub intrinsic: IntrinsicFunction,
126}
127
128impl fmt::Display for IntrinsicFunction {
129    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130        let name = match self {
131            //  Codepoint
132            Self::CodepointToString => "codepoint_to_string",
133            Self::CodepointToInt => "codepoint_to_int",
134
135            // Byte
136            Self::ByteToString => "byte_to_string",
137            Self::ByteToInt => "byte_to_int",
138            Self::ByteToFloat => "byte_to_float",
139            Self::ByteToCodepoint => "byte_to_char",
140
141            // Bool
142            Self::BoolToString => "bool_to_string",
143
144            // Float
145            Self::FloatRound => "float_round",
146            Self::FloatFloor => "float_floor",
147            Self::FloatSqrt => "float_sqrt",
148            Self::FloatSign => "float_sign",
149            Self::FloatAbs => "float_abs",
150            Self::FloatRnd => "float_rnd",
151            Self::FloatCos => "float_cos",
152            Self::FloatSin => "float_sin",
153            Self::FloatAcos => "float_acos",
154            Self::FloatAsin => "float_asin",
155            Self::FloatAtan2 => "float_atan2",
156            Self::FloatMin => "float_min",
157            Self::FloatMax => "float_max",
158            Self::FloatClamp => "float_clamp",
159            Self::FloatToString => "float_to_string",
160
161            // Int
162            Self::IntAbs => "int_abs",
163            Self::IntRnd => "int_rnd",
164            Self::IntMax => "int_max",
165            Self::IntMin => "int_min",
166            Self::IntToFloat => "int_to_float",
167            Self::IntClamp => "int_clamp",
168            Self::IntToString => "int_to_string",
169
170            // String
171            Self::StringLen => "string_len",
172            Self::StringToString => "string_to_string",
173            Self::StringStartsWith => "string_starts_with",
174            Self::StringToInt => "string_to_int",
175            Self::StringToFloat => "string_to_float",
176
177            // Enum
178            Self::EnumDiscriminant => "enum_discriminant",
179            Self::EnumFromDiscriminant => "enum_from_discriminant",
180
181            // Vec
182            Self::VecPush => "vec_push",
183            Self::VecExtend => "vec_extend",
184            Self::VecPop => "vec_pop",
185            Self::VecSlice => "vec_slice",
186            Self::VecRemoveIndex => "vec_remove_index",
187            Self::VecRemoveIndexGetValue => "vec_remove_index_get_value",
188            Self::VecRemoveFirstIndexGetValue => "vec_remove_first_get_value",
189            Self::VecClear => "vec_clear",
190            Self::VecGet => "vec_get",
191            Self::TransformerFor => "vec_for",
192            Self::VecIsEmpty => "vec_is_empty",
193            Self::TransformerWhile => "transformer_while",
194            Self::TransformerFindMap => "transformer_find_map",
195            Self::VecLen => "vec_len",
196            Self::VecCapacity => "vec_capacity",
197            Self::TransformerAny => "vec_any",
198            Self::TransformerAll => "vec_all",
199            Self::TransformerMap => "vec_map",
200            Self::TransformerFilter => "vec_filter",
201            Self::TransformerFilterMap => "vec_filter_map",
202            Self::TransformerFind => "vec_find",
203            Self::TransformerFold => "vec_fold",
204            Self::VecSwap => "vec_swap",
205            Self::VecInsert => "vec_insert",
206            Self::VecFirst => "vec_first",
207            Self::VecLast => "vec_last",
208
209            // Map
210            Self::MapHas => "map_has",
211            Self::MapRemove => "map_remove",
212            Self::MapLen => "map_len",
213            Self::MapCapacity => "map_len",
214            Self::MapIsEmpty => "map_is_empty",
215
216            // Grid
217            Self::GridSet => "grid_set",
218            Self::GridGet => "grid_get",
219            Self::GridWidth => "grid_width",
220            Self::GridHeight => "grid_height",
221
222            // Sparse
223            Self::SparseAdd => "sparse_add",
224            Self::SparseRemove => "sparse_remove",
225            Self::SparseIsAlive => "sparse_is_alive",
226
227            // Other
228            Self::Float2Magnitude => "float2_magnitude",
229            Self::RuntimePanic => "runtime_panic",
230            Self::RuntimeHalt => "runtime_halt",
231            Self::RuntimeStep => "runtime_step",
232            Self::RangeInit => "rinit",
233        };
234
235        write!(f, "{name}")
236    }
237}