Do not edit AST nodes, make a copy first

Fixes error "typechecked nodes may not be modified"
This commit is contained in:
Mark Spanbroek 2023-06-07 16:23:55 +02:00 committed by markspanbroek
parent b18444a6d0
commit 8daae27089
2 changed files with 5 additions and 0 deletions

View File

@ -22,23 +22,27 @@ template chain(option: typed, identifier: untyped{nkIdent}): untyped =
macro chain(option: typed, infix: untyped{nkInfix}): untyped =
# chain is of shape: option.?left `operator` right
let infix = infix.copyNimTree()
let left = infix[1]
infix[1] = quote do: `option`.?`left`
infix
macro chain(option: typed, bracket: untyped{nkBracketExpr}): untyped =
# chain is of shape: option.?left[right]
let bracket = bracket.copyNimTree()
let left = bracket[0]
bracket[0] = quote do: `option`.?`left`
bracket
macro chain(option: typed, dot: untyped{nkDotExpr}): untyped =
# chain is of shape: option.?left.right
let dot = dot.copyNimTree()
let left = dot[0]
dot[0] = quote do: `option`.?`left`
dot
macro chain(option: typed, call: untyped{nkCall}): untyped =
let call = call.copyNimTree()
let procedure = call[0]
if call.len == 1:
# chain is of shape: option.?procedure()

View File

@ -12,6 +12,7 @@ proc undoSymbolResolution(expression, ident: NimNode): NimNode =
if expression.kind in symbolKinds and eqIdent($expression, $ident):
return ident
let expression = expression.copyNimTree()
for i in 0..<expression.len:
expression[i] = undoSymbolResolution(expression[i], ident)