intuicio_frontend_simpleton/library/
array.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::{library::closure::Closure, Array, Boolean, Function, Integer, Reference};
use intuicio_core::{context::Context, define_native_struct, registry::Registry, IntuicioStruct};
use intuicio_derive::{intuicio_function, intuicio_method, intuicio_methods, IntuicioStruct};

#[intuicio_function(module_name = "array", use_registry)]
pub fn new(registry: &Registry, capacity: Reference) -> Reference {
    Reference::new_array(
        Array::with_capacity(capacity.read::<Integer>().map(|v| *v as _).unwrap_or(0)),
        registry,
    )
}

#[intuicio_function(module_name = "array")]
pub fn reserve(mut array: Reference, additional: Reference) -> Reference {
    array
        .write::<Array>()
        .unwrap()
        .reserve(*additional.read::<Integer>().unwrap() as usize);
    Reference::null()
}

#[intuicio_function(module_name = "array", use_registry)]
pub fn size(registry: &Registry, array: Reference) -> Reference {
    Reference::new_integer(array.read::<Array>().unwrap().len() as Integer, registry)
}

#[intuicio_function(module_name = "array", use_registry)]
pub fn capacity(registry: &Registry, array: Reference) -> Reference {
    Reference::new_integer(
        array.read::<Array>().unwrap().capacity() as Integer,
        registry,
    )
}

#[intuicio_function(module_name = "array")]
pub fn clear(mut array: Reference) -> Reference {
    array.write::<Array>().unwrap().clear();
    Reference::null()
}

#[intuicio_function(module_name = "array", use_registry)]
pub fn contains(registry: &Registry, map: Reference, value: Reference) -> Reference {
    let result = map
        .read::<Array>()
        .unwrap()
        .iter()
        .any(|item| value.does_share_reference(item, true));
    Reference::new_boolean(result, registry)
}

#[intuicio_function(module_name = "array", use_registry)]
pub fn find(
    registry: &Registry,
    array: Reference,
    value: Reference,
    reverse: Reference,
) -> Reference {
    if *reverse.read::<Boolean>().unwrap() {
        array
            .read::<Array>()
            .unwrap()
            .iter()
            .rev()
            .position(|item| value.does_share_reference(item, true))
            .map(|index| Reference::new_integer(index as Integer, registry))
            .unwrap_or_default()
    } else {
        array
            .read::<Array>()
            .unwrap()
            .iter()
            .position(|item| value.does_share_reference(item, true))
            .map(|index| Reference::new_integer(index as Integer, registry))
            .unwrap_or_default()
    }
}

#[intuicio_function(module_name = "array")]
pub fn push(mut array: Reference, value: Reference) -> Reference {
    array.write::<Array>().unwrap().push(value);
    Reference::null()
}

#[intuicio_function(module_name = "array")]
pub fn insert(mut array: Reference, index: Reference, value: Reference) -> Reference {
    array
        .write::<Array>()
        .unwrap()
        .insert(*index.read::<Integer>().unwrap() as _, value);
    Reference::null()
}

#[intuicio_function(module_name = "array")]
pub fn pop(mut array: Reference) -> Reference {
    array.write::<Array>().unwrap().pop().unwrap_or_default()
}

#[intuicio_function(module_name = "array")]
pub fn remove(mut array: Reference, index: Reference) -> Reference {
    array
        .write::<Array>()
        .unwrap()
        .remove(*index.read::<Integer>().unwrap() as _)
}

#[intuicio_function(module_name = "array")]
pub fn set(mut array: Reference, index: Reference, value: Reference) -> Reference {
    array
        .write::<Array>()
        .unwrap()
        .get_mut(*index.read::<Integer>().unwrap() as usize)
        .map(|item| std::mem::replace(item, value))
        .unwrap_or_default()
}

#[intuicio_function(module_name = "array")]
pub fn get(array: Reference, index: Reference) -> Reference {
    array
        .read::<Array>()
        .unwrap()
        .get(*index.read::<Integer>().unwrap() as usize)
        .cloned()
        .unwrap_or_default()
}

#[intuicio_function(module_name = "array", use_registry)]
pub fn slice(
    registry: &Registry,
    array: Reference,
    index: Reference,
    count: Reference,
) -> Reference {
    let index = *index.read::<Integer>().unwrap() as usize;
    let count = *count.read::<Integer>().unwrap() as usize;
    Reference::new_array(
        array.read::<Array>().unwrap()[index..(index + count)].to_vec(),
        registry,
    )
}

#[intuicio_function(module_name = "array", use_registry)]
pub fn join(registry: &Registry, a: Reference, b: Reference) -> Reference {
    Reference::new_array(
        a.read::<Array>()
            .unwrap()
            .iter()
            .chain(b.read::<Array>().unwrap().iter())
            .cloned()
            .collect::<Array>(),
        registry,
    )
}

#[intuicio_function(module_name = "array", use_registry)]
pub fn iter(registry: &Registry, array: Reference, reversed: Reference) -> Reference {
    Reference::new(
        ArrayIter {
            array,
            index: 0,
            reversed: *reversed.read::<Boolean>().unwrap(),
            next: Reference::new(
                Closure {
                    function: Function::by_name("next", "array_iter", registry).unwrap(),
                    captured: vec![],
                },
                registry,
            ),
        },
        registry,
    )
}

#[intuicio_function(module_name = "array", use_context, use_registry)]
pub fn collect(context: &mut Context, registry: &Registry, iterator: Reference) -> Reference {
    let mut result = Array::new();
    loop {
        let value = crate::library::iter::next(context, registry, iterator.clone());
        if value.is_null() {
            break;
        }
        result.push(value);
    }
    Reference::new_array(result, registry)
}

#[derive(IntuicioStruct, Default)]
#[intuicio(name = "ArrayIter", module_name = "array_iter")]
pub struct ArrayIter {
    #[intuicio(ignore)]
    pub array: Reference,
    #[intuicio(ignore)]
    pub index: usize,
    #[intuicio(ignore)]
    pub reversed: bool,
    pub next: Reference,
}

#[intuicio_methods(module_name = "array_iter")]
impl ArrayIter {
    #[intuicio_method()]
    pub fn next(mut iterator: Reference) -> Reference {
        let mut iterator = iterator.write::<ArrayIter>().unwrap();
        let array = iterator.array.clone();
        let array = array.read::<Array>().unwrap();
        if iterator.index >= array.len() {
            return Reference::null();
        }
        let index = if iterator.reversed {
            array.len() - iterator.index - 1
        } else {
            iterator.index
        };
        iterator.index += 1;
        array.get(index).cloned().unwrap_or_default()
    }
}

pub fn install(registry: &mut Registry) {
    registry.add_type(define_native_struct! {
        registry => mod array struct Array (Array) {}
        [override_send = true]
    });
    registry.add_function(new::define_function(registry));
    registry.add_function(reserve::define_function(registry));
    registry.add_function(size::define_function(registry));
    registry.add_function(capacity::define_function(registry));
    registry.add_function(clear::define_function(registry));
    registry.add_function(contains::define_function(registry));
    registry.add_function(find::define_function(registry));
    registry.add_function(push::define_function(registry));
    registry.add_function(insert::define_function(registry));
    registry.add_function(pop::define_function(registry));
    registry.add_function(remove::define_function(registry));
    registry.add_function(set::define_function(registry));
    registry.add_function(get::define_function(registry));
    registry.add_function(slice::define_function(registry));
    registry.add_function(join::define_function(registry));
    registry.add_function(iter::define_function(registry));
    registry.add_function(collect::define_function(registry));
    registry.add_type(ArrayIter::define_struct(registry));
    registry.add_function(ArrayIter::next__define_function(registry));
}