2022-04-04 12:29:23 +02:00
|
|
|
# Copyright 2022 Mattia Giambirtone & All Contributors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2022-04-28 18:06:53 +02:00
|
|
|
## The Peon runtime environment
|
|
|
|
import types
|
|
|
|
import ../config
|
|
|
|
import ../frontend/meta/bytecode
|
2022-04-04 12:29:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
type
|
2022-04-28 18:06:53 +02:00
|
|
|
PeonVM* = ref object
|
|
|
|
## The Peon Virtual Machine
|
|
|
|
stack: seq[PeonObject]
|
|
|
|
ip: int # Instruction pointer
|
|
|
|
sp: int # Stack pointer
|
|
|
|
cache: array[6, PeonObject] # Singletons cache
|
|
|
|
chunk: Chunk # Piece of bytecode to execute
|
|
|
|
|
|
|
|
|
|
|
|
proc newPeonVM*: PeonVM =
|
|
|
|
## Initializes a new, blank VM
|
|
|
|
## for executing Peon bytecode
|
|
|
|
new(result)
|
|
|
|
result.ip = 0
|
|
|
|
result.sp = 0
|
|
|
|
result.stack = newSeqOfCap[PeonObject](INITIAL_STACK_SIZE)
|
|
|
|
result.cache[0] = newNil()
|
|
|
|
result.cache[1] = newBool(true)
|
|
|
|
result.cache[2] = newBool(false)
|
|
|
|
result.cache[3] = newInf(true)
|
|
|
|
result.cache[4] = newInf(false)
|
|
|
|
result.cache[5] = newNan()
|
|
|
|
for _ in 0..<INITIAL_STACK_SIZE:
|
|
|
|
result.stack.add(result.cache[0])
|
|
|
|
|
2022-04-29 17:15:57 +02:00
|
|
|
# Getters for singleton types (they are cached!)
|
2022-04-28 18:06:53 +02:00
|
|
|
|
|
|
|
proc getNil*(self: PeonVM): Nil = Nil(self.cache[0])
|
|
|
|
|
|
|
|
proc getBool*(self: PeonVM, value: bool): Bool =
|
|
|
|
if value:
|
|
|
|
return Bool(self.cache[1])
|
|
|
|
return Bool(self.cache[2])
|
|
|
|
|
|
|
|
proc getInf*(self: PeonVM, positive: bool): Inf =
|
|
|
|
if positive:
|
|
|
|
return types.Inf(self.cache[3])
|
|
|
|
return types.Inf(self.cache[4])
|
|
|
|
|
|
|
|
proc getNan*(self: PeonVM): Nan = types.Nan(self.cache[5])
|
|
|
|
|
2022-04-29 17:15:57 +02:00
|
|
|
# Stack primitives
|
2022-04-28 18:06:53 +02:00
|
|
|
|
|
|
|
proc push(self: PeonVM, obj: PeonObject) =
|
2022-04-29 17:15:57 +02:00
|
|
|
## Pushes a Peon object onto the
|
|
|
|
## stack
|
2022-04-28 18:06:53 +02:00
|
|
|
if self.sp >= self.stack.high():
|
|
|
|
for _ in 0..self.stack.len():
|
|
|
|
self.stack.add(newNil())
|
|
|
|
self.stack[self.sp] = obj
|
|
|
|
inc(self.sp)
|
|
|
|
|
|
|
|
|
|
|
|
proc pop(self: PeonVM): PeonObject =
|
2022-04-29 17:15:57 +02:00
|
|
|
## Pops a Peon object off the
|
|
|
|
## stack, decreasing the stack
|
|
|
|
## pointer. The object is returned
|
2022-04-28 18:06:53 +02:00
|
|
|
dec(self.sp)
|
|
|
|
return self.stack[self.sp]
|
|
|
|
|
|
|
|
|
|
|
|
proc readByte(self: PeonVM, chunk: Chunk): uint8 =
|
|
|
|
inc(self.ip)
|
|
|
|
return chunk.code[self.ip - 1]
|
|
|
|
|
|
|
|
|
|
|
|
proc dispatch*(self: PeonVM) =
|
|
|
|
## Main bytecode dispatch loop
|
|
|
|
var instruction: OpCode
|
|
|
|
while true:
|
|
|
|
stdout.write("[")
|
|
|
|
for i, e in self.stack:
|
|
|
|
stdout.write($(e[]))
|
|
|
|
if i < self.stack.high():
|
|
|
|
stdout.write(", ")
|
|
|
|
echo "]"
|
|
|
|
instruction = OpCode(self.readByte(self.chunk))
|
|
|
|
echo instruction
|
|
|
|
case instruction:
|
|
|
|
of OpCode.True:
|
|
|
|
self.push(self.getBool(true))
|
|
|
|
of OpCode.False:
|
|
|
|
self.push(self.getBool(false))
|
|
|
|
of OpCode.Nan:
|
|
|
|
self.push(self.getNan())
|
|
|
|
of OpCode.Nil:
|
|
|
|
self.push(self.getNil())
|
|
|
|
of OpCode.Inf:
|
|
|
|
self.push(self.getInf(true))
|
|
|
|
of UnaryPlus:
|
|
|
|
self.push(self.pop())
|
|
|
|
of OpCode.Return:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
|
|
|
|
proc run*(self: PeonVM, chunk: Chunk) =
|
|
|
|
## Executes a piece of Peon bytecode.
|
|
|
|
self.chunk = chunk
|
|
|
|
self.sp = 0
|
|
|
|
self.ip = 0
|
|
|
|
self.dispatch()
|