forked from luna/jorts
finish impl for ConstantLong
This commit is contained in:
parent
2d33e03efb
commit
14fa63e1f6
2 changed files with 27 additions and 24 deletions
|
@ -15,26 +15,26 @@ pub const OpCode = AllOpcodes{};
|
||||||
fn simpleInstruction(
|
fn simpleInstruction(
|
||||||
stdout: var,
|
stdout: var,
|
||||||
comptime name: []const u8,
|
comptime name: []const u8,
|
||||||
offset: usize,
|
index: usize,
|
||||||
) !usize {
|
) !usize {
|
||||||
try stdout.print("{}\n", name);
|
try stdout.print("{}\n", name);
|
||||||
return offset + 1;
|
return index + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn constantInstruction(
|
fn constantInstruction(
|
||||||
stdout: var,
|
stdout: var,
|
||||||
comptime name: []const u8,
|
comptime name: []const u8,
|
||||||
chunk: *Chunk,
|
chunk: *Chunk,
|
||||||
offset: usize,
|
index: usize,
|
||||||
) !usize {
|
) !usize {
|
||||||
// get the constant's index in constants slice
|
// get the constant's index in constants slice
|
||||||
var idx = chunk.code[offset + 1];
|
var idx = chunk.code[index + 1];
|
||||||
|
|
||||||
try stdout.print("\t{}\t{} '", name, idx);
|
try stdout.print("\t{}\t{} '", name, idx);
|
||||||
try value.printValue(stdout, chunk.constants.values[idx]);
|
try value.printValue(stdout, chunk.constants.values[idx]);
|
||||||
try stdout.print("'\n");
|
try stdout.print("'\n");
|
||||||
|
|
||||||
return offset + 2;
|
return index + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn constantLongInstruction(
|
fn constantLongInstruction(
|
||||||
|
@ -43,24 +43,19 @@ fn constantLongInstruction(
|
||||||
chunk: *Chunk,
|
chunk: *Chunk,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
) !usize {
|
) !usize {
|
||||||
// get the constant's index in constants slice
|
// constantLong uses three u8's that encode a u24 as the
|
||||||
var v0: u8 = chunk.code[offset + 3];
|
// contants' index.
|
||||||
var v1: u8 = chunk.code[offset + 2];
|
var v3: u8 = chunk.code[offset + 1];
|
||||||
var v2: u8 = chunk.code[offset + 1];
|
var v2: u8 = chunk.code[offset + 2];
|
||||||
|
var v1: u8 = chunk.code[offset + 3];
|
||||||
|
|
||||||
// TODO: this does not work. just decreased first term
|
var idx: u24 = (@intCast(u24, v3) << 16) | (@intCast(u24, v2) << 8) | v1;
|
||||||
// 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 stdout.print("\t{}\t{} '", name, idx);
|
||||||
try value.printValue(stdout, chunk.constants.values[idx]);
|
try value.printValue(stdout, chunk.constants.values[idx]);
|
||||||
try stdout.print("'\n");
|
try stdout.print("'\n");
|
||||||
|
|
||||||
return offset + 2;
|
return offset + 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const Chunk = struct {
|
pub const Chunk = struct {
|
||||||
|
@ -112,13 +107,18 @@ pub const Chunk = struct {
|
||||||
try self.write(OpCode.Constant, line);
|
try self.write(OpCode.Constant, line);
|
||||||
try self.write(@intCast(u8, constant_idx), line);
|
try self.write(@intCast(u8, constant_idx), line);
|
||||||
} else {
|
} else {
|
||||||
// TODO: convert the usize to u24, and from
|
var idx_u24: u24 = @intCast(u24, constant_idx);
|
||||||
// u24, split it into three u8's.
|
|
||||||
|
|
||||||
// also convert from u8 back to u24.
|
const mask = @intCast(u24, 0xff);
|
||||||
// i know that we can do from two u8's to go to a u16
|
|
||||||
// with (v1 << 7) | v0.
|
const v1: u8 = @intCast(u8, idx_u24 & mask);
|
||||||
try self.write(0, line);
|
const v2: u8 = @intCast(u8, (idx_u24 >> 8) & mask);
|
||||||
|
const v3: u8 = @intCast(u8, (idx_u24 >> 16) & mask);
|
||||||
|
|
||||||
|
try self.write(OpCode.ConstantLong, line);
|
||||||
|
try self.write(v3, line);
|
||||||
|
try self.write(v2, line);
|
||||||
|
try self.write(v1, line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -115,7 +115,10 @@ 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.writeConstant(1.2, 123);
|
var i: usize = 0;
|
||||||
|
while (i < 260) : (i += 1) {
|
||||||
|
var constant = try chk.writeConstant(1.2, 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");
|
||||||
|
|
Loading…
Reference in a new issue