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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use std::vec;

use arret_syntax::span::Span;

use crate::mir::builder::{Builder, TryToBuilder};
use crate::mir::value;
use crate::mir::value::types::TypeHint;
use crate::mir::value::Value;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ListValueLen {
    Exact(usize),
    Min(usize),
}

impl ListValueLen {
    pub fn lower_bound(&self) -> usize {
        match self {
            ListValueLen::Exact(len) => *len,
            ListValueLen::Min(len) => *len,
        }
    }
}

impl std::ops::Add for ListValueLen {
    type Output = ListValueLen;

    fn add(self, other: ListValueLen) -> ListValueLen {
        match (self, other) {
            (ListValueLen::Exact(self_len), ListValueLen::Exact(other_len)) => {
                ListValueLen::Exact(self_len + other_len)
            }

            _ => ListValueLen::Min(self.lower_bound() + other.lower_bound()),
        }
    }
}

pub fn list_value_len(value: &Value) -> ListValueLen {
    use arret_runtime::boxed;

    match value {
        Value::List(fixed, rest) => {
            let fixed_len = ListValueLen::Exact(fixed.len());

            match rest {
                Some(rest) => fixed_len + list_value_len(rest),
                None => fixed_len,
            }
        }

        Value::Const(any_ref) => match any_ref.downcast_ref::<boxed::List<boxed::Any>>() {
            Some(list_ref) => ListValueLen::Exact(list_ref.len()),
            None => ListValueLen::Min(0),
        },

        Value::Reg(reg_value) => {
            if !reg_value.possible_type_tags.contains(boxed::TypeTag::Pair) {
                // Must be empty
                ListValueLen::Exact(0)
            } else if !reg_value.possible_type_tags.contains(boxed::TypeTag::Nil) {
                if let TypeHint::KnownListLen(len) = reg_value.type_hint {
                    ListValueLen::Exact(len)
                } else {
                    // Cannot be empty
                    ListValueLen::Min(1)
                }
            } else {
                ListValueLen::Min(0)
            }
        }

        _ => ListValueLen::Min(0),
    }
}

pub struct UnsizedListIterator {
    fixed: vec::IntoIter<Value>,
    rest: Option<Value>,
}

impl UnsizedListIterator {
    pub fn new(value: Value) -> Self {
        Self {
            fixed: Vec::new().into_iter(),
            rest: Some(value),
        }
    }

    /// Returns the next element in the list
    ///
    /// It is undefined if the list has no more elements. This function may panic, generate
    /// nonsense code, generate code that crashes at runtime, etc.
    #[must_use]
    pub fn next_unchecked(&mut self, b: &mut impl TryToBuilder, span: Span) -> Value {
        if let Some(next) = self.fixed.next() {
            return next;
        }

        let rest_value = self
            .rest
            .take()
            .expect("ran off the end of list with no rest argument");

        match rest_value {
            Value::List(fixed, rest) => {
                // Become our tail
                self.fixed = fixed.into_vec().into_iter();
                self.rest = rest.map(|rest| *rest);

                self.next_unchecked(b, span)
            }
            Value::Const(any_ref) => {
                use arret_runtime::boxed;

                let const_pair = any_ref
                    .downcast_ref::<boxed::Pair<boxed::Any>>()
                    .expect("tried to pop off non-pair constant");

                let tail = const_pair.rest();
                self.rest = if tail.is_empty() {
                    None
                } else {
                    Some(tail.into())
                };

                const_pair.head().into()
            }
            Value::Reg(reg_value) => {
                let b = b
                    .try_to_builder()
                    .expect("popping rest argument without builder");

                self.build_rest_next(b, span, &reg_value)
            }
            other => unimplemented!("popping rest argument off value {:?}", other),
        }
    }

    /// Returns a Value containing the rest of the iterator
    #[must_use]
    pub fn into_rest(self) -> Value {
        Value::List(self.fixed.collect(), self.rest.map(Box::new))
    }

    fn build_rest_next(
        &mut self,
        b: &mut Builder,
        span: Span,
        current_rest_value: &value::RegValue,
    ) -> Value {
        use crate::mir::ops::*;
        use crate::mir::value::build_reg::reg_to_boxed_reg;
        use arret_runtime::abitype;

        let needed_pair_type = abitype::BoxedAbiType::Pair(&abitype::BoxedAbiType::Any);
        let current_rest_reg = reg_to_boxed_reg(b, span, current_rest_value, &needed_pair_type);

        let head_reg = b.push_reg(span, OpKind::LoadBoxedPairHead, current_rest_reg.into());
        let rest_reg = b.push_reg(span, OpKind::LoadBoxedPairRest, current_rest_reg.into());

        self.rest =
            Some(value::RegValue::new(rest_reg, abitype::TOP_LIST_BOXED_ABI_TYPE.into()).into());

        value::RegValue::new(head_reg, abitype::BoxedAbiType::Any.into()).into()
    }
}

pub struct SizedListIterator {
    size: usize,
    unsized_list_iterator: UnsizedListIterator,
}

impl SizedListIterator {
    pub fn try_new(value: &Value) -> Option<Self> {
        match list_value_len(value) {
            ListValueLen::Exact(size) => Some(Self {
                size,
                unsized_list_iterator: UnsizedListIterator::new(value.clone()),
            }),
            _ => None,
        }
    }
}

impl SizedListIterator {
    pub fn next(&mut self, b: &mut impl TryToBuilder, span: Span) -> Option<Value> {
        if self.size == 0 {
            return None;
        }

        self.size -= 1;
        Some(self.unsized_list_iterator.next_unchecked(b, span))
    }

    #[must_use]
    pub fn into_rest(self) -> Value {
        self.unsized_list_iterator.into_rest()
    }

    pub fn len(&self) -> usize {
        self.size
    }

    pub fn is_empty(&self) -> bool {
        self.size == 0
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use arret_runtime::boxed;
    use arret_runtime::boxed::prelude::*;

    use crate::source::EMPTY_SPAN;

    #[test]
    fn list_len() {
        use crate::mir::builder::BuiltReg;
        use crate::mir::ops::RegId;
        use arret_runtime::abitype;

        let mut heap = boxed::Heap::empty();
        let elements = &[1, 2, 3];

        // Start with three fixed values
        let fixed_values: Box<[Value]> = elements
            .iter()
            .map(|element| boxed::Int::new(&mut heap, *element).into())
            .collect();

        // Have a constant list tail
        let boxed_list_tail =
            boxed::List::from_values(&mut heap, elements.iter().cloned(), boxed::Int::new);

        let const_list_tail = Value::List(Box::new([]), Some(Box::new(boxed_list_tail.into())));

        // Add the fixed values (3 elements) to the constant tail (3 elements)
        let list_value = Value::List(fixed_values.clone(), Some(Box::new(const_list_tail)));

        // The length should be 6
        assert_eq!(ListValueLen::Exact(6), list_value_len(&list_value));

        // Try 3 fixed values with a completely unknown tail
        let list_with_unknown_tail = Value::List(
            fixed_values.clone(),
            Some(Box::new(
                value::RegValue::new(
                    BuiltReg::Local(RegId::alloc()),
                    abitype::BoxedAbiType::Any.into(),
                )
                .into(),
            )),
        );

        // Length should be at least 3
        assert_eq!(
            ListValueLen::Min(3),
            list_value_len(&list_with_unknown_tail)
        );

        // Try 3 fixed values with a pair tail
        let list_with_pair_tail = Value::List(
            fixed_values.clone(),
            Some(Box::new(
                value::RegValue::new(BuiltReg::Local(RegId::alloc()), boxed::TypeTag::Pair.into())
                    .into(),
            )),
        );

        // Length should be at least 4
        assert_eq!(ListValueLen::Min(4), list_value_len(&list_with_pair_tail));

        // Try 3 fixed values with a nil tail
        let list_with_nil_tail = Value::List(
            fixed_values,
            Some(Box::new(
                value::RegValue::new(BuiltReg::Local(RegId::alloc()), boxed::TypeTag::Nil.into())
                    .into(),
            )),
        );

        // Length should be at exactly 3
        assert_eq!(ListValueLen::Exact(3), list_value_len(&list_with_nil_tail));
    }

    #[test]
    fn const_unsized_list_iter() {
        let mut heap = boxed::Heap::empty();

        let elements = &[1, 2, 3];

        let boxed_list =
            boxed::List::from_values(&mut heap, elements.iter().cloned(), boxed::Int::new);

        let mut iter = UnsizedListIterator {
            fixed: Vec::new().into_iter(),
            rest: Some(boxed_list.into()),
        };

        for expected in elements {
            let next_value = iter.next_unchecked(&mut None, EMPTY_SPAN);

            if let Value::Const(next_ref) = next_value {
                let expected_ref = boxed::Int::new(&mut heap, *expected).as_any_ref();
                assert!(expected_ref.eq_in_heap(&heap, &next_ref));
            } else {
                panic!("expected const value, got {:?}", next_value);
            }
        }
    }

    #[test]
    fn fixed_list_value_unsized_iter() {
        let mut heap = boxed::Heap::empty();

        let elements = &[1, 2, 3];

        let element_values: Vec<Value> = elements
            .iter()
            .map(|element| boxed::Int::new(&mut heap, *element).into())
            .collect();

        let mut iter = UnsizedListIterator {
            fixed: element_values.into_iter(),
            rest: None,
        };

        for expected in elements {
            let next_value = iter.next_unchecked(&mut None, EMPTY_SPAN);

            if let Value::Const(next_ref) = next_value {
                let expected_ref = boxed::Int::new(&mut heap, *expected).as_any_ref();
                assert!(expected_ref.eq_in_heap(&heap, &next_ref));
            } else {
                panic!("expected const value, got {:?}", next_value);
            }
        }
    }
}