Move projectPath into Preserves library

This commit is contained in:
Emery Hemingway 2023-10-28 00:25:20 +01:00
parent 00609f3b6f
commit 090b4d77ef
3 changed files with 7 additions and 13 deletions

View File

@ -364,16 +364,10 @@ func analyse*(p: Pattern): Analysis =
var path: Path
walk(result, path, p)
func projectPath*(v: Value; path: Path): Option[Value] =
result = some(v)
for index in path:
result = preserves.step(result.get, index)
if result.isNone: break
func projectPaths*(v: Value; paths: Paths): Option[Captures] =
var res = newSeq[Value](paths.len)
for i, path in paths:
var vv = projectPath(v, path)
var vv = step(v, path)
if vv.isSome: res[i] = get(vv)
else: return
some res
@ -382,22 +376,22 @@ func matches*(pat: Pattern; pr: Value): bool =
let analysis = analyse(pat)
assert analysis.constPaths.len == analysis.constValues.len
for i, path in analysis.constPaths:
let v = projectPath(pr, path)
let v = step(pr, path)
if v.isNone : return false
if analysis.constValues[i] != v.get: return false
for path in analysis.capturePaths:
if isNone projectPath(pr, path): return false
if isNone step(pr, path): return false
true
func capture*(pat: Pattern; pr: Value): seq[Value] =
let analysis = analyse(pat)
assert analysis.constPaths.len == analysis.constValues.len
for i, path in analysis.constPaths:
let v = projectPath(pr, path)
let v = step(pr, path)
if v.isNone : return @[]
if analysis.constValues[i] != v.get: return @[]
for path in analysis.capturePaths:
let v = projectPath(pr, path)
let v = step(pr, path)
if v.isNone: return @[]
result.add(get v)

View File

@ -215,7 +215,7 @@ proc extendWalk(node: Node; popCount: Natural; stepIndex: Value; pat: Pattern; p
table[class] = result.nextNode
new result.nextNode.continuation
for a in node.continuation.cache:
var v = projectPath(a, path)
var v = step(a, path)
if v.isSome and class == classOf(get v):
result.nextNode.continuation.cache.incl a
result.popCount = 0

View File

@ -9,4 +9,4 @@ srcDir = "src"
# Dependencies
requires "https://github.com/khchen/hashlib.git#84e0247555e4488594975900401baaf5bbbfb531", "nim >= 2.0.0", "https://git.syndicate-lang.org/ehmry/preserves-nim.git >= 20231019"
requires "https://github.com/khchen/hashlib.git#84e0247555e4488594975900401baaf5bbbfb531", "nim >= 2.0.0", "https://git.syndicate-lang.org/ehmry/preserves-nim.git >= 20231028"