From cb34a22a530e2723e095b23f8db0d6f64b2ef245 Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Tue, 28 Dec 2021 16:37:30 +0100 Subject: [PATCH] Fixed a bug in the debugger when visualizing encoded dictionary elements and simplified dispatching code inside Compiler.expression() for literals --- .gitignore | 1 + src/backend/compiler.nim | 16 +++++----------- src/config.nim | 10 +++++----- src/util/debugger.nim | 6 ++++-- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 8291762..c7bb95a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ nimblecache/ htmldocs/ main *.jplc +.vscode/ diff --git a/src/backend/compiler.nim b/src/backend/compiler.nim index 4cff62d..79f5b06 100644 --- a/src/backend/compiler.nim +++ b/src/backend/compiler.nim @@ -632,19 +632,14 @@ proc expression(self: Compiler, node: ASTNode) = of binaryExpr: # Binary expressions such as 2 ^ 5 and 0.66 * 3.14 self.binary(BinaryExpr(node)) - of intExpr, hexExpr, binExpr, octExpr, strExpr, falseExpr, trueExpr, infExpr, nanExpr, floatExpr, nilExpr: - # Fortunately for us, all of these AST nodes types inherit from the base LiteralExpr - # type - self.literal(LiteralExpr(node)) - of tupleExpr, setExpr, listExpr: - # Since all of these AST nodes share - # the same structure, and the kind + of intExpr, hexExpr, binExpr, octExpr, strExpr, falseExpr, trueExpr, infExpr, nanExpr, floatExpr, nilExpr, + tupleExpr, setExpr, listExpr, dictExpr: + # Since all of these AST nodes mostly share + # the same overall structure, and the kind # discriminant is enough to tell one # from the other, why bother with # specialized cases when one is enough? - self.literal(ListExpr(node)) - of dictExpr: - self.literal(DictExpr(node)) + self.literal(node) else: self.error(&"invalid AST node of kind {node.kind} at expression(): {node} (This is an internal error and most likely a bug)") # TODO @@ -655,7 +650,6 @@ proc statement(self: Compiler, node: ASTNode) = of exprStmt: self.expression(ExprStmt(node).expression) self.emitByte(Pop) # Expression statements discard their value. Their main use case is side effects in function calls - # TODO of NodeKind.ifStmt: self.ifStmt(IfStmt(node)) of delStmt: diff --git a/src/config.nim b/src/config.nim index e43682f..4212016 100644 --- a/src/config.nim +++ b/src/config.nim @@ -15,18 +15,18 @@ import strformat const BYTECODE_MARKER* = "JAPL_BYTECODE" -const MAP_LOAD_FACTOR* = 0.75 # Load factor for builtin hashmaps -const HEAP_GROW_FACTOR* = 2 # How much extra memory to allocate for dynamic arrays and garbage collection when resizing +const MAP_LOAD_FACTOR* = 0.75 # Load factor for builtin hashmaps +const HEAP_GROW_FACTOR* = 2 # How much extra memory to allocate for dynamic arrays and garbage collection when resizing const MAX_STACK_FRAMES* = 800 # The maximum number of stack frames at any one time. Acts as a recursion limiter (1 frame = 1 call) const JAPL_VERSION* = (major: 0, minor: 4, patch: 0) const JAPL_RELEASE* = "alpha" const JAPL_COMMIT_HASH* = "e234c8caaff25f6edf69329cf8a17531cb7914dd" const JAPL_BRANCH* = "master" -const DEBUG_TRACE_VM* = false # Traces VM execution -const SKIP_STDLIB_INIT* = false # Skips stdlib initialization (can be imported manually) +const DEBUG_TRACE_VM* = false # Traces VM execution +const SKIP_STDLIB_INIT* = false # Skips stdlib initialization (can be imported manually) 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 DEBUG_TRACE_COMPILER* = false # Traces the compiler const JAPL_VERSION_STRING* = &"JAPL {JAPL_VERSION.major}.{JAPL_VERSION.minor}.{JAPL_VERSION.patch} {JAPL_RELEASE} ({JAPL_BRANCH}, {CompileDate}, {CompileTime}, {JAPL_COMMIT_HASH[0..8]}) [Nim {NimVersion}] on {hostOS} ({hostCPU})" const HELP_MESSAGE* = """The JAPL programming language, Copyright (C) 2021 Mattia Giambirtone & All Contributors diff --git a/src/util/debugger.nim b/src/util/debugger.nim index 2b043c3..5839e8b 100644 --- a/src/util/debugger.nim +++ b/src/util/debugger.nim @@ -138,10 +138,12 @@ proc collectionInstruction(instruction: OpCode, chunk: Chunk, offset: int): int setForegroundColor(fgGreen) of BuildDict: var elements: seq[tuple[key: ASTNode, value: ASTNode]] = @[] - for n in countup(0, (elemCount - 1) * 2): + for n in countup(0, (elemCount - 1) * 2, 2): elements.add((key: chunk.consts[n], value: chunk.consts[n + 1])) + printDebug("Elements: ") setForegroundColor(fgYellow) - printDebug(&"""Elements: [{elements.join(", ")}]""") + stdout.write(&"""[{elements.join(", ")}]""") + setForegroundColor(fgGreen) else: discard # Unreachable echo ""