diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-07-29 06:30:47 -0500 |
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-07-29 06:30:47 -0500 |
| commit | 9334af0c76c9410b3d0028febd4b1972ab7d4d7e (patch) | |
| tree | 40665df61158ace0951b1cdf887035b42e9e4fca /src/codegen.zig | |
| parent | e5d311f91ecda870c8e50d3f9c860b99e244a908 (diff) | |
| download | calico-9334af0c76c9410b3d0028febd4b1972ab7d4d7e.tar.gz | |
Started symbol table and restructured AST
Diffstat (limited to 'src/codegen.zig')
| -rw-r--r-- | src/codegen.zig | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index 98501c2..a1df34f 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -18,37 +18,51 @@ pub const Generator = struct { self.code.deinit(); } - fn genExit(self: *Generator, expr: parse.NodeExpr) !void { + fn genExit(self: *Generator, exit: parse.NodeExit) !void { + const expr = exit.expr; const newCode = - switch (expr) { - .intLit => try std.fmt.allocPrint(self.allocator, + switch (expr.kind) { + .intLit => |intlit| try std.fmt.allocPrint(self.allocator, \\ mov rax, 60 \\ mov rdi, {d} \\ syscall \\ , .{ - expr.intLit.intlit.intLit, + intlit.intlit.intLit, }), - .ident => try std.fmt.allocPrint(self.allocator, + .ident => |ident| try std.fmt.allocPrint(self.allocator, \\ mov rax, 60 \\ mov rdi, [{s}] \\ syscall \\ , .{ - expr.ident.ident.ident, + ident.ident.ident, }), }; try self.code.appendSlice(newCode); self.allocator.free(newCode); } + fn genVar(self: *Generator, value: parse.NodeVar) !void { + const str = try std.fmt.allocPrint(self.allocator, + \\section .data + \\ {s}: dw {d} + \\ + , .{ value.ident.ident, switch (value.expr.kind) { + .intLit => |intlit| intlit.intlit.intLit, + else => return error.NotImplemented, + } }); + defer self.allocator.free(str); + try self.code.insertSlice(0, str); + } + fn genValue(self: *Generator, value: parse.NodeValue) !void { const str = try std.fmt.allocPrint(self.allocator, \\section .data \\ {s}: dw {d} \\ - , .{ value.ident.ident, switch (value.value) { - .intLit => value.value.intLit.intlit.intLit, + , .{ value.ident.ident, switch (value.expr.kind) { + .intLit => |intlit| intlit.intlit.intLit, else => return error.NotImplemented, } }); defer self.allocator.free(str); @@ -57,21 +71,21 @@ pub const Generator = struct { fn genAssign(self: *Generator, assign: parse.NodeAssign) !void { const newCode = - switch (assign.value) { - .intLit => try std.fmt.allocPrint(self.allocator, + switch (assign.expr.kind) { + .intLit => |intlit| try std.fmt.allocPrint(self.allocator, \\ mov rax, {d} \\ mov [{s}], rax \\ , .{ - assign.value.intLit.intlit.intLit, + intlit.intlit.intLit, assign.ident.ident, }), - .ident => try std.fmt.allocPrint(self.allocator, + .ident => |ident| try std.fmt.allocPrint(self.allocator, \\ mov rax, [{s}] \\ mov [{s}], rax \\ , .{ - assign.value.ident.ident.ident, + ident.ident.ident, assign.ident.ident, }), }; @@ -87,10 +101,11 @@ pub const Generator = struct { \\ ); for (self.root) |stmt| { - switch (stmt) { - .exit => try self.genExit(stmt.exit.expr), - .value => try self.genValue(stmt.value), - .assign => try self.genAssign(stmt.assign), + switch (stmt.kind) { + .exit => |exit| try self.genExit(exit), + .defValue => |defValue| try self.genValue(defValue), + .defVar => |defVar| try self.genVar(defVar), + .assignVar => |assign| try self.genAssign(assign), } } return self.code.items; |
