/home/squattingmonk/Code/nim/playground/peg_t.nim(34, 19) Hint: 'val`gensym502' is declared but not used [XDeclaredButNotUsed] /home/squattingmonk/Code/nim/playground/peg_t.nim(17, 26) template/generic instantiation of `eventParser` from here /home/squattingmonk/.choosenim/toolchains/nim-2.0.0/lib/pure/pegs.nim(1062, 18) Error: 'parseIt' is not GC-safe as it accesses 'opStack' which is a global using GC'ed memory
Because this is a template invocation and not an actual proc definition, I'm not sure how to go about getting around this. The solution I'm using so far is to wrap everything into a proc:
import std/[strutils, pegs]
let
pegAst = """
Expr <- Sum
Sum <- Product (('+' / '-')Product)*
Product <- Value (('*' / '/')Value)*
Value <- [0-9]+ / '(' Expr ')'
""".peg
txt = "(5+3)/2-7*22"
proc parseArithExpr(s: string): int =
var
pStack: seq[string] = @[]
valStack: seq[float] = @[]
opStack = ""
let
p = pegAst.eventParser:
pkNonTerminal:
enter:
pStack.add p.nt.name
leave:
pStack.setLen pStack.high
if length > 0:
let matchStr = s.substr(start, start+length-1)
case p.nt.name
of "Value":
try:
valStack.add matchStr.parseFloat
echo valStack
except ValueError:
discard
of "Sum", "Product":
try:
let val = matchStr.parseFloat
except ValueError:
if valStack.len > 1 and opStack.len > 0:
valStack[^2] = case opStack[^1]
of '+': valStack[^2] + valStack[^1]
of '-': valStack[^2] - valStack[^1]
of '*': valStack[^2] * valStack[^1]
else: valStack[^2] / valStack[^1]
valStack.setLen valStack.high
echo valStack
opStack.setLen opStack.high
echo opStack
pkChar:
leave:
if length == 1 and "Value" != pStack[^1]:
let matchChar = s[start]
opStack.add matchChar
echo opStack
s.p
let pLen = parseArithExpr(txt)
Is there a better way to do this?
Error: a thread var cannot be initialized explicitly; this would only run for the main thread