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
//  AST.rs
//    by Lut99
//
//  Created:
//    31 Aug 2022, 09:25:11
//  Last edited:
//    06 Feb 2024, 11:38:47
//  Auto updated?
//    Yes
//
//  Description:
//!   Prints the `brane-ast` AST.
//

use std::collections::HashSet;
use std::io::Write;

use crate::ast::{Edge, EdgeInstr, FunctionDef, SymTable, TaskDef, Workflow};
use crate::data_type::DataType;
pub use crate::errors::AstError as Error;
use crate::func_id::FunctionId;


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

/// Generates a properly padded line number.
macro_rules! line_number {
    ($l:expr) => {
        format!("{:>1$}", $l, LINE_SIZE)
    };
}





/***** CONSTANTS *****/
/// Determines the number of decimals the line numbers will always have
const LINE_SIZE: usize = 4;
/// Determines the increase in indentation for every nested level.
const INDENT_SIZE: usize = 4;





/***** TRAVERSAL FUNCTIONS *****/
/// Prints the global table of the Workflow.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `table`: The SymTable we print.
/// - `indent`: The indent with which to print the table.
///
/// # Returns
/// Nothing, but does print the table to stdout.
fn pass_table(writer: &mut impl Write, table: &SymTable, indent: usize) -> std::io::Result<()> {
    // Simply print all fields in a less-cluttering-to-most-cluttering order

    // Variables first
    for v in &table.vars {
        writeln!(writer, "{}Var {}: {};", indent!(indent), &v.name, v.data_type)?;
    }
    if !table.vars.is_empty() {
        writeln!(writer)?;
    }

    // Then, print all normal functions...
    for f in &table.funcs {
        writeln!(
            writer,
            "{}Function {}({}){};",
            indent!(indent),
            &f.name,
            f.args.iter().map(|a| format!("{a}")).collect::<Vec<String>>().join(", "),
            if f.ret != DataType::Void { format!(" -> {}", f.ret) } else { String::new() },
        )?;
    }
    // ...and all tasks
    for t in &table.tasks {
        match t {
            TaskDef::Compute(def) => {
                if !def.requirements.is_empty() {
                    writeln!(writer, "{}#[requirements = {:?}]", indent!(indent), def.requirements)?;
                }
                writeln!(
                    writer,
                    "{}Task<Compute> {}{}::{}({}){};",
                    indent!(indent),
                    def.package,
                    if !def.version.is_latest() { format!("<{}>", def.version) } else { String::new() },
                    &def.function.name,
                    def.function.args.iter().enumerate().map(|(i, a)| format!("{}: {}", def.args_names[i], a)).collect::<Vec<String>>().join(", "),
                    if def.function.ret != DataType::Void { format!(" -> {}", def.function.ret) } else { String::new() },
                )?;
            },
            TaskDef::Transfer => writeln!(writer, "Task<Transfer>;")?,
        }
    }
    if !table.vars.is_empty() || !table.funcs.is_empty() || !table.tasks.is_empty() {
        writeln!(writer)?;
    }

    // Finally print the class definitions
    for c in &table.classes {
        writeln!(
            writer,
            "{}Class {}{} {{",
            indent!(indent),
            if let Some(package) = &c.package { format!("{package}::") } else { String::new() },
            &c.name
        )?;
        // Print all properties
        for p in &c.props {
            writeln!(writer, "{}property {}: {};", indent!(INDENT_SIZE + indent), &p.name, p.data_type)?;
        }
        // Print all functions
        for m in &c.methods {
            let m: &FunctionDef = &table.funcs[*m];
            writeln!(
                writer,
                "{}method &{}({}){};",
                indent!(INDENT_SIZE + indent),
                &m.name,
                m.args.iter().map(|a| format!("{a}")).collect::<Vec<String>>().join(", "),
                if m.ret != DataType::Void { format!(" -> {}", m.ret) } else { String::new() },
            )?;
        }
        writeln!(writer, "{}}};", indent!(indent))?;
    }
    if !table.vars.is_empty() || !table.funcs.is_empty() || !table.tasks.is_empty() || !table.classes.is_empty() {
        writeln!(writer)?;
    }

    // And the results
    for (name, avail) in table.results.iter() {
        writeln!(writer, "{}IntermediateResult '{}' -> '{:?}'", indent!(INDENT_SIZE), name, avail)?;
    }

    // Done
    Ok(())
}

/// Prints a given Edge buffer to stdout.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `index`: The starting index in the edges list to follow.
/// - `edges`: The list of Edges to print.
/// - `table`: The SymTable we use to resolve indices.
/// - `indent`: The indent with which to print the buffer.
/// - `done`: A set of nodes we've already seen that we use to avoid getting stuck forever.
///
/// # Returns
/// Nothing, but does print the buffer to stdout.
fn pass_edges(
    writer: &mut impl Write,
    index: usize,
    edges: &[Edge],
    table: &SymTable,
    indent: usize,
    done: &mut HashSet<usize>,
) -> std::io::Result<()> {
    // Loop until we are out-of-bounds or encounter a stop
    let mut i: usize = index;
    while i < edges.len() {
        // If we've alread done this one, stop
        if done.contains(&i) {
            writeln!(writer, "{} {}<Link to {}>", indent!(LINE_SIZE), indent!(indent), i)?;
            break;
        }
        done.insert(i);

        // Get the node's value
        let node: &Edge = &edges[i];

        // Match on it
        use Edge::*;
        match node {
            Node { task, locs, at, input, result, metadata, next } => {
                // Write the Node as a task call
                writeln!(
                    writer,
                    "{} {}Node({}){}{}{}{}",
                    line_number!(i),
                    indent!(indent),
                    match &table.task(*task) {
                        TaskDef::Compute(def) => format!(
                            "{}{}::{}",
                            def.package,
                            if !def.version.is_latest() { format!("<{}>", def.version) } else { String::new() },
                            def.function.name
                        ),
                        TaskDef::Transfer => "__builtin::transfer".into(),
                    },
                    if locs.is_restrictive() { format!(" <limited to: {}>", locs.restricted().join(",")) } else { String::new() },
                    if let Some(at) = at { format!(" @{at}") } else { String::new() },
                    if !input.is_empty() || result.is_some() {
                        format!(
                            " [{} -> {}]",
                            if !input.is_empty() {
                                input
                                    .iter()
                                    .map(|(name, avail)| {
                                        format!("'{}'{}", name, if let Some(avail) = avail { format!(" ({avail:?})") } else { String::new() })
                                    })
                                    .collect::<Vec<String>>()
                                    .join(", ")
                                    .to_string()
                            } else {
                                "''".into()
                            },
                            if let Some(name) = result { format!("'{name}'") } else { "''".into() },
                        )
                    } else {
                        String::new()
                    },
                    if !metadata.is_empty() {
                        format!(
                            " <{}>",
                            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() },
                                    if let Some((assigner, signature)) = &md.signature {
                                        format!(" <{assigner}.{signature}>")
                                    } else {
                                        String::new()
                                    }
                                ))
                                .collect::<Vec<String>>()
                                .join(",")
                        )
                    } else {
                        String::new()
                    },
                )?;

                // Move to the next node
                i = *next;
            },
            Linear { instrs, next } => {
                // Write the instructions within this edge
                write!(writer, "{} {}[", line_number!(i), indent!(indent))?;
                let mut first: bool = true;
                for i in instrs {
                    if first {
                        first = false;
                    } else {
                        write!(writer, "\n{} {} ", indent!(LINE_SIZE), indent!(indent))?;
                    }
                    pass_edge_instr(writer, i, table)?;
                }
                writeln!(writer, "]")?;

                // Move to the next node
                i = *next;
            },
            Stop {} => {
                // Write a simple stop
                writeln!(writer, "{} {}<Stop>", line_number!(i), indent!(indent))?;

                // Do still move to the next node, if any
                i += 1;
            },

            Branch { true_next, false_next, merge } => {
                // Add the merge point to the 'already done' map for this loop
                let rem_merge: Option<bool> = merge.as_ref().map(|m| done.insert(*m));

                // Write the two branches of the branch
                write!(writer, "{} {}Branch {{", line_number!(i), indent!(indent))?;
                if merge.is_none() || merge.as_ref().unwrap() != true_next {
                    writeln!(writer)?;
                    pass_edges(writer, *true_next, edges, table, INDENT_SIZE + indent, done)?;
                    write!(writer, "{} {}", indent!(LINE_SIZE), indent!(indent))?;
                }
                write!(writer, "}} {{")?;
                if false_next.is_some() && (merge.is_none() || merge.as_ref().unwrap() != false_next.as_ref().unwrap()) {
                    writeln!(writer)?;
                    pass_edges(writer, false_next.unwrap(), edges, table, INDENT_SIZE + indent, done)?;
                    write!(writer, "{} {}", indent!(LINE_SIZE), indent!(indent))?;
                }
                writeln!(writer, "}}")?;

                // Remove the 'already done' to make sure it is written next
                if rem_merge.is_some() && rem_merge.unwrap() {
                    done.remove(merge.as_ref().unwrap());
                }

                // Move to the next node
                match merge {
                    Some(merge) => {
                        i = *merge;
                    },
                    None => i += 1,
                }
            },
            Parallel { branches, merge } => {
                // Add the merge point to the 'already done' map for this loop
                let rem_merge: bool = done.insert(*merge);

                // Write the branches of the branch, in sequence
                write!(writer, "{} {}Parallel {{", line_number!(i), indent!(indent))?;
                let mut first: bool = true;
                for i in 0..branches.len() {
                    if *merge != branches[i] {
                        // Add the next branch
                        let rem_merge: bool = if i < branches.len() - 1 { done.insert(branches[i + 1]) } else { false };

                        // Print it
                        if first {
                            first = false;
                        } else {
                            write!(writer, "}} {{")?;
                        }
                        writeln!(writer)?;
                        pass_edges(writer, branches[i], edges, table, INDENT_SIZE + indent, done)?;
                        write!(writer, "{} {}", indent!(LINE_SIZE), indent!(indent))?;

                        // Remove the next one again
                        if rem_merge {
                            done.remove(&branches[i + 1]);
                        }
                    }
                }
                writeln!(writer, "}}")?;

                // Remove the 'already done' to make sure it is written next
                if rem_merge {
                    done.remove(merge);
                }

                // Move to the next node
                i = *merge;
            },
            Join { merge, next } => {
                // Write the join itself
                writeln!(writer, "{} {}Join({:?})", line_number!(i), indent!(indent), *merge)?;

                // Move to the next node
                i = *next;
            },

            Loop { cond, body, next } => {
                // Write the loop
                writeln!(writer, "{} {}Loop {{", line_number!(i), indent!(indent))?;
                pass_edges(writer, *cond, edges, table, INDENT_SIZE + indent, done)?;
                write!(writer, "{} {}}} {{", indent!(LINE_SIZE), indent!(indent))?;
                if next.is_none() || body != next.as_ref().unwrap() {
                    writeln!(writer)?;
                    pass_edges(writer, *body, edges, table, INDENT_SIZE + indent, done)?;
                    write!(writer, "{} {}", indent!(LINE_SIZE), indent!(indent))?;
                }
                writeln!(writer, "}}")?;

                // Move to the next node
                match next {
                    Some(next) => {
                        i = *next;
                    },
                    None => {
                        i += 1;
                    },
                }
            },

            Call { input, result, next } => {
                // Just print it
                writeln!(
                    writer,
                    "{} {}Call{}",
                    line_number!(i),
                    indent!(indent),
                    if !input.is_empty() || !result.is_empty() {
                        format!(
                            " [{} -> {}]",
                            if !input.is_empty() {
                                input.iter().map(|name| format!("'{name}'")).collect::<Vec<String>>().join(" or ").to_string()
                            } else {
                                "<none>".into()
                            },
                            if !result.is_empty() {
                                result.iter().map(|name| format!("'{name}'")).collect::<Vec<String>>().join(" or ").to_string()
                            } else {
                                "<none>".into()
                            },
                        )
                    } else {
                        String::new()
                    },
                )?;

                // Move to the next node
                i = *next;
            },
            Return { result } => {
                writeln!(
                    writer,
                    "{} {}Return{}",
                    line_number!(i),
                    indent!(indent),
                    if !result.is_empty() {
                        format!(
                            " [returns {}]",
                            if !result.is_empty() {
                                result.iter().map(|name| format!("'{name}'")).collect::<Vec<String>>().join(" or ").to_string()
                            } else {
                                "<none>".into()
                            },
                        )
                    } else {
                        String::new()
                    }
                )?;
                i += 1
            },
        }
    }

    // Done
    Ok(())
}

/// Prints a given EdgeInstr to stdout.
///
/// # Arguments
/// - `writer`: The `Write`r to write to.
/// - `instr`: The EdgeInstr to print.
/// - `table`: The VirtualSymTable we use to resolve indices.
///
/// # Returns
/// Nothing, but does print the instruction to stdout.
fn pass_edge_instr(writer: &mut impl Write, instr: &EdgeInstr, table: &SymTable) -> std::io::Result<()> {
    // Match the instruction
    use EdgeInstr::*;
    match instr {
        Cast { res_type } => {
            write!(writer, "{instr} {res_type}")?;
        },

        Branch { next } => {
            write!(writer, "{instr} {next}")?;
        },
        BranchNot { next } => {
            write!(writer, "{instr} {next}")?;
        },

        Proj { field } => {
            write!(writer, "{instr} {field}")?;
        },

        Array { length, res_type } => {
            write!(writer, "{instr} {res_type},{length}")?;
        },
        ArrayIndex { res_type } => {
            write!(writer, "{instr} {res_type}")?;
        },
        Instance { def } => {
            write!(writer, "{} {}", instr, table.class(*def).name)?;
        },

        VarDec { def } => {
            write!(writer, "{} {}", instr, table.var(*def).name)?;
        },
        VarUndec { def } => {
            write!(writer, "{} {}", instr, table.var(*def).name)?;
        },
        VarGet { def } => {
            write!(writer, "{} {}", instr, table.var(*def).name)?;
        },
        VarSet { def } => {
            write!(writer, "{} {}", instr, table.var(*def).name)?;
        },

        Boolean { value } => {
            write!(writer, "{instr} {value}")?;
        },
        Integer { value } => {
            write!(writer, "{instr} {value}")?;
        },
        Real { value } => {
            write!(writer, "{instr} {value}")?;
        },
        String { value } => {
            write!(
                writer,
                "{} \"{}\"",
                instr,
                value.replace('\\', "\\\\").replace('\n', "\\n").replace('\t', "\\t").replace('\r', "\\r").replace('\"', "\\\"")
            )?;
        },
        Function { def } => {
            write!(writer, "{} {}", instr, table.func(FunctionId::Func(*def)).name)?;
        },

        // Any other instruction is just printing it without any value
        instr => {
            write!(writer, "{instr}")?;
        },
    }

    // Done
    Ok(())
}





/***** LIBRARY *****/
/// Starts printing the root of the AST (i.e., a Workflow).
///
/// # Arguments
/// - `root`: The root node of the tree on which this compiler pass will be done.
/// - `writer`: The `Write`r to write to.
///
/// # Errors
/// This pass may error if we failed to write to the given writer.
pub fn do_traversal(root: &Workflow, mut writer: impl Write) -> Result<(), Vec<Error>> {
    let Workflow { id, table, metadata, user, graph, funcs } = root;

    if let Err(err) = writeln!(&mut writer, "Workflow '{id}' {{") {
        return Err(vec![Error::WriteError { err }]);
    };

    // Print parsed metadata
    if user.is_some() || !metadata.is_empty() {
        // Print the user
        if let Some(user) = &**user {
            // Write it
            if let Err(err) = writeln!(&mut writer, "{}User: '{}'", indent!(INDENT_SIZE), user) {
                return Err(vec![Error::WriteError { err }]);
            };

            // Write a newline separating the tags
            if !metadata.is_empty() {
                if let Err(err) = writeln!(&mut writer) {
                    return Err(vec![Error::WriteError { err }]);
                };
            }
        }

        // Print the metadata
        for md in metadata.iter() {
            if let Err(err) = writeln!(
                &mut writer,
                "{}#{}.{}{}",
                indent!(INDENT_SIZE),
                if md.owner.contains(' ') { format!("\"{}\"", md.owner) } else { md.owner.clone() },
                if md.tag.contains(' ') { format!("\"{}\"", md.tag) } else { md.tag.clone() },
                if let Some((assigner, signature)) = &md.signature { format!(" <{assigner}.{signature}>") } else { String::new() }
            ) {
                return Err(vec![Error::WriteError { err }]);
            };
        }
        if let Err(err) = writeln!(&mut writer) {
            return Err(vec![Error::WriteError { err }]);
        };
        if let Err(err) = writeln!(&mut writer) {
            return Err(vec![Error::WriteError { err }]);
        };
        if let Err(err) = writeln!(&mut writer) {
            return Err(vec![Error::WriteError { err }]);
        };
    }

    // First up: print the workflow table
    if let Err(err) = pass_table(&mut writer, table, INDENT_SIZE) {
        return Err(vec![Error::WriteError { err }]);
    };
    if !root.table.vars.is_empty()
        || !root.table.funcs.is_empty()
        || !root.table.tasks.is_empty()
        || !root.table.classes.is_empty()
        || !root.table.results.is_empty()
    {
        if let Err(err) = writeln!(&mut writer) {
            return Err(vec![Error::WriteError { err }]);
        };
        if let Err(err) = writeln!(&mut writer) {
            return Err(vec![Error::WriteError { err }]);
        };
        if let Err(err) = writeln!(&mut writer) {
            return Err(vec![Error::WriteError { err }]);
        };
    }

    // Print the main function body (and thus all function bodies)
    if let Err(err) = writeln!(&mut writer, "{}<Main>", indent!(INDENT_SIZE)) {
        return Err(vec![Error::WriteError { err }]);
    };
    if let Err(err) = pass_edges(&mut writer, 0, graph, table, INDENT_SIZE, &mut HashSet::new()) {
        return Err(vec![Error::WriteError { err }]);
    };

    // Print the functions
    for (i, f) in funcs.iter() {
        if let Err(err) = writeln!(&mut writer) {
            return Err(vec![Error::WriteError { err }]);
        };
        if let Err(err) = writeln!(&mut writer, "{}<Function {} ({})>", indent!(INDENT_SIZE), *i, table.funcs[*i].name) {
            return Err(vec![Error::WriteError { err }]);
        };
        if let Err(err) = pass_edges(&mut writer, 0, f, table, INDENT_SIZE, &mut HashSet::new()) {
            return Err(vec![Error::WriteError { err }]);
        };
    }

    // Done
    if let Err(err) = writeln!(&mut writer, "}}") {
        return Err(vec![Error::WriteError { err }]);
    };
    Ok(())
}