|
5 | 5 |
|
6 | 6 | pub const print_expr -> fn (node: *Expr) void { |
7 | 7 | if (node == cast<*Expr>(0)) { return; } |
8 | | - |
9 | | - if (node.type == ast::ExprKind::EXPR_NUMBER) { |
10 | | - let num_node: *Number = cast<*Number>(node); |
11 | | - output(num_node.value); |
12 | | - } elif (node.type == ast::ExprKind::EXPR_IDENT) { |
13 | | - let ident_node: *Ident = cast<*Ident>(node); |
14 | | - output(ident_node.name); |
15 | | - } elif (node.type == ast::ExprKind::EXPR_STRING) { |
16 | | - let str_node: *StringLit = cast<*StringLit>(node); |
17 | | - output("''", str_node.value, "''"); |
18 | | - } elif (node.type == ast::ExprKind::EXPR_BINARY) { |
19 | | - let bin_node: *Binary = cast<*Binary>(node); |
20 | | - let binop: *char = string::from_char(bin_node.op); |
21 | | - defer { free(binop); } |
22 | | - |
23 | | - output("("); |
24 | | - print_expr(bin_node.left); |
25 | | - output(" ", binop, " "); |
26 | | - print_expr(bin_node.right); |
27 | | - output(")"); |
28 | | - } elif (node.type == ast::ExprKind::EXPR_UNARY) { |
29 | | - let unary_node: *Unary = cast<*Unary>(node); |
30 | | - let unop: *char = string::from_char(unary_node.op); |
31 | | - defer { free(unop); } |
32 | 8 |
|
33 | | - output(unop); |
34 | | - print_expr(unary_node.operand); |
35 | | - } elif (node.type == ast::ExprKind::EXPR_GROUP) { |
36 | | - let group_node: *Group = cast<*Group>(node); |
37 | | - |
38 | | - output("("); |
39 | | - print_expr(group_node.expr); |
40 | | - output(")"); |
| 9 | + switch (node.type) { |
| 10 | + ast::ExprKind::EXPR_NUMBER -> { |
| 11 | + let num_node: *Number = cast<*Number>(node); |
| 12 | + output(num_node.value); |
| 13 | + } |
| 14 | + ast::ExprKind::EXPR_IDENT -> { |
| 15 | + let ident_node: *Ident = cast<*Ident>(node); |
| 16 | + output(ident_node.name); |
| 17 | + } |
| 18 | + ast::ExprKind::EXPR_STRING -> { |
| 19 | + let str_node: *StringLit = cast<*StringLit>(node); |
| 20 | + output("''", str_node.value, "''"); |
| 21 | + } |
| 22 | + ast::ExprKind::EXPR_BINARY -> { |
| 23 | + let bin_node: *Binary = cast<*Binary>(node); |
| 24 | + let binop: *char = string::from_char(bin_node.op); |
| 25 | + defer { free(binop); } |
| 26 | + |
| 27 | + output("("); |
| 28 | + print_expr(bin_node.left); |
| 29 | + output(" ", binop, " "); |
| 30 | + print_expr(bin_node.right); |
| 31 | + output(")"); |
| 32 | + } |
| 33 | + ast::ExprKind::EXPR_UNARY -> { |
| 34 | + let unary_node: *Unary = cast<*Unary>(node); |
| 35 | + let unop: *char = string::from_char(unary_node.op); |
| 36 | + defer { free(unop); } |
| 37 | + |
| 38 | + output(unop); |
| 39 | + print_expr(unary_node.operand); |
| 40 | + } |
| 41 | + ast::ExprKind::EXPR_GROUP -> { |
| 42 | + let group_node: *Group = cast<*Group>(node); |
| 43 | + |
| 44 | + output("("); |
| 45 | + print_expr(group_node.expr); |
| 46 | + output(")"); |
| 47 | + } |
| 48 | + _ -> output("Node not found: ", node.type); |
41 | 49 | } |
42 | 50 | } |
43 | 51 |
|
44 | 52 | pub const print_stmt -> fn (node: *Stmt) void { |
45 | 53 | if (node == cast<*Stmt>(0)) { return; } |
46 | 54 |
|
47 | | - if (node.type == ast::StmtKind::STMT_PROGRAM) { |
48 | | - let program_node: *Program = cast<*Program>(node); |
49 | | - print_expr(program_node.nodes); |
| 55 | + switch(node.type) { |
| 56 | + ast::StmtKind::STMT_PROGRAM -> { |
| 57 | + let prog_node: *Program = cast<*Program>(node); |
| 58 | + print_expr(prog_node.nodes); |
| 59 | + } |
| 60 | + |
| 61 | + _ -> output("Node not found: ", node.type); |
50 | 62 | } |
51 | 63 |
|
52 | 64 | output("\n"); |
|
0 commit comments