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    ByteToString,
13    // Bool
14    BoolToString,
15
16    // Int
17    IntAbs,
18    IntRnd,
19    IntMax,
20    IntMin,
21    IntClamp,
22    IntToFloat,
23    IntToString,
24
25    // Float
26    FloatRound,
27    FloatFloor,
28    FloatSqrt,
29    FloatSign,
30    FloatAbs,
31    FloatRnd,
32    FloatCos,
33    FloatSin,
34    FloatAcos,
35    FloatAsin,
36    FloatAtan2,
37    FloatMin,
38    FloatMax,
39    FloatClamp,
40    FloatToString,
41
42    // String
43    StringLen,
44    StringToString,
45    // TODO: StringSubscript, StringConcat
46
47    // Range
48    RangeInit,
49
50    // Vec
51    VecPush,
52    VecPop,
53    VecRemoveIndex,
54    VecRemoveIndexGetValue,
55    VecRemoveFirstIndexGetValue,
56    VecClear,
57    VecSwap,
58    VecInsert,
59    VecFirst,
60    VecGet,
61    VecLast,
62    VecLen,
63    VecCapacity,
64    VecIsEmpty,
65
66    // Transformer
67    TransformerFor,
68    TransformerWhile,
69    TransformerFindMap,
70    TransformerAny,
71    TransformerAll,
72    TransformerMap,
73    TransformerFilter,
74    TransformerFilterMap,
75    TransformerFind,
76    TransformerFold,
77
78    // Map
79    MapIsEmpty,
80    MapHas,
81    MapRemove,
82    MapLen,
83    MapCapacity,
84
85    // Grid
86    GridSet,
87    GridGet,
88    GridWidth,
89    GridHeight,
90
91    // Sparse
92    SparseAdd,
93    SparseRemove,
94    SparseIsAlive,
95
96    Float2Magnitude,
97
98    RuntimePanic,
99    RuntimeHalt,
100    RuntimeStep,
101}
102
103pub type IntrinsicFunctionDefinitionRef = Rc<IntrinsicFunctionDefinition>;
104
105#[derive(Clone, PartialEq, Eq, Debug)]
106pub struct IntrinsicFunctionDefinition {
107    pub name: String,
108    pub signature: Signature,
109    pub intrinsic: IntrinsicFunction,
110}
111
112impl fmt::Display for IntrinsicFunction {
113    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114        let name = match self {
115            Self::ByteToString => "byte_to_string",
116            // Bool
117            Self::BoolToString => "bool_to_string",
118
119            // Float
120            Self::FloatRound => "float_round",
121            Self::FloatFloor => "float_floor",
122            Self::FloatSqrt => "float_sqrt",
123            Self::FloatSign => "float_sign",
124            Self::FloatAbs => "float_abs",
125            Self::FloatRnd => "float_rnd",
126            Self::FloatCos => "float_cos",
127            Self::FloatSin => "float_sin",
128            Self::FloatAcos => "float_acos",
129            Self::FloatAsin => "float_asin",
130            Self::FloatAtan2 => "float_atan2",
131            Self::FloatMin => "float_min",
132            Self::FloatMax => "float_max",
133            Self::FloatClamp => "float_clamp",
134            Self::FloatToString => "float_to_string",
135
136            // Int
137            Self::IntAbs => "int_abs",
138            Self::IntRnd => "int_rnd",
139            Self::IntMax => "int_max",
140            Self::IntMin => "int_min",
141            Self::IntToFloat => "int_to_float",
142            Self::IntClamp => "int_clamp",
143            Self::IntToString => "int_to_string",
144
145            // String
146            Self::StringLen => "string_len",
147            Self::StringToString => "string_to_string",
148
149            // Vec
150            Self::VecPush => "vec_push",
151            Self::VecPop => "vec_pop",
152            Self::VecRemoveIndex => "vec_remove_index",
153            Self::VecRemoveIndexGetValue => "vec_remove_index_get_value",
154            Self::VecRemoveFirstIndexGetValue => "vec_remove_first_get_value",
155            Self::VecClear => "vec_clear",
156            Self::VecGet => "vec_get",
157            Self::TransformerFor => "vec_for",
158            Self::VecIsEmpty => "vec_is_empty",
159            Self::TransformerWhile => "transformer_while",
160            Self::TransformerFindMap => "transformer_find_map",
161            Self::VecLen => "vec_len",
162            Self::VecCapacity => "vec_capacity",
163            Self::TransformerAny => "vec_any",
164            Self::TransformerAll => "vec_all",
165            Self::TransformerMap => "vec_map",
166            Self::TransformerFilter => "vec_filter",
167            Self::TransformerFilterMap => "vec_filter_map",
168            Self::TransformerFind => "vec_find",
169            Self::TransformerFold => "vec_fold",
170            Self::VecSwap => "vec_swap",
171            Self::VecInsert => "vec_insert",
172            Self::VecFirst => "vec_first",
173            Self::VecLast => "vec_last",
174
175            // Map
176            Self::MapHas => "map_has",
177            Self::MapRemove => "map_remove",
178            Self::MapLen => "map_len",
179            Self::MapCapacity => "map_len",
180            Self::MapIsEmpty => "map_is_empty",
181
182            // Grid
183            Self::GridSet => "grid_set",
184            Self::GridGet => "grid_get",
185            Self::GridWidth => "grid_width",
186            Self::GridHeight => "grid_height",
187
188            // Sparse
189            Self::SparseAdd => "sparse_add",
190            Self::SparseRemove => "sparse_remove",
191            Self::SparseIsAlive => "sparse_is_alive",
192
193            // Other
194            Self::Float2Magnitude => "float2_magnitude",
195            Self::RuntimePanic => "runtime_panic",
196            Self::RuntimeHalt => "runtime_halt",
197            Self::RuntimeStep => "runtime_step",
198            Self::RangeInit => "rinit",
199        };
200
201        write!(f, "{name}")
202    }
203}