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()
|
let node = parser.parse()
|
||||||
if parser.hadError:
|
if parser.hadError:
|
||||||
return rsCompileError
|
return rsCompileError
|
||||||
|
when debugDumpAst:
|
||||||
|
echo $node
|
||||||
let emitter = newEmitter(name, node)
|
let emitter = newEmitter(name, node)
|
||||||
emitter.emit()
|
emitter.emit()
|
||||||
if emitter.hadError:
|
if emitter.hadError:
|
||||||
|
|
|
@ -3,6 +3,7 @@ import ../types/value
|
||||||
|
|
||||||
import strformat
|
import strformat
|
||||||
import strutils
|
import strutils
|
||||||
|
import sequtils
|
||||||
|
|
||||||
type
|
type
|
||||||
NodeKind* = enum
|
NodeKind* = enum
|
||||||
|
@ -76,22 +77,16 @@ proc `$`*(node: Node): string =
|
||||||
result = &"(and {node.left} {node.right})"
|
result = &"(and {node.left} {node.right})"
|
||||||
of nkBlockExpr:
|
of nkBlockExpr:
|
||||||
let labels = node.labels.join(", ")
|
let labels = node.labels.join(", ")
|
||||||
result = &"(block labels: {labels} elements: "
|
let elements = node.children.map(`$`).join(", ")
|
||||||
for ch in node.children:
|
result = &"(block labels: {labels} elements: {elements})"
|
||||||
result &= $ch & ", "
|
|
||||||
result[^1] = ')'
|
|
||||||
of nkBreak:
|
of nkBreak:
|
||||||
result = &"(break {node.label})"
|
result = &"(break {node.label})"
|
||||||
of nkCall:
|
of nkCall:
|
||||||
result = &"(call {node.function} "
|
let args = node.arguments.map(`$`).join(", ")
|
||||||
for ch in node.arguments:
|
result = &"(call {node.function}({args}))"
|
||||||
result &= $ch & ", "
|
|
||||||
result[^1] = ')'
|
|
||||||
of nkColonCall:
|
of nkColonCall:
|
||||||
result = &"(:call {node.function} "
|
let args = node.arguments.map(`$`).join(", ")
|
||||||
for ch in node.arguments:
|
result = &"(:call {node.function} ({args}))"
|
||||||
result &= $ch & ", "
|
|
||||||
result[^1] = ')'
|
|
||||||
of nkConst:
|
of nkConst:
|
||||||
result = &"(const {node.constant})"
|
result = &"(const {node.constant})"
|
||||||
of nkDiv:
|
of nkDiv:
|
||||||
|
@ -119,10 +114,8 @@ proc `$`*(node: Node): string =
|
||||||
of nkLess:
|
of nkLess:
|
||||||
result = &"(less {node.left} {node.right})"
|
result = &"(less {node.left} {node.right})"
|
||||||
of nkList:
|
of nkList:
|
||||||
result = &"(list "
|
let elems = node.elems.map(`$`).join(", ")
|
||||||
for ch in node.elems:
|
result = &"(list {elems})"
|
||||||
result &= &"{ch}, "
|
|
||||||
result[^1] = ')'
|
|
||||||
of nkMinus:
|
of nkMinus:
|
||||||
result = &"(- {node.left} {node.right})"
|
result = &"(- {node.left} {node.right})"
|
||||||
of nkMult:
|
of nkMult:
|
||||||
|
@ -145,11 +138,8 @@ proc `$`*(node: Node): string =
|
||||||
of nkSetIndex:
|
of nkSetIndex:
|
||||||
result = &"({node.sCollection}[{node.sIndex}] = {node.sValue})"
|
result = &"({node.sCollection}[{node.sIndex}] = {node.sValue})"
|
||||||
of nkTable:
|
of nkTable:
|
||||||
var keys = ""
|
let keys = node.keys.map(`$`).join(", ")
|
||||||
var values = ""
|
let values = node.values.map(`$`).join(", ")
|
||||||
for i in 0..node.keys.high:
|
|
||||||
keys &= &"{node.keys[i]}, "
|
|
||||||
values &= &"{node.values[i]}, "
|
|
||||||
result = &"(table keys: {keys}, values: {values})"
|
result = &"(table keys: {keys}, values: {values})"
|
||||||
of nkTrue:
|
of nkTrue:
|
||||||
result = "(true)"
|
result = "(true)"
|
||||||
|
|
|
@ -3,7 +3,8 @@ type
|
||||||
leBasic, leRdstdin
|
leBasic, leRdstdin
|
||||||
ReadlineInterruptedException* = object of CatchableError
|
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!)
|
# vm debug options (setting any to true will slow runtime down!)
|
||||||
const debugVM* = defined(debug)
|
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
|
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