diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-07-27 00:31:28 -0500 |
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-07-27 00:31:28 -0500 |
| commit | 2605d1e8aa158e8fce80853cf064cc5e2e41e0a9 (patch) | |
| tree | 081edf18e84d22306e7c04c28d594f1ccfac14dc /src/codegen.zig | |
| parent | 8f55e2fa7059ef215a1b8369300f0a85103b079b (diff) | |
| download | calico-2605d1e8aa158e8fce80853cf064cc5e2e41e0a9.tar.gz | |
Completely removed ast code and restructured
Diffstat (limited to 'src/codegen.zig')
| -rw-r--r-- | src/codegen.zig | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/codegen.zig b/src/codegen.zig new file mode 100644 index 0000000..c8f414e --- /dev/null +++ b/src/codegen.zig @@ -0,0 +1,36 @@ +const std = @import("std"); +const parse = @import("parser.zig"); + +pub const Generator = struct { + root: parse.NodeExit, + allocator: std.mem.Allocator, + code: std.ArrayList(u8), + + pub fn init(allocator: std.mem.Allocator, root: parse.NodeExit) Generator { + return .{ + .root = root, + .allocator = allocator, + .code = std.ArrayList(u8).init(allocator), + }; + } + + fn genExit(self: *Generator) ![]const u8 { + return try std.fmt.allocPrint(self.allocator, + \\ mov rax, 60 + \\ mov rdi, {} + \\ syscall + \\ + , .{self.root.expr.intLit.intLit}); + } + + pub fn generate(self: *Generator) ![]const u8 { + try self.code.appendSlice( + \\global _start: + \\ + ); + const exitStmt = try self.genExit(); + defer self.allocator.free(exitStmt); + try self.code.appendSlice(exitStmt); + return try self.code.toOwnedSlice(); + } +}; |
