Add constants
parent
79a574f053
commit
23070e0900
|
@ -1,5 +1,7 @@
|
|||
const std = @import("std");
|
||||
const print = std.debug.print;
|
||||
const value = @import("./value.zig");
|
||||
const ValueArray = value.ValueArray;
|
||||
|
||||
pub const OpCode = enum(u8) {
|
||||
OP_RETURN,
|
||||
|
@ -9,22 +11,26 @@ pub const Chunk = struct {
|
|||
code: []u8,
|
||||
count: usize,
|
||||
capacity: usize,
|
||||
constants: ValueArray,
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
/// Initializes a new Chunk
|
||||
pub fn init(allocator: std.mem.Allocator) !Chunk {
|
||||
return .{
|
||||
.count = 0,
|
||||
.capacity = 0,
|
||||
.allocator = allocator,
|
||||
.code = try allocator.alloc(u8, 0),
|
||||
.constants = try ValueArray.init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn write_chunck(self: *Chunk, byte: u8) !void {
|
||||
/// Writes a byte to this chunk
|
||||
pub fn write(self: *Chunk, byte: u8) !void {
|
||||
// If the code slice is full
|
||||
if (self.count == self.capacity) {
|
||||
const old_capacity = self.capacity;
|
||||
self.capacity = if (old_capacity < 8) 8 else old_capacity * 2;
|
||||
self.capacity = grow_capacity(old_capacity);
|
||||
self.code = try self.allocator.realloc(self.code, self.capacity);
|
||||
}
|
||||
|
||||
|
@ -39,6 +45,7 @@ pub const Chunk = struct {
|
|||
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});
|
||||
|
||||
|
@ -48,6 +55,7 @@ pub const Chunk = struct {
|
|||
}
|
||||
}
|
||||
|
||||
/// Prints the value of a single instruction
|
||||
fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
|
||||
print("{d:0>4} ", .{offset});
|
||||
|
||||
|
@ -61,8 +69,14 @@ pub const Chunk = struct {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn add_constant(self: *Chunk, v: value.Value) !usize {
|
||||
return try self.constants.add_constant(v);
|
||||
}
|
||||
|
||||
/// Destroys this chunk
|
||||
pub fn deinit(self: Chunk) void {
|
||||
self.allocator.free(self.code);
|
||||
self.constants.deinit();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -70,3 +84,7 @@ fn simple_instruction(comptime name: []const u8, offset: usize) usize {
|
|||
print("{s}\n", .{name});
|
||||
return offset + 1;
|
||||
}
|
||||
|
||||
inline fn grow_capacity(old: usize) usize {
|
||||
return if (old < 8) 8 else old * 2;
|
||||
}
|
||||
|
|
14
src/main.zig
14
src/main.zig
|
@ -11,8 +11,10 @@ pub fn main() !void {
|
|||
var c = try chunk.Chunk.init(alloc);
|
||||
defer c.deinit();
|
||||
|
||||
try c.write_chunck(@intFromEnum(chunk.OpCode.OP_RETURN));
|
||||
try c.write_chunck(@intFromEnum(chunk.OpCode.OP_RETURN));
|
||||
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN));
|
||||
try c.write(@intFromEnum(chunk.OpCode.OP_RETURN));
|
||||
|
||||
_ = try c.add_constant(0.0);
|
||||
c.dissasemble_chunk("test chunk");
|
||||
}
|
||||
|
||||
|
@ -20,9 +22,9 @@ test "chunk test" {
|
|||
var c = try chunk.Chunk.init(std.testing.allocator);
|
||||
defer c.deinit();
|
||||
|
||||
try c.write_chunck('a');
|
||||
try c.write_chunck('b');
|
||||
try c.write('a');
|
||||
try c.write('b');
|
||||
try c.free_chunck();
|
||||
try c.write_chunck('J');
|
||||
try c.write_chunck('H');
|
||||
try c.write('J');
|
||||
try c.write('H');
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub const Value = f64;
|
||||
|
||||
pub const ValueArray = struct {
|
||||
capacity: usize,
|
||||
count: usize,
|
||||
values: []Value,
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) !ValueArray {
|
||||
return .{ .capacity = 0, .count = 0, .values = try allocator.alloc(Value, 0), .allocator = allocator };
|
||||
}
|
||||
|
||||
pub fn write(self: *ValueArray, value: Value) !void {
|
||||
if (self.count == self.capacity) {
|
||||
const old = self.capacity;
|
||||
self.capacity = if (old < 8) 8 else old * 2;
|
||||
self.values = try self.allocator.realloc(self.values, self.capacity);
|
||||
}
|
||||
|
||||
self.values[self.count] = value;
|
||||
self.count += 1;
|
||||
}
|
||||
|
||||
pub fn add_constant(self: *ValueArray, value: Value) !usize {
|
||||
try self.write(value);
|
||||
return self.count - 1;
|
||||
}
|
||||
|
||||
pub fn deinit(self: ValueArray) void {
|
||||
self.allocator.free(self.values);
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue