Make `projectPaths` fail without side-effects

Fix ehmry/xdg_open_ng#2
This commit is contained in:
Emery Hemingway 2022-03-18 10:14:20 -05:00
parent fac3002fc8
commit 76c9a6377d
2 changed files with 12 additions and 8 deletions

View File

@ -9,14 +9,17 @@ type
Value = Preserve[Ref]
Path = seq[Value]
proc projectPath(v: Value; path: Path): Value =
result = v
func projectPath(v: Value; path: Path): Option[Value] =
result = some(v)
for index in path:
result = result[index]
result = preserves.step(result.get, index)
if result.isNone: break
proc projectPaths(v: Value; paths: seq[Path]): seq[Value] =
result.setLen(paths.len)
for i, path in paths: result[i] = projectPath(v, path)
func projectPaths(v: Value; paths: seq[Path]): seq[Value] =
result = newSeq[Value](paths.len)
for i, path in paths:
var vv = projectPath(v, path)
if vv.isSome: result[i] = get(vv)
type Class = distinct string
@ -148,7 +151,8 @@ proc extend(node: Node; popCount: Natural; stepIndex: Value; pat: Pattern; path:
new result.nextNode.continuation
table[class] = result.nextNode
for a in node.continuation.cachedAssertions:
if class == classOf projectPath(a, path):
var v = projectPath(a, path)
if v.isSome and class == classOf(get(v)):
result.nextNode.continuation.cachedAssertions.incl a
result.popCount = 0
template walkKey(pat: Pattern; stepIndex: Value) =

View File

@ -9,4 +9,4 @@ srcDir = "src"
# Dependencies
requires "nim >= 1.4.8", "nimSHA2 >= 0.1.1", "preserves"
requires "nim >= 1.4.8", "nimSHA2 >= 0.1.1", "preserves > 3.2.0"