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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
//  DSL.rs
//    by Lut99
//
//  Created:
//    18 Aug 2022, 13:46:22
//  Last edited:
//    12 Dec 2023, 14:57:30
//  Auto updated?
//    Yes
//
//  Description:
//!   Prints the `brane-dsl` AST in BraneScript-like Syntax.
//

use std::io::Write;

use brane_dsl::ast::{self as dsl_ast, Attribute, Block, Expr, Identifier, Literal, Program, Property, PropertyExpr, Stmt};
use brane_dsl::location::AllowedLocations;

pub use crate::errors::AstError as Error;


/***** TESTS *****/
#[cfg(test)]
pub mod tests {
    use brane_dsl::ParserOptions;
    use brane_shr::utilities::{create_data_index, create_package_index, test_on_dsl_files};
    use specifications::data::DataIndex;
    use specifications::package::PackageIndex;

    use super::*;
    use crate::{CompileResult, CompileStage, compile_program_to};


    /// 'Tests' the traversal by printing the AST for every node.
    #[test]
    fn test_print() {
        test_on_dsl_files("BraneScript", |path, code| {
            println!("{}", (0..80).map(|_| '-').collect::<String>());
            println!("File '{}' gave us:", path.display());

            // Load the package index
            let pindex: PackageIndex = create_package_index();
            let dindex: DataIndex = create_data_index();

            // Run up to this traversal
            let program: Program = match compile_program_to(code.as_bytes(), &pindex, &dindex, &ParserOptions::bscript(), CompileStage::None) {
                CompileResult::Program(p, warns) => {
                    // Print warnings if any
                    for w in warns {
                        w.prettyprint(path.to_string_lossy(), &code);
                    }
                    p
                },
                CompileResult::Eof(err) => {
                    // Print the error
                    err.prettyprint(path.to_string_lossy(), &code);
                    panic!("Failed to parse file (see output above)");
                },
                CompileResult::Err(errs) => {
                    // Print the errors
                    for e in errs {
                        e.prettyprint(path.to_string_lossy(), &code);
                    }
                    panic!("Failed to parse file (see output above)");
                },

                _ => {
                    unreachable!();
                },
            };

            // Now print the tree
            do_traversal(program, std::io::stdout()).unwrap();
            println!("{}\n\n", (0..80).map(|_| '-').collect::<String>());
        });
    }
}





/***** MACROS ******/
/// Generates the correct number of spaces for an indent.
macro_rules! indent {
    ($n_spaces:expr) => {
        ((0..$n_spaces).map(|_| ' ').collect::<String>())
    };
}





/***** CONSTANTS *****/
/// Determines the increase in indentation for every nested level.
const INDENT_SIZE: usize = 4;





/***** TRAVERSAL FUNCTIONS *****/
/// Prints a Stmt node.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `stmt`: The Stmt to traverse.
/// - `indent`: The current base indent of all new lines to write.
///
/// # Returns
/// Nothing, but does print it.
pub fn pass_stmt(writer: &mut impl Write, stmt: &Stmt, indent: usize) -> std::io::Result<()> {
    // Match on the statement itself
    use Stmt::*;
    match stmt {
        Attribute(attr) => {
            // Print the attribute
            pass_attr(writer, attr, false, indent)?;
        },
        AttributeInner(attr) => {
            // Print the attribute
            pass_attr(writer, attr, true, indent)?;
        },

        Block { block } => {
            // Pass over the block instead, but do print the indentation first.
            write!(writer, "{}", indent!(indent))?;
            pass_block(writer, block, indent)?;
            writeln!(writer)?;
        },

        Import { name, version, st_funcs: _, st_classes: _, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }

            // Print as an import statement
            write!(writer, "{}import ", indent!(indent))?;
            // Print the identifier
            pass_identifier(writer, name)?;
            // Print the version, optionally
            if let Literal::Semver { range, .. } = &version {
                if range.is_some() {
                    write!(writer, "[")?;
                    pass_literal(writer, version)?;
                    write!(writer, "]")?;
                }
            } else {
                panic!("Got a non-Semver Literal '{version:?}' in an import statement; this should never happen!");
            }
            // Do newline
            writeln!(writer)?;
        },
        FuncDef { ident, params, code, st_entry: _, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }

            // Print the 'func' prefix
            write!(writer, "{}func ", indent!(indent))?;
            // Print the identifier
            pass_identifier(writer, ident)?;
            // Print the parameters
            write!(writer, "(")?;
            let mut first = true;
            for p in params {
                if first {
                    first = false;
                } else {
                    write!(writer, ", ")?;
                }
                pass_identifier(writer, p)?;
            }
            // Print the block
            write!(writer, ") ")?;
            pass_block(writer, code, indent)?;
            writeln!(writer)?;
        },
        ClassDef { ident, props, methods, st_entry: _, symbol_table: _, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }

            // Print the 'class' prefix
            write!(writer, "{}class ", indent!(indent))?;
            // Print the identifier
            pass_identifier(writer, ident)?;
            // Print the class opening
            writeln!(writer, " {{")?;
            // Print the properties
            let largest_prop: usize = props.iter().map(|p| p.name.value.len()).max().unwrap_or(0);
            for p in props {
                pass_property(writer, p, largest_prop, indent + 3)?;
            }
            // Print a newline if any properties have been written
            if !props.is_empty() {
                writeln!(writer)?;
            }
            // Print the methods
            for m in methods {
                pass_stmt(writer, m, indent + 3)?;
            }
            // Finally, print the closing bracket
            writeln!(writer, "{}}}", indent!(indent))?;
        },
        Return { expr, data_type: _, output: _, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }

            // Print the return
            write!(writer, "{}return", indent!(indent))?;
            // If there is an expression, print it
            if let Some(expr) = expr {
                write!(writer, " ")?;
                pass_expr(writer, expr, indent)?;
            }
            // Print the semicolon
            writeln!(writer, ";")?;
        },

        If { cond, consequent, alternative, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }
            // Print the if first + its condition
            write!(writer, "{}if (", indent!(indent))?;
            pass_expr(writer, cond, indent)?;
            write!(writer, ") ")?;
            // Print the if-block
            pass_block(writer, consequent, indent)?;
            // If there is an else, do that
            if let Some(alternative) = alternative {
                write!(writer, " else ")?;
                pass_block(writer, alternative, indent)?;
            }
            writeln!(writer)?;
        },
        For { initializer, condition, increment, consequent, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }
            // Print the three for parts
            write!(writer, "{}for (", indent!(indent))?;
            pass_stmt(writer, initializer, indent)?;
            write!(writer, " ")?;
            pass_expr(writer, condition, indent)?;
            write!(writer, "; ")?;
            pass_stmt(writer, increment, indent)?;
            write!(writer, ") ")?;
            // Print the block
            pass_block(writer, consequent, indent)?;
            writeln!(writer)?;
        },
        While { condition, consequent, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }
            // Print the while + its condition
            write!(writer, "{}while (", indent!(indent))?;
            pass_expr(writer, condition, indent)?;
            write!(writer, ") ")?;
            // Print the block
            pass_block(writer, consequent, indent)?;
            writeln!(writer)?;
        },
        Parallel { result, blocks, merge: _, st_entry: _, attrs, range: _ } => {
            // Print the statement's attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }

            // If there is a result, parse that first
            write!(writer, "{}", indent!(indent))?;
            if let Some(result) = result {
                write!(writer, "let ")?;
                pass_identifier(writer, result)?;
                write!(writer, " = ")?;
            }
            // Print the parallel thingy
            writeln!(writer, "parallel [")?;
            // Print the blocks
            for b in blocks {
                // Pass over the block instead, but do print the indentation first.
                write!(writer, "{}", indent!(indent + INDENT_SIZE))?;
                pass_block(writer, b, indent)?;
                writeln!(writer)?;
            }
            writeln!(writer, "{}]", indent!(indent))?;
        },

        LetAssign { name, value, st_entry: _, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }

            // Print the let thingy first + the name
            write!(writer, "{}let ", indent!(indent))?;
            pass_identifier(writer, name)?;
            // Print the expression
            write!(writer, " := ")?;
            pass_expr(writer, value, indent)?;
            writeln!(writer, ";")?;
        },
        Assign { name, value, st_entry: _, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }

            // Just print the identifier
            write!(writer, "{}", indent!(indent))?;
            pass_identifier(writer, name)?;
            // Print the expression
            write!(writer, " := ")?;
            pass_expr(writer, value, indent)?;
            writeln!(writer, ";")?;
        },
        Expr { expr, data_type: _, attrs, range: _ } => {
            // Print the attributes
            for attr in attrs {
                pass_attr(writer, attr, false, indent)?;
            }

            // Print the expression + semicolon
            write!(writer, "{}", indent!(indent))?;
            pass_expr(writer, expr, indent)?;
            writeln!(writer, ";")?;
        },

        Empty {} => {},
    }

    // Done
    Ok(())
}

/// Prints an attribute.
///
/// # Arguments
/// - `writer`: The [`Write`]r to write to.
/// - `attr`: The [`Attribute`] to traverse.
/// - `is_inner`: If true, prints as an inner attribute (i.e., `#![...]` instead of `#[...]`).
/// - `indent`: The current base indentation of all new lines to write.
///
/// # Errors
/// This function may error if we failed to write to `writer`.
pub fn pass_attr(writer: &mut impl Write, attr: &Attribute, is_inner: bool, indent: usize) -> std::io::Result<()> {
    // Print the preceding tokens
    write!(writer, "{}#{}[", indent!(indent), if is_inner { "!" } else { "" })?;
    // Print the attribute value
    match attr {
        Attribute::KeyPair { key, value, range: _ } => {
            pass_identifier(writer, key)?;
            write!(writer, " = ")?;
            pass_literal(writer, value)?;
        },
        Attribute::List { key, values, range: _ } => {
            pass_identifier(writer, key)?;
            write!(writer, "(")?;
            let mut first: bool = true;
            for value in values {
                if first {
                    first = false;
                } else {
                    write!(writer, ", ")?;
                }
                pass_literal(writer, value)?;
            }
            write!(writer, ")")?;
        },
    }
    // Print the suffixing tokens
    writeln!(writer, "]")?;

    // Done!
    Ok(())
}

/// Prints a Block node.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `block`: The Block to traverse.
/// - `indent`: The current base indent of all new lines to write.
///
/// # Returns
/// Nothing, but does print it.
pub fn pass_block(writer: &mut impl Write, block: &Block, indent: usize) -> std::io::Result<()> {
    // Print the curly bracket (no indent used, since it's expression position)
    writeln!(writer, "{{")?;

    // Write the block's attributes as inner ones
    for attr in &block.attrs {
        pass_attr(writer, attr, true, indent + INDENT_SIZE)?;
    }

    // Print all statements with extra indent
    for s in block.stmts.iter() {
        pass_stmt(writer, s, indent + INDENT_SIZE)?;
    }

    // Print the closing curly bracket
    write!(writer, "{}}}", indent!(indent))?;

    // Done
    Ok(())
}

/// Prints an Identifier node.
///
/// This will always be printed as a non-statement, so no indentation required.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `identifier`: The Identifier to traverse.
///
/// # Returns
/// Nothing, but does print it.
pub fn pass_identifier(writer: &mut impl Write, identifier: &Identifier) -> std::io::Result<()> {
    // Print the full value
    write!(writer, "{}", identifier.value)
}

/// Prints a Property node.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `prop`: The Property to traverse.
/// - `largest_prop`: The longest property name. It will auto-pad all names to this length to make a nice list.
/// - `indent`: The current base indent of all new lines to write.
///
/// # Returns
/// Nothing, but does print it.
pub fn pass_property(writer: &mut impl Write, prop: &Property, largest_prop: usize, indent: usize) -> std::io::Result<()> {
    // Print the identation, then the name identifier
    write!(writer, "{}", indent!(indent))?;
    pass_identifier(writer, &prop.name)?;
    // Print the colon, then the data type
    writeln!(writer, "{} : {};", indent!(largest_prop - prop.name.value.len()), prop.data_type)?;

    // Done
    Ok(())
}

/// Prints an Expr node.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `expr`: The Expr to traverse.
/// - `indent`: The current base indent of all new lines to write.
///
/// # Returns
/// Nothing, but does print it.
pub fn pass_expr(writer: &mut impl Write, expr: &Expr, indent: usize) -> std::io::Result<()> {
    // Match the expression
    use Expr::*;
    match expr {
        Cast { expr, target, .. } => {
            // Print the cast operator
            write!(writer, "(({target}) ")?;
            // Print the expression
            pass_expr(writer, expr, indent)?;
            // Print the closing bracket
            write!(writer, ")")?;
        },

        Call { expr, args, st_entry: _, locations, input: _, result: _, metadata, range: _ } => {
            // Print the identifying expression
            pass_expr(writer, expr, indent)?;
            // Print the arguments
            write!(writer, "(")?;
            let mut first = true;
            for a in args {
                if first {
                    first = false;
                } else {
                    write!(writer, ", ")?;
                }
                pass_expr(writer, a, indent)?;
            }
            // Print the closing bracket
            write!(writer, ")")?;

            // Print locations
            if let AllowedLocations::Exclusive(locs) = locations {
                write!(writer, " @[{}]", locs.iter().map(|l| l.into()).collect::<Vec<String>>().join(","))?;
            }
            // Print metadata
            if !metadata.is_empty() {
                write!(
                    writer,
                    " <{}>",
                    metadata
                        .iter()
                        .map(|md| format!(
                            "#{}.{}",
                            if md.owner.contains(' ') { format!("\"{}\"", md.owner) } else { md.owner.clone() },
                            if md.tag.contains(' ') { format!("\"{}\"", md.tag) } else { md.tag.clone() }
                        ))
                        .collect::<Vec<String>>()
                        .join(" ")
                )?;
            }
        },
        Array { values, .. } => {
            // Print the values wrapped in '[]'
            write!(writer, "[")?;
            let mut first = true;
            for v in values {
                if first {
                    first = false;
                } else {
                    write!(writer, ", ")?;
                }
                pass_expr(writer, v, indent)?;
            }
            write!(writer, "]")?;
        },
        ArrayIndex { array, index, .. } => {
            // Print the array first
            pass_expr(writer, array, indent)?;
            // Print the index expression wrapped in '[]'
            write!(writer, "[")?;
            pass_expr(writer, index, indent)?;
            write!(writer, "]")?;
        },
        Pattern { exprs, .. } => {
            // We use ad-hoc syntax for now
            write!(writer, "Pattern<")?;
            let mut first = true;
            for e in exprs {
                if first {
                    first = false;
                } else {
                    write!(writer, ", ")?;
                }
                pass_expr(writer, e, indent)?;
            }
            write!(writer, ">")?;
        },

        UnaOp { op, expr, .. } => {
            // How to print exact is operator-determined
            match op {
                dsl_ast::UnaOp::Idx { .. } => {
                    // Print the expr expression wrapped in '[]'
                    write!(writer, "[")?;
                    pass_expr(writer, expr, indent)?;
                    write!(writer, "]")?;
                },
                dsl_ast::UnaOp::Not { .. } => {
                    // Print the logical negation, then the expression
                    write!(writer, "(!")?;
                    pass_expr(writer, expr, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::UnaOp::Neg { .. } => {
                    // Print the mathmatical negation, then the expression
                    write!(writer, "(-")?;
                    pass_expr(writer, expr, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::UnaOp::Prio { .. } => {
                    // Print simply in between brackets
                    write!(writer, "(")?;
                    pass_expr(writer, expr, indent)?;
                    write!(writer, ")")?;
                },
            }
        },
        BinOp { op, lhs, rhs, .. } => {
            // How to print exact is operator-determined
            match op {
                dsl_ast::BinOp::And { .. } => {
                    // Print lhs && rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " && ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Or { .. } => {
                    // Print lhs || rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " || ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },

                dsl_ast::BinOp::Add { .. } => {
                    // Print lhs + rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " + ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Sub { .. } => {
                    // Print lhs - rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " - ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Mul { .. } => {
                    // Print lhs * rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " * ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Div { .. } => {
                    // Print lhs / rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " / ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Mod { .. } => {
                    // Print lhs % rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " % ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },

                dsl_ast::BinOp::Eq { .. } => {
                    // Print lhs == rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " == ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Ne { .. } => {
                    // Print lhs != rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " != ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Lt { .. } => {
                    // Print lhs < rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " < ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Le { .. } => {
                    // Print lhs <= rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " <= ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Gt { .. } => {
                    // Print lhs > rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " > ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                dsl_ast::BinOp::Ge { .. } => {
                    // Print lhs >= rhs
                    write!(writer, "(")?;
                    pass_expr(writer, lhs, indent)?;
                    write!(writer, " >= ")?;
                    pass_expr(writer, rhs, indent)?;
                    write!(writer, ")")?;
                },
                // dsl_ast::BinOp::Proj{ .. } => {
                //     // Print lhs.rhs
                //     write!(writer, "(")?;
                //     pass_expr(writer, lhs, indent)?;
                //     write!(writer, ".")?;
                //     pass_expr(writer, rhs, indent)?;
                //     write!(writer, ")")?;
                // },
            }
        },
        Proj { lhs, rhs, .. } => {
            // Print lhs.rhs
            pass_expr(writer, lhs, indent)?;
            write!(writer, ".")?;
            pass_expr(writer, rhs, indent)?;
        },

        Instance { name, properties, .. } => {
            // Print 'new Name'
            write!(writer, "new ")?;
            pass_identifier(writer, name)?;
            // Print the body
            writeln!(writer, " {{")?;
            let largest_prop: usize = properties.iter().map(|p| p.name.value.len()).max().unwrap_or(0);
            for p in properties {
                // Print the proprerty name followed by its value
                pass_property_expr(writer, p, largest_prop, indent + 3)?;
            }
            // Print the closing thingy
            write!(writer, "{}}}", indent!(indent))?;
        },
        VarRef { name, .. } => {
            // Print the identifier
            pass_identifier(writer, name)?;
        },
        Identifier { name, .. } => {
            // Print the identifier
            pass_identifier(writer, name)?;
        },
        Literal { literal } => {
            // Print the literal
            pass_literal(writer, literal)?;
        },

        Empty {} => {},
    }

    // Done
    Ok(())
}

/// Prints a PropertyExpr node.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `prop`: The PropertyExpr to traverse.
/// - `largest_prop`: The longest property name. It will auto-pad all names to this length to make a nice list.
///
/// # Returns
/// Nothing, but does print it.
pub fn pass_property_expr(writer: &mut impl Write, prop: &PropertyExpr, largest_prop: usize, indent: usize) -> std::io::Result<()> {
    // Print the identation, then the name identifier
    write!(writer, "{}", indent!(indent))?;
    pass_identifier(writer, &prop.name)?;
    // Print the colon, then the expression
    write!(writer, "{} : ", indent!(largest_prop - prop.name.value.len()))?;
    pass_expr(writer, &prop.value, indent + 3)?;
    // Finally print the comma
    writeln!(writer, ",")?;

    // Done
    Ok(())
}

/// Prints a Literal node.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `literal`: The Literal to traverse.
///
/// # Returns
/// Nothing, but does print it.
pub fn pass_literal(writer: &mut impl Write, literal: &Literal) -> std::io::Result<()> {
    // Match on the exact literal kind
    use Literal::*;
    match literal {
        Null { .. } => {
            write!(writer, "null")?;
        },
        Boolean { value, .. } => {
            write!(writer, "{}", if *value { "true" } else { "false" })?;
        },
        Integer { value, .. } => {
            write!(writer, "{}", *value)?;
        },
        Real { value, .. } => {
            write!(writer, "{}", *value)?;
        },
        String { value, .. } => {
            write!(writer, "\"{value}\"")?;
        },
        Semver { value, .. } => {
            write!(writer, "{value}")?;
        },
        Void { .. } => {
            write!(writer, "<void>")?;
        },
    }

    // Done
    Ok(())
}





/***** LIBRARY *****/
/// Starts printing the root of the AST (i.e., a series of statements).
///
/// # Arguments
/// - `root`: The root node of the tree on which this compiler pass will be done.
/// - `writer`: The `Write`r to write to.
///
/// # Returns
/// The same root node as went in (since this compiler pass performs no transformations on the tree).
///
/// # Errors
/// This pass doesn't really error, but is here for convention purposes.
pub fn do_traversal(root: Program, mut writer: impl Write) -> Result<Program, Vec<Error>> {
    // Write the metadata
    if let Err(err) = writeln!(
        &mut writer,
        "{}\n",
        root.metadata
            .iter()
            .map(|md| format!(
                "#{}.{}",
                if md.owner.contains(' ') { format!("\"{}\"", md.owner) } else { md.owner.clone() },
                if md.tag.contains(' ') { format!("\"{}\"", md.tag) } else { md.tag.clone() }
            ))
            .collect::<Vec<String>>()
            .join(" ")
    ) {
        return Err(vec![Error::WriteError { err }]);
    }
    // Write the attributes
    for a in &root.block.attrs {
        if let Err(err) = pass_attr(&mut writer, a, true, 0) {
            return Err(vec![Error::WriteError { err }]);
        }
    }
    if let Err(err) = writeln!(&mut writer) {
        return Err(vec![Error::WriteError { err }]);
    }
    // Iterate over all statements and run the appropriate match
    for s in &root.block.stmts {
        if let Err(err) = pass_stmt(&mut writer, s, 0) {
            return Err(vec![Error::WriteError { err }]);
        }
    }

    // Done
    Ok(root)
}