Added support for parsing and interpreting symbols with spaces and custom sigils.
h3rald h3rald@h3rald.com
Sat, 12 Dec 2020 21:54:23 +0100
2 files changed,
28 insertions(+),
9 deletions(-)
M
core/interpreter.nim
→
core/interpreter.nim
@@ -228,13 +228,21 @@ i.currSym = val
let symbol = val.symVal if i.scope.hasSymbol(symbol): i.apply i.scope.getSymbol(symbol) - else: - let sigil = "" & symbol[0] - if symbol.len > 1 and i.scope.hasSigil(sigil): - i.stack.add(MinValue(kind: minString, strVal: symbol[1..symbol.len-1])) + else: + var qIndex = symbol.find('"') + if qIndex > 0: + let sigil = symbol[0..qIndex-1] + if not i.scope.hasSigil(sigil): + raiseUndefined("Undefined sigil '$1'"%sigil) + i.stack.add(MinValue(kind: minString, strVal: symbol[qIndex+1..symbol.len-2])) i.apply(i.scope.getSigil(sigil)) else: - raiseUndefined("Undefined symbol '$1'" % [val.symVal]) + let sigil = "" & symbol[0] + if symbol.len > 1 and i.scope.hasSigil(sigil): + i.stack.add(MinValue(kind: minString, strVal: symbol[1..symbol.len-1])) + i.apply(i.scope.getSigil(sigil)) + else: + raiseUndefined("Undefined symbol '$1'" % [val.symVal]) discard i.trace.pop elif val.kind == minDictionary and val.objType != "module": # Dictionary must be copied every time they are interpreted, otherwise when they are used in cycles they reference each other.
M
core/parser.nim
→
core/parser.nim
@@ -148,7 +148,7 @@ "invalid token",
"string expected", "')' expected", "'}' expected", - "'\"' or \"'\" expected", + "'\"' expected", "'*/' expected", "EOF expected", "expression expected"@@ -326,8 +326,18 @@ var pos = my.bufpos
var buf = my.buf if not(buf[pos] in Whitespace): while not(buf[pos] in WhiteSpace) and not(buf[pos] in ['\0', ')', '(', '}', '{']): - add(my.a, buf[pos]) - inc(pos) + if buf[pos] == '"': + add(my.a, buf[pos]) + my.bufpos = pos + let r = parseString(my) + if r == tkError: + result = tkError + return + add(my.a, buf[pos]) + return + else: + add(my.a, buf[pos]) + inc(pos) my.bufpos = pos proc skip(my: var MinParser) =@@ -649,7 +659,8 @@ result = MinValue(kind: minSymbol, symVal: p.a, column: p.getColumn, line: p.lineNumber, filename: p.filename)
p.a = "" discard getToken(p) else: - raiseUndefined(p, "Undefined value: '"&p.a&"'") + let err = "Undefined or invalid value: "&p.a + raiseUndefined(p, err) result.filename = p.filename proc compileMinValue*(p: var MinParser, i: In, push = true, indent = ""): seq[string] {.extern:"min_exported_symbol_$1".}=