Merge pull request #16 from Productive2/master

Endscope fix, more debug
This commit is contained in:
Productive2 2020-10-25 15:12:57 +01:00 committed by GitHub
commit 2809c6387b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 0 deletions

View File

@ -16,6 +16,7 @@
import strutils
import sequtils
import algorithm
import strformat
import lexer
@ -26,6 +27,7 @@ import types/jobject
import types/jstring
import types/function
import tables
import config
when isMainModule:
import util/debug
@ -150,6 +152,8 @@ proc compileError(self: ref Compiler, message: string) =
proc emitByte(self: ref Compiler, byt: OpCode|uint8) =
## Emits a single bytecode instruction and writes it
## to the current chunk being compiled
when DEBUG_TRACE_COMPILER:
echo "Compiler.emitByte byt:" & $byt & " (uint8 value of " & $(uint8 byt) & ")"
self.currentChunk.writeChunk(uint8 byt, self.parser.previous.line)
@ -666,10 +670,15 @@ proc endScope(self: ref Compiler) =
## Ends a scope, popping off any local that
## was created inside it along the way
self.scopeDepth = self.scopeDepth - 1
var start: Natural = self.localCount
while self.localCount > 0 and self.locals[self.localCount - 1].depth > self.scopeDepth:
self.emitByte(OpCode.Pop)
self.localCount = self.localCount - 1
if start >= self.localCount:
self.locals.delete(self.localCount, start)
proc emitJump(self: ref Compiler, opcode: OpCode): int =
## Emits a jump instruction with a placeholder offset