1
1
mirror of https://github.com/github/semantic.git synced 2024-12-02 02:08:48 +03:00
semantic/prototype/Doubt/Interpreter.swift
2015-10-15 11:29:56 -04:00

38 lines
950 B
Swift

/// An interpreter of `Algorithm`s.
public struct Interpreter<Term: TermType> {
public typealias Diff = Free<Term.LeafType, Patch<Term>>
private let equals: (Term, Term) -> Bool
private func recur(a: Term, _ b: Term) -> Diff? {
if equals(a, b) { return Diff(b) }
let algorithm: Algorithm<Term, Diff>
switch (a.unwrap, b.unwrap) {
case let (.Keyed(a), .Keyed(b)):
algorithm = .Roll(.ByKey(a, b, Syntax.Keyed >>> Diff.Roll >>> Algorithm.Pure))
case let (.Indexed(a), .Indexed(b)):
algorithm = .Roll(.ByIndex(a, b, Syntax.Indexed >>> Diff.Roll >>> Algorithm.Pure))
default:
algorithm = .Roll(.Recursive(a, b, Algorithm.Pure))
}
return recur(algorithm)
}
private func recur(algorithm: Algorithm<Term, Diff>) -> Diff? {
switch algorithm {
case let .Pure(diff):
return diff
default:
return nil
}
}
public func run(a: Term, _ b: Term) -> Diff {
return recur(a, b) ?? .Replace(a, b)
}
}
import Prelude