Added support for OP_CONSTANT_LONG

This commit is contained in:
nocturn9x 2020-08-07 19:38:52 +02:00
parent ab6cb16e42
commit ee9444a3bf
3 changed files with 35 additions and 8 deletions

View File

@ -11,8 +11,11 @@ proc main() =
echo "Creating arbitrary chunk"
var chunk: Chunk = initChunk()
var index: int = chunk.addConstant(Value(kind: FLOAT, floatValue: 1.2))
var index2: array[3, uint8] = chunk.writeConstant(Value(kind: INT, intValue: 56))
chunk.writeChunk(uint8 OP_CONSTANT, 1)
chunk.writeChunk(uint8 index, 1)
chunk.writeChunk(uint8 OP_CONSTANT_LONG, 1)
chunk.writeChunk(index2, 1)
chunk.writeChunk(uint8 OP_RETURN, 1)
echo "Disassembling chunk"
chunk.disassembleChunk("test chunk")

View File

@ -16,10 +16,16 @@ proc initChunk*(): Chunk =
result = Chunk(consts: initValueArray(), code: @[], lines: @[])
proc writeChunk*(self: Chunk, byte: uint8, line: int) =
self.code.add(byte)
proc writeChunk*(self: Chunk, byt: uint8, line: int) =
self.code.add(byt)
self.lines.add(line)
proc writeChunk*(self: Chunk, bytes: array[3, uint8], line: int) =
for byt in bytes:
self.writeChunk(byt, line)
proc freeChunk*(self: var Chunk) =
self.consts = ValueArray(values: @[])
self.code = @[]
@ -29,3 +35,10 @@ proc freeChunk*(self: var Chunk) =
proc addConstant*(chunk: var Chunk, constant: Value): int =
chunk.consts.values.add(constant)
return len(chunk.consts.values) - 1 # The index of the constant
proc writeConstant*(chunk: var Chunk, constant: Value): array[3, uint8] =
let index = chunk.addConstant(constant)
result = cast[array[3, uint8]](index)

View File

@ -4,12 +4,22 @@ import strformat
proc simpleInstruction(name: string, index: int): int =
var index = index
echo &"\tOpCode at offset: {name}"
echo ""
return index + 1
proc constantLongInstruction(name: string, chunk: Chunk, offset: int): int =
# Rebuild the index
var constantArray: array[3, uint8] = [chunk.code[offset + 1], chunk.code[offset + 2], chunk.code[offset + 3]]
var constant: int
copyMem(constant.addr, unsafeAddr(constantArray), sizeof(constantArray))
echo &"\tOpCode at offset: {name}, points to {constant}"
printValue(chunk.consts.values[constant])
echo ""
return offset + 4
proc constantInstruction(name: string, chunk: Chunk, offset: int): int =
var constant = chunk.code[offset + 1]
echo &"\tOpCode at offset: {name}, points to index {constant}"
@ -22,18 +32,19 @@ proc disassembleInstruction*(chunk: Chunk, offset: int): int =
echo &"Current offset: {offset}\nCurrent line: {chunk.lines[offset]}"
var opcode = OpCode(chunk.code[offset])
if opcode == OP_RETURN:
simpleInstruction("OP_RETURN", offset)
result = simpleInstruction("OP_RETURN", offset)
elif opcode == OP_CONSTANT:
constantInstruction("OP_CONSTANT", chunk, offset)
result = constantInstruction("OP_CONSTANT", chunk, offset)
elif opcode == OP_CONSTANT_LONG:
result = constantLongInstruction("OP_CONSTANT_LONG", chunk, offset)
else:
echo &"Unknown opcode {opcode} at index {offset}"
return offset + 1
result = offset + 1
proc disassembleChunk*(chunk: Chunk, name: string) =
echo &"==== JAPL VM Debugger - Chunk '{name}' ====\n"
var index = 0
echo chunk.lines
while index < chunk.code.len:
index = disassembleInstruction(chunk, index)
echo &"==== Debug session ended - Chunk '{name}' ===="