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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright (c) 2019 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

use std::collections::HashMap;
use std::rc::Rc;
use std::result::Result;

use super::obj::{self, Name, Obj, Pair};
use super::parse::parse_stmt;

/// Ref is a native object stored in an Obj.
pub struct Ref {
    name: &'static str,
}

impl ToString for Ref {
    fn to_string(&self) -> String {
        let mut s = String::with_capacity(self.name.len() + 11);
        s.push_str("<function ");
        s.push_str(self.name);
        s.push('>');
        return s;
    }
}

/// Fun is an extension function object.
pub trait Fun {
    fn invoke(&self, list: &Obj) -> Result<Obj, String>;
}

/// FunMut is an extension function object with side-effects.
pub trait FunMut {
    fn invoke(&mut self, list: &Obj) -> Result<Obj, String>;
}

pub(crate) enum FnImpl<'f> {
    Eval(fn(&mut Frame, &Obj) -> Result<Obj, String>),
    Fn(fn(&Obj) -> Result<Obj, String>),
    Fun(&'f Fun),
    FunMut(&'f mut FunMut),
}

pub(crate) struct FnEntry<'f> {
    pub imp: FnImpl<'f>,
    obj: Obj,
}

/// Domain of extension functions.
pub struct Domain<'f> {
    entries: HashMap<&'static str, FnEntry<'f>>,
}

impl<'f> Domain<'f> {
    pub fn new() -> Self {
        Domain {
            entries: HashMap::new(),
        }
    }

    pub(crate) fn register_eval(
        &mut self,
        name: &'static str,
        f: fn(&mut Frame, &Obj) -> Result<Obj, String>,
    ) {
        self.entries.insert(
            name,
            FnEntry {
                imp: FnImpl::Eval(f),
                obj: Rc::new(Ref { name: name }),
            },
        );
    }

    pub fn register(&mut self, name: &'static str, f: fn(&Obj) -> Result<Obj, String>) {
        self.entries.insert(
            name,
            FnEntry {
                imp: FnImpl::Fn(f),
                obj: Rc::new(Ref { name: name }),
            },
        );
    }

    pub fn register_fun(&mut self, name: &'static str, f: &'f Fun) {
        self.entries.insert(
            name,
            FnEntry {
                imp: FnImpl::Fun(f),
                obj: Rc::new(Ref { name: name }),
            },
        );
    }

    pub fn register_fun_mut(&mut self, name: &'static str, f: &'f mut FunMut) {
        self.entries.insert(
            name,
            FnEntry {
                imp: FnImpl::FunMut(f),
                obj: Rc::new(Ref { name: name }),
            },
        );
    }
}

#[derive(Clone)]
pub struct Binding {
    pub name: String,
    pub value: Obj,
}

/// Incrementally constructed state.
#[derive(Clone)]
pub struct State {
    pub env: Obj,
    pub result: Binding,
}

impl State {
    pub fn new() -> Self {
        let nil = obj::nil();

        State {
            env: obj::pair(
                obj::pair(obj::name("_".to_string()), nil.clone()),
                nil.clone(),
            ),
            result: Binding {
                name: "".to_string(),
                value: nil,
            },
        }
    }
}

pub(crate) struct Frame<'m, 'f> {
    pub domain: &'m mut Domain<'f>,
    pub env: Obj,
}

impl<'m, 'f> Frame<'m, 'f> {
    pub fn lookup_ref(&mut self, fref: &Ref) -> Option<&mut FnEntry<'f>> {
        self.domain.entries.get_mut(fref.name)
    }
}

/// Parse and evaluate a statement.  A derived state with the result value is
/// returned.
pub fn eval_stmt<'m>(domain: &'m mut Domain, state: State, s: &str) -> Result<State, String> {
    if s.trim_start().len() == 0 {
        return Ok(State {
            env: state.env,
            result: Binding {
                name: "".to_string(),
                value: obj::nil(),
            },
        });
    }

    let (stmt, paren) = parse_stmt(s)?;

    let mut var = "_".to_string();

    let mut frame = Frame {
        domain: domain,
        env: state.env.clone(),
    };

    let value = if paren {
        eval_expr(&mut frame, &stmt)
    } else {
        let head = stmt.downcast_ref::<Pair>().unwrap();

        if let Some(name) = head.0.downcast_ref::<Name>() {
            if name.0.starts_with("!") {
                if name.0.len() == 1 {
                    var = choose_name(&frame);
                } else {
                    var = name.0[1..].to_string();
                }

                if let Some(tail) = head.1.downcast_ref::<Pair>() {
                    // Evaluate "!var (exp)" as "!var exp", but
                    // evaluate "!var (e) x" as it is.
                    if tail.0.is::<Pair>() && tail.1.is::<()>() {
                        eval_expr(&mut frame, &tail.0)
                    } else {
                        eval_toplevel_expr(&mut frame, &head.1)
                    }
                } else {
                    // End of list.
                    eval_name(&mut frame, "_")
                }
            } else {
                eval_toplevel_expr(&mut frame, &stmt)
            }
        } else {
            eval_toplevel_expr(&mut frame, &stmt)
        }
    }?;

    let mut new = obj::pair(
        obj::pair(obj::name(var.to_string()), value.clone()),
        cdr(&state.env).clone(), // Skip innermost layer with old _ value.
    );

    if var != "_" {
        // Bubble old _ value up to innermost new layer.
        new = obj::pair(car(&state.env).clone(), new);
    }

    Ok(State {
        env: new,
        result: Binding {
            name: var.to_string(),
            value: value,
        },
    })
}

fn eval_toplevel_expr(frame: &mut Frame, list_obj: &Obj) -> Result<Obj, String> {
    if let Some(result) = eval_call(frame, list_obj.downcast_ref::<Pair>().unwrap()) {
        result
    } else {
        let list_obj = eval_args(frame, list_obj)?;
        let list = list_obj.downcast_ref::<Pair>().unwrap();
        if list.1.is::<()>() {
            Ok(list.0.clone()) // Convert (x) to x.
        } else {
            Ok(list_obj)
        }
    }
}

pub(crate) fn eval_expr(frame: &mut Frame, expr: &Obj) -> Result<Obj, String> {
    if let Some(pair) = expr.downcast_ref::<Pair>() {
        match eval_call(frame, pair) {
            Some(result) => result,
            None => expected_function(),
        }
    } else if let Some(name) = expr.downcast_ref::<Name>() {
        eval_name(frame, &name.0)
    } else {
        Ok(expr.clone())
    }
}

fn eval_call(frame: &mut Frame, list: &Pair) -> Option<Result<Obj, String>> {
    match eval_expr(frame, &list.0) {
        Ok(x) => {
            let fref = x.downcast_ref::<Ref>()?;
            Some(if let Some(entry) = frame.domain.entries.get(fref.name) {
                if let FnImpl::Eval(f) = entry.imp {
                    f(frame, &list.1)
                } else {
                    match eval_args(frame, &list.1) {
                        Ok(args) => match frame.lookup_ref(fref).unwrap().imp {
                            FnImpl::Fn(f) => f(&args),
                            FnImpl::Fun(ref f) => f.invoke(&args),
                            FnImpl::FunMut(ref mut f) => f.invoke(&args),
                            _ => panic!(),
                        },

                        Err(msg) => Err(msg),
                    }
                }
            } else {
                missing_function()
            })
        }

        Err(msg) => Some(Err(msg)),
    }
}

fn eval_args(frame: &mut Frame, list: &Obj) -> Result<Obj, String> {
    Ok(if let Some(pair) = list.downcast_ref::<Pair>() {
        let car = eval_expr(frame, &pair.0)?;
        let cdr = eval_args(frame, &pair.1)?;
        obj::pair(car, cdr)
    } else {
        list.clone() // It must be nil.
    })
}

fn eval_name(frame: &Frame, s: &str) -> Result<Obj, String> {
    let mut level = &frame.env.clone();

    loop {
        let binding = car(level).clone();
        if car(&binding).downcast_ref::<Name>().unwrap().0 == s {
            return Ok(cdr(&binding).clone());
        }

        level = cdr(&level);
        if level.is::<()>() {
            break;
        }
    }

    if let Some(x) = frame.domain.entries.get(s) {
        return Ok(x.obj.clone());
    }

    Err(s.to_string())
}

fn choose_name<'m, 'f>(frame: &Frame<'m, 'f>) -> String {
    for i in 1.. {
        let mut s = String::new();
        s.push('$');
        s.push_str(&i.to_string());
        if !eval_name(frame, &s).is_ok() {
            return s;
        }
    }

    panic!();
}

pub(crate) fn expected_function() -> Result<Obj, String> {
    Err("not a function".to_string())
}

pub(crate) fn missing_function() -> Result<Obj, String> {
    Err("function implementation is missing".to_string())
}

fn car(pair: &Obj) -> &Obj {
    &pair.downcast_ref::<Pair>().unwrap().0
}

fn cdr(pair: &Obj) -> &Obj {
    &pair.downcast_ref::<Pair>().unwrap().1
}

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

    fn eval<'m>(s: &str, d: &'m mut Domain) -> Obj {
        eval_stmt(d, State::new(), s).unwrap().result.value.clone()
    }

    struct Id;

    impl Fun for Id {
        fn invoke(&self, args: &Obj) -> Result<Obj, String> {
            if let Some(pair) = args.downcast_ref::<Pair>() {
                if pair.1.is::<()>() {
                    return Ok(pair.0.clone());
                }
            }

            Err("id: wrong number of arguments".to_string())
        }
    }

    struct Greet {
        greetings: i32,
    }

    impl FunMut for Greet {
        fn invoke(&mut self, args: &Obj) -> Result<Obj, String> {
            if let Some(pair) = args.downcast_ref::<Pair>() {
                if let Some(b) = pair.0.downcast_ref::<bool>() {
                    if *b {
                        self.greetings += 1;
                        return Ok(obj::string("hello, world".to_string()));
                    }
                }
            }

            Ok(obj::nil())
        }
    }

    #[test]
    fn test_eval_stmt() {
        let id = Id {};
        let mut greet1 = Greet { greetings: 0 };
        let mut greet2 = Greet { greetings: 0 };
        let mut d = Domain::new();
        d.register_fun("id", &id);
        d.register_fun_mut("greet-1", &mut greet1);
        d.register_fun_mut("greet-2", &mut greet2);

        assert_eq!(*eval("id 123", &mut d).downcast_ref::<i64>().unwrap(), 123);

        assert_eq!(
            *eval("(greet-1 true)", &mut d)
                .downcast_ref::<String>()
                .unwrap(),
            "hello, world"
        );

        assert_eq!(
            *eval("id (id -123)", &mut d).downcast_ref::<i64>().unwrap(),
            -123
        );

        eval("(greet-2 true)", &mut d);
        eval("(greet-2 true)", &mut d);
        eval("(greet-2 false)", &mut d);
        assert_eq!(greet1.greetings, 1);
        assert_eq!(greet2.greetings, 2);
    }

    #[test]
    fn test_eval_list() {
        let r = eval("1", &mut Domain::new());
        assert_eq!(*r.downcast_ref::<i64>().unwrap(), 1);

        let r = eval("2 3", &mut Domain::new());
        let head = r.downcast_ref::<Pair>().unwrap();
        assert_eq!(*head.0.downcast_ref::<i64>().unwrap(), 2);
        let tail = head.1.downcast_ref::<Pair>().unwrap();
        assert_eq!(*tail.0.downcast_ref::<i64>().unwrap(), 3);
        tail.1.downcast_ref::<()>().unwrap();

        let r = eval("4 5 6", &mut Domain::new());
        let head = r.downcast_ref::<Pair>().unwrap();
        assert_eq!(*head.0.downcast_ref::<i64>().unwrap(), 4);
        let mid = head.1.downcast_ref::<Pair>().unwrap();
        assert_eq!(*mid.0.downcast_ref::<i64>().unwrap(), 5);
        let tail = mid.1.downcast_ref::<Pair>().unwrap();
        assert_eq!(*tail.0.downcast_ref::<i64>().unwrap(), 6);
        tail.1.downcast_ref::<()>().unwrap();
    }

    #[test]
    fn test_state() {
        let id = Id {};
        let mut d = Domain::new();
        d.register_fun("id", &id);

        let s = State::new();

        let s = eval_stmt(&mut d, s, "!x id true").unwrap();
        let s = eval_stmt(&mut d, s, "id x").unwrap();
        assert_eq!(s.result.value.downcast_ref::<bool>().unwrap().clone(), true);
        let s = eval_stmt(&mut d, s, "x").unwrap();
        assert_eq!(s.result.value.downcast_ref::<bool>().unwrap().clone(), true);
        let s = eval_stmt(&mut d, s, "id _").unwrap();
        assert_eq!(s.result.value.downcast_ref::<bool>().unwrap().clone(), true);

        let s = eval_stmt(&mut d, s, "id 123").unwrap();
        let s = eval_stmt(&mut d, s, "id _").unwrap();
        assert_eq!(s.result.value.downcast_ref::<i64>().unwrap().clone(), 123);
        let s = eval_stmt(&mut d, s, "!y").unwrap();
        let s = eval_stmt(&mut d, s, "id y").unwrap();
        assert_eq!(s.result.value.downcast_ref::<i64>().unwrap().clone(), 123);

        let s = eval_stmt(&mut d, s, r#"id "abc""#).unwrap();
        let s = eval_stmt(&mut d, s, "!").unwrap();
        let s = eval_stmt(&mut d, s, "id $1").unwrap();
        assert_eq!(
            s.result.value.downcast_ref::<String>().unwrap().clone(),
            "abc"
        );

        let s = eval_stmt(&mut d, s, "id x").unwrap();
        assert_eq!(s.result.value.downcast_ref::<bool>().unwrap().clone(), true);

        let s = eval_stmt(&mut d, s, "id y").unwrap();
        assert_eq!(s.result.value.downcast_ref::<i64>().unwrap().clone(), 123);

        let s = eval_stmt(&mut d, s, "id $1").unwrap();
        assert_eq!(
            s.result.value.downcast_ref::<String>().unwrap().clone(),
            "abc"
        );

        assert!(eval_stmt(&mut d, s.clone(), "(!z id false)").is_err());

        let s = eval_stmt(&mut d, s, "!z id false").unwrap();
        let s = eval_stmt(&mut d, s, "(id z)").unwrap();
        assert_eq!(
            s.result.value.downcast_ref::<bool>().unwrap().clone(),
            false
        );
        let s = eval_stmt(&mut d, s, "(id z)").unwrap();
        let s = eval_stmt(&mut d, s, "(id _)").unwrap();
        assert_eq!(
            s.result.value.downcast_ref::<bool>().unwrap().clone(),
            false
        );

        let s = eval_stmt(&mut d, s, "!-- id (id 555)").unwrap();
        let s = eval_stmt(&mut d, s, "id --").unwrap();
        assert_eq!(s.result.value.downcast_ref::<i64>().unwrap().clone(), 555);

        let s = eval_stmt(&mut d, s, "!$3 id 3").unwrap();
        let s = eval_stmt(&mut d, s, "id $3").unwrap();
        assert_eq!(s.result.value.downcast_ref::<i64>().unwrap().clone(), 3);

        let s = eval_stmt(&mut d, s, "! id 2").unwrap();
        let s = eval_stmt(&mut d, s, "id $2").unwrap();
        assert_eq!(s.result.value.downcast_ref::<i64>().unwrap().clone(), 2);

        let s = eval_stmt(&mut d, s, "! id 4").unwrap();
        let s = eval_stmt(&mut d, s, "id $4").unwrap();
        assert_eq!(s.result.value.downcast_ref::<i64>().unwrap().clone(), 4);
    }
}