add dump AST, make AST printing cleaner
This commit is contained in:
parent
04dd2bf73f
commit
ef794ea881
|
@ -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:
|
||||
|
|
|
@ -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)"
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue