blisp 0.4.7

A lisp like statically typed programing language for no_std.
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
use crate::{parser::Expr, Pos};
use alloc::{
    collections::{btree_map::Entry, BTreeMap, LinkedList},
    format,
    string::String,
};

#[derive(Debug)]
pub struct MacroErr {
    pub pos: Pos,
    pub msg: &'static str,
}

/// `e1` is a pattern and `e2` is an expression to be matched.
pub fn match_pattern(e1: &Expr, e2: &Expr, ctx: &mut BTreeMap<String, LinkedList<Expr>>) -> bool {
    match (e1, e2) {
        (Expr::ID(left, _), _) => {
            if let Some('$') = left.chars().next() {
                // If `e1` is `$id`, then a map from `$id` to `e1` is added to `ctx`.
                let entry = ctx.entry(left.clone());
                match entry {
                    Entry::Vacant(ent) => {
                        let mut list = LinkedList::new();
                        list.push_back(e2.clone());
                        ent.insert(list);

                        true
                    }
                    Entry::Occupied(ent) => {
                        let exprs = ent.get();
                        if exprs.len() != 1 {
                            false
                        } else {
                            eq_expr(exprs.front().unwrap(), e2)
                        }
                    }
                }
            } else if left == "_" {
                true
            } else {
                matches!(e2, Expr::ID(right, _) if left == right)
            }
        }
        (Expr::Bool(left, _), Expr::Bool(right, _)) => left == right,
        (Expr::Char(left, _), Expr::Char(right, _)) => left == right,
        (Expr::Num(left, _), Expr::Num(right, _)) => left == right,
        (Expr::Str(left, _), Expr::Str(right, _)) => left == right,
        (Expr::Tuple(left, _), Expr::Tuple(right, _)) => match_list(left, right, ctx),
        (Expr::Apply(left, _), Expr::Apply(right, _)) => match_list(left, right, ctx),
        (Expr::List(left, _), Expr::List(right, _)) => match_list(left, right, ctx),
        _ => false,
    }
}

pub fn match_list(
    left: &LinkedList<Expr>,
    right: &LinkedList<Expr>,
    ctx: &mut BTreeMap<String, LinkedList<Expr>>,
) -> bool {
    let mut prev = None;
    let mut it_left = left.iter();
    let mut it_right = right.iter();

    loop {
        match (it_left.next(), it_right.next()) {
            (Some(e1), Some(e2)) => {
                if let Expr::ID(id, _) = e1 {
                    if id == "..." {
                        if let Some(key) = &prev {
                            let Some(exprs) = ctx.get_mut(key) else {
                                return false;
                            };
                            exprs.push_back(e2.clone());
                            break;
                        }
                    } else {
                        prev = Some(id.clone());
                    }
                }

                if !match_pattern(e1, e2, ctx) {
                    return false;
                }
            }
            (Some(e1), None) => {
                if let Expr::ID(id, _) = e1 {
                    return id == "...";
                } else {
                    return false;
                }
            }
            (None, Some(_)) => return false,
            _ => return true,
        }
    }

    let key = prev.unwrap();
    let exprs = ctx.get_mut(&key).unwrap();
    for expr in it_right {
        exprs.push_back(expr.clone());
    }

    true
}

fn eq_expr(e1: &Expr, e2: &Expr) -> bool {
    match (e1, e2) {
        (Expr::ID(left, _), Expr::ID(right, _)) => left == right,
        (Expr::Bool(left, _), Expr::Bool(right, _)) => left == right,
        (Expr::Char(left, _), Expr::Char(right, _)) => left == right,
        (Expr::Num(left, _), Expr::Num(right, _)) => left == right,
        (Expr::Str(left, _), Expr::Str(right, _)) => left == right,
        (Expr::Tuple(left, _), Expr::Tuple(right, _)) => eq_exprs(left, right),
        (Expr::Apply(left, _), Expr::Apply(right, _)) => eq_exprs(left, right),
        (Expr::List(left, _), Expr::List(right, _)) => eq_exprs(left, right),
        _ => false,
    }
}

fn eq_exprs(es1: &LinkedList<Expr>, es2: &LinkedList<Expr>) -> bool {
    if es1.len() != es2.len() {
        return false;
    }

    es1.iter().zip(es2.iter()).all(|(e1, e2)| eq_expr(e1, e2))
}

pub(crate) fn process_macros(exprs: &mut LinkedList<Expr>) -> Result<Macros, MacroErr> {
    let macros = parse_macros(exprs)?;
    let mut expander = MacroExpander::new(&macros);

    for expr in exprs.iter_mut() {
        expander.apply_macros(expr)?;
    }

    Ok(macros)
}

pub(crate) fn apply(expr: &mut Expr, macros: &Macros) -> Result<(), MacroErr> {
    MacroExpander::new(macros).apply_macros(expr)
}

struct MacroExpander<'a> {
    macros: &'a Macros,
    fresh_counter: u64,
}

impl<'a> MacroExpander<'a> {
    fn new(macros: &'a Macros) -> MacroExpander<'a> {
        MacroExpander {
            macros,
            fresh_counter: 0,
        }
    }

    fn apply_macros(&mut self, expr: &mut Expr) -> Result<(), MacroErr> {
        if let Expr::Apply(exprs, _) = expr {
            if let Some(Expr::ID(id, _)) = exprs.front() {
                if id == "macro" {
                    return Ok(());
                }
            }
        }

        self.apply_macros_recursively(expr, 0)
    }

    fn apply_macros_expr(
        &mut self,
        pos: Pos,
        expr: &Expr,
        count: u8,
    ) -> Result<Option<Expr>, MacroErr> {
        if count == 0xff {
            return Err(MacroErr {
                pos,
                msg: "too deep macro",
            });
        }

        for (_, rules) in self.macros.iter() {
            for rule in rules.iter() {
                let mut ctx = BTreeMap::new();
                if match_pattern(&rule.pattern, expr, &mut ctx) {
                    let expr = self.expand(pos, &rule.template, &ctx).pop_front().unwrap();

                    if let Some(e) = self.apply_macros_expr(pos, &expr, count + 1)? {
                        return Ok(Some(e));
                    } else {
                        return Ok(Some(expr));
                    }
                }
            }
        }

        Ok(None)
    }

    fn apply_macros_recursively(&mut self, expr: &mut Expr, count: u8) -> Result<(), MacroErr> {
        if count == 0xff {
            panic!("{}: too deep macro", expr.get_pos());
        }

        if let Some(e) = self.apply_macros_expr(expr.get_pos(), expr, count)? {
            *expr = e;
        }

        match expr {
            Expr::Apply(exprs, _) | Expr::List(exprs, _) | Expr::Tuple(exprs, _) => {
                for expr in exprs {
                    self.apply_macros_recursively(expr, count + 1)?;
                }
            }
            _ => (),
        }

        Ok(())
    }

    fn expand(
        &mut self,
        pos: Pos,
        template: &Expr,
        ctx: &BTreeMap<String, LinkedList<Expr>>,
    ) -> LinkedList<Expr> {
        let template = self.freshen_template(template);
        expand_expr(pos, &template, ctx)
    }

    fn freshen_template(&mut self, template: &Expr) -> Expr {
        let mut env = BTreeMap::new();
        self.rename_expr(template, &mut env)
    }

    fn rename_expr(&mut self, expr: &Expr, env: &mut BTreeMap<String, String>) -> Expr {
        match expr {
            Expr::ID(id, pos) => {
                if let Some(renamed) = env.get(id) {
                    Expr::ID(renamed.clone(), *pos)
                } else {
                    expr.clone()
                }
            }
            Expr::Bool(_, _) | Expr::Char(_, _) | Expr::Num(_, _) | Expr::Str(_, _) => expr.clone(),
            Expr::List(exprs, pos) => Expr::List(self.rename_exprs(exprs, env), *pos),
            Expr::Tuple(exprs, pos) => Expr::Tuple(self.rename_exprs(exprs, env), *pos),
            Expr::Apply(exprs, pos) => {
                let Some(head) = exprs.front() else {
                    return expr.clone();
                };

                match head {
                    Expr::ID(id, _) if id == "lambda" => self.rename_lambda(exprs, *pos, env),
                    Expr::ID(id, _) if id == "let" => self.rename_let(exprs, *pos, env),
                    Expr::ID(id, _) if id == "match" => self.rename_match(exprs, *pos, env),
                    _ => Expr::Apply(self.rename_exprs(exprs, env), *pos),
                }
            }
        }
    }

    fn rename_exprs(
        &mut self,
        exprs: &LinkedList<Expr>,
        env: &mut BTreeMap<String, String>,
    ) -> LinkedList<Expr> {
        let mut result = LinkedList::new();

        for expr in exprs {
            result.push_back(self.rename_expr(expr, env));
        }

        result
    }

    fn rename_lambda(
        &mut self,
        exprs: &LinkedList<Expr>,
        pos: Pos,
        env: &mut BTreeMap<String, String>,
    ) -> Expr {
        let mut result = LinkedList::new();
        let mut iter = exprs.iter();

        result.push_back(iter.next().unwrap().clone());

        if let Some(args) = iter.next() {
            let mut local_env = env.clone();
            result.push_back(self.rename_lambda_args(args, &mut local_env));

            for expr in iter {
                result.push_back(self.rename_expr(expr, &mut local_env));
            }
        }

        Expr::Apply(result, pos)
    }

    fn rename_lambda_args(
        &mut self,
        args: &Expr,
        env: &mut BTreeMap<String, String>,
    ) -> Expr {
        match args {
            Expr::Apply(exprs, pos) => {
                let mut renamed = LinkedList::new();
                for expr in exprs {
                    renamed.push_back(self.rename_binder(expr, env));
                }
                Expr::Apply(renamed, *pos)
            }
            _ => self.rename_expr(args, env),
        }
    }

    fn rename_let(
        &mut self,
        exprs: &LinkedList<Expr>,
        pos: Pos,
        env: &mut BTreeMap<String, String>,
    ) -> Expr {
        let mut result = LinkedList::new();
        let mut iter = exprs.iter();

        result.push_back(iter.next().unwrap().clone());

        let mut body_env = env.clone();
        if let Some(bindings) = iter.next() {
            result.push_back(self.rename_let_bindings(bindings, env, &mut body_env));
        }

        for expr in iter {
            result.push_back(self.rename_expr(expr, &mut body_env));
        }

        Expr::Apply(result, pos)
    }

    fn rename_let_bindings(
        &mut self,
        bindings: &Expr,
        env: &mut BTreeMap<String, String>,
        body_env: &mut BTreeMap<String, String>,
    ) -> Expr {
        match bindings {
            Expr::Apply(defs, pos) => {
                let mut renamed_defs = LinkedList::new();

                for def in defs {
                    match def {
                        Expr::Apply(def_exprs, def_pos) if def_exprs.len() == 2 => {
                            let mut def_iter = def_exprs.iter();
                            let pattern = def_iter.next().unwrap();
                            let value = def_iter.next().unwrap();

                            let value = self.rename_expr(value, env);
                            let pattern = self.rename_pattern(pattern, body_env);

                            let mut renamed_def = LinkedList::new();
                            renamed_def.push_back(pattern);
                            renamed_def.push_back(value);
                            renamed_defs.push_back(Expr::Apply(renamed_def, *def_pos));
                        }
                        _ => renamed_defs.push_back(self.rename_expr(def, env)),
                    }
                }

                Expr::Apply(renamed_defs, *pos)
            }
            _ => self.rename_expr(bindings, env),
        }
    }

    fn rename_match(
        &mut self,
        exprs: &LinkedList<Expr>,
        pos: Pos,
        env: &mut BTreeMap<String, String>,
    ) -> Expr {
        let mut result = LinkedList::new();
        let mut iter = exprs.iter();

        result.push_back(iter.next().unwrap().clone());

        if let Some(cond) = iter.next() {
            result.push_back(self.rename_expr(cond, env));
        }

        for case in iter {
            result.push_back(self.rename_match_case(case, env));
        }

        Expr::Apply(result, pos)
    }

    fn rename_match_case(&mut self, case: &Expr, env: &mut BTreeMap<String, String>) -> Expr {
        match case {
            Expr::Apply(case_exprs, pos) if case_exprs.len() == 2 => {
                let mut iter = case_exprs.iter();
                let pattern = iter.next().unwrap();
                let body = iter.next().unwrap();

                let mut case_env = env.clone();
                let pattern = self.rename_pattern(pattern, &mut case_env);
                let body = self.rename_expr(body, &mut case_env);

                let mut renamed_case = LinkedList::new();
                renamed_case.push_back(pattern);
                renamed_case.push_back(body);
                Expr::Apply(renamed_case, *pos)
            }
            _ => self.rename_expr(case, env),
        }
    }

    fn rename_pattern(&mut self, pattern: &Expr, env: &mut BTreeMap<String, String>) -> Expr {
        match pattern {
            Expr::ID(_, _) => self.rename_binder(pattern, env),
            Expr::Tuple(exprs, pos) => {
                let mut renamed = LinkedList::new();
                for expr in exprs {
                    renamed.push_back(self.rename_pattern(expr, env));
                }
                Expr::Tuple(renamed, *pos)
            }
            Expr::Apply(exprs, pos) => {
                let mut renamed = LinkedList::new();
                let mut iter = exprs.iter();

                if let Some(head) = iter.next() {
                    renamed.push_back(self.rename_expr(head, env));
                }

                for expr in iter {
                    renamed.push_back(self.rename_pattern(expr, env));
                }

                Expr::Apply(renamed, *pos)
            }
            _ => pattern.clone(),
        }
    }

    fn rename_binder(&mut self, expr: &Expr, env: &mut BTreeMap<String, String>) -> Expr {
        match expr {
            Expr::ID(id, pos) => {
                if id == "_" || is_pattern_var(id) {
                    expr.clone()
                } else {
                    let fresh = self.fresh_name(id);
                    env.insert(id.clone(), fresh.clone());
                    Expr::ID(fresh, *pos)
                }
            }
            _ => self.rename_expr(expr, env),
        }
    }

    fn fresh_name(&mut self, id: &str) -> String {
        let fresh = format!("__blisp_macro_{}_{}", self.fresh_counter, id);
        self.fresh_counter += 1;
        fresh
    }
}

pub(crate) type Macros = BTreeMap<String, LinkedList<MacroRule>>;

#[derive(Debug)]
pub(crate) struct MacroRule {
    pattern: Expr,
    template: Expr,
}

fn parse_macros(exprs: &LinkedList<Expr>) -> Result<Macros, MacroErr> {
    let mut result = BTreeMap::new();

    for e in exprs.iter() {
        if let Expr::Apply(es, _) = e {
            let mut it = es.iter();

            let Some(front) = it.next() else {
                continue;
            };

            if let Expr::ID(id_macro, _) = front {
                if id_macro == "macro" {
                    let id = it.next();
                    let Some(Expr::ID(id, _)) = id else {
                        return Err(MacroErr {
                            pos: e.get_pos(),
                            msg: "invalid macro",
                        });
                    };

                    let mut rules = LinkedList::new();
                    for rule in it {
                        let Expr::Apply(rule_exprs, _) = rule else {
                            return Err(MacroErr {
                                pos: rule.get_pos(),
                                msg: "invalid macro rule",
                            });
                        };

                        if rule_exprs.len() != 2 {
                            return Err(MacroErr {
                                pos: rule.get_pos(),
                                msg: "the number of arguments of a macro rule is not 2",
                            });
                        }

                        let mut rule_it = rule_exprs.iter();

                        let mut pattern = rule_it.next().unwrap().clone();
                        if let Expr::Apply(arguments, _) = &mut pattern {
                            if let Some(Expr::ID(front, _)) = arguments.front_mut() {
                                if front == "_" {
                                    *front = id.clone();
                                } else if front != id {
                                    return Err(MacroErr {
                                        pos: pattern.get_pos(),
                                        msg: "invalid macro pattern",
                                    });
                                }
                            }

                            let template = rule_it.next().unwrap().clone();

                            rules.push_back(MacroRule { pattern, template });
                        } else {
                            return Err(MacroErr {
                                pos: pattern.get_pos(),
                                msg: "invalid macro pattern",
                            });
                        };
                    }

                    if let Entry::Vacant(entry) = result.entry(id.clone()) {
                        entry.insert(rules);
                    } else {
                        return Err(MacroErr {
                            pos: e.get_pos(),
                            msg: "multiply defined",
                        });
                    }
                }
            }
        }
    }

    Ok(result)
}

fn is_pattern_var(id: &str) -> bool {
    matches!(id.chars().next(), Some('$'))
}

fn expand_expr(pos: Pos, template: &Expr, ctx: &BTreeMap<String, LinkedList<Expr>>) -> LinkedList<Expr> {
    match template {
        Expr::ID(id, _) => {
            if let Some(exprs) = ctx.get(id) {
                let expr = exprs.front().unwrap();
                let mut result = LinkedList::new();
                result.push_back(expr.clone());
                result
            } else {
                let mut result: LinkedList<Expr> = LinkedList::new();
                result.push_back(template.clone());
                result
            }
        }
        Expr::Apply(templates, _) => {
            let exprs = expand_list(pos, templates, ctx);
            let mut result = LinkedList::new();

            result.push_back(Expr::Apply(exprs, pos));
            result
        }
        Expr::List(templates, _) => {
            let exprs = expand_list(pos, templates, ctx);
            let mut result = LinkedList::new();
            result.push_back(Expr::List(exprs, pos));
            result
        }
        Expr::Tuple(templates, _) => {
            let exprs = expand_list(pos, templates, ctx);
            let mut result = LinkedList::new();
            result.push_back(Expr::Tuple(exprs, pos));
            result
        }
        expr => {
            let mut result = LinkedList::new();
            result.push_back(expr.clone());
            result
        }
    }
}

fn expand_list(
    pos: Pos,
    templates: &LinkedList<Expr>,
    ctx: &BTreeMap<String, LinkedList<Expr>>,
) -> LinkedList<Expr> {
    let mut result = LinkedList::new();

    let mut prev = None;

    for template in templates {
        if let Expr::ID(id, _) = template {
            if id == "..." {
                if let Some(p) = &prev {
                    if let Some(exprs) = ctx.get(p) {
                        let mut it = exprs.iter();
                        let _ = it.next();

                        for expr in it {
                            result.push_back(expr.clone());
                        }
                    } else {
                        prev = None;
                    }
                } else {
                    prev = None;
                }

                continue;
            } else {
                prev = Some(id.clone());
            }
        } else {
            prev = None;
        }

        let mut exprs = expand_expr(pos, template, ctx);
        result.append(&mut exprs);
    }

    result
}