scheme-rs 0.1.0

Embedded scheme for the Rust ecosystem
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
use crate::{
    ast::{Expression, ParseContext},
    env::{Binding, Environment, Local, Scope, add_binding},
    exceptions::Exception,
    gc::{Gc, Trace},
    proc::Procedure,
    records::{Record, RecordTypeDescriptor, SchemeCompatible, rtd},
    symbols::Symbol,
    syntax::{Identifier, Span, Syntax},
    value::Value,
};
use scheme_rs_macros::{bridge, maybe_async, maybe_await, runtime_fn};
use std::{
    collections::{HashMap, HashSet},
    sync::Arc,
};

mod error {
    use crate::exceptions::{Message, SyntaxViolation};

    use super::*;

    pub(super) fn no_pattern_variables_in_template(form: &Syntax) -> Exception {
        Exception::from((
            Message::new("no pattern variables in template"),
            SyntaxViolation::new(form.clone(), None),
        ))
    }
    pub(super) fn too_few_ellipsis(sym: Symbol, form: &Syntax) -> Exception {
        Exception::from((
            Message::new(format!("too few ellipsis for pattern variable {sym}",)),
            SyntaxViolation::new(form.clone(), None),
        ))
    }

    pub(super) fn too_many_ellipsis(sym: Symbol, form: &Syntax) -> Exception {
        Exception::from((
            Message::new(format!("too many ellipsis for pattern variable {sym}")),
            SyntaxViolation::new(form.clone(), None),
        ))
    }
}

#[derive(Clone, Trace, Debug)]
pub struct SyntaxRule {
    pub pattern: Pattern,
    pub binds: Local,
    pub fender: Option<Expression>,
    pub output_expression: Expression,
}

impl SyntaxRule {
    #[maybe_async]
    pub(crate) fn compile<'a>(
        ctxt: &ParseContext,
        keywords: &HashSet<&'a Identifier>,
        pattern: &'a Syntax,
        fender: Option<&'a Syntax>,
        output_expression: &'a Syntax,
        env: &Environment,
    ) -> Result<Self, Exception> {
        let mut variables = HashMap::new();
        let pattern_scope = Scope::new();
        let pattern = Pattern::compile(pattern, keywords, 0, pattern_scope, &mut variables);
        let binds = Local::gensym();
        let env = env.new_syntax_case_contour(pattern_scope, binds, variables);
        let fender = if let Some(fender) = fender {
            let mut fender = fender.clone();
            fender.add_scope(pattern_scope);
            Some(maybe_await!(Expression::parse(ctxt, fender, &env))?)
        } else {
            None
        };
        let mut output_expression = output_expression.clone();
        output_expression.add_scope(pattern_scope);
        let output_expression = maybe_await!(Expression::parse(ctxt, output_expression, &env))?;
        Ok(Self {
            pattern,
            binds,
            fender,
            output_expression,
        })
    }
}

#[derive(Clone, Debug, Trace)]
pub enum Pattern {
    Underscore,
    Ellipsis(Box<Pattern>),
    List(Vec<Pattern>),
    Vector(Vec<Pattern>),
    Variable(Binding),
    Keyword(Identifier),
    Literal(Value),
}

impl Pattern {
    pub fn compile<'a>(
        expr: &'a Syntax,
        keywords: &HashSet<&'a Identifier>,
        curr_nesting_level: usize,
        scope: Scope,
        variables: &mut HashMap<Binding, usize>,
    ) -> Self {
        match expr {
            Syntax::Identifier { ident, .. } if ident.sym == "_" => Self::Underscore,
            Syntax::Identifier { ident, .. } if keywords.contains(&ident) => {
                Self::Keyword(ident.clone())
            }
            Syntax::Identifier { ident, .. } => {
                let mut ident = ident.clone();
                ident.add_scope(scope);
                let binding = Binding::new();
                add_binding(ident, binding);
                variables.insert(binding, curr_nesting_level);
                Self::Variable(binding)
            }
            Syntax::List { list, .. } => Self::List(Self::compile_slice(
                list,
                keywords,
                curr_nesting_level,
                scope,
                variables,
            )),
            Syntax::Vector { vector, .. } => Self::Vector(Self::compile_slice(
                vector,
                keywords,
                curr_nesting_level,
                scope,
                variables,
            )),
            Syntax::Wrapped { value, .. } => Self::Literal(value.clone()),
        }
    }

    fn compile_slice<'a>(
        mut expr: &'a [Syntax],
        keywords: &HashSet<&'a Identifier>,
        curr_nesting_level: usize,
        scope: Scope,
        variables: &mut HashMap<Binding, usize>,
    ) -> Vec<Self> {
        let mut output = Vec::new();
        loop {
            match expr {
                [] => break,
                [
                    pattern,
                    Syntax::Identifier {
                        ident: ellipsis, ..
                    },
                    tail @ ..,
                ] if ellipsis.sym == "..." => {
                    output.push(Self::Ellipsis(Box::new(Pattern::compile(
                        pattern,
                        keywords,
                        curr_nesting_level + 1,
                        scope,
                        variables,
                    ))));
                    expr = tail;
                }
                [head, tail @ ..] => {
                    output.push(Self::compile(
                        head,
                        keywords,
                        curr_nesting_level,
                        scope,
                        variables,
                    ));
                    expr = tail;
                }
            }
        }
        output
    }

    fn matches(&self, expr: &Syntax, expansion_level: &mut ExpansionLevel) -> bool {
        match self {
            Self::Underscore => !expr.is_null(),
            Self::Variable(binding) => {
                assert!(
                    expansion_level
                        .binds
                        .insert(*binding, expr.clone())
                        .is_none()
                );
                true
            }
            Self::Literal(lhs) => {
                if let Syntax::Wrapped { value: rhs, .. } = expr {
                    lhs.equal(rhs)
                } else {
                    false
                }
            }
            Self::Keyword(lhs) => {
                if let Syntax::Identifier { ident: rhs, .. } = expr {
                    lhs.free_identifier_equal(rhs)
                } else {
                    false
                }
            }
            Self::List(list) => match_list(list, expr, expansion_level),
            Self::Vector(vec) => match_vec(vec, expr, expansion_level),
            // We shouldn't ever see this outside of lists
            Self::Ellipsis(_) => unreachable!(),
        }
    }
}

fn match_ellipsis(
    patterns: &[Pattern],
    exprs: &[Syntax],
    expansion_level: &mut ExpansionLevel,
) -> bool {
    // The ellipsis gets to consume any extra items, thus the difference:
    let Some(extra_items) = (exprs.len() + 1).checked_sub(patterns.len()) else {
        return false;
    };

    let mut expr_iter = exprs.iter();
    for pattern in patterns.iter() {
        if let Pattern::Ellipsis(muncher) = pattern {
            // Gobble up the extra items:
            for i in 0..extra_items {
                if expansion_level.expansions.len() <= i {
                    expansion_level.expansions.push(ExpansionLevel::default());
                }
                let expr = expr_iter.next().unwrap();
                if !muncher.matches(expr, &mut expansion_level.expansions[i]) {
                    return false;
                }
            }
        } else {
            // Otherwise, match the pattern normally
            let expr = expr_iter.next().unwrap();
            if !pattern.matches(expr, expansion_level) {
                return false;
            }
        }
    }

    assert!(expr_iter.next().is_none());

    true
}

fn match_list(patterns: &[Pattern], expr: &Syntax, expansion_level: &mut ExpansionLevel) -> bool {
    assert!(!patterns.is_empty());

    let exprs = match expr {
        Syntax::List { list, .. } => list,
        Syntax::Wrapped { value, .. } if value.is_null() => std::slice::from_ref(expr),
        _ => return false,
    };

    if patterns.iter().any(|p| matches!(p, Pattern::Ellipsis(_))) {
        match_ellipsis(patterns, exprs, expansion_level)
    } else if let Some((cdr, head)) = patterns.split_last() {
        // The pattern is an list that contains no ellipsis.
        // Match in order until the last pattern, then match that to the nth
        // cdr.
        let mut exprs = exprs.iter();
        for pattern in head.iter() {
            let Some(expr) = exprs.next() else {
                continue;
            };
            if !pattern.matches(expr, expansion_level) {
                return false;
            }
        }
        // Match the cdr:
        let exprs: Vec<_> = exprs.cloned().collect();
        match exprs.as_slice() {
            [] => false,
            [x] => cdr.matches(x, expansion_level),
            _ => cdr.matches(
                &Syntax::new_list(exprs, expr.span().clone()),
                expansion_level,
            ),
        }
    } else {
        false
    }
}

fn match_vec(patterns: &[Pattern], expr: &Syntax, expansion_level: &mut ExpansionLevel) -> bool {
    let Syntax::Vector { vector: exprs, .. } = expr else {
        return false;
    };

    let contains_ellipsis = patterns.iter().any(|p| matches!(p, Pattern::Ellipsis(_)));

    if contains_ellipsis {
        match_ellipsis(patterns, exprs, expansion_level)
    } else if patterns.len() != exprs.len() {
        false
    } else {
        for (pattern, expr) in patterns.iter().zip(exprs.iter()) {
            if !pattern.matches(expr, expansion_level) {
                return false;
            }
        }
        true
    }
}

impl SchemeCompatible for Pattern {
    fn rtd() -> Arc<RecordTypeDescriptor> {
        rtd!(name: "pattern", sealed: true, opaque: true)
    }
}

#[derive(Clone, Debug, Default, Trace)]
pub struct ExpansionLevel {
    binds: HashMap<Binding, Syntax>,
    expansions: Vec<ExpansionLevel>,
}

impl SchemeCompatible for ExpansionLevel {
    fn rtd() -> Arc<RecordTypeDescriptor> {
        rtd!(name: "expansion-level", sealed: true, opaque: true)
    }
}

#[derive(Trace, Debug)]
pub struct ExpansionCombiner {
    pub(crate) uses: HashMap<Binding, usize>,
}

impl SchemeCompatible for ExpansionCombiner {
    fn rtd() -> Arc<RecordTypeDescriptor> {
        rtd!(name: "expansion-combiner", sealed: true, opaque: true)
    }
}

#[runtime_fn]
unsafe extern "C" fn matches(pattern: *const (), value: *const ()) -> *const () {
    let pattern = unsafe { Value::from_raw_inc_rc(pattern) };
    let pattern = pattern.try_to_rust_type::<Pattern>().unwrap();

    let value = unsafe { Value::from_raw_inc_rc(value) };
    let syntax = Syntax::wrap(value);

    let mut expansions = ExpansionLevel::default();
    if pattern.matches(&syntax, &mut expansions) {
        Value::into_raw(Value::from(Record::from_rust_type(expansions)))
    } else {
        Value::into_raw(Value::from(false))
    }
}

impl ExpansionCombiner {
    fn combine_expansions(&self, expansions: &[ExpansionLevel]) -> ExpansionLevel {
        let binds = self
            .uses
            .iter()
            .filter_map(|(binding, idx)| {
                expansions[*idx]
                    .binds
                    .get(binding)
                    .map(|expansion| (*binding, expansion.clone()))
            })
            .collect::<HashMap<_, _>>();
        let max_expansions = expansions
            .iter()
            .map(|exp| exp.expansions.len())
            .max()
            .unwrap_or(0);
        let expansions = (0..max_expansions)
            .map(|i| {
                let expansions = expansions
                    .iter()
                    .map(|exp| {
                        if exp.expansions.len() <= i {
                            ExpansionLevel::default()
                        } else {
                            exp.expansions[i].clone()
                        }
                    })
                    .collect::<Vec<_>>();
                self.combine_expansions(&expansions)
            })
            .collect::<Vec<_>>();
        ExpansionLevel { binds, expansions }
    }
}

#[derive(Clone, Debug, Trace)]
pub enum Template {
    // Null,
    Ellipsis(Box<Template>),
    List(Vec<Template>),
    Vector(Vec<Template>),
    Variable(Binding),
    Wrapped(Syntax),
}

impl Template {
    pub(crate) fn compile(
        expr: &Syntax,
        env: &Environment,
        expansions: &mut HashMap<Binding, Local>,
    ) -> Result<Self, Exception> {
        check_ellipsis(expr, env)?;
        Self::compile_inner(expr, env, expansions)
    }

    fn is_wrapped(&self) -> bool {
        matches!(self, Self::Wrapped(_))
    }

    fn new_list(list: Vec<Template>, span: Span) -> Self {
        if list.iter().all(Template::is_wrapped) {
            Self::Wrapped(Syntax::new_list(
                list.into_iter()
                    .map(|wrapped| match wrapped {
                        Template::Wrapped(wrapped) => wrapped,
                        _ => unreachable!(),
                    })
                    .collect(),
                span,
            ))
        } else {
            Self::List(list)
        }
    }

    fn new_vector(vec: Vec<Template>, span: Span) -> Self {
        if vec.iter().all(Template::is_wrapped) {
            Self::Wrapped(Syntax::new_vector(
                vec.into_iter()
                    .map(|wrapped| match wrapped {
                        Template::Wrapped(wrapped) => wrapped,
                        _ => unreachable!(),
                    })
                    .collect(),
                span,
            ))
        } else {
            Self::Vector(vec)
        }
    }

    fn compile_inner(
        expr: &Syntax,
        env: &Environment,
        expansions: &mut HashMap<Binding, Local>,
    ) -> Result<Self, Exception> {
        match expr {
            Syntax::List { list, span, .. } => {
                if let [
                    Syntax::Identifier {
                        ident: ellipsis, ..
                    },
                    template,
                    _,
                ] = list.as_slice()
                    && ellipsis.sym == "..."
                {
                    Self::compile_escaped(template, env, expansions)
                } else {
                    Ok(Self::new_list(
                        Self::compile_slice(list, env, expansions)?,
                        span.clone(),
                    ))
                }
            }
            Syntax::Vector { vector, span, .. } => Ok(Self::new_vector(
                Self::compile_slice(vector, env, expansions)?,
                span.clone(),
            )),
            Syntax::Identifier { ident, .. } if ident.sym == "..." => {
                Err(Exception::syntax(expr.clone(), None))
            }
            Syntax::Identifier { ident, span, .. } => {
                if let Some(binding) = ident.resolve()
                    && let Some((expansion, _)) = env.lookup_pattern_variable(binding)
                {
                    expansions.insert(binding, expansion);
                    Ok(Self::Variable(binding))
                } else {
                    Ok(Self::Wrapped(Syntax::Identifier {
                        ident: ident.clone(),
                        span: span.clone(),
                    }))
                }
            }
            wrapped => Ok(Self::Wrapped(wrapped.clone())),
        }
    }

    pub(crate) fn compile_escaped(
        expr: &Syntax,
        env: &Environment,
        expansions: &mut HashMap<Binding, Local>,
    ) -> Result<Self, Exception> {
        match expr {
            Syntax::List { list, span, .. } => Ok(Self::new_list(
                Self::compile_slice_escaped(list, env, expansions)?,
                span.clone(),
            )),
            Syntax::Vector { vector, span, .. } => Ok(Self::new_vector(
                Self::compile_slice_escaped(vector, env, expansions)?,
                span.clone(),
            )),
            Syntax::Identifier { ident, span, .. } => {
                if let Some(binding) = ident.resolve()
                    && let Some((expansion, _)) = env.lookup_pattern_variable(binding)
                {
                    expansions.insert(binding, expansion);
                    Ok(Self::Variable(binding))
                } else {
                    Ok(Self::Wrapped(Syntax::Identifier {
                        ident: ident.clone(),
                        span: span.clone(),
                    }))
                }
            }
            wrapped => Ok(Self::Wrapped(wrapped.clone())),
        }
    }

    fn compile_slice(
        mut expr: &[Syntax],
        env: &Environment,
        expansions: &mut HashMap<Binding, Local>,
    ) -> Result<Vec<Self>, Exception> {
        let mut output = Vec::new();
        loop {
            match expr {
                [] => break,
                [
                    template,
                    Syntax::Identifier {
                        ident: ellipsis, ..
                    },
                    tail @ ..,
                ] if ellipsis.sym == "..." => {
                    let mut compiled =
                        Self::Ellipsis(Box::new(Self::compile_inner(template, env, expansions)?));
                    let mut tail = tail;
                    while matches!(tail.first(), Some(Syntax::Identifier { ident, ..}) if ident.sym == "...")
                    {
                        compiled = Self::Ellipsis(Box::new(compiled));
                        tail = &tail[1..];
                    }
                    output.push(compiled);
                    expr = tail;
                }
                [head, tail @ ..] => {
                    output.push(Self::compile_inner(head, env, expansions)?);
                    expr = tail;
                }
            }
        }
        Ok(output)
    }

    fn compile_slice_escaped(
        exprs: &[Syntax],
        env: &Environment,
        expansions: &mut HashMap<Binding, Local>,
    ) -> Result<Vec<Self>, Exception> {
        exprs
            .iter()
            .map(|expr| Self::compile_escaped(expr, env, expansions))
            .collect()
    }

    fn expand(&self, binds: &Binds<'_>, curr_span: Span) -> Option<Value> {
        match self {
            Self::List(list) => expand_list(list, binds, curr_span.clone()),
            Self::Vector(vec) => expand_vec(vec, binds, curr_span.clone()),
            Self::Variable(binding) => Some(Syntax::unwrap(binds.get_bind(*binding)?)),
            Self::Ellipsis(_) => unreachable!(),
            Self::Wrapped(Syntax::Wrapped { value, .. }) if value.is_null() => Some(Value::null()),
            Self::Wrapped(wrapped) => Some(Value::from(wrapped.clone())),
        }
    }

    fn expand_nested(&self, binds: &Binds<'_>, curr_span: Span) -> Option<Vec<Value>> {
        let mut output = Vec::new();
        if let Template::Ellipsis(template) = self {
            for expansion in &binds.curr_expansion_level.expansions {
                let new_level = binds.new_level(expansion);
                let Some(result) = template.expand_nested(&new_level, curr_span.clone()) else {
                    break;
                };
                output.extend(result);
            }
        } else {
            output.push(self.expand(binds, curr_span.clone())?);
        }
        Some(output)
    }
}

fn expand_list(items: &[Template], binds: &Binds<'_>, curr_span: Span) -> Option<Value> {
    let mut expanded = Vec::new();
    for item in items {
        expanded.extend(item.expand_nested(binds, curr_span.clone())?);
    }
    let Some(mut output) = expanded.pop() else {
        return Some(Value::null());
    };
    for expanded in expanded.into_iter().rev() {
        output = Value::from((expanded, output));
    }
    Some(output)
}

fn expand_vec(items: &[Template], binds: &Binds<'_>, curr_span: Span) -> Option<Value> {
    let mut output = Vec::new();
    for item in items {
        output.extend(item.expand_nested(binds, curr_span.clone())?);
    }
    Some(Value::from(output))
}

fn check_ellipsis(expr: &Syntax, env: &Environment) -> Result<(), Exception> {
    let binds = match expr {
        Syntax::List { list, .. } => {
            if let [
                Syntax::Identifier {
                    ident: ellipsis, ..
                },
                template,
                _,
            ] = list.as_slice()
                && ellipsis.sym == "..."
            {
                check_escaped_template(template, env)
            } else {
                check_subtemplate(list, env)?
            }
        }
        Syntax::Vector { vector, .. } => check_subtemplate(vector, env)?,
        Syntax::Identifier { ident, .. } => {
            if let Some(binding) = ident.resolve()
                && let Some((_, depth)) = env.lookup_pattern_variable(binding)
                && depth != 0
            {
                return Err(error::too_few_ellipsis(ident.sym, expr));
            } else {
                return Ok(());
            }
        }
        _ => return Ok(()),
    };

    if let Some((sym, _)) = binds.iter().find(|(_, depth)| *depth > 0) {
        return Err(error::too_few_ellipsis(*sym, expr));
    }
    let has_proper_pattern_var = binds.iter().any(|(_, depth)| *depth == 0);
    if !has_proper_pattern_var && let Some((sym, _)) = binds.iter().find(|(_, depth)| *depth < 0) {
        return Err(error::too_many_ellipsis(*sym, expr));
    }

    Ok(())
}

fn check_template(expr: &Syntax, env: &Environment) -> Result<Vec<(Symbol, isize)>, Exception> {
    match expr {
        Syntax::List { list, .. } => {
            if let [
                Syntax::Identifier {
                    ident: ellipsis, ..
                },
                template,
                _,
            ] = list.as_slice()
                && ellipsis.sym == "..."
            {
                Ok(check_escaped_template(template, env))
            } else {
                check_subtemplate(list, env)
            }
        }
        Syntax::Vector { vector, .. } => check_subtemplate(vector, env),
        Syntax::Identifier { ident, .. } => {
            // TODO: Probably should cache these resolves at some point...
            if let Some(binding) = ident.resolve()
                && let Some((_, depth)) = env.lookup_pattern_variable(binding)
            {
                Ok(vec![(ident.sym, depth as isize)])
            } else {
                Ok(Vec::new())
            }
        }
        _ => Ok(Vec::new()),
    }
}

fn check_subtemplate(
    expr: &[Syntax],
    env: &Environment,
) -> Result<Vec<(Symbol, isize)>, Exception> {
    match expr {
        [] => Ok(Vec::new()),
        [template, tail @ ..] => {
            let mut num_ellipsis = 0;
            let mut tail = tail;
            while matches!(tail.first(), Some(Syntax::Identifier { ident, ..}) if ident.sym == "...")
            {
                num_ellipsis += 1;
                tail = &tail[1..];
            }
            let mut patterns = check_template(template, env)?;
            if patterns.is_empty() && num_ellipsis > 0 {
                return Err(error::no_pattern_variables_in_template(template));
            }
            for pattern in &mut patterns {
                pattern.1 -= num_ellipsis;
            }
            patterns.extend(check_subtemplate(tail, env)?);
            Ok(patterns)
        }
    }
}

fn check_escaped_template(expr: &Syntax, env: &Environment) -> Vec<(Symbol, isize)> {
    match expr {
        Syntax::List { list, .. } => list
            .iter()
            .flat_map(|template| check_escaped_template(template, env))
            .collect(),
        Syntax::Vector { vector, .. } => vector
            .iter()
            .flat_map(|template| check_escaped_template(template, env))
            .collect(),
        Syntax::Identifier { ident, .. } => {
            if let Some(binding) = ident.resolve()
                && let Some((_, depth)) = env.lookup_pattern_variable(binding)
            {
                vec![(ident.sym, depth as isize)]
            } else {
                Vec::new()
            }
        }
        _ => Vec::new(),
    }
}

impl SchemeCompatible for Template {
    fn rtd() -> Arc<RecordTypeDescriptor> {
        rtd!(name: "template", sealed: true, opaque: true)
    }
}

#[runtime_fn]
unsafe extern "C" fn expand_template(
    template: *const (),
    expansion_combiner: *const (),
    expansions: *const *const (),
    num_expansions: u32,
) -> *const () {
    // TODO: A lot of probably unnecessary cloning here, we'll need to fix it up
    // eventually

    let template = unsafe { Value::from_raw_inc_rc(template) };
    let template = template.try_to_rust_type::<Template>().unwrap();

    let expansion_combiner = unsafe { Value::from_raw_inc_rc(expansion_combiner) };
    let expansion_combiner = expansion_combiner
        .try_to_rust_type::<ExpansionCombiner>()
        .unwrap();

    let expansions = (0..num_expansions)
        .map(|i| {
            let expansion = unsafe { Value::from_raw_inc_rc(expansions.add(i as usize).read()) };
            let expansion = expansion.try_to_rust_type::<ExpansionLevel>().unwrap();
            expansion.as_ref().clone()
        })
        .collect::<Vec<_>>();

    let combined_expansions = expansion_combiner.combine_expansions(&expansions);
    let binds = Binds::new_top(&combined_expansions);

    // TODO: get a real span in here
    let expanded = template.expand(&binds, Span::default()).unwrap();

    Value::into_raw(expanded)
}

#[derive(Debug)]
pub struct Binds<'a> {
    curr_expansion_level: &'a ExpansionLevel,
    parent_expansion_level: Option<&'a Binds<'a>>,
}

impl<'a> Binds<'a> {
    fn new_top(top_expansion_level: &'a ExpansionLevel) -> Self {
        Self {
            curr_expansion_level: top_expansion_level,
            parent_expansion_level: None,
        }
    }

    fn new_level<'b: 'a>(&'b self, next_expansion_level: &'b ExpansionLevel) -> Binds<'b> {
        Binds {
            curr_expansion_level: next_expansion_level,
            parent_expansion_level: Some(self),
        }
    }

    fn get_bind(&self, binding: Binding) -> Option<Syntax> {
        if let bind @ Some(_) = self.curr_expansion_level.binds.get(&binding) {
            bind.cloned()
        } else if let Some(up) = self.parent_expansion_level {
            up.get_bind(binding)
        } else {
            None
        }
    }
}

#[runtime_fn]
unsafe extern "C" fn error_no_patterns_match() -> i64 {
    let condition = Exception::error("no patterns match".to_string());
    Value::into_raw(Value::from(condition)) as i64
}

#[bridge(name = "make-variable-transformer", lib = "(rnrs base builtins (6))")]
pub fn make_variable_transformer(proc: &Value) -> Result<Vec<Value>, Exception> {
    let proc: Procedure = proc.clone().try_into()?;
    let mut var_transformer = proc.0.as_ref().clone();
    var_transformer.is_variable_transformer = true;
    Ok(vec![Value::from(Procedure(Gc::new(var_transformer)))])
}