diff options
author | Mathias Magnusson <mathias@magnusson.space> | 2025-06-02 15:17:22 +0200 |
---|---|---|
committer | Mathias Magnusson <mathias@magnusson.space> | 2025-06-02 15:17:22 +0200 |
commit | a39f75cae1d74d89efc0a871bba953c1f1af3b1b (patch) | |
tree | d7ff616ba70aea4424d93be17c885e202904ce8e /src/compile.zig | |
parent | 939ee7606fbfe3afa6b6a008fb617120a6dd8114 (diff) | |
download | huginn-a39f75cae1d74d89efc0a871bba953c1f1af3b1b.tar.gz |
add statements ending in ; and allow parsing multiple of them
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; } |