From ef794ea881753d88cb879fbf214c65fd190600ef Mon Sep 17 00:00:00 2001 From: prod2 <95874442+prod2@users.noreply.github.com> Date: Sat, 3 Dec 2022 12:19:35 +0100 Subject: [PATCH] add dump AST, make AST printing cleaner --- src/nds.nim | 2 ++ src/ndspkg/compv2/node.nim | 32 +++++++++++--------------------- src/ndspkg/config.nim | 3 ++- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/nds.nim b/src/nds.nim index 8a81229..2c4c4b9 100644 --- a/src/nds.nim +++ b/src/nds.nim @@ -26,6 +26,8 @@ proc interpret(name: string, source: string): Result = let node = parser.parse() if parser.hadError: return rsCompileError + when debugDumpAst: + echo $node let emitter = newEmitter(name, node) emitter.emit() if emitter.hadError: diff --git a/src/ndspkg/compv2/node.nim b/src/ndspkg/compv2/node.nim index 03e52f8..8e5439b 100644 --- a/src/ndspkg/compv2/node.nim +++ b/src/ndspkg/compv2/node.nim @@ -3,6 +3,7 @@ import ../types/value import strformat import strutils +import sequtils type NodeKind* = enum @@ -76,22 +77,16 @@ proc `$`*(node: Node): string = result = &"(and {node.left} {node.right})" of nkBlockExpr: let labels = node.labels.join(", ") - result = &"(block labels: {labels} elements: " - for ch in node.children: - result &= $ch & ", " - result[^1] = ')' + let elements = node.children.map(`$`).join(", ") + result = &"(block labels: {labels} elements: {elements})" of nkBreak: result = &"(break {node.label})" of nkCall: - result = &"(call {node.function} " - for ch in node.arguments: - result &= $ch & ", " - result[^1] = ')' + let args = node.arguments.map(`$`).join(", ") + result = &"(call {node.function}({args}))" of nkColonCall: - result = &"(:call {node.function} " - for ch in node.arguments: - result &= $ch & ", " - result[^1] = ')' + let args = node.arguments.map(`$`).join(", ") + result = &"(:call {node.function} ({args}))" of nkConst: result = &"(const {node.constant})" of nkDiv: @@ -119,10 +114,8 @@ proc `$`*(node: Node): string = of nkLess: result = &"(less {node.left} {node.right})" of nkList: - result = &"(list " - for ch in node.elems: - result &= &"{ch}, " - result[^1] = ')' + let elems = node.elems.map(`$`).join(", ") + result = &"(list {elems})" of nkMinus: result = &"(- {node.left} {node.right})" of nkMult: @@ -145,11 +138,8 @@ proc `$`*(node: Node): string = of nkSetIndex: result = &"({node.sCollection}[{node.sIndex}] = {node.sValue})" of nkTable: - var keys = "" - var values = "" - for i in 0..node.keys.high: - keys &= &"{node.keys[i]}, " - values &= &"{node.values[i]}, " + let keys = node.keys.map(`$`).join(", ") + let values = node.values.map(`$`).join(", ") result = &"(table keys: {keys}, values: {values})" of nkTrue: result = "(true)" diff --git a/src/ndspkg/config.nim b/src/ndspkg/config.nim index 5eca083..662ce25 100644 --- a/src/ndspkg/config.nim +++ b/src/ndspkg/config.nim @@ -3,7 +3,8 @@ type leBasic, leRdstdin ReadlineInterruptedException* = object of CatchableError -const debugDumpChunk* = defined(debug) +const debugDumpAst* = defined(debug) # dump AST after parsing +const debugDumpChunk* = defined(debug) # dump emitted chunk # vm debug options (setting any to true will slow runtime down!) const debugVM* = defined(debug) const assertionsVM* = defined(debug) or defined(release) # sanity checks in the VM, such as the stack being empty at the end