File size: 2,307 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import assert from "node:assert/strict";

function indent(lines, how_much)
{
    return lines.map(line => " ".repeat(how_much) + line);
}

export function print_syntax_tree(statements)
{
    let code = [];

    for(let statement of statements)
    {
        if(typeof statement === "string")
        {
            code.push(statement);
        }
        else if(statement.type === "switch")
        {
            assert(statement.condition);

            const cases = [];

            for(let case_ of statement.cases)
            {
                assert(case_.conditions.length >= 1);

                cases.push(case_.conditions.join(" | ") + " => {");
                cases.push.apply(cases, indent(print_syntax_tree(case_.body), 4));
                cases.push(`},`);
            }

            if(statement.default_case)
            {
                const varname = statement.default_case.varname || "_";
                cases.push(`${varname} => {`);
                cases.push.apply(cases, indent(print_syntax_tree(statement.default_case.body), 4));
                cases.push(`}`);
            }

            code.push(`match ${statement.condition} {`);
            code.push.apply(code, indent(cases, 4));
            code.push(`}`);
        }
        else if(statement.type === "if-else")
        {
            assert(statement.if_blocks.length >= 1);

            let first_if_block = statement.if_blocks[0];

            code.push(`if ${first_if_block.condition} {`);
            code.push.apply(code, indent(print_syntax_tree(first_if_block.body), 4));
            code.push(`}`);

            for(let i = 1; i < statement.if_blocks.length; i++)
            {
                let if_block = statement.if_blocks[i];

                code.push(`else if ${if_block.condition} {`);
                code.push.apply(code, indent(print_syntax_tree(if_block.body), 4));
                code.push(`}`);
            }

            if(statement.else_block)
            {
                code.push(`else {`);
                code.push.apply(code, indent(print_syntax_tree(statement.else_block.body), 4));
                code.push(`}`);
            }
        }
        else
        {
            assert(false, "Unexpected type: " + statement.type, "In:", statement);
        }
    }

    return code;
}