Skip to main content

lisette_syntax/program/
native_operation.rs

1use super::NativeTypeKind;
2
3#[derive(Clone, Copy)]
4pub struct NativeOperation<'a> {
5    pub kind: NativeTypeKind,
6    pub method: &'a str,
7}
8
9impl<'a> NativeOperation<'a> {
10    pub fn new(kind: NativeTypeKind, method: &'a str) -> Self {
11        Self { kind, method }
12    }
13    pub fn mutates_receiver(self) -> bool {
14        matches!(
15            (self.kind, self.method),
16            (NativeTypeKind::Map, "delete") | (NativeTypeKind::Slice, "copy_from")
17        )
18    }
19    pub fn grows_slice(self) -> bool {
20        self.kind == NativeTypeKind::Slice && matches!(self.method, "append" | "reserve")
21    }
22    pub fn returns_fresh_slice(self) -> bool {
23        matches!(
24            (self.kind, self.method),
25            (
26                NativeTypeKind::Slice,
27                "append" | "reserve" | "clone" | "filter" | "map"
28            ) | (NativeTypeKind::Array, "to_slice")
29        )
30    }
31
32    pub fn indexes_receiver(self) -> bool {
33        matches!(
34            (self.kind, self.method),
35            (
36                NativeTypeKind::Slice | NativeTypeKind::Array | NativeTypeKind::Map,
37                "get"
38            )
39        )
40    }
41
42    pub fn read_only_without_callbacks(self) -> bool {
43        let Self {
44            kind: native_type,
45            method,
46        } = self;
47        match native_type {
48            NativeTypeKind::String => matches!(
49                method,
50                "length"
51                    | "is_empty"
52                    | "contains"
53                    | "split"
54                    | "starts_with"
55                    | "ends_with"
56                    | "byte_at"
57                    | "rune_at"
58                    | "bytes"
59                    | "runes"
60                    | "substring"
61            ),
62            NativeTypeKind::Array => matches!(method, "length" | "get" | "to_slice"),
63            NativeTypeKind::Slice => matches!(
64                method,
65                "length"
66                    | "is_empty"
67                    | "capacity"
68                    | "get"
69                    | "append"
70                    | "enumerate"
71                    | "clone"
72                    | "join"
73            ),
74            NativeTypeKind::Map => matches!(method, "length" | "is_empty" | "get" | "clone"),
75            NativeTypeKind::Channel | NativeTypeKind::Sender | NativeTypeKind::Receiver => {
76                matches!(method, "length" | "is_empty" | "capacity")
77            }
78            NativeTypeKind::EnumeratedSlice => method == "clone",
79        }
80    }
81}
82
83#[derive(Clone, Copy)]
84pub enum NativeInlinePackage {
85    None,
86    Slices,
87    Strings,
88    Prelude,
89}
90
91#[derive(Clone, Copy)]
92pub enum NativeInlineForm {
93    Call(&'static str),
94
95    IsEmpty,
96    Receiver,
97
98    Conversion(&'static str),
99
100    Index,
101}
102
103pub struct NativeInlineRule {
104    types: &'static [NativeTypeKind],
105    method: &'static str,
106    arity: RuleArity,
107    pub form: NativeInlineForm,
108    pub package: NativeInlinePackage,
109}
110
111#[derive(Clone, Copy)]
112enum RuleArity {
113    Exact(usize),
114    Variadic,
115}
116
117impl RuleArity {
118    fn accepts(self, actual: usize) -> bool {
119        match self {
120            Self::Exact(expected) => actual == expected,
121            Self::Variadic => true,
122        }
123    }
124}
125
126impl NativeInlineRule {
127    fn matches(&self, native_type: NativeTypeKind, method: &str, arity: usize) -> bool {
128        self.method == method && self.types.contains(&native_type) && self.arity.accepts(arity)
129    }
130}
131
132static INLINE_METHODS: &[NativeInlineRule] = &[
133    NativeInlineRule {
134        types: &[
135            NativeTypeKind::Slice,
136            NativeTypeKind::Map,
137            NativeTypeKind::Channel,
138            NativeTypeKind::Sender,
139            NativeTypeKind::Receiver,
140            NativeTypeKind::String,
141            NativeTypeKind::Array,
142        ],
143        method: "length",
144        arity: RuleArity::Exact(0),
145        form: NativeInlineForm::Call("len"),
146        package: NativeInlinePackage::None,
147    },
148    NativeInlineRule {
149        types: &[
150            NativeTypeKind::Slice,
151            NativeTypeKind::Channel,
152            NativeTypeKind::Sender,
153            NativeTypeKind::Receiver,
154        ],
155        method: "capacity",
156        arity: RuleArity::Exact(0),
157        form: NativeInlineForm::Call("cap"),
158        package: NativeInlinePackage::None,
159    },
160    NativeInlineRule {
161        types: &[
162            NativeTypeKind::Slice,
163            NativeTypeKind::Map,
164            NativeTypeKind::Channel,
165            NativeTypeKind::Sender,
166            NativeTypeKind::Receiver,
167            NativeTypeKind::String,
168        ],
169        method: "is_empty",
170        arity: RuleArity::Exact(0),
171        form: NativeInlineForm::IsEmpty,
172        package: NativeInlinePackage::None,
173    },
174    NativeInlineRule {
175        types: &[NativeTypeKind::Slice],
176        method: "enumerate",
177        arity: RuleArity::Exact(0),
178        form: NativeInlineForm::Receiver,
179        package: NativeInlinePackage::None,
180    },
181    NativeInlineRule {
182        types: &[NativeTypeKind::String],
183        method: "bytes",
184        arity: RuleArity::Exact(0),
185        form: NativeInlineForm::Conversion("byte"),
186        package: NativeInlinePackage::None,
187    },
188    NativeInlineRule {
189        types: &[NativeTypeKind::String],
190        method: "runes",
191        arity: RuleArity::Exact(0),
192        form: NativeInlineForm::Conversion("rune"),
193        package: NativeInlinePackage::None,
194    },
195    NativeInlineRule {
196        types: &[NativeTypeKind::Map],
197        method: "delete",
198        arity: RuleArity::Exact(1),
199        form: NativeInlineForm::Call("delete"),
200        package: NativeInlinePackage::None,
201    },
202    NativeInlineRule {
203        types: &[NativeTypeKind::Slice],
204        method: "copy_from",
205        arity: RuleArity::Exact(1),
206        form: NativeInlineForm::Call("copy"),
207        package: NativeInlinePackage::None,
208    },
209    NativeInlineRule {
210        types: &[NativeTypeKind::Slice],
211        method: "contains",
212        arity: RuleArity::Exact(1),
213        form: NativeInlineForm::Call("Contains"),
214        package: NativeInlinePackage::Slices,
215    },
216    NativeInlineRule {
217        types: &[NativeTypeKind::String],
218        method: "contains",
219        arity: RuleArity::Exact(1),
220        form: NativeInlineForm::Call("Contains"),
221        package: NativeInlinePackage::Strings,
222    },
223    NativeInlineRule {
224        types: &[NativeTypeKind::String],
225        method: "split",
226        arity: RuleArity::Exact(1),
227        form: NativeInlineForm::Call("Split"),
228        package: NativeInlinePackage::Strings,
229    },
230    NativeInlineRule {
231        types: &[NativeTypeKind::String],
232        method: "starts_with",
233        arity: RuleArity::Exact(1),
234        form: NativeInlineForm::Call("HasPrefix"),
235        package: NativeInlinePackage::Strings,
236    },
237    NativeInlineRule {
238        types: &[NativeTypeKind::String],
239        method: "ends_with",
240        arity: RuleArity::Exact(1),
241        form: NativeInlineForm::Call("HasSuffix"),
242        package: NativeInlinePackage::Strings,
243    },
244    NativeInlineRule {
245        types: &[NativeTypeKind::String],
246        method: "byte_at",
247        arity: RuleArity::Exact(1),
248        form: NativeInlineForm::Index,
249        package: NativeInlinePackage::None,
250    },
251    NativeInlineRule {
252        types: &[NativeTypeKind::String],
253        method: "rune_at",
254        arity: RuleArity::Exact(1),
255        form: NativeInlineForm::Call("RuneAt"),
256        package: NativeInlinePackage::Prelude,
257    },
258    NativeInlineRule {
259        types: &[NativeTypeKind::Slice],
260        method: "join",
261        arity: RuleArity::Exact(1),
262        form: NativeInlineForm::Call("Join"),
263        package: NativeInlinePackage::Strings,
264    },
265    NativeInlineRule {
266        types: &[NativeTypeKind::Slice],
267        method: "any",
268        arity: RuleArity::Exact(1),
269        form: NativeInlineForm::Call("ContainsFunc"),
270        package: NativeInlinePackage::Slices,
271    },
272    NativeInlineRule {
273        types: &[NativeTypeKind::Slice],
274        method: "reserve",
275        arity: RuleArity::Exact(1),
276        form: NativeInlineForm::Call("Grow"),
277        package: NativeInlinePackage::Slices,
278    },
279    NativeInlineRule {
280        types: &[NativeTypeKind::Slice],
281        method: "append",
282        arity: RuleArity::Exact(0),
283        form: NativeInlineForm::Receiver,
284        package: NativeInlinePackage::None,
285    },
286    NativeInlineRule {
287        types: &[NativeTypeKind::Slice],
288        method: "append",
289        arity: RuleArity::Variadic,
290        form: NativeInlineForm::Call("append"),
291        package: NativeInlinePackage::None,
292    },
293];
294
295impl NativeOperation<'_> {
296    pub fn inline_rule(self, arity: usize) -> Option<&'static NativeInlineRule> {
297        INLINE_METHODS
298            .iter()
299            .find(|rule| rule.matches(self.kind, self.method, arity))
300    }
301    pub fn reads_array_in_place(self) -> bool {
302        self.kind == NativeTypeKind::Array && matches!(self.method, "to_slice" | "get")
303    }
304
305    pub fn reads_elements_only(self, arity: usize) -> bool {
306        matches!(
307            (self.method, arity),
308            ("length" | "is_empty" | "clone", 0) | ("get" | "contains" | "equals" | "join", 1)
309        )
310    }
311    pub fn lowers_to_plain_call(self, arity: usize) -> bool {
312        if matches!(self.method, "substring" | "equals" | "clone") || self.reads_array_in_place() {
313            return true;
314        }
315        self.inline_rule(arity).is_none_or(|rule| {
316            matches!(
317                rule.method,
318                "delete"
319                    | "contains"
320                    | "split"
321                    | "starts_with"
322                    | "ends_with"
323                    | "rune_at"
324                    | "join"
325                    | "any"
326            )
327        })
328    }
329}