diff --git a/src/backend/vm.nim b/src/backend/vm.nim index c45e100..c971eeb 100644 --- a/src/backend/vm.nim +++ b/src/backend/vm.nim @@ -31,7 +31,7 @@ when debugVM or debugMem or debugGC: import std/terminal -{.push checks:off.} # The VM is a critical point where checks are deleterious +{.push checks:on.} # The VM is a critical point where checks are deleterious type PeonVM* = ref object @@ -54,6 +54,7 @@ type operands: seq[uint64] # Our operand stack cache: array[6, uint64] # Singletons cache frames: seq[uint64] # Stores the bottom of stack frames + closures: seq[uint64] # Stores closure offsets closedOver: seq[uint64] # Stores variables that do not have stack semantics results: seq[uint64] # Stores function's results (return values) gc: PeonGC @@ -62,7 +63,8 @@ type ## peon objects String, List, Dict, Tuple, - CustomType + CustomType, + Closure HeapObject* = object ## A tagged box for a heap-allocated ## peon object @@ -429,11 +431,11 @@ proc peekc(self: PeonVM, distance: int = 0): uint64 {.used.} = return self.calls[self.calls.high() + distance] -proc getc(self: PeonVM, idx: uint64): uint64 = +proc getc(self: PeonVM, idx: int): uint64 = ## Accessor method that abstracts ## indexing our call stack through stack ## frames - return self.calls[idx + self.frames[^1]] + return self.calls[idx.uint64 + self.frames[^1]] proc setc(self: PeonVM, idx: uint, val: uint64) = @@ -636,6 +638,13 @@ when debugVM: # So nim shuts up if i < self.results.high(): stdout.styledWrite(fgYellow, ", ") styledEcho fgMagenta, "]" + if self.closures.len() !> 0: + stdout.styledWrite(fgBlue, "Closure offsets: ", fgMagenta, "[") + for i, e in self.closures: + stdout.styledWrite(fgYellow, $e) + if i < self.closures.high(): + stdout.styledWrite(fgYellow, ", ") + styledEcho fgMagenta, "]" discard readLine stdin @@ -713,6 +722,7 @@ proc dispatch*(self: PeonVM) = self.results.add(self.getNil()) # Creates a new call frame self.frames.add(uint64(self.calls.len() - 2)) + self.closures.add(self.closedOver.len().uint64) # Loads the arguments onto the stack for _ in 0..