const std = @import("std"); const jv = @import("../jv.zig"); const Instr = @import("./compile.zig").Instr; pub const ExecuteError = error{ Unimplemented, InvalidType, InternalError, }; const SaveableStack = @import("./saveable_stack.zig").SaveableStack; const ValueStack = struct { const Self = @This(); const Stack = SaveableStack(jv.Value); stack: Stack, pub fn init(allocator: std.mem.Allocator) !Self { return .{ .stack = try Stack.init(allocator), }; } pub fn deinit(self: *Self) void { self.stack.deinit(); } pub fn push(self: *Self, value: jv.Value) !void { try self.stack.push(value); } pub fn pop(self: *Self) jv.Value { return self.stack.pop(); } pub fn popInteger(self: *Self) ExecuteError!i64 { const value = self.pop(); return switch (value) { .integer => |i| i, else => error.InvalidType, }; } pub fn popNumber(self: *Self) ExecuteError!f64 { const value = self.pop(); return switch (value) { .integer => |i| @floatFromInt(i), .float => |f| f, else => error.InvalidType, }; } pub fn popString(self: *Self) ExecuteError![]const u8 { const value = self.pop(); return switch (value) { .string => |s| s, else => error.InvalidType, }; } pub fn popArray(self: *Self) ExecuteError!jv.Array { const value = self.pop(); return switch (value) { .array => |a| a, else => error.InvalidType, }; } pub fn popObject(self: *Self) ExecuteError!jv.Object { const value = self.pop(); return switch (value) { .object => |o| o, else => error.InvalidType, }; } pub fn dup(self: *Self) !void { const top = self.stack.top().*; try self.push(top); } pub fn swap(self: *Self) !void { std.debug.assert(self.ensureSize(2)); const a = self.pop(); const b = self.pop(); try self.push(a); try self.push(b); } pub fn save(self: *Self) !void { try self.stack.save(); } pub fn restore(self: *Self) void { self.stack.restore(); } pub fn ensureSize(self: *Self, n: usize) bool { return self.stack.ensureSize(n); } }; pub fn execute(allocator: std.mem.Allocator, instrs: []const Instr, input: jv.Value) !jv.Value { var value_stack = try ValueStack.init(allocator); defer value_stack.deinit(); try value_stack.push(input); const len = instrs.len; var pc: usize = 0; while (pc < len) { const cur = instrs[pc]; switch (cur) { .nop => {}, .subexp_begin => try value_stack.dup(), .subexp_end => try value_stack.swap(), .array_index => { std.debug.assert(value_stack.ensureSize(2)); const array = try value_stack.popArray(); const index: usize = @intCast(try value_stack.popInteger()); const result = if (index < array.items.len) array.items[index] else .null; try value_stack.push(result); }, .add => { std.debug.assert(value_stack.ensureSize(3)); _ = value_stack.pop(); const lhs = try value_stack.popInteger(); const rhs = try value_stack.popInteger(); const result = lhs + rhs; try value_stack.push(.{ .integer = result }); }, .object_key => |key| { std.debug.assert(value_stack.ensureSize(1)); const obj = try value_stack.popObject(); const result = obj.get(key) orelse .null; try value_stack.push(result); }, .literal => |value| { std.debug.assert(value_stack.ensureSize(1)); _ = value_stack.pop(); try value_stack.push(value.*); }, } pc += 1; } return value_stack.pop(); }