Fixes call frames, Op.SetLocal

This commit is contained in:
Productive2 2020-12-26 20:05:45 +01:00
parent bfef0aa1a5
commit 7d059bd50e
3 changed files with 16 additions and 3 deletions

2
build.py Normal file → Executable file
View File

@ -1,3 +1,5 @@
#!/usr/bin/python
# Copyright 2020 Mattia Giambirtone
#
# Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -29,6 +29,12 @@ type
stack*: ref seq[ptr Obj]
proc clear*(self: CallFrame): int =
## Returns how much to clear, and clears that many
while self.stack.len() > self.slot:
discard self.stack.pop()
inc result
proc getView*(self: CallFrame): seq[ptr Obj] =
result = self.stack[self.slot..self.stack.high()]

View File

@ -224,7 +224,8 @@ proc call(self: var VM, function: ptr Function, argCount: uint8): bool =
if self.frameCount == FRAMES_MAX:
self.error(newRecursionError("max recursion depth exceeded"))
return false
var frame = CallFrame(function: function, ip: 0, slot: argCount, stack: self.stack) # TODO:
let slot = self.stack.high() - argCount
var frame = CallFrame(function: function, ip: 0, slot: slot, stack: self.stack) # TODO:
# Check why this raises NilAccessError when high recursion limit is hit
self.frames.add(frame)
self.frameCount += 1
@ -538,7 +539,6 @@ proc run(self: var VM, repl: bool): InterpretResult =
frame[frame.readBytes(), stackOffset] = self.peek(0)
else:
frame[int frame.readByte(), stackOffset] = self.peek(0)
discard self.pop()
of OpCode.DeleteLocal:
# TODO: Inspect potential issues with the GC
if frame.len > 255:
@ -575,8 +575,13 @@ proc run(self: var VM, repl: bool): InterpretResult =
if self.frameCount == 0:
discard self.pop()
return OK
self.stackTop -= frame.clear()
# frame.clear() clears the stack and returns the amount cleared
self.push(retResult)
self.stackTop = len(frame.getView()) - 1 # TODO
# self.stackTop = len(frame.getView()) - 1 # TODO
frame = self.frames[self.frameCount - 1]