zlox/src/utils.zig

27 lines
727 B
Zig
Raw Normal View History

2024-08-24 18:04:47 +02:00
const std = @import("std");
const debug = std.debug;
const Chunk = @import("./chunk.zig").Chunk;
const print_value = @import("./values.zig").print_value;
2024-08-24 15:11:45 +02:00
pub fn grow_capacity(capacity: usize) usize {
if (capacity < 8) {
return 8;
}
return capacity * 2;
}
2024-08-24 18:04:47 +02:00
pub fn simple_instruction(opcode_name: []const u8, offset: usize) usize {
debug.print("{s:16}\n", .{opcode_name});
return offset + 1;
}
pub fn constant_instruction(opcode_name: []const u8, chunk: Chunk, offset: usize) usize {
const constant = chunk.code[offset + 1];
debug.print("{s:16} {d:4} '", .{ opcode_name, constant });
print_value(chunk.constants.values[constant]);
debug.print("'\n", .{});
return offset + 2;
}