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
832
833
834
835
836
837
838
#![macro_use]

// TODO: use a real logging framework
macro_rules! log {
    ($($e:expr),*) => {
        // print!( $($e),* );
    };
}

macro_rules! icp {
    () => {
        panic!("ICP: can't happen")
    };
    ( $($arg:expr),* ) => {
        panic!("ICP: {}", format!( $($arg),* ) )
    };
}

// Assoc

macro_rules! expr_ify {
    ($e:expr) => {
        $e
    };
}

macro_rules! assoc_n {
    () => { crate::util::assoc::Assoc::new() };
    ( $k:tt => $v:expr $(, $k_cdr:tt => $v_cdr:expr)* ) => {
        assoc_n!( $( $k_cdr => $v_cdr ),* ).set(crate::name::n(expr_ify!($k)), $v)
    };
    ( ($k:expr) => $v:expr $(, $k_cdr:tt => $v_cdr:expr)* ) => {
        assoc_n!( $( $k_cdr => $v_cdr ),* ).set(::name::n($k), $v)
    };
}

// Beta

macro_rules! beta_connector {
    ($lhs:tt : $rhs:tt) => {
        crate::beta::Basic(crate::name::n(expr_ify!($lhs)), crate::name::n(expr_ify!($rhs)))
    };
    ($lhs:tt = $rhs:tt) => {
        crate::beta::SameAs(
            crate::name::n(expr_ify!($lhs)),
            Box::new(raw_ast!(VariableReference(crate::name::n(expr_ify!($rhs))))),
        )
    };
    // TODO: this needs a better notation, somehow
    ($lhs:tt == $rhs:tt) => {
        crate::beta::SameAs(crate::name::n(expr_ify!($lhs)), Box::new(ast!($rhs)))
    };
    ($lhs:tt += $rhs:tt) => {
        crate::beta::SameAs(crate::name::n(expr_ify!($lhs)), Box::new(u!($rhs)))
    };
}

macro_rules! beta {
    ( [] ) => { crate::beta::Nothing };
    ( [* $body:tt ]) => {
        {
            let sub = beta!($body);
            let drivers = sub.names_mentioned();
            crate::beta::ShadowAll(Box::new(sub), drivers)
        }
    };
    ( [ forall $name:tt $( $rest:tt )*] ) => {
        crate::beta::Shadow(Box::new(
            crate::beta::Underspecified(  crate::name::n(expr_ify!($name)))),
               Box::new(beta!( [ $( $rest )* ] )))
    };
    ( [ prot $name:tt $( $rest:tt )*] ) => {
        crate::beta::Shadow(Box::new(
            crate::beta::Protected(       crate::name::n(expr_ify!($name)))),
               Box::new(beta!( [ $( $rest )* ] )))
    };
    ( [ unusable $name:tt $( $rest:tt )*] ) => {
        crate::beta::Shadow(Box::new(
            crate::beta::BoundButNotUsable(crate::name::n(expr_ify!($name)))),
               Box::new(beta!( [ $( $rest )* ] )))
    };
    // Just makes things prettier by not ending everything in " ▷ ∅":
    ( [ $name:tt $connector:tt $t:tt ] ) => {
        beta_connector!($name $connector $t)
    };
    ( [ $name:tt $connector:tt $t:tt
        $( $rest:tt )*
         ] ) => {
        crate::beta::Shadow(Box::new(beta_connector!($name $connector $t)),
                            Box::new(beta!( [ $( $rest )* ] )))
    };
}

macro_rules! ebeta {
    ( [] ) => { crate::beta::ExportBeta::Nothing };
    ( [* $body:tt ]) => {
        {
            let sub = ebeta!($body);
            let drivers = sub.names_mentioned();
            crate::beta::ExportBeta::ShadowAll(Box::new(sub), drivers)
        }
    };
    ( [ $name:tt $( $rest:tt )*] ) => {
        crate::beta::ExportBeta::Shadow(Box::new(crate::beta::ExportBeta::Use(crate::name::n(expr_ify!($name)))),
               Box::new(ebeta!( [ $( $rest )* ] )))
    };
}

// Read

macro_rules! tokens_s {
    () => {
        ""
    };
    ($($contents:tt)*) => {
        &vec![ $( $contents ),* ].join(" ")
    }
}

macro_rules! t_elt {
    ( [ $e:expr ;  $( $list:tt )* ] ) => {
        {
            let mut toks = vec![::name::n(concat!($e,"["))];
            toks.append(&mut tokens!($($list)*).t);
            toks.push(::name::n(concat!("]", $e)));
            toks
        }
    };
    ( { $e:expr ;  $( $list:tt )* } ) => {
        {
            let mut toks = vec![::name::n(concat!($e,"{"))];
            toks.append(&mut tokens!($($list)*).t);
            toks.push(::name::n(concat!("}", $e)));
            toks
        }
    };
    ( ( $e:expr ;  $( $list:tt )* ) ) => {
        {
            let mut toks = vec![::name::n(concat!($e,"("))];
            toks.append(&mut tokens!($($list)*).t);
            toks.push(::name::n(concat!(")", $e)));
            toks
        }
    };
    ($e:expr) => { vec![::name::n(& $e.replace(" ", "_"))] }
}

// Ast

// AstContents to Ast.
// Whenever possible, use `ast!` directly (or, in tests, `u!`).
macro_rules! raw_ast {
    ($ast_kind:ident) => {
        crate::ast::Ast(std::rc::Rc::new(crate::ast::LocatedAst {
            c: crate::ast::AstContents::$ast_kind,
            // TODO: would Rust file info be useful?
            file_id: 0, begin: 0, end: 0
        }))
    };
    ($ast_kind:ident ( $( $body:expr ),* ) ) => {
        crate::ast::Ast(std::rc::Rc::new(crate::ast::LocatedAst {
            c: crate::ast::AstContents::$ast_kind( $( $body ),* ),
            // TODO: would Rust file info be useful?
            file_id: 0, begin: 0, end: 0
        }))
    }
}

macro_rules! ast_shape {
    ($($contents:tt)*) => { raw_ast!(Shape(vec![ $(  ast!($contents) ),* ] ))};
}

macro_rules! ast {
    ( (trivial) ) => { raw_ast!(Trivial) };
    ( (++ $pos:tt $sub:tt) ) => {
        raw_ast!(QuoteMore(ast!($sub), $pos))
    };
    ( (-- $depth:tt $sub:tt ) ) => {
        raw_ast!(QuoteLess(ast!($sub), $depth))
    };
    ( (import $beta:tt $sub:tt) ) => {
        raw_ast!(ExtendEnv(ast!($sub), beta!($beta)))
    };
    ( (import_phaseless $beta:tt $sub:tt) ) => {
        raw_ast!(ExtendEnvPhaseless(ast!($sub), beta!($beta)))
    };
    /* // not sure we'll need this
    ( (* $env:expr => $new_env:ident / $($n:expr),* ; $($sub_ar"gs:tt)*) ) => {
        {
            let mut res = vec![];

            for $new_env in $env.march_all( &vec![$($n),*] ) {
                res.push(ast!($sub))
            }
            res.reverse();
            Shape(res)
        }
    };*/
    ( (vr $var:expr) ) => { raw_ast!(VariableReference(crate::name::Name::from($var))) };
    // Like the last clause, but explicit about being an atom:
    ( (at $var:expr) ) => { raw_ast!(Atom(crate::name::Name::from($var))) };
    ( (, $interpolate:expr)) => { $interpolate };
    // TODO: maybe we should use commas for consistency:
    ( ( $( $list:tt )* ) ) => { ast_shape!( $($list)* ) };
    ( { - $($mbe_arg:tt)* } ) => {
        raw_ast!(IncompleteNode(mbe!( $($mbe_arg)* )))
    };
    ( { $nt:tt $form:tt => $beta:tt : $($mbe_arg:tt)*} ) => {
        raw_ast!(Node(crate::core_forms::find($nt, $form), mbe!( $($mbe_arg)* ),
                    ebeta!($beta)))
    };
    ( { $form:expr => $beta:tt ; $($mbe_arg:tt)*} ) => {
        raw_ast!(Node($form, mbe!( $($mbe_arg)* ), ebeta!($beta)))
    };
    ( { $form:expr; [ $($mbe_arg:tt)* ] }) => {
        ast!( { $form ; $($mbe_arg)* } )
    };
    ( { $form:expr; $($mbe_arg:tt)* }) => {
        raw_ast!(Node($form, mbe!( $($mbe_arg)* ), crate::beta::ExportBeta::Nothing))
    };
    ( { $nt:tt $form:tt : $($mbe_arg:tt)* }) => {
        raw_ast!(Node(crate::core_forms::find($nt, $form), mbe!( $($mbe_arg)* ),
                    crate::beta::ExportBeta::Nothing))
    };
    // Accepts either `&str` (most common) or `Names`:
    ($e:expr) => { raw_ast!(Atom(crate::name::Name::from($e))) };
}

// These construct spanned type errors (so, for type synthesis, not subtyping)

macro_rules! ty_err_val {
    ( $name:tt ( $($arg:expr),* ) at $loc:expr) => {
        crate::util::err::sp(crate::ty::TyErr::$name( $($arg),* ), $loc.clone())
    }
}

macro_rules! ty_err {
    ( $name:tt ( $($arg:expr),* ) at $loc:expr) => {
        return Err(ty_err_val!( $name ( $($arg),* ) at $loc))
    }
}

macro_rules! ty_exp { // type expectation
    ( $got:expr, $expected:expr, $loc:expr) => {
        if $got != $expected {
            ty_err!(Mismatch((*$got).clone(), (*$expected).clone()) at $loc)
        }
    }
}

macro_rules! ty_err_p { // type error pattern
    ( $name:tt ( $($arg:pat),* ) ) => {
        Err( crate::util::err::Spanned { body: crate::ty::TyErr::$name( $($arg),* ), loc: _ } )
    }
}

// EnvMBE

// These macros generate `EnvMBE<Ast>`s, not arbitrary `EnvMBE`s,
//  which is a little un-abstract, but is the main usage.

// Wait a second, I'm writing in Rust right now! I'll use an MBE macro to implement an MBE literal!
macro_rules! mbe_one_name {
    // `elt` ought to have an interpolation that references `new_env`
    // TODO: maybe this (and the parser!) ought to add 0-times-repeated leaves to `leaf_locations`
    /* TYPE PUN ALERT: $env has to be something with a `march_all` method;
        there's no trait enforcing this.

       But wait, isn't preventing this kind of nonsense the whole point of this project?

       Well, you know the old saying:
        "While the mice are implementing the cat, the mice will play."
    */
    ($k:tt => [* $env:expr =>($($n:expr),*) $new_env:ident : $elt:tt]) => {
        {
            let mut v = vec![];
            let marchee = vec![$(crate::name::n($n)),*];
            for $new_env in $env.march_all(&marchee) {
                v.push( mbe_one_name!($k => $elt));
            }
            crate::util::mbe::EnvMBE::new_from_anon_repeat(v)
        }
    };

    ($k:tt => [@ $n:tt $($elt:tt),*]) => {
        crate::util::mbe::EnvMBE::new_from_named_repeat(
            crate::name::n(expr_ify!($n)),
            vec![ $( mbe_one_name!($k => $elt) ),* ]
        )
    };

    ($k:tt => [$($elt:tt),*]) => {
        crate::util::mbe::EnvMBE::new_from_anon_repeat(
            vec![ $( mbe_one_name!($k => $elt) ),* ])
    };

    // since `Ast`s go on the RHS, we have to have a distinctive interpolation syntax
    ($k:tt => (,seq $e:expr)) => {
        {
            let mut v = vec![];
            for elt in $e {
                v.push(crate::util::mbe::EnvMBE::new_from_leaves(assoc_n!($k => elt)))
            }
            crate::util::mbe::EnvMBE::new_from_anon_repeat(v)
        }
    };

    ($k:tt => (@ $rep_n:tt ,seq $e:expr)) => {
        {
            let mut v = vec![];
            for elt in $e {
                v.push(crate::util::mbe::EnvMBE::new_from_leaves(assoc_n!($k => elt)))
            }
            crate::util::mbe::EnvMBE::new_from_named_repeat(crate::name::n(expr_ify!($rep_n)), v)
        }
    };

    // For parsing reasons, we only accept expressions that are TTs.
    // It's hard to generalize the `mbe!` interface so that it accepts exprs
    // or `[]`-surrounded trees of them.
    ($k:tt => $leaf:tt) => {
        crate::util::mbe::EnvMBE::new_from_leaves(assoc_n!($k => ast!($leaf)))
    }
}

// Eventually, this ought to support more complex structures
macro_rules! mbe {
    ( $( $lhs:tt => $rhs:tt ),* ) => {{
        let single_name_mbes = vec![ $( mbe_one_name!($lhs => $rhs) ),*];
        let mut res = crate::util::mbe::EnvMBE::new();
        for m in &single_name_mbes {
            res = res.merge(m);
        }
        res
    }}
}

// FormPat

// TODO #8: `ast!` and `form_pat!` are inconsistent with each other.
macro_rules! form_pat {
    ((lit $e:expr)) => {
        crate::grammar::FormPat::Literal(
            std::rc::Rc::new(crate::grammar::FormPat::Call(crate::name::n("DefaultToken"))),
            crate::name::n($e))
    };
    ((name_lit $e:expr)) => {
        crate::grammar::FormPat::Literal(
            std::rc::Rc::new(crate::grammar::FormPat::Call(crate::name::n("DefaultWord"))),
            crate::name::n($e))
    };
    ((lit_aat $e:expr)) => {
        crate::grammar::FormPat::Literal(
            std::rc::Rc::new(crate::grammar::new_scan(r"\s*(\S+)", None)),
            crate::name::n($e))
    };
    ((name_lit__by_name $e:expr)) => {
        crate::grammar::FormPat::Literal(
            std::rc::Rc::new(crate::grammar::FormPat::Call(crate::name::n("DefaultWord"))),
            $e)
    };
    ((lit_tok $tok:tt, $lit:expr)) => {
        crate::grammar::FormPat::Literal(
            std::rc::Rc::new(form_pat!($tok)),
            crate::name::n($lit))
    };
    ((scan_cat $e:expr, $cat:expr)) => {
        crate::grammar::new_scan($e, Some(String::from($cat)))
    };
    ((scan $e:expr)) => {
        crate::grammar::new_scan($e, None)
    };
    ((reserved $body:tt, $( $res:tt )*)) => {
        crate::grammar::FormPat::Reserved(std::rc::Rc::new(form_pat!($body)), vec![$( n($res) ),*])
    };
    ((reserved_by_name_vec $body:tt, $names:expr)) => {
        crate::grammar::FormPat::Reserved(std::rc::Rc::new(form_pat!($body)), $names)
    };
    ((common $body:tt)) => {
        crate::grammar::FormPat::Common(std::rc::Rc::new(form_pat!($body)))
    };
    ((anyways $a:tt)) => { crate::grammar::FormPat::Anyways(ast!($a)) };
    ((impossible)) => { crate::grammar::FormPat::Impossible };
    (atom) => { crate::grammar::FormPat::Call(crate::name::n("AtomNotInPat")) };
    (varref) => { crate::grammar::FormPat::VarRef(
        std::rc::Rc::new(crate::grammar::FormPat::Call(crate::name::n("DefaultAtom")))
    ) };
    ((varref_call $n:tt)) => { crate::grammar::FormPat::VarRef(
        std::rc::Rc::new(crate::grammar::FormPat::Call(crate::name::n($n)))
    ) };
    (varref_aat) => { crate::grammar::FormPat::VarRef(
        std::rc::Rc::new(crate::grammar::new_scan(r"\s*(\S+)", None))
    ) };
    ((delim $n:expr, $d:expr, $body:tt)) => {
        crate::grammar::FormPat::Seq(vec![
            std::rc::Rc::new(crate::grammar::FormPat::Literal(
                std::rc::Rc::new(crate::grammar::FormPat::Call(crate::name::n("OpenDelim"))),
                crate::name::n($n))),
            std::rc::Rc::new(form_pat!($body)),
            {
                let mut main_tok = $n.to_owned();
                main_tok.pop();
                std::rc::Rc::new(crate::grammar::FormPat::Literal(
                    std::rc::Rc::new(crate::grammar::FormPat::Call(crate::name::n("CloseDelim"))),
                    crate::name::n(&format!("{}{}", crate::read::delim($d).close(), main_tok))))
            }])
    };
    ((star $body:tt)) => { crate::grammar::FormPat::Star(std::rc::Rc::new(form_pat!($body))) };
    ((plus $body:tt)) => { crate::grammar::FormPat::Plus(std::rc::Rc::new(form_pat!($body))) };
    ((alt $($body:tt),* )) => { crate::grammar::FormPat::Alt(vec![
        $( std::rc::Rc::new(form_pat!($body)) ),* ] )};
    ((biased $lhs:tt, $rhs:tt)) => {
        crate::grammar::FormPat::Biased(std::rc::Rc::new(form_pat!($lhs)),
                                 std::rc::Rc::new(form_pat!($rhs))) };
    ((call $n:expr)) => { crate::grammar::FormPat::Call(crate::name::n($n)) };
    ((call_by_name $n:expr)) => { crate::grammar::FormPat::Call($n) };
    ((scope $f:expr)) => { crate::grammar::FormPat::Scope($f, crate::beta::ExportBeta::Nothing) };
    ((scope $f:expr, $ebeta:tt)) => { crate::grammar::FormPat::Scope($f, ebeta!($ebeta)) };
    ((pick $body:tt, $n:expr)) => {
        crate::grammar::FormPat::Pick(std::rc::Rc::new(form_pat!($body)), crate::name::n($n))
    };
    ((named $n:expr, $body:tt)) => {
        crate::grammar::FormPat::Named(crate::name::n($n), std::rc::Rc::new(form_pat!($body)))
    };
    ((import $beta:tt, $body:tt)) => {
        crate::grammar::FormPat::NameImport(std::rc::Rc::new(form_pat!($body)), beta!($beta))
    };
    ((import_phaseless $beta:tt, $body:tt)) => {
        crate::grammar::FormPat::NameImportPhaseless(
            std::rc::Rc::new(form_pat!($body)), beta!($beta))
    };
    ((++ $pos:tt $body:tt)) => { // `pos` should be an expr, but I didn't want a comma. Name it.
        crate::grammar::FormPat::QuoteDeepen(std::rc::Rc::new(form_pat!($body)), $pos)
    };
    ((-- $depth:tt $body:tt)) => {
        crate::grammar::FormPat::QuoteEscape(std::rc::Rc::new(form_pat!($body)), $depth)
    };
    ((extend_nt $lhs:tt, $n:expr, $f:expr)) => {
        crate::grammar::FormPat::SynImport(
            std::rc::Rc::new(form_pat!($lhs)),
            std::rc::Rc::new(crate::grammar::FormPat::Call(crate::name::n($n))),
            crate::grammar::SyntaxExtension(std::rc::Rc::new(Box::new($f))))
    };
    ((extend $lhs:tt, $body:tt, $f:expr)) => {
        crate::grammar::FormPat::SynImport(
            std::rc::Rc::new(form_pat!($lhs)),
            std::rc::Rc::new(form_pat!($body)),
            crate::grammar::SyntaxExtension(std::rc::Rc::new(Box::new($f))))
    };
    ( [$($body:tt),*] ) => {
        crate::grammar::FormPat::Seq(vec![ $( std::rc::Rc::new(form_pat!($body)) ),* ])};
    ((, $interpolate:expr)) => { $interpolate }
}

macro_rules! syn_env {
    () => { crate::util::assoc::Assoc::new() };
    ( $k:tt => $rhs:tt $(, $k_cdr:tt => $rhs_cdr:tt)* ) => {
        syn_env!( $( $k_cdr => $rhs_cdr ),* )
            .set(crate::name::n(expr_ify!($k)), Rc::new(form_pat!($rhs)))
    };
}

// Utility for constructing `Custom` walks
// Seems impossible to make this a function, for lifetime/sizedness reasons.
macro_rules! cust_rc_box {
    ($contents:expr) => {
        crate::ast_walk::WalkRule::Custom(std::rc::Rc::new(Box::new($contents)))
    };
}

// Form definitions

macro_rules! basic_typed_form {
    ( $p:tt, $gen_type:expr, $eval:expr ) => {
        Rc::new(crate::form::Form {
            name: crate::name::n("unnamed form"),
            grammar: Rc::new(form_pat!($p)),
            type_compare: crate::form::Positive(crate::ast_walk::WalkRule::NotWalked),
            synth_type: crate::form::Positive($gen_type),
            quasiquote: crate::form::Both(
                crate::ast_walk::WalkRule::LiteralLike,
                crate::ast_walk::WalkRule::LiteralLike,
            ),
            eval: crate::form::Positive($eval),
        })
    };
}

macro_rules! typed_form {
    ( $name:expr, $p:tt, $gen_type:expr, $eval:expr ) => {
        Rc::new(crate::form::Form {
            name: crate::name::n($name),
            grammar: Rc::new(form_pat!($p)),
            type_compare: crate::form::Positive(crate::ast_walk::WalkRule::NotWalked),
            synth_type: crate::form::Positive($gen_type),
            quasiquote: crate::form::Both(
                crate::ast_walk::WalkRule::LiteralLike,
                crate::ast_walk::WalkRule::LiteralLike,
            ),
            eval: crate::form::Positive($eval),
        })
    };
}

macro_rules! negative_typed_form {
    ( $name:expr, $p:tt, $gen_type:expr, $eval:expr ) => {
        Rc::new(crate::form::Form {
            name: crate::name::n($name),
            grammar: Rc::new(form_pat!($p)),
            type_compare: crate::form::Positive(crate::ast_walk::WalkRule::NotWalked),
            synth_type: crate::form::Negative($gen_type),
            quasiquote: crate::form::Both(
                crate::ast_walk::WalkRule::LiteralLike,
                crate::ast_walk::WalkRule::LiteralLike,
            ),
            eval: crate::form::Negative($eval),
        })
    };
}

// Value
// (This is exported so `cli.rs` can use it. TODO: bring those tests back into the library.)
#[macro_export]
macro_rules! val {
    (i $i:expr) => { crate::runtime::eval::Value::Int(::num::bigint::BigInt::from($i)) };
    (b $b:expr) => {
        crate::runtime::eval::Value::Enum( crate::name::n(if $b {"True"} else {"False"}), vec![])
    };
    (s $s:expr) => {
        crate::runtime::eval::Value::Text( String::from($s) )
    };
    (f $body:tt, $params:expr, $env:tt) => {
        crate::runtime::eval::Value::Function(
            std::rc::Rc::new(::runtime::eval::Closure(ast!($body), $params, assoc_n! $env)))
    };
    (bif $f:expr) => {
        crate::runtime::eval::Value::BuiltInFunction(::runtime::eval::BIF(std::rc::Rc::new($f)))
    };
    (ast $body:tt) => {
        crate::runtime::eval::Value::AbstractSyntax(ast!($body))
    };
    (struct $( $k:tt => $v:tt ),* ) => {
        crate::runtime::eval::Value::Struct(assoc_n!( $( $k => val! $v),* ))
    };
    (enum $nm:expr, $($v:tt),*) => {
        crate::runtime::eval::Value::Enum(crate::name::n($nm), vec![ $( val! $v ),* ])
    };
    (seq $($v:tt)*) => {
        crate::runtime::eval::Value::Sequence(vec![ $( std::rc::Rc::new(val! $v) ),* ])
    };
    (cell $v:tt) => {
        crate::runtime::eval::Value::Cell(std::rc::Rc::new(std::cell::RefCell::new(val! $v) ))
    };
    (, $interpolate:expr) => { $interpolate }
}

// core_values stuff

macro_rules! mk_type { // TODO: maybe now use find_core_form and un-thread $se?
    ( [ ( $( $param:tt ),* )  -> $ret_t:tt ] ) => {
        ast!( { crate::core_forms::find_core_form("Type", "fn") ;
                  "param" => [ $((, mk_type!($param) )),*],
                  "ret" => (, mk_type!($ret_t))
        })
    };
    ( $n:tt ) => { ast!({ "Type" $n : }) }; // atomic type
}

// Define a typed function
macro_rules! tf {
    (  [ ( $($param_t:tt),* ) -> $ret_t:tt ] ,
       ( $($param_p:pat),* ) => $body:expr) => {
        TypedValue {
            ty: mk_type!([ ( $($param_t),* ) -> $ret_t ] ),
            val: core_fn!( $($param_p),* => $body )
        }
    };
    (  $n:tt, $e:expr ) => {
        TypedValue {
            ty: mk_type!( $n ),
            val: $e
        }
    }
}

// Like `tf!`, but actually uses `ast!`, which is more flexible than `mk_type!`
macro_rules! tyf {
    ( $t:tt, ( $($param_p:pat),* ) => $body:expr ) => {
        TypedValue { ty: ast!($t), val: core_fn!($($param_p),* => $body) }
    }
}

macro_rules! bind_patterns {
    ( $iter:expr; () => $body:expr ) => { $body };
    ( $iter:expr; ($p_car:pat, $($p_cdr:pat,)* ) => $body:expr ) => {
        #[allow(unreachable_patterns)]  // in case `$p_car` is irrefutable
        match $iter.next() {
            Some($p_car) => {
                bind_patterns!($iter; ($( $p_cdr, )*) => $body)
            }
            None => { icp!("too few arguments"); }
            Some(ref other) => { icp!("[type error] in argument: {:#?}", other); }
        }
    }
}

macro_rules! core_fn {
    ( $($p:pat),* => $body:expr ) => {
        BuiltInFunction(BIF(Rc::new(
            move | args | {
                let mut argi = args.into_iter();
                bind_patterns!(argi; ($( $p, )*) => $body )
            }
        )))
    }
}

// Alpha
#[macro_export]
macro_rules! without_freshening {
    ($( $body:tt )*) => {{
        let mut orig: bool = false;
        crate::alpha::freshening_enabled.with(|f| {
            orig = *f.borrow();
            *f.borrow_mut() = false;
        });
        { $( $body )* }
        crate::alpha::freshening_enabled.with(|f| {
            *f.borrow_mut() = orig;
        });
    }}
}

// for core_forms

// Unpacking `Ast`s into environments is a pain, so here's a macro for it
macro_rules! expect_node {
    ( ($node:expr ; $form:expr) $env:ident ; $body:expr ) => {
        // This is tied to the signature of `Custom`
        if let crate::ast::Node(ref f, ref $env, _) = $node.c() {
            if *f == $form {
                $body
            } else {
                // TODO: make it possible to specify which one
                panic!(
                    "ICP or type error: Expected a {:#?} node, got {:#?}, which is {:#?}.",
                    $form, $node, *f
                )
            }
        } else {
            panic!(
                "ICP or type error: Expected a {:#?} node, got {:#?}, which isn't a node.",
                $form, $node
            )
        }
    };
}

macro_rules! expect_ty_node {
    ( ($node:expr ; $form:expr ; $loc:expr) $env:ident ; $body:expr ) => {{
        // This is tied to the signature of `Custom`
        let $env = $node.ty_destructure($form, $loc)?;
        $body
    }};
}

macro_rules! _get_leaf_operation {
    ($env:expr, =, $name:tt) => {
        $env.get_leaf_or_panic(&crate::name::n(stringify!($name)))
    };
    ($env:expr, *=, $name:tt) => {
        $env.get_rep_leaf_or_panic(crate::name::n(stringify!($name)))
    };
}

// Bind names based on the contents of a `Node`.
// Use `=` for plain leaves, or `*=` for repeated leaves.
// This uses barewords like `u!` does, but it's fine for runtime use.
// TODO: use this a lot more
macro_rules! node_let {
    ( $node:expr => {$nt:tt $form:tt } $( $n:ident $operation:tt $name:tt ),* ) => (
        // Extra element is the ensure it's a tuple and not trigger `unused_params`.
        let ( (), $( $n ),* ) = {
            expect_node!( ($node ;
                crate::core_forms::find_core_form(stringify!($nt), stringify!($form))) env ;
                    ((), $( _get_leaf_operation!(env, $operation, $name) ),* )
            )
        };
    )
}

// TODO: get rid of these; just use `form_pat!((alt …))` directly.
macro_rules! forms_to_form_pat {
    ( $( $form:expr ),* ) => {
        form_pat!((alt $( (scope $form) ),* ))
    }
}

macro_rules! forms_to_form_pat_export {
    ( $( $form:expr => $export:tt),* ) => {
        form_pat!((alt $( (scope $form, $export) ),* ))
    }
}

// panicking destructor (when the type system should offer protection)

macro_rules! extract {
    (($v:expr) $( $expected:path = ( $( $sub:pat ),* ) => $body:expr);* ) => {
        match * $v {
            $( $expected ( $($sub),* ) => { $body } )*
            _ => { icp!("{:#?} isn't a {:#?}", $v, stringify!( $($expected),* )) }
        }
    }
}

// Reification helper (doesn't work on parameterized types...)
// TODO: just delete this, or actually add `Smuggled(std::any::Any)` to `Value`.
macro_rules! cop_out_reifiability {
    ( $underlying_type:ty, $ty_name:tt ) => {
        impl Reifiable for $underlying_type {
            fn ty_name() -> Name { n(stringify!($ty_name)) }

            fn reify(&self) -> Value { Value::Smuggled(self.clone()) }

            fn reflect(v: &Value) -> Self {
                extract!((v) Value::Smuggled = (ref s) =>
                    s.downcast_ref::<Self>().expect("Smuggling has gone terribly wrong!").clone())
            }
        }
    }
}

macro_rules! vrep {
    ( $( $contents:tt )*) => {
        vrep_accum![ ( $( $contents )* , ) ]
    };
}

macro_rules! vrep_accum {
    // For ease of parsing, expects a trailing comma!
    (($elt:expr, $($rest:tt)*)  $($accum:tt)* ) => {
        // ... and produces a leading comma
        vrep_accum!(($($rest)*)  $($accum)* , crate::util::vrep::VRepElt::Single($elt))
    };
    (($elt:expr => ( $( $driver:expr),* ), $($rest:tt)*)  $($accum:tt)* ) => {
        vrep_accum!(($($rest)*)
                    $($accum)* ,
                    crate::util::vrep::VRepElt::Rep($elt,
                        vec![ $( crate::name::n(stringify!($driver)) ),* ])
                )
    };
    // Expect the leading comma:
    (() , $($accum:tt)* ) => {
        crate::util::vrep::VRep(vec![ $( $accum )* ])
    };
}

// Testing
#[macro_export]
macro_rules! assert_m {
    ($got:expr, $expected:pat => $body:stmt) => {{
        let got = $got;
        match got.clone() {
            $expected => {
                // The `()` is actually a unit to avoid an "unnecessary trailing semicolon warning".
                // The `;` is to keep `cargo fmt` from removing the non-unnecessary `{}`.
                $body();
            }
            _ => assert!(false, "{:#?} does not match {:#?}", got, stringify!($expected)),
        }
    }};
    // Deprecated:
    ($got:expr, $expected:pat, $body:expr) => {{
        let got = $got;
        match got.clone() {
            $expected => assert!($body),
            _ => assert!(false, "{:#?} does not match {:#?}", got, stringify!($expected)),
        }
    }};
    ($got:expr, $expected:pat) => {
        assert_m!($got, $expected, true)
    };
}

macro_rules! layer_watch {
    {$layer:ident : $( $body:stmt );* } => {
        $layer.with(|l| l.borrow_mut().0 += 1); // layers
        $layer.with(|l| l.borrow_mut().1 += 1); // steps
        let res = {
            $( $body )*
        };
        $layer.with(|l| l.borrow_mut().0 -= 1);
        res
    }
}

// "Layer debug"
macro_rules! ld {
    ($layer:ident, $enabled:expr, $template:tt, $($arg:expr),*) => {{
        if $enabled.with(|e| *e) {
            let layers = $layer.with(|l| l.borrow().0) - 1;
            for i in 1..layers {
                if i % 2 == 0 {
                    print!("║ ")
                } else {
                    print!("│ ");
                }
            }
            if layers > 0 {
                if layers % 2 == 0 {
                    print!("╠═")
                } else {
                    print!("├─");
                }
            }
            print!($template, $($arg),*);
            print!(" ({})", $layer.with(|l| l.borrow().1));
            println!();
        }
    }}
}

// "Layer debug, continued"
macro_rules! lc {
    ($layer:ident, $enabled:expr, $template:tt, $($arg:expr),*) => {{
        if $enabledwith(|e| *) {
            let layers = $layer.with(|l| l.borrow().0) - 1;
            for i in 1..(layers+1) {
                if i % 2 == 0 {
                    print!("║ ")
                } else {
                    print!("│ ");
                }
            }
            println!($template, $($arg),*);
        }
    }}
}