Quality of life improvements and untested support for debugging the new BuildList/Set/Tuple/Dict instructions

This commit is contained in:
nocturn9x 2021-11-13 19:47:59 +01:00
parent 4f400d7aab
commit 15a2403120
1 changed files with 68 additions and 31 deletions

View File

@ -18,86 +18,123 @@ import multibyte
import strformat import strformat
import strutils
import terminal import terminal
proc printName(name: string) = proc nl = stdout.write("\n")
proc printDebug(s: string, newline: bool = false) =
stdout.write(&"DEBUG - Disassembler -> {s}")
if newline:
nl()
proc printName(name: string, newline: bool = false) =
setForegroundColor(fgRed) setForegroundColor(fgRed)
write stdout, name stdout.write(name)
setForegroundColor(fgGreen) setForegroundColor(fgGreen)
if newline:
nl()
proc nl = proc printInstruction(instruction: OpCode, newline: bool = false) =
write stdout, "\n" printDebug("Instruction: ")
printName($instruction)
if newline:
nl()
proc simpleInstruction(name: string, offset: int): int =
write stdout, &"DEBUG - Disassembler->\tInstruction: " proc simpleInstruction(instruction: OpCode, offset: int): int =
printName(name) printInstruction(instruction)
nl() nl()
return offset + 1 return offset + 1
proc byteInstruction(name: string, chunk: Chunk, offset: int): int = proc byteInstruction(instruction: OpCode, chunk: Chunk, offset: int): int =
var slot = chunk.code[offset + 1] var slot = chunk.code[offset + 1]
write stdout, &"DEBUG - Disassembler->\tInstruction: " printInstruction(instruction)
printName(name) stdout.write(&", points to slot {slot}")
write stdout, &", points to slot {slot}"
nl() nl()
return offset + 2 return offset + 2
proc constantInstruction(name: string, chunk: Chunk, offset: int): int = proc constantInstruction(instruction: OpCode, chunk: Chunk, offset: int): int =
# Rebuild the index # Rebuild the index
var constant = [chunk.code[offset + 1], chunk.code[offset + 2], chunk.code[offset + 3]].fromTriple() var constant = [chunk.code[offset + 1], chunk.code[offset + 2], chunk.code[offset + 3]].fromTriple()
write stdout, &"DEBUG - Disassembler ->\tInstruction: " printInstruction(instruction)
printName(name) stdout.write(&", points to slot ")
write stdout, &", points to slot "
setForegroundColor(fgYellow) setForegroundColor(fgYellow)
write stdout, &"{constant}" stdout.write(&"{constant}")
nl() nl()
let obj = chunk.consts[constant] let obj = chunk.consts[constant]
setForegroundColor(fgGreen) setForegroundColor(fgGreen)
stdout.write(&"DEBUG - Disassembler ->\tOperand: ") printDebug("Operand: ")
setForegroundColor(fgYellow) setForegroundColor(fgYellow)
stdout.write($obj) stdout.write(&"{obj}\n")
setForegroundColor(fgGreen) setForegroundColor(fgGreen)
stdout.write("\nDEBUG - Disassembler ->\tValue kind: ") printDebug("Value kind: ")
setForegroundColor(fgYellow) setForegroundColor(fgYellow)
stdout.write(&"{obj.kind}\n") stdout.write(&"{obj.kind}\n")
return offset + 4 return offset + 4
proc jumpInstruction(name: string, chunk: Chunk, offset: int): int = proc jumpInstruction(instruction: OpCode, chunk: Chunk, offset: int): int =
var jumpArray: array[2, uint8] = [chunk.code[offset + 1], chunk.code[offset + 2]] var jump = [chunk.code[offset + 1], chunk.code[offset + 2]].fromDouble()
write stdout, &"DEBUG - Disassembler ->\tInstruction: " printInstruction(instruction)
printName(name) printDebug(&"Jump size: {jump}")
write stdout, &"\nDEBUG - Disassembler ->:\tJump size: {jumpArray.fromDouble()} ( = {$jumpArray[0]}, {$jumpArray[1]})"
nl() nl()
return offset + 3 return offset + 3
proc collectionInstruction(instruction: OpCode, chunk: Chunk, offset: int): int =
var elemCount = int([chunk.code[offset + 1], chunk.code[offset + 2], chunk.code[offset + 3]].fromTriple())
printInstruction(instruction)
case instruction:
of BuildList, BuildTuple, BuildSet:
var elements: seq[ASTNode] = @[]
for n in countup(0, elemCount - 1):
elements.add(chunk.consts[n])
printDebug("")
setForegroundColor(fgYellow)
stdout.write(&"""Elements: [{elements.join(", ")}]""")
setForegroundColor(fgGreen)
of BuildDict:
var elements: seq[tuple[key: ASTNode, value: ASTNode]] = @[]
for n in countup(0, (elemCount - 1) * 2):
elements.add((key: chunk.consts[n], value: chunk.consts[n + 1]))
setForegroundColor(fgYellow)
printDebug(&"""Elements: [{elements.join(", ")}]""")
else:
discard # Unreachable
return offset + 2 + elemCount
proc disassembleInstruction*(chunk: Chunk, offset: int): int = proc disassembleInstruction*(chunk: Chunk, offset: int): int =
## Takes one bytecode instruction and prints it ## Takes one bytecode instruction and prints it
setForegroundColor(fgGreen) setForegroundColor(fgGreen)
stdout.write(&"DEBUG - Disassembler ->\tOffset: ") printDebug("Offset: ")
setForegroundColor(fgYellow) setForegroundColor(fgYellow)
stdout.write(&"{offset}") echo &"{offset}"
setForegroundColor(fgGreen) setForegroundColor(fgGreen)
stdout.write("\nDEBUG - Disassembler ->\tLine: ") printDebug("Line: ")
setForegroundColor(fgYellow) setForegroundColor(fgYellow)
stdout.write(&"{chunk.getLine(offset)}\n") stdout.write(&"{chunk.getLine(offset)}\n")
setForegroundColor(fgGreen) setForegroundColor(fgGreen)
var opcode = OpCode(chunk.code[offset]) var opcode = OpCode(chunk.code[offset])
case opcode: case opcode:
of simpleInstructions: of simpleInstructions:
result = simpleInstruction($opcode, offset) result = simpleInstruction(opcode, offset)
of constantInstructions: of constantInstructions:
result = constantInstruction($opcode, chunk, offset) result = constantInstruction(opcode, chunk, offset)
of byteInstructions: of byteInstructions:
result = byteInstruction($opcode, chunk, offset) result = byteInstruction(opcode, chunk, offset)
of jumpInstructions: of jumpInstructions:
result = jumpInstruction($opcode, chunk, offset) result = jumpInstruction(opcode, chunk, offset)
of collectionInstructions:
result = collectionInstruction(opcode, chunk, offset)
else: else:
echo &"DEBUG - Unknown opcode {opcode} at index {offset}" echo &"DEBUG - Unknown opcode {opcode} at index {offset}"
result = offset + 1 result = offset + 1