Skip to content

Commit fbae450

Browse files
author
TheDevConnor
committed
Fixed switch cases in the tc and llvm
1 parent 25aa80e commit fbae450

6 files changed

Lines changed: 407 additions & 243 deletions

File tree

LPBS/ast.lx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,41 +131,36 @@ pub const create_group_node -> fn (expr: *Expr) *Expr {
131131

132132
#takes_ownership
133133
pub const free_expr -> fn (node: *Expr) void {
134-
if (node == cast<*Expr>(0)) {
135-
return;
136-
}
137-
138-
if (node.type == ExprKind::EXPR_NUMBER) {
139-
// nothing inside to free
140-
} elif (node.type == ExprKind::EXPR_IDENT) {
141-
// nothing inside to free
142-
} elif (node.type == ExprKind::EXPR_STRING) {
143-
// nothing inside to free
144-
} elif (node.type == ExprKind::EXPR_BINARY) {
145-
let bin_node: *Binary = cast<*Binary>(node);
146-
free_expr(bin_node.left);
147-
free_expr(bin_node.right);
148-
} elif (node.type == ExprKind::EXPR_UNARY) {
149-
let unary_node: *Unary = cast<*Unary>(node);
150-
free_expr(unary_node.operand);
151-
} elif (node.type == ExprKind::EXPR_GROUP) {
152-
let group_node: *Group = cast<*Group>(node);
153-
free_expr(group_node.expr);
134+
if (node == cast<*Expr>(0)) { return; }
135+
136+
switch(node.type) {
137+
ExprKind::EXPR_NUMBER, ExprKind::EXPR_IDENT, ExprKind::EXPR_STRING -> {}
138+
ExprKind::EXPR_BINARY -> {
139+
let bin_node: *Binary = cast<*Binary>(node);
140+
free_expr(bin_node.left); free_expr(bin_node.right);
141+
}
142+
ExprKind::EXPR_UNARY -> {
143+
let unary_node: *Unary = cast<*Unary>(node);
144+
free_expr(unary_node.operand);
145+
}
146+
ExprKind::EXPR_GROUP -> {
147+
let group_node: *Group = cast<*Group>(node);
148+
free_expr(group_node.expr);
149+
}
150+
_ -> output("Node not found to free: ", node.type);
154151
}
155152

156153
free(node);
157154
}
158155

159156
#takes_ownership
160157
pub const free_stmt -> fn (node: *Stmt) void {
161-
if (node == cast<*Stmt>(0)) {
162-
return;
163-
}
158+
if (node == cast<*Stmt>(0)) { return; }
164159

165160
if (node.type == StmtKind::STMT_PROGRAM) {
166161
let program_node: *Program = cast<*Program>(node);
167162
free_expr(program_node.nodes);
168163
}
169164

170165
free(node);
171-
}
166+
}

LPBS/ast_helper.lx

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,60 @@
55

66
pub const print_expr -> fn (node: *Expr) void {
77
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); }
328

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);
4149
}
4250
}
4351

4452
pub const print_stmt -> fn (node: *Stmt) void {
4553
if (node == cast<*Stmt>(0)) { return; }
4654

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);
5062
}
5163

5264
output("\n");

LPBS/main.lx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pub const main -> fn () int {
1111
let speed: Timer = time::timer_start();
1212

13-
let path: *char = "/home/TheDevConnor/Luma/LPBS/build.lmb";
13+
let path: *char = "/home/connor/Projects/Luma/LPBS/build.lmb";
1414
let file: *char = io::read_file(path);
1515
let lex: *Token = lexer::scan(file);
1616

0 commit comments

Comments
 (0)