From 1d228c6310276b5521654b68e765ada10b0e48d6 Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Thu, 2 Jun 2022 12:19:18 +0200 Subject: [PATCH] Fixed logic bug within if/else construct --- src/backend/vm.nim | 2 +- src/config.nim | 2 +- src/frontend/compiler.nim | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/backend/vm.nim b/src/backend/vm.nim index 74aa9e3..69f30e8 100644 --- a/src/backend/vm.nim +++ b/src/backend/vm.nim @@ -484,7 +484,7 @@ proc dispatch*(self: PeonVM) = of JumpIfFalsePop: let ip = self.readLong() if not self.peek().boolean: - self.ip = ip + self.ip += ip discard self.pop() of JumpIfFalseOrPop: if not self.peek().boolean: diff --git a/src/config.nim b/src/config.nim index 218807b..12c2291 100644 --- a/src/config.nim +++ b/src/config.nim @@ -27,7 +27,7 @@ when len(PEON_COMMIT_HASH) != 40: const PEON_BRANCH* = "master" when len(PEON_BRANCH) > 255: {.fatal: "The git branch name's length must be less than or equal to 255 characters".} -const DEBUG_TRACE_VM* = true # Traces VM execution +const DEBUG_TRACE_VM* = false # Traces VM execution const DEBUG_TRACE_GC* = false # Traces the garbage collector (TODO) const DEBUG_TRACE_ALLOCATION* = false # Traces memory allocation/deallocation const DEBUG_TRACE_COMPILER* = false # Traces the compiler diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index 13555da..b40b5f0 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -1119,11 +1119,11 @@ proc ifStmt(self: Compiler, node: IfStmt) = if not self.enableOptimizations: self.emitByte(Pop) self.statement(node.thenBranch) + let jump2 = self.emitJump(JumpForwards) self.patchJump(jump) if node.elseBranch != nil: - let jump = self.emitJump(JumpForwards) self.statement(node.elseBranch) - self.patchJump(jump) + self.patchJump(jump2) proc emitLoop(self: Compiler, begin: int) = @@ -1555,7 +1555,7 @@ proc compile*(self: Compiler, ast: seq[Declaration], file: string): Chunk = name: "", returnType: nil, args: @[]), - codePos: 13, + codePos: 13, # Jump address is hardcoded name: newIdentExpr(Token(lexeme: "", kind: Identifier)), line: -1) self.names.add(main)