Fixed a bug in the debugger when visualizing encoded dictionary elements and simplified dispatching code inside Compiler.expression() for literals

This commit is contained in:
nocturn9x 2021-12-28 16:37:30 +01:00
parent 71dfac87dd
commit cb34a22a53
4 changed files with 15 additions and 18 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ nimblecache/
htmldocs/
main
*.jplc
.vscode/

View File

@ -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:

View File

@ -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 ""