If statement now pops from the stack, added debug machinery for new opcodes

This commit is contained in:
nocturn9x 2020-08-19 13:10:34 +02:00
parent a9f35d2134
commit 941e728a92
2 changed files with 12 additions and 1 deletions

View File

@ -1,6 +1,5 @@
import strutils
import algorithm
import bitops
import strformat
import lexer
import meta/chunk
@ -475,9 +474,11 @@ proc ifStatement(self: var Compiler) =
self.expression()
self.parser.consume(RP, "The if condition must be parenthesized")
var jump: int = self.emitJump(OP_JUMP_IF_FALSE)
self.emitByte(OP_POP)
self.statement()
var elseJump = self.emitJump(OP_JUMP)
self.patchJump(jump)
self.emitByte(OP_POP)
if self.parser.match(ELSE):
self.statement()
self.patchJump(elseJump)

View File

@ -34,6 +34,12 @@ proc constantInstruction(name: string, chunk: Chunk, offset: int): int =
return offset + 2
proc jumpInstruction(name: string, sign: int, chunk: Chunk, offset: int): int =
var jump = uint16 (chunk.code[offset + 1] shr 8)
jump = jump or chunk.code[offset + 2]
echo &"\tOpCode at offset: {name}\n\tJump size: {jump}\n"
return offset + 3
proc disassembleInstruction*(chunk: Chunk, offset: int): int =
echo &"Current offset: {offset}\nCurrent line: {chunk.lines[offset]}"
var opcode = OpCode(chunk.code[offset])
@ -91,6 +97,10 @@ proc disassembleInstruction*(chunk: Chunk, offset: int): int =
result = byteInstruction("OP_DELETE_LOCAL", chunk, offset)
elif opcode == OP_DELETE_GLOBAL:
result = simpleInstruction("OP_DELETE_GLOBAL", offset)
elif opcode == OP_JUMP_IF_FALSE:
result = jumpInstruction("OP_JUMP_IF_FALSE", 1, chunk, offset)
elif opcode == OP_JUMP:
result = jumpInstruction("OP_JUMP", 1, chunk, offset)
else:
echo &"Unknown opcode {opcode} at index {offset}"
result = offset + 1