Compare commits
2 Commits
23070e0900
...
0f891e4275
Author | SHA1 | Date | |
---|---|---|---|
0f891e4275 | |||
975280be8f |
@ -5,10 +5,12 @@ const ValueArray = value.ValueArray;
|
||||
|
||||
pub const OpCode = enum(u8) {
|
||||
OP_RETURN,
|
||||
OP_CONSTANT,
|
||||
};
|
||||
|
||||
pub const Chunk = struct {
|
||||
code: []u8,
|
||||
lines: []u32,
|
||||
count: usize,
|
||||
capacity: usize,
|
||||
constants: ValueArray,
|
||||
@ -21,30 +23,26 @@ pub const Chunk = struct {
|
||||
.capacity = 0,
|
||||
.allocator = allocator,
|
||||
.code = try allocator.alloc(u8, 0),
|
||||
.lines = try allocator.alloc(u32, 0),
|
||||
.constants = try ValueArray.init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
/// Writes a byte to this chunk
|
||||
pub fn write(self: *Chunk, byte: u8) !void {
|
||||
pub fn write(self: *Chunk, byte: u8, line: u32) !void {
|
||||
// If the code slice is full
|
||||
if (self.count == self.capacity) {
|
||||
const old_capacity = self.capacity;
|
||||
self.capacity = grow_capacity(old_capacity);
|
||||
self.code = try self.allocator.realloc(self.code, self.capacity);
|
||||
self.lines = try self.allocator.realloc(self.lines, self.capacity);
|
||||
}
|
||||
|
||||
self.code[self.count] = byte;
|
||||
self.lines[self.count] = line;
|
||||
self.count += 1;
|
||||
}
|
||||
|
||||
/// Reinitializes the state of the chunk.
|
||||
pub fn free_chunck(self: *Chunk) !void {
|
||||
self.count = 0;
|
||||
self.capacity = 0;
|
||||
self.code = try self.allocator.realloc(self.code, 0);
|
||||
}
|
||||
|
||||
/// Prints the current state of the chunk to stderr
|
||||
pub fn dissasemble_chunk(self: *Chunk, name: []const u8) void {
|
||||
print("== {s} ==\n", .{name});
|
||||
@ -59,9 +57,17 @@ pub const Chunk = struct {
|
||||
fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
|
||||
print("{d:0>4} ", .{offset});
|
||||
|
||||
// Print the line number
|
||||
if (offset > 0 and self.lines[offset] == self.lines[offset - 1]) {
|
||||
print(" | ", .{});
|
||||
} else {
|
||||
print("{d: >4} ", .{self.lines[offset]});
|
||||
}
|
||||
|
||||
const instruction = self.code[offset];
|
||||
switch (instruction) {
|
||||
@intFromEnum(OpCode.OP_RETURN) => return simple_instruction("OP_RETURN", offset),
|
||||
@intFromEnum(OpCode.OP_CONSTANT) => return self.constant_instruction("OP_CONSTANT", offset),
|
||||
else => {
|
||||
print("unknown opcode {d}\n", .{instruction});
|
||||
return offset + 1;
|
||||
@ -76,8 +82,19 @@ pub const Chunk = struct {
|
||||
/// Destroys this chunk
|
||||
pub fn deinit(self: Chunk) void {
|
||||
self.allocator.free(self.code);
|
||||
self.allocator.free(self.lines);
|
||||
self.constants.deinit();
|
||||
}
|
||||
|
||||
fn constant_instruction(self: *Chunk, comptime name: []const u8, offset: usize) usize {
|
||||
const constant_addr = self.code[offset + 1];
|
||||
const constant_value = self.constants.values[constant_addr];
|
||||
|
||||
print("{s: <16} {d: >4} '", .{ name, constant_addr });
|
||||
print_value(constant_value);
|
||||
print("'\n", .{});
|
||||
return offset + 2;
|
||||
}
|
||||
};
|
||||
|
||||
fn simple_instruction(comptime name: []const u8, offset: usize) usize {
|
||||
@ -85,6 +102,10 @@ fn simple_instruction(comptime name: []const u8, offset: usize) usize {
|
||||
return offset + 1;
|
||||
}
|
||||
|
||||
fn print_value(v: value.Value) void {
|
||||
print("{d}", .{v});
|
||||
}
|
||||
|
||||
inline fn grow_capacity(old: usize) usize {
|
||||
return if (old < 8) 8 else old * 2;
|
||||
}
|
||||
|
18
src/main.zig
18
src/main.zig
@ -1,5 +1,6 @@
|
||||
const std = @import("std");
|
||||
const chunk = @import("./chunk.zig");
|
||||
const OpCode = chunk.OpCode;
|
||||
|
||||
pub fn main() !void {
|
||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
||||
@ -11,10 +12,12 @@ pub fn main() !void {
|
||||
var c = try chunk.Chunk.init(alloc);
|
||||
defer c.deinit();
|
||||
|
||||
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN));
|
||||
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN));
|
||||
const constant_idx = try c.add_constant(1.2);
|
||||
try c.write(@intFromEnum(OpCode.OP_CONSTANT), 1);
|
||||
try c.write(@truncate(constant_idx), 1);
|
||||
|
||||
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN), 1);
|
||||
|
||||
_ = try c.add_constant(0.0);
|
||||
c.dissasemble_chunk("test chunk");
|
||||
}
|
||||
|
||||
@ -22,9 +25,8 @@ test "chunk test" {
|
||||
var c = try chunk.Chunk.init(std.testing.allocator);
|
||||
defer c.deinit();
|
||||
|
||||
try c.write('a');
|
||||
try c.write('b');
|
||||
try c.free_chunck();
|
||||
try c.write('J');
|
||||
try c.write('H');
|
||||
try c.write('a', 0);
|
||||
try c.write('b', 1);
|
||||
try c.write('J', 2);
|
||||
try c.write('H', 3);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user