diff options
Diffstat (limited to 'src/compile.zig')
-rw-r--r-- | src/compile.zig | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/compile.zig b/src/compile.zig index 9d70a58..6a79efd 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -2,7 +2,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const root = @import("root"); const Token = root.Lexer.Token; -const Expr = root.Expr; +const parse = root.parse; const Location = root.Lexer.Location; pub const VReg = enum(u32) { _ }; @@ -84,7 +84,7 @@ pub const Block = struct { } }; -pub fn compile(allocator: Allocator, source: []const u8, expr: *const Expr) !Block { +pub fn compile(allocator: Allocator, source: []const u8, stmts: []parse.Stmt) !Block { const instrs: std.ArrayListUnmanaged(Instr) = try .initCapacity(allocator, 0); var ctx: CompileContext = .{ .allocator = allocator, @@ -92,7 +92,9 @@ pub fn compile(allocator: Allocator, source: []const u8, expr: *const Expr) !Blo .register_counter = 0, .instrs = instrs, }; - _ = try ctx.compileExpr(expr); + for (stmts) |stmt| { + try ctx.compileStmt(stmt); + } return .init(allocator, ctx.instrs.items); } @@ -108,7 +110,13 @@ const CompileContext = struct { try self.instrs.append(self.allocator, instr); } - fn compileExpr(self: *Self, expr: *const Expr) !VReg { + fn compileStmt(self: *Self, stmt: parse.Stmt) !void { + switch (stmt.type) { + .expr => |expr| _ = try self.compileExpr(expr), + } + } + + fn compileExpr(self: *Self, expr: *const parse.Expr) !VReg { const dest = self.register(); switch (expr.type) { .integer_literal => try addInstr(self, .{ @@ -148,7 +156,6 @@ const CompileContext = struct { } }, .identifier => return error.CantCompileIdentifierExpr, - .invalid => return error.CantCompileInvalidExpr, } return dest; } |