Cleaned up and renamed some things
This commit is contained in:
parent
19a089f4a2
commit
f7733d925f
|
@ -24,7 +24,7 @@ import ../util/multibyte
|
||||||
|
|
||||||
|
|
||||||
import strutils
|
import strutils
|
||||||
when DEBUG_TRACE_VM:
|
when debugVM:
|
||||||
import std/strformat
|
import std/strformat
|
||||||
|
|
||||||
|
|
||||||
|
@ -315,7 +315,7 @@ proc dispatch*(self: PeonVM) =
|
||||||
var instruction {.register.}: OpCode
|
var instruction {.register.}: OpCode
|
||||||
while true:
|
while true:
|
||||||
{.computedgoto.} # https://nim-lang.org/docs/manual.html#pragmas-computedgoto-pragma
|
{.computedgoto.} # https://nim-lang.org/docs/manual.html#pragmas-computedgoto-pragma
|
||||||
when DEBUG_TRACE_VM:
|
when debugVM:
|
||||||
echo &"IP: {self.ip}"
|
echo &"IP: {self.ip}"
|
||||||
echo &"Instruction: {OpCode(self.chunk.code[self.ip])}"
|
echo &"Instruction: {OpCode(self.chunk.code[self.ip])}"
|
||||||
if self.calls.len() !> 0:
|
if self.calls.len() !> 0:
|
||||||
|
@ -450,7 +450,7 @@ proc dispatch*(self: PeonVM) =
|
||||||
# Stores the value at the top of the operand stack
|
# Stores the value at the top of the operand stack
|
||||||
# into the given call stack index
|
# into the given call stack index
|
||||||
let idx = self.readLong()
|
let idx = self.readLong()
|
||||||
when DEBUG_TRACE_VM:
|
when debugVM:
|
||||||
assert idx.int - self.calls.high() <= 1, "StoreVar index is bigger than the length of the call stack"
|
assert idx.int - self.calls.high() <= 1, "StoreVar index is bigger than the length of the call stack"
|
||||||
if idx + self.frames[^1] <= self.calls.high().uint:
|
if idx + self.frames[^1] <= self.calls.high().uint:
|
||||||
self.setc(idx, self.pop())
|
self.setc(idx, self.pop())
|
||||||
|
|
|
@ -14,25 +14,28 @@
|
||||||
|
|
||||||
import strformat
|
import strformat
|
||||||
|
|
||||||
const debugVM {.booldefine.} = false
|
# Debug various components of peon
|
||||||
const BYTECODE_MARKER* = "PEON_BYTECODE"
|
const debugLexer* {.booldefine.} = false
|
||||||
const HEAP_GROW_FACTOR* = 2 # How much extra memory to allocate for dynamic arrays and garbage collection when resizing
|
const debugParser* {.booldefine.} = false
|
||||||
|
const debugCompiler* {.booldefine.} = false
|
||||||
|
const debugVM* {.booldefine.} = false
|
||||||
|
const debugGC* {.booldefine.} = false
|
||||||
|
const debugMem* {.booldefine.} = false
|
||||||
|
const debugSerializer* {.booldefine.} = false
|
||||||
|
const PeonBytecodeMarker* = "PEON_BYTECODE"
|
||||||
|
const HeapGrowFactor* = 2 # How much extra memory to allocate for dynamic arrays and garbage collection when resizing
|
||||||
when HEAP_GROW_FACTOR <= 1:
|
when HEAP_GROW_FACTOR <= 1:
|
||||||
{.fatal: "Heap growth factor must be > 1".}
|
{.fatal: "Heap growth factor must be > 1".}
|
||||||
const PEON_VERSION* = (major: 0, minor: 1, patch: 0)
|
const PeonVersion* = (major: 0, minor: 1, patch: 0)
|
||||||
const PEON_RELEASE* = "alpha"
|
const PeonRelease* = "alpha"
|
||||||
const PEON_COMMIT_HASH* = "b273cd744883458a4a6354a0cc5f4f5d0f560c31"
|
const PeonCommitHash* = "b273cd744883458a4a6354a0cc5f4f5d0f560c31"
|
||||||
when len(PEON_COMMIT_HASH) != 40:
|
when len(PeonCommitHash) != 40:
|
||||||
{.fatal: "The git commit hash must be exactly 40 characters long".}
|
{.fatal: "The git commit hash must be exactly 40 characters long".}
|
||||||
const PEON_BRANCH* = "unboxed-types"
|
const PeonBranch* = "unboxed-types"
|
||||||
when len(PEON_BRANCH) > 255:
|
when len(PEON_BRANCH) > 255:
|
||||||
{.fatal: "The git branch name's length must be less than or equal to 255 characters".}
|
{.fatal: "The git branch name's length must be less than or equal to 255 characters".}
|
||||||
const DEBUG_TRACE_VM* = debugVM # Traces VM execution
|
const PeonVersionString* = &"Peon {PeonVersion.major}.{PeonVersion.minor}.{PeonVersion.patch} {PeonRelease} ({PeonBranch}, {CompileDate}, {CompileTime}, {PeonCommitHash[0..8]}) [Nim {NimVersion}] on {hostOS} ({hostCPU})"
|
||||||
const DEBUG_TRACE_GC* = false # Traces the garbage collector (TODO)
|
const HelpMessage* = """The peon programming language, Copyright (C) 2022 Mattia Giambirtone & All Contributors
|
||||||
const DEBUG_TRACE_ALLOCATION* = false # Traces memory allocation/deallocation
|
|
||||||
const DEBUG_TRACE_COMPILER* = false # Traces the compiler
|
|
||||||
const PEON_VERSION_STRING* = &"Peon {PEON_VERSION.major}.{PEON_VERSION.minor}.{PEON_VERSION.patch} {PEON_RELEASE} ({PEON_BRANCH}, {CompileDate}, {CompileTime}, {PEON_COMMIT_HASH[0..8]}) [Nim {NimVersion}] on {hostOS} ({hostCPU})"
|
|
||||||
const HELP_MESSAGE* = """The peon programming language, Copyright (C) 2022 Mattia Giambirtone & All Contributors
|
|
||||||
|
|
||||||
This program is free software, see the license distributed with this program or check
|
This program is free software, see the license distributed with this program or check
|
||||||
http://www.apache.org/licenses/LICENSE-2.0 for more info.
|
http://www.apache.org/licenses/LICENSE-2.0 for more info.
|
||||||
|
|
|
@ -282,8 +282,6 @@ proc step(self: Compiler): ASTNode {.inline.} =
|
||||||
proc emitByte(self: Compiler, byt: OpCode | uint8) {.inline.} =
|
proc emitByte(self: Compiler, byt: OpCode | uint8) {.inline.} =
|
||||||
## Emits a single byte, writing it to
|
## Emits a single byte, writing it to
|
||||||
## the current chunk being compiled
|
## the current chunk being compiled
|
||||||
when DEBUG_TRACE_COMPILER:
|
|
||||||
echo &"DEBUG - Compiler: Emitting {$byt}"
|
|
||||||
self.chunk.write(uint8 byt, self.peek().token.line)
|
self.chunk.write(uint8 byt, self.peek().token.line)
|
||||||
|
|
||||||
|
|
||||||
|
|
12
src/main.nim
12
src/main.nim
|
@ -27,14 +27,6 @@ import config
|
||||||
# Forward declarations
|
# Forward declarations
|
||||||
proc getLineEditor: LineEditor
|
proc getLineEditor: LineEditor
|
||||||
|
|
||||||
# Handy dandy compile-time constants
|
|
||||||
const debugLexer {.booldefine.} = false
|
|
||||||
const debugParser {.booldefine.} = false
|
|
||||||
const debugCompiler {.booldefine.} = false
|
|
||||||
const debugSerializer {.booldefine.} = false
|
|
||||||
const debugRuntime {.booldefine.} = false
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
proc repl =
|
proc repl =
|
||||||
styledEcho fgMagenta, "Welcome into the peon REPL!"
|
styledEcho fgMagenta, "Welcome into the peon REPL!"
|
||||||
|
@ -131,7 +123,7 @@ proc repl =
|
||||||
styledEcho fgGreen, "OK"
|
styledEcho fgGreen, "OK"
|
||||||
else:
|
else:
|
||||||
styledEcho fgRed, "Corrupted"
|
styledEcho fgRed, "Corrupted"
|
||||||
when debugRuntime:
|
when debugVM:
|
||||||
styledEcho fgCyan, "\n\nExecution step: "
|
styledEcho fgCyan, "\n\nExecution step: "
|
||||||
vm.run(serialized.chunk)
|
vm.run(serialized.chunk)
|
||||||
except LexingError:
|
except LexingError:
|
||||||
|
@ -266,7 +258,7 @@ proc runFile(f: string, interactive: bool = false, fromString: bool = false) =
|
||||||
styledEcho fgGreen, "OK"
|
styledEcho fgGreen, "OK"
|
||||||
else:
|
else:
|
||||||
styledEcho fgRed, "Corrupted"
|
styledEcho fgRed, "Corrupted"
|
||||||
when debugRuntime:
|
when debugVM:
|
||||||
styledEcho fgCyan, "\n\nExecution step: "
|
styledEcho fgCyan, "\n\nExecution step: "
|
||||||
vm.run(serialized.chunk)
|
vm.run(serialized.chunk)
|
||||||
except LexingError:
|
except LexingError:
|
||||||
|
|
|
@ -62,7 +62,7 @@ proc newSerializer*(self: Serializer = nil): Serializer =
|
||||||
|
|
||||||
proc writeHeaders(self: Serializer, stream: var seq[byte]) =
|
proc writeHeaders(self: Serializer, stream: var seq[byte]) =
|
||||||
## Writes the Peon bytecode headers in-place into a byte stream
|
## Writes the Peon bytecode headers in-place into a byte stream
|
||||||
stream.extend(BYTECODE_MARKER.toBytes())
|
stream.extend(PeonBytecodeMarker.toBytes())
|
||||||
stream.add(byte(PEON_VERSION.major))
|
stream.add(byte(PEON_VERSION.major))
|
||||||
stream.add(byte(PEON_VERSION.minor))
|
stream.add(byte(PEON_VERSION.minor))
|
||||||
stream.add(byte(PEON_VERSION.patch))
|
stream.add(byte(PEON_VERSION.patch))
|
||||||
|
@ -106,10 +106,10 @@ proc readHeaders(self: Serializer, stream: seq[byte], serialized: Serialized): i
|
||||||
## Reads the bytecode headers from a given stream
|
## Reads the bytecode headers from a given stream
|
||||||
## of bytes
|
## of bytes
|
||||||
var stream = stream
|
var stream = stream
|
||||||
if stream[0..<len(BYTECODE_MARKER)] != BYTECODE_MARKER.toBytes():
|
if stream[0..<len(PeonBytecodeMarker)] != PeonBytecodeMarker.toBytes():
|
||||||
self.error("malformed bytecode marker")
|
self.error("malformed bytecode marker")
|
||||||
result += len(BYTECODE_MARKER)
|
result += len(PeonBytecodeMarker)
|
||||||
stream = stream[len(BYTECODE_MARKER)..^1]
|
stream = stream[len(PeonBytecodeMarker)..^1]
|
||||||
serialized.version = (major: int(stream[0]), minor: int(stream[1]), patch: int(stream[2]))
|
serialized.version = (major: int(stream[0]), minor: int(stream[1]), patch: int(stream[2]))
|
||||||
stream = stream[3..^1]
|
stream = stream[3..^1]
|
||||||
result += 3
|
result += 3
|
||||||
|
|
Loading…
Reference in New Issue