poco-scheme 0.0.1

A toy Scheme implementation
Documentation
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use std::{fmt, iter, rc::Rc};

use lexpr::sexp;
use log::debug;

use crate::{util::ShowSlice, Value};

macro_rules! syntax_error {
    ($fmt:literal) => { SyntaxError::Message($fmt.into()) };
    ($fmt:literal, $($args:expr),*) => { SyntaxError::Message(format!($fmt, $($args),*)) }
}

#[derive(Debug)]
pub enum Params {
    Any(Box<str>),
    Exact(Vec<Box<str>>),
    AtLeast(Vec<Box<str>>, Box<str>),
}

#[derive(Debug)]
struct EnvFrame {
    idents: Vec<Box<str>>,
    rec_bodies: Vec<lexpr::Value>,
}

impl EnvFrame {
    fn new(idents: Vec<Box<str>>) -> Self {
        EnvFrame {
            idents,
            rec_bodies: Vec::new(),
        }
    }
}

#[derive(Default, Debug, Clone)]
pub struct EnvIndex(usize, usize);

impl EnvIndex {
    pub fn level(&self) -> usize {
        self.0
    }

    pub fn slot(&self) -> usize {
        self.1
    }
}

#[derive(Debug)]
pub struct EnvStack {
    frames: Vec<EnvFrame>,
}

impl EnvStack {
    pub fn initial<T>(idents: impl IntoIterator<Item = T>) -> Self
    where
        T: Into<Box<str>>,
    {
        EnvStack {
            frames: vec![EnvFrame {
                idents: idents.into_iter().map(Into::into).collect(),
                rec_bodies: Vec::new(),
            }],
        }
    }

    pub fn is_empty(&self) -> bool {
        self.frames.is_empty()
    }

    pub fn with_pushed<T, F>(&mut self, params: &Params, f: F) -> Result<(Vec<Ast>, T), SyntaxError>
    where
        F: FnOnce(&mut Self) -> Result<T, SyntaxError>,
    {
        match params {
            Params::Any(ident) => self.frames.push(EnvFrame::new(vec![ident.clone()])),
            Params::Exact(idents) => self.frames.push(EnvFrame::new(idents.to_vec())),
            Params::AtLeast(idents, rest) => {
                let mut idents = idents.to_vec();
                idents.push(rest.clone());
                self.frames.push(EnvFrame::new(idents));
            }
        }
        debug!("extended env-stack {:?}", self);
        match f(self) {
            Ok(v) => {
                let bodies = self.reap_rec_bodies()?;
                self.pop();
                debug!("done with extended env-stack {:?} -> {:?}", self, bodies);
                Ok((bodies, v))
            }
            Err(e) => {
                self.pop();
                Err(e)
            }
        }
    }

    fn pop(&mut self) {
        assert!(self.frames.len() > 1);
        self.frames.pop().unwrap();
    }

    pub fn lookup(&self, name: &str) -> Option<EnvIndex> {
        for (level, frame) in self.frames.iter().rev().enumerate() {
            if let Some(i) = frame.idents.iter().position(|ident| ident.as_ref() == name) {
                return Some(EnvIndex(level, i));
            }
        }
        None
    }

    pub fn bind_rec(&mut self, name: &str, body: lexpr::Value) {
        let last = self.frames.len() - 1;
        self.frames[last].idents.push(name.into());
        self.frames[last].rec_bodies.push(body);
        debug!("bound {} recursively -> {:?}", name, self);
    }

    fn last_frame_mut(&mut self) -> &mut EnvFrame {
        let last = self.frames.len() - 1;
        &mut self.frames[last]
    }

    pub fn reap_rec_bodies(&mut self) -> Result<Vec<Ast>, SyntaxError> {
        let bodies = self.last_frame_mut().rec_bodies.split_off(0);
        bodies
            .into_iter()
            .map(|b| Ok(Ast::expr(&b, self, NonTail)?))
            .collect::<Result<_, _>>()
    }
}

impl Params {
    pub fn new(v: &lexpr::Value) -> Result<Self, SyntaxError> {
        use lexpr::Value::*;
        match v {
            Null => Ok(Params::Exact(vec![])),
            Cons(cell) => match cell.to_ref_vec() {
                (params, Null) => Ok(Params::Exact(param_list(&params)?)),
                (params, rest) => Ok(Params::AtLeast(param_list(&params)?, param_rest(rest)?)),
            },
            _ => Ok(Params::Any(param_rest(v)?)),
        }
    }

    pub fn env_slots(&self) -> usize {
        match self {
            Params::Any(_) => 1,
            Params::Exact(names) => names.len(),
            Params::AtLeast(names, _) => names.len() + 1,
        }
    }

    /// Form the values for argument vector
    pub fn values(&self, args: Vec<Value>) -> Result<Vec<Value>, Value> {
        match self {
            Params::Any(_) => Ok(vec![Value::list(args)]),
            Params::Exact(names) => {
                if names.len() != args.len() {
                    Err(make_error!(
                        "parameter length mismatch; got ({}), expected ({})",
                        ShowSlice(&args),
                        ShowSlice(names)
                    ))
                } else {
                    Ok(args)
                }
            }
            Params::AtLeast(names, _) => {
                if names.len() > args.len() {
                    Err(make_error!(
                        "too few parameters; got ({}), expected ({})",
                        ShowSlice(&args),
                        ShowSlice(names)
                    ))
                } else {
                    let (named, rest) = args.split_at(names.len());
                    let values = named
                        .iter()
                        .cloned()
                        .chain(iter::once(Value::list(rest.iter().cloned())))
                        .collect();
                    Ok(values)
                }
            }
        }
    }
}

fn param_list(params: &[&lexpr::Value]) -> Result<Vec<Box<str>>, SyntaxError> {
    params
        .iter()
        .map(|p| {
            p.as_symbol()
                .ok_or(SyntaxError::ExpectedSymbol)
                .map(Into::into)
        })
        .collect()
}

fn param_rest(rest: &lexpr::Value) -> Result<Box<str>, SyntaxError> {
    rest.as_symbol()
        .ok_or(SyntaxError::ExpectedSymbol)
        .map(Into::into)
}

#[derive(Debug)]
pub enum Ast {
    Datum(lexpr::Value),
    Lambda(Rc<Lambda>),
    If(Box<Conditional>),
    Apply(Box<Application>),
    TailCall(Box<Application>),
    EnvRef(EnvIndex),
    Seq(Vec<Ast>, Rc<Ast>),
    Bind(Body),
}

#[derive(Debug)]
pub struct Application {
    pub op: Ast,
    pub operands: Vec<Ast>,
}

#[derive(Debug)]
pub struct Conditional {
    pub cond: Ast,
    pub consequent: Rc<Ast>,
    pub alternative: Rc<Ast>,
}

#[derive(Debug)]
pub struct Lambda {
    pub params: Params,
    pub body: Body,
}

#[derive(Debug)]
pub struct Body {
    pub bound_exprs: Vec<Ast>,
    pub expr: Rc<Ast>,
}

impl Body {
    fn new(bound_exprs: Vec<Ast>, expr: Ast) -> Self {
        Body {
            bound_exprs,
            expr: Rc::new(expr),
        }
    }
}

impl Lambda {
    fn new(
        params: Params,
        exprs: &[&lexpr::Value],
        stack: &mut EnvStack,
    ) -> Result<Self, SyntaxError> {
        let (bound_exprs, body_exprs) =
            stack.with_pushed(&params, |stack| -> Result<_, SyntaxError> {
                let mut body_exprs = Vec::with_capacity(exprs.len());
                let mut definitions = true;
                for (i, expr) in exprs.iter().enumerate() {
                    let tail = if i + 1 == exprs.len() { Tail } else { NonTail };
                    if definitions {
                        if let Some(ast) = Ast::definition(expr, stack, tail)? {
                            body_exprs.push(ast);
                            definitions = false;
                        }
                    } else {
                        body_exprs.push(Ast::expr(expr, stack, tail)?);
                    }
                }
                Ok(body_exprs)
            })?;
        Ok(Lambda {
            params,
            body: Body::new(bound_exprs, Ast::seq(body_exprs)),
        })
    }

    pub fn env_slots(&self) -> usize {
        self.params.env_slots() + self.body.bound_exprs.len()
    }

    pub fn alloc_locals(&self, args: Vec<Value>) -> Result<Vec<Value>, Value> {
        let mut locals = self.params.values(args)?;
        locals.resize(self.env_slots(), Value::Unspecified);
        Ok(locals)
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum TailPosition {
    Tail,
    NonTail,
}

use TailPosition::*;

impl Ast {
    pub fn expr(
        expr: &lexpr::Value,
        stack: &mut EnvStack,
        tail: TailPosition,
    ) -> Result<Ast, SyntaxError> {
        debug!("forming AST for {} in {:?}", expr, stack);
        match expr {
            lexpr::Value::Null => Err(SyntaxError::EmptyApplication),
            lexpr::Value::Nil
            | lexpr::Value::Char(_)
            | lexpr::Value::Keyword(_)
            | lexpr::Value::Bytes(_)
            | lexpr::Value::Vector(_) => Err(SyntaxError::UnsupportedDatum(expr.clone())),
            lexpr::Value::Bool(_) => Ok(Ast::Datum(expr.clone())),
            lexpr::Value::Number(_) => Ok(Ast::Datum(expr.clone())),
            lexpr::Value::String(_) => Ok(Ast::Datum(expr.clone())),
            lexpr::Value::Symbol(ident) => stack
                .lookup(ident)
                .map(Ast::EnvRef)
                .ok_or_else(|| SyntaxError::UnboundIdentifier(ident.to_owned())),
            lexpr::Value::Cons(cell) => {
                let (first, rest) = cell.as_pair();
                match first.as_symbol() {
                    Some("quote") => {
                        let args = proper_list(rest)?;
                        if args.len() != 1 {
                            return Err(syntax_error!("`quote' expects a single form"));
                        }
                        Ok(Ast::Datum(args[0].clone()))
                    }
                    Some("lambda") => {
                        let args = proper_list(rest)?;
                        if args.len() < 2 {
                            return Err(syntax_error!("`lambda` expects at least two forms"));
                        }
                        Ast::lambda(args[0], &args[1..], stack)
                    }
                    Some("begin") => {
                        let mut exprs = proper_list(rest)?;
                        if let Some(last_expr) = exprs.pop() {
                            let tail_ast = Ast::expr(last_expr, stack, tail)?;
                            if exprs.is_empty() {
                                Ok(tail_ast)
                            } else {
                                let seq = exprs
                                    .iter()
                                    .map(|expr| Ok(Ast::expr(expr, stack, NonTail)?))
                                    .collect::<Result<_, _>>()?;
                                Ok(Ast::Seq(seq, Rc::new(tail_ast)))
                            }
                        } else {
                            Ok(Ast::Datum(lexpr::Value::Nil))
                        }
                    }
                    Some("define") => {
                        Err(syntax_error!("`define` not allowed in expression context"))
                    }
                    Some("if") => {
                        let args = proper_list(rest)?;
                        if args.len() < 2 {
                            return Err(syntax_error!("`if` expects at least two forms"));
                        }
                        let cond = Ast::expr(&args[0], stack, NonTail)?;
                        let consequent = Ast::expr(&args[1], stack, tail)?;
                        let alternative = if args.len() == 3 {
                            Ast::expr(&args[2], stack, tail)?
                        } else if args.len() == 2 {
                            Ast::Datum(lexpr::Value::Nil)
                        } else {
                            return Err(syntax_error!(
                                "`if` expects at least no more than three forms"
                            ));
                        };
                        Ok(Ast::conditional(cond, consequent, alternative))
                    }
                    _ => {
                        let arg_exprs = proper_list(rest)?;
                        let op = Ast::expr(first, stack, NonTail)?;
                        let operands = arg_exprs
                            .into_iter()
                            .map(|arg| Ok(Ast::expr(arg, stack, NonTail)?))
                            .collect::<Result<Vec<Ast>, _>>()?;
                        if tail == Tail {
                            Ok(Ast::tail_call(op, operands))
                        } else {
                            Ok(Ast::apply(op, operands))
                        }
                    }
                }
            }
        }
    }

    pub fn definition(
        expr: &lexpr::Value,
        stack: &mut EnvStack,
        tail: TailPosition,
    ) -> Result<Option<Ast>, SyntaxError> {
        // Check for definition, return `Ok(None)` if found
        if let lexpr::Value::Cons(cell) = expr {
            let (first, rest) = cell.as_pair();
            match first.as_symbol() {
                Some("define") => {
                    let args = proper_list(rest)?;
                    if args.len() < 2 {
                        return Err(syntax_error!("`define` expects at least two forms"));
                    }
                    match args[0] {
                        lexpr::Value::Symbol(ident) => {
                            if args.len() != 2 {
                                return Err(syntax_error!(
                                    "`define` for variable expects one value form"
                                ));
                            }
                            stack.bind_rec(ident, args[1].clone()); // TODO: clone
                            Ok(None)
                        }
                        lexpr::Value::Cons(cell) => {
                            let ident = cell.car().as_symbol().ok_or_else(|| {
                                syntax_error!("invalid use of `define': non-identifier")
                            })?;
                            let body = lexpr::Value::list(args[1..].iter().map(|e| (*e).clone()));
                            let lambda = lexpr::Value::cons(
                                sexp!(lambda),
                                lexpr::Value::cons(cell.cdr().clone(), body),
                            );
                            stack.bind_rec(ident, lambda);
                            Ok(None)
                        }
                        _ => Err(syntax_error!("invalid `define' form")),
                    }
                }
                Some("begin") => {
                    // TODO: if this contains definitions /and/ expressions,
                    // the definitions are not resolved before evaluating
                    // the expressions.
                    let exprs = proper_list(rest)?;
                    // TODO: This is similar to `Lambda::new`
                    let mut body_exprs = Vec::with_capacity(exprs.len());
                    let mut definitions = true;
                    for (i, expr) in exprs.iter().enumerate() {
                        let tail = if i + 1 == exprs.len() { tail } else { NonTail };
                        if definitions {
                            if let Some(ast) = Ast::definition(expr, stack, tail)? {
                                body_exprs.push(ast);
                                definitions = false;
                            }
                        } else {
                            body_exprs.push(Ast::expr(expr, stack, tail)?)
                        }
                    }
                    if body_exprs.is_empty() {
                        return Ok(None);
                    }
                    let bodies = stack.reap_rec_bodies()?;
                    if bodies.is_empty() {
                        Ok(Some(Ast::seq(body_exprs)))
                    } else {
                        Ok(Some(Ast::Bind(Body {
                            bound_exprs: bodies,
                            expr: Rc::new(Ast::seq(body_exprs)),
                        })))
                    }
                }
                _ => Ok(Some(Ast::expr(expr, stack, tail)?)),
            }
        } else {
            // Otherwise, it must be an expression
            Ok(Some(Ast::expr(expr, stack, tail)?))
        }
    }

    fn lambda(
        params: &lexpr::Value,
        body: &[&lexpr::Value],
        stack: &mut EnvStack,
    ) -> Result<Self, SyntaxError> {
        let params = Params::new(params)?;
        Ok(Ast::Lambda(Rc::new(Lambda::new(params, body, stack)?)))
    }

    fn tail_call(op: Ast, operands: Vec<Ast>) -> Self {
        Ast::TailCall(Box::new(Application { op, operands }))
    }

    fn apply(op: Ast, operands: Vec<Ast>) -> Self {
        Ast::Apply(Box::new(Application { op, operands }))
    }

    fn conditional(cond: Ast, consequent: Ast, alternative: Ast) -> Self {
        Ast::If(Box::new(Conditional {
            cond,
            consequent: consequent.into(),
            alternative: alternative.into(),
        }))
    }

    fn seq(mut seq: Vec<Ast>) -> Self {
        if let Some(last) = seq.pop() {
            if seq.is_empty() {
                last
            } else {
                Ast::Seq(seq, Rc::new(last))
            }
        } else {
            Ast::Datum(lexpr::Value::Nil)
        }
    }
}

#[derive(Debug)]
pub enum SyntaxError {
    EmptyApplication,
    UnsupportedDatum(lexpr::Value),
    ExpectedSymbol,
    ImproperList(Vec<lexpr::Value>, lexpr::Value),
    NonList(lexpr::Value),
    Message(String),
    UnboundIdentifier(Box<str>),
}

impl fmt::Display for SyntaxError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use SyntaxError::*;
        match self {
            EmptyApplication => write!(f, "empty application"),
            UnsupportedDatum(datum) => write!(f, "unsupported datum `{}`", datum),
            ExpectedSymbol => write!(f, "expected symbol"),
            ImproperList(elts, tail) => {
                write!(f, "improper list `({} . {})'", ShowSlice(&elts), tail)
            }
            NonList(value) => write!(f, "non-list `{}'", value),
            Message(msg) => write!(f, "{}", msg),
            UnboundIdentifier(ident) => write!(f, "unbound identifier `{}`", ident),
        }
    }
}

impl std::error::Error for SyntaxError {}

fn proper_list(expr: &lexpr::Value) -> Result<Vec<&lexpr::Value>, SyntaxError> {
    match expr {
        lexpr::Value::Cons(cell) => match cell.to_ref_vec() {
            (args, tail) => {
                if tail != &lexpr::Value::Null {
                    Err(SyntaxError::ImproperList(
                        args.into_iter().cloned().collect(),
                        tail.clone(),
                    ))
                } else {
                    Ok(args)
                }
            }
        },
        lexpr::Value::Null => Ok(Vec::new()),
        value => Err(SyntaxError::NonList(value.clone())),
    }
}