From f7733d925f580125c06589a119bc3a27b1321b43 Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Wed, 17 Aug 2022 19:23:11 +0200 Subject: [PATCH] Cleaned up and renamed some things --- src/backend/vm.nim | 6 +++--- src/config.nim | 31 +++++++++++++++++-------------- src/frontend/compiler.nim | 2 -- src/main.nim | 12 ++---------- src/util/serializer.nim | 8 ++++---- 5 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/backend/vm.nim b/src/backend/vm.nim index b0b1f05..d443492 100644 --- a/src/backend/vm.nim +++ b/src/backend/vm.nim @@ -24,7 +24,7 @@ import ../util/multibyte import strutils -when DEBUG_TRACE_VM: +when debugVM: import std/strformat @@ -315,7 +315,7 @@ proc dispatch*(self: PeonVM) = var instruction {.register.}: OpCode while true: {.computedgoto.} # https://nim-lang.org/docs/manual.html#pragmas-computedgoto-pragma - when DEBUG_TRACE_VM: + when debugVM: echo &"IP: {self.ip}" echo &"Instruction: {OpCode(self.chunk.code[self.ip])}" if self.calls.len() !> 0: @@ -450,7 +450,7 @@ proc dispatch*(self: PeonVM) = # Stores the value at the top of the operand stack # into the given call stack index 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" if idx + self.frames[^1] <= self.calls.high().uint: self.setc(idx, self.pop()) diff --git a/src/config.nim b/src/config.nim index cfd6dd5..27b78bf 100644 --- a/src/config.nim +++ b/src/config.nim @@ -14,25 +14,28 @@ import strformat -const debugVM {.booldefine.} = false -const BYTECODE_MARKER* = "PEON_BYTECODE" -const HEAP_GROW_FACTOR* = 2 # How much extra memory to allocate for dynamic arrays and garbage collection when resizing +# Debug various components of peon +const debugLexer* {.booldefine.} = false +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: {.fatal: "Heap growth factor must be > 1".} -const PEON_VERSION* = (major: 0, minor: 1, patch: 0) -const PEON_RELEASE* = "alpha" -const PEON_COMMIT_HASH* = "b273cd744883458a4a6354a0cc5f4f5d0f560c31" -when len(PEON_COMMIT_HASH) != 40: +const PeonVersion* = (major: 0, minor: 1, patch: 0) +const PeonRelease* = "alpha" +const PeonCommitHash* = "b273cd744883458a4a6354a0cc5f4f5d0f560c31" +when len(PeonCommitHash) != 40: {.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: {.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 DEBUG_TRACE_GC* = false # Traces the garbage collector (TODO) -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 +const PeonVersionString* = &"Peon {PeonVersion.major}.{PeonVersion.minor}.{PeonVersion.patch} {PeonRelease} ({PeonBranch}, {CompileDate}, {CompileTime}, {PeonCommitHash[0..8]}) [Nim {NimVersion}] on {hostOS} ({hostCPU})" +const HelpMessage* = """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 http://www.apache.org/licenses/LICENSE-2.0 for more info. diff --git a/src/frontend/compiler.nim b/src/frontend/compiler.nim index 89bf469..a025e3f 100644 --- a/src/frontend/compiler.nim +++ b/src/frontend/compiler.nim @@ -282,8 +282,6 @@ proc step(self: Compiler): ASTNode {.inline.} = proc emitByte(self: Compiler, byt: OpCode | uint8) {.inline.} = ## Emits a single byte, writing it to ## the current chunk being compiled - when DEBUG_TRACE_COMPILER: - echo &"DEBUG - Compiler: Emitting {$byt}" self.chunk.write(uint8 byt, self.peek().token.line) diff --git a/src/main.nim b/src/main.nim index 2dced78..8255cf9 100644 --- a/src/main.nim +++ b/src/main.nim @@ -27,14 +27,6 @@ import config # Forward declarations 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 = styledEcho fgMagenta, "Welcome into the peon REPL!" @@ -131,7 +123,7 @@ proc repl = styledEcho fgGreen, "OK" else: styledEcho fgRed, "Corrupted" - when debugRuntime: + when debugVM: styledEcho fgCyan, "\n\nExecution step: " vm.run(serialized.chunk) except LexingError: @@ -266,7 +258,7 @@ proc runFile(f: string, interactive: bool = false, fromString: bool = false) = styledEcho fgGreen, "OK" else: styledEcho fgRed, "Corrupted" - when debugRuntime: + when debugVM: styledEcho fgCyan, "\n\nExecution step: " vm.run(serialized.chunk) except LexingError: diff --git a/src/util/serializer.nim b/src/util/serializer.nim index 68dd6a8..74ca49d 100644 --- a/src/util/serializer.nim +++ b/src/util/serializer.nim @@ -62,7 +62,7 @@ proc newSerializer*(self: Serializer = nil): Serializer = proc writeHeaders(self: Serializer, stream: var seq[byte]) = ## 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.minor)) 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 ## of bytes var stream = stream - if stream[0..