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::rc::Rc;
8use swamp_script_types::prelude::*;
9
10#[derive(Debug, Hash, Clone)]
11pub enum IntrinsicFunction {
12    FloatRound,
13    FloatFloor,
14    FloatSqrt,
15    FloatSign,
16    FloatAbs,
17    FloatRnd,
18    FloatCos,
19    FloatSin,
20    FloatAcos,
21    FloatAsin,
22    FloatAtan2,
23    FloatMin,
24    FloatMax,
25    FloatClamp,
26
27    // Int
28    IntAbs,
29    IntRnd,
30    IntMax,
31    IntMin,
32    IntClamp,
33    IntToFloat,
34
35    // String
36    StringLen,
37
38    // Vec
39    VecFromSlice,
40    VecPush,
41    VecPop,
42    VecRemoveIndex,
43    VecClear,
44    VecCreate,
45    VecSubscript,
46    VecSubscriptMut,
47    VecIter,
48    VecIterMut,
49
50    VecSelfPush,
51    VecSelfExtend,
52
53    // Map
54    MapCreate,
55    MapFromSlicePair,
56    MapHas,
57    MapRemove,
58    MapLen,
59    MapIter,
60    MapIterMut,
61    MapSubscript,
62    MapSubscriptMut,
63
64    // Sparse
65    SparseCreate,
66    SparseFromSlice,
67    SparseIter,
68    SparseIterMut,
69    SparseSubscript,
70    SparseSubscriptMut,
71    SparseHas,
72    SparseRemove,
73
74    Float2Magnitude,
75    SparseAdd,
76    VecLen,
77    VecIsEmpty,
78    SparseNew,
79}
80
81pub type IntrinsicFunctionDefinitionRef = Rc<IntrinsicFunctionDefinition>;
82#[derive(Clone, Debug)]
83pub struct IntrinsicFunctionDefinition {
84    pub name: String,
85    pub signature: Signature,
86    pub intrinsic: IntrinsicFunction,
87}
88
89impl fmt::Display for IntrinsicFunction {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        let name = match self {
92            // Float
93            Self::FloatRound => "float_round",
94            Self::FloatFloor => "float_floor",
95            Self::FloatSqrt => "float_sqrt",
96            Self::FloatSign => "float_sign",
97            Self::FloatAbs => "float_abs",
98            Self::FloatRnd => "float_rnd",
99            Self::FloatCos => "float_cos",
100            Self::FloatSin => "float_sin",
101            Self::FloatAcos => "float_acos",
102            Self::FloatAsin => "float_asin",
103            Self::FloatAtan2 => "float_atan2",
104            Self::FloatMin => "float_min",
105            Self::FloatMax => "float_max",
106            Self::FloatClamp => "float_clamp",
107
108            // Int
109            Self::IntAbs => "int_abs",
110            Self::IntRnd => "int_rnd",
111            Self::IntMax => "int_max",
112            Self::IntMin => "int_min",
113            Self::IntToFloat => "int_to_float",
114            Self::IntClamp => "int_clamp",
115
116            // String
117            Self::StringLen => "string_len",
118
119            // Vec
120            Self::VecFromSlice => "vec_from_slice",
121            Self::VecPush => "vec_push",
122            Self::VecPop => "vec_pop",
123            Self::VecRemoveIndex => "vec_remove",
124            Self::VecClear => "vec_clear",
125            Self::VecCreate => "vec_create",
126            Self::VecSubscriptMut => "vec_subscript_mut",
127            Self::VecSubscript => "vec_subscript",
128            Self::VecIter => "vec_iter",
129            Self::VecIterMut => "vec_iter_mut",
130            Self::VecLen => "vec_len",
131            Self::VecIsEmpty => "vec_is_empty",
132
133            Self::VecSelfPush => "vec_self_push",
134            Self::VecSelfExtend => "vec_self_extend",
135
136            // Map
137            Self::MapCreate => "map_create",
138            Self::MapFromSlicePair => "map_from_slice_pair",
139            Self::MapHas => "map_has",
140            Self::MapRemove => "map_remove",
141            Self::MapLen => "map_len",
142            Self::MapSubscriptMut => "map_subscript_mut",
143            Self::MapSubscript => "map_subscript",
144            Self::MapIter => "map_iter",
145            Self::MapIterMut => "map_iter_mut",
146
147            // Sparse
148            Self::SparseNew => "sparse_new",
149            Self::SparseCreate => "sparse_create",
150            Self::SparseFromSlice => "sparse_from_slice",
151            Self::SparseAdd => "sparse_add",
152            Self::SparseHas => "sparse_has",
153            Self::SparseRemove => "sparse_remove",
154            Self::SparseSubscriptMut => "sparse_subscript_mut",
155            Self::SparseSubscript => "sparse_subscript",
156            Self::SparseIter => "sparse_iter",
157            Self::SparseIterMut => "sparse_iter_mut",
158
159            // Other
160            Self::Float2Magnitude => "float2_magnitude",
161        };
162
163        write!(f, "{name}")
164    }
165}