add incomplete ConstantLong instruction
- move ValueList's count to usize for ConstantLong
This commit is contained in:
parent
ba78b39300
commit
2d33e03efb
3 changed files with 55 additions and 5 deletions
|
@ -6,7 +6,8 @@ const Allocator = std.mem.Allocator;
|
||||||
// hack. ugly hack. zig has compiler crash.
|
// hack. ugly hack. zig has compiler crash.
|
||||||
const AllOpcodes = struct {
|
const AllOpcodes = struct {
|
||||||
pub Constant: u8 = 0,
|
pub Constant: u8 = 0,
|
||||||
pub Return: u8 = 1,
|
pub ConstantLong: u8 = 1,
|
||||||
|
pub Return: u8 = 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const OpCode = AllOpcodes{};
|
pub const OpCode = AllOpcodes{};
|
||||||
|
@ -36,6 +37,32 @@ fn constantInstruction(
|
||||||
return offset + 2;
|
return offset + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn constantLongInstruction(
|
||||||
|
stdout: var,
|
||||||
|
comptime name: []const u8,
|
||||||
|
chunk: *Chunk,
|
||||||
|
offset: usize,
|
||||||
|
) !usize {
|
||||||
|
// get the constant's index in constants slice
|
||||||
|
var v0: u8 = chunk.code[offset + 3];
|
||||||
|
var v1: u8 = chunk.code[offset + 2];
|
||||||
|
var v2: u8 = chunk.code[offset + 1];
|
||||||
|
|
||||||
|
// TODO: this does not work. just decreased first term
|
||||||
|
// to fix a compile error.
|
||||||
|
|
||||||
|
// we should also move the actual printing into its own
|
||||||
|
// function too since constantInstruction and
|
||||||
|
// constantLongInstruction share code.
|
||||||
|
var idx: u24 = (v2 << 4) | (v1 << 7) | v0;
|
||||||
|
|
||||||
|
try stdout.print("\t{}\t{} '", name, idx);
|
||||||
|
try value.printValue(stdout, chunk.constants.values[idx]);
|
||||||
|
try stdout.print("'\n");
|
||||||
|
|
||||||
|
return offset + 2;
|
||||||
|
}
|
||||||
|
|
||||||
pub const Chunk = struct {
|
pub const Chunk = struct {
|
||||||
count: usize,
|
count: usize,
|
||||||
lines: []usize,
|
lines: []usize,
|
||||||
|
@ -77,6 +104,24 @@ pub const Chunk = struct {
|
||||||
return self.constants.count - 1;
|
return self.constants.count - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn writeConstant(self: *Chunk, val: value.Value, line: usize) !void {
|
||||||
|
try self.constants.write(val);
|
||||||
|
var constant_idx = self.constants.count - 1;
|
||||||
|
|
||||||
|
if (constant_idx < 256) {
|
||||||
|
try self.write(OpCode.Constant, line);
|
||||||
|
try self.write(@intCast(u8, constant_idx), line);
|
||||||
|
} else {
|
||||||
|
// TODO: convert the usize to u24, and from
|
||||||
|
// u24, split it into three u8's.
|
||||||
|
|
||||||
|
// also convert from u8 back to u24.
|
||||||
|
// i know that we can do from two u8's to go to a u16
|
||||||
|
// with (v1 << 7) | v0.
|
||||||
|
try self.write(0, line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn disassembleInstruction(
|
pub fn disassembleInstruction(
|
||||||
self: *Chunk,
|
self: *Chunk,
|
||||||
stdout: var,
|
stdout: var,
|
||||||
|
@ -96,6 +141,13 @@ pub const Chunk = struct {
|
||||||
return try simpleInstruction(stdout, "OP_RETURN", index);
|
return try simpleInstruction(stdout, "OP_RETURN", index);
|
||||||
} else if (instruction == OpCode.Constant) {
|
} else if (instruction == OpCode.Constant) {
|
||||||
return try constantInstruction(stdout, "OP_CONSTANT", self, index);
|
return try constantInstruction(stdout, "OP_CONSTANT", self, index);
|
||||||
|
} else if (instruction == OpCode.ConstantLong) {
|
||||||
|
return try constantLongInstruction(
|
||||||
|
stdout,
|
||||||
|
"OP_CONSTANT_LONG",
|
||||||
|
self,
|
||||||
|
index,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
try stdout.print("Unknown opcode: {}\n", instruction);
|
try stdout.print("Unknown opcode: {}\n", instruction);
|
||||||
return index + 1;
|
return index + 1;
|
||||||
|
|
|
@ -115,9 +115,7 @@ pub fn main() !void {
|
||||||
//var opcode_byte: u8 = @enumToInt(chunk.OpCode.Return);
|
//var opcode_byte: u8 = @enumToInt(chunk.OpCode.Return);
|
||||||
//try chk.write(chunk.OpCode.Return);
|
//try chk.write(chunk.OpCode.Return);
|
||||||
|
|
||||||
var constant = try chk.addConstant(1.2);
|
var constant = try chk.writeConstant(1.2, 123);
|
||||||
try chk.write(chunk.OpCode.Constant, 123);
|
|
||||||
try chk.write(constant, 123);
|
|
||||||
try chk.write(chunk.OpCode.Return, 123);
|
try chk.write(chunk.OpCode.Return, 123);
|
||||||
|
|
||||||
try chk.disassemble(stdout, "test chunk");
|
try chk.disassemble(stdout, "test chunk");
|
||||||
|
|
|
@ -10,7 +10,7 @@ pub fn printValue(stdout: var, value: Value) !void {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const ValueList = struct {
|
pub const ValueList = struct {
|
||||||
count: u8,
|
count: usize,
|
||||||
values: []Value,
|
values: []Value,
|
||||||
allocator: *Allocator,
|
allocator: *Allocator,
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue