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
343
344
use core::fmt;
use std::{iter::Zip, slice::IterMut, vec::IntoIter};

use crate::callable::core::Builtin;

#[derive(Debug, Clone)]
pub enum Expr {
    Null,
    NA,
    Inf,
    More,
    Continue,
    Break,
    Ellipsis(Option<String>),
    Missing,
    Bool(bool),
    Number(f64),
    Integer(i32),
    String(String),
    Symbol(String),
    List(ExprList),
    Function(ExprList, Box<Expr>),
    Call(Box<Expr>, ExprList),
    Primitive(Box<dyn Builtin>),
}

impl PartialEq for Expr {
    fn eq(&self, other: &Self) -> bool {
        use Expr::*;
        match (self, other) {
            (Null, Null) => true,
            (NA, NA) => true,
            (Inf, Inf) => true,
            (Continue, Continue) => true,
            (Break, Break) => true,
            (Ellipsis(l), Ellipsis(r)) => l == r,
            (Missing, Missing) => true,
            (Bool(l), Bool(r)) => l == r,
            (Number(l), Number(r)) => l == r,
            (Integer(l), Integer(r)) => l == r,
            (String(l), String(r)) => l == r,
            (Symbol(l), Symbol(r)) => l == r,
            (List(l), List(r)) => l == r,
            (Primitive(l), Primitive(r)) => l == r,
            (Function(largs, lbody), Function(rargs, rbody)) => largs == rargs && lbody == rbody,
            (Call(lwhat, largs), Call(rwhat, rargs)) => lwhat == rwhat && largs == rargs,
            _ => false,
        }
    }
}

impl Expr {
    pub fn as_primitive<T>(x: T) -> Self
    where
        T: Builtin + 'static,
    {
        Self::Primitive(Box::new(x))
    }

    pub fn new_primitive_call<T>(x: T, args: ExprList) -> Self
    where
        T: Builtin + 'static,
    {
        let p = Self::as_primitive(x);
        Self::Call(Box::new(p), args)
    }
}

impl fmt::Display for Expr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Expr::Null => write!(f, "NULL"),
            Expr::Missing => write!(f, ""),
            Expr::Break => write!(f, "break"),
            Expr::Continue => write!(f, "continue"),
            Expr::Bool(true) => write!(f, "TRUE"),
            Expr::Bool(false) => write!(f, "FALSE"),
            Expr::Number(x) => write!(f, "{}", x),
            Expr::Integer(x) => write!(f, "{}L", x),
            Expr::String(x) => write!(f, "\"{}\"", x),
            Expr::Symbol(x) => write!(f, "{}", x),
            Expr::List(x) => write!(f, "{}", x),
            Expr::Ellipsis(None) => write!(f, "..."),
            Expr::Ellipsis(Some(s)) => write!(f, "..{s}"),
            Expr::Call(what, args) => match &**what {
                Expr::Primitive(p) => write!(f, "{}", p.rfmt_call(args)),
                Expr::String(s) | Expr::Symbol(s) => write!(f, "{}({})", s, args),
                rexpr => write!(f, "{}({})", rexpr, args),
            },
            Expr::Function(head, body) => write!(f, "function({}) {}", head, body),
            Expr::Primitive(p) => write!(f, "Primitive(\"{}\")", p.rfmt()),
            x => write!(f, "{:?}", x),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct ExprList {
    pub keys: Vec<Option<String>>, // TODO: use Vec<RExprListKey>
    pub values: Vec<Expr>,
}

impl ExprList {
    pub fn push_named(&mut self, key: Option<String>, value: Expr) {
        self.keys.push(key);
        self.values.push(value);
    }
}

impl fmt::Display for ExprList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let pairs: Vec<String> = self
            .values
            .iter()
            .enumerate()
            .map(|(i, v)| match (&self.keys[i], v) {
                (Some(k), Expr::Missing) => k.to_string(),
                (Some(k), _) => format!("{} = {}", k, v),
                (None, v) => format!("{}", v),
            })
            .collect();

        write!(f, "{}", pairs.join(", "))
    }
}

impl IntoIterator for ExprList {
    type Item = (Option<String>, Expr);
    type IntoIter = <Zip<IntoIter<Option<String>>, IntoIter<Expr>> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.keys.into_iter().zip(self.values)
    }
}

impl<'a> IntoIterator for &'a mut ExprList {
    type Item = (&'a mut Option<String>, &'a mut Expr);
    type IntoIter = <Zip<IterMut<'a, Option<String>>, IterMut<'a, Expr>> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.keys.iter_mut().zip(self.values.iter_mut())
    }
}

impl FromIterator<(Option<String>, Expr)> for ExprList {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = (Option<String>, Expr)>,
    {
        let (keys, values) = iter.into_iter().unzip();
        ExprList { keys, values }
    }
}

impl FromIterator<Expr> for ExprList {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = Expr>,
    {
        let values: Vec<Expr> = iter.into_iter().collect();
        ExprList { keys: vec![None; values.len()], values }
    }
}

impl ExprList {
    pub fn new() -> ExprList {
        ExprList { ..Default::default() }
    }

    pub fn get_named(&self, key: &String) -> Option<Expr> {
        // self.keys.iter()
        //     .enumerate().rev()
        //     .find(move |(_, i)| *i == &Some(key.clone()))
        //     .and_then(|(idx, _)| self.values.get(self.keys.len().saturating_sub(idx)))
        //     .and_then(|expr| Some(expr.clone()))

        let first_name_index = self.keys.iter().rev().position(|i| i.as_ref() == Some(key));
        match first_name_index {
            Some(index) => self.values.get(index).cloned(),
            _ => None,
        }
    }

    pub fn get(&self, index: usize) -> Option<Expr> {
        if index < self.values.len() {
            Some(self.values[index].clone())
        } else {
            None
        }
    }

    pub fn pop(&mut self) -> Option<(Option<String>, Expr)> {
        if let Some(k) = self.keys.pop() {
            if let Some(v) = self.values.pop() {
                return Some((k, v));
            }
        }

        None
    }

    pub fn push(&mut self, pair: (Option<String>, Expr)) {
        let (key, value) = pair;
        self.keys.push(key);
        self.values.push(value);
    }

    pub fn append(&mut self, mut other: Self) -> &mut ExprList {
        self.keys.append(&mut other.keys);
        self.values.append(&mut other.values);
        self
    }

    pub fn position_ellipsis(&self) -> Option<usize> {
        self.values
            .iter()
            .position(|i| matches!(i, Expr::Ellipsis(_)))
    }

    pub fn pop_trailing(&mut self) -> ExprList {
        if let Some(index) = self.position_ellipsis() {
            let keys_trailing = self.keys.drain(index..self.keys.len()).collect();
            let vals_trailing = self.values.drain(index..self.values.len()).collect();

            ExprList { keys: keys_trailing, values: vals_trailing }
        } else {
            ExprList::new()
        }
    }

    pub fn remove_named(&mut self, key: &str) -> Option<(Option<String>, Expr)> {
        let first_named_index = self.keys.iter().position(|i| i == &Some(key.to_string()));
        if let Some(index) = first_named_index {
            Some((self.keys.remove(index), self.values.remove(index)))
        } else {
            None
        }
    }

    pub fn remove(&mut self, index: usize) -> Option<(Option<String>, Expr)> {
        if index < self.keys.len() {
            Some((self.keys.remove(index), self.values.remove(index)))
        } else {
            None
        }
    }

    pub fn insert_named(&mut self, key: String, value: Expr) -> usize {
        if let Some(index) = self.keys.iter().position(|i| i == &Some(key.clone())) {
            self.values[index] = value;
            index
        } else {
            self.keys.push(Some(key.to_string()));
            self.values.push(value);
            self.values.len()
        }
    }

    pub fn insert(&mut self, index: usize, value: Expr) -> usize {
        if index < self.values.len() {
            self.keys.insert(index, None);
            self.values.insert(index, value);
            index
        } else {
            let n = index - self.values.len();
            self.keys.extend(vec![None; n + 1]);
            self.values.extend(vec![Expr::Null; n]);
            self.values.push(value);
            index
        }
    }

    pub fn binary_args(self) -> ((Option<String>, Expr), (Option<String>, Expr)) {
        let mut argstream = self.into_iter();
        let Some(lhs) = argstream.next() else {
            unimplemented!()
        };

        let Some(rhs) = argstream.next() else {
            unimplemented!()
        };

        (lhs, rhs)
    }

    pub fn unnamed_binary_args(self) -> (Expr, Expr) {
        let mut argstream = self.into_iter();
        let Some((_, lhs)) = argstream.next() else {
            unimplemented!()
        };

        let Some((_, rhs)) = argstream.next() else {
            unimplemented!()
        };

        (lhs, rhs)
    }

    pub fn unnamed_unary_arg(self) -> Expr {
        let mut argstream = self.into_iter();
        let Some((_, lhs)) = argstream.next() else {
            unimplemented!()
        };

        lhs
    }

    /// Converts unnamed value symbols into missing named parameters
    pub fn as_formals(self) -> ExprList {
        self.into_iter()
            .map(|(k, v)| match (k, v) {
                (None, Expr::Symbol(param)) => (Some(param.clone()), Expr::Missing),
                other => other,
            })
            .collect()
    }

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

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl From<Vec<Expr>> for ExprList {
    fn from(values: Vec<Expr>) -> Self {
        ExprList { keys: vec![None; values.len()], values }
    }
}

impl From<Expr> for ExprList {
    fn from(value: Expr) -> Self {
        ExprList { keys: vec![None], values: vec![value] }
    }
}

impl From<Vec<(Option<String>, Expr)>> for ExprList {
    fn from(values: Vec<(Option<String>, Expr)>) -> Self {
        ExprList::from_iter(values)
    }
}