diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-07-27 14:17:46 -0500 |
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-07-27 14:17:46 -0500 |
| commit | 46e8f2f827bf176a5e480ac9ff96806ced594bde (patch) | |
| tree | 492d7455e0da179a0fbe9d0af36d516bc1417a10 /src/codegen.zig | |
| parent | 2605d1e8aa158e8fce80853cf064cc5e2e41e0a9 (diff) | |
| download | calico-46e8f2f827bf176a5e480ac9ff96806ced594bde.tar.gz | |
Added proper codegen and parsetree
Diffstat (limited to 'src/codegen.zig')
| -rw-r--r-- | src/codegen.zig | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index c8f414e..a200e23 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -2,25 +2,37 @@ const std = @import("std"); const parse = @import("parser.zig"); pub const Generator = struct { - root: parse.NodeExit, + root: []const parse.NodeStmt, allocator: std.mem.Allocator, code: std.ArrayList(u8), - pub fn init(allocator: std.mem.Allocator, root: parse.NodeExit) Generator { + pub fn init(allocator: std.mem.Allocator, stmts: []const parse.NodeStmt) Generator { return .{ - .root = root, + .root = stmts, .allocator = allocator, .code = std.ArrayList(u8).init(allocator), }; } - fn genExit(self: *Generator) ![]const u8 { + pub fn deinit(self: *Generator) void { + self.code.deinit(); + } + + fn genExit(self: *Generator, expr: parse.NodeExpr) ![]const u8 { return try std.fmt.allocPrint(self.allocator, \\ mov rax, 60 - \\ mov rdi, {} + \\ mov rdi, {d} \\ syscall \\ - , .{self.root.expr.intLit.intLit}); + , .{switch (expr) { + .intLit => expr.intLit.intlit.intLit, + else => return error.NotImplemented, + }}); + } + + fn genValue(self: *Generator) ![]const u8 { + _ = self; + return error.NotImplemented; } pub fn generate(self: *Generator) ![]const u8 { @@ -28,9 +40,13 @@ pub const Generator = struct { \\global _start: \\ ); - const exitStmt = try self.genExit(); - defer self.allocator.free(exitStmt); - try self.code.appendSlice(exitStmt); - return try self.code.toOwnedSlice(); + for (self.root) |stmt| { + const code = switch (stmt) { + .exit => try self.genExit(stmt.exit.expr), + .value => try self.genValue(), + }; + try self.code.appendSlice(code); + } + return self.code.items; } }; |
