1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 18:23:44 +03:00

Remove Term.Empty.

This commit is contained in:
Rob Rix 2015-09-29 12:48:53 -04:00
parent 17271ff2ce
commit c3e8d9a801
3 changed files with 24 additions and 41 deletions

View File

@ -4,17 +4,15 @@ public enum Diff: Comparable, CustomDebugStringConvertible, CustomDocConvertible
indirect case Copy(Syntax<Diff>) indirect case Copy(Syntax<Diff>)
public static func Insert(term: Term) -> Diff { public static func Insert(term: Term) -> Diff {
return .Patch(.Empty, term) return .Patch(Term(.Empty), term)
} }
public static func Delete(term: Term) -> Diff { public static func Delete(term: Term) -> Diff {
return .Patch(term, .Empty) return .Patch(term, Term(.Empty))
} }
public init(_ term: Term) { public init(_ term: Term) {
switch term { switch term {
case .Empty:
self = .Empty
case let .Roll(s): case let .Roll(s):
self = .Copy(s.map(Diff.init)) self = .Copy(s.map(Diff.init))
} }
@ -55,36 +53,27 @@ public enum Diff: Comparable, CustomDebugStringConvertible, CustomDocConvertible
} }
public init(_ a: Term, _ b: Term) { public init(_ a: Term, _ b: Term) {
switch (a, b) { switch (a.syntax, b.syntax) {
case (.Empty, .Empty): case let (.Apply(a, aa), .Apply(b, bb)):
self = .Empty self = .Copy(.Apply(Diff(a, b), Diff.diff(aa, bb)))
case let (.Roll(a), .Roll(b)): case let (.Abstract(p1, b1), .Abstract(p2, b2)):
switch (a, b) { self = .Copy(.Abstract(Diff.diff(p1, p2), Diff.diff(b1, b2)))
case let (.Apply(a, aa), .Apply(b, bb)):
self = .Copy(.Apply(Diff(a, b), Diff.diff(aa, bb)))
case let (.Abstract(p1, b1), .Abstract(p2, b2)): case let (.Assign(n1, v1), .Assign(n2, v2)) where n1 == n2:
self = .Copy(.Abstract(Diff.diff(p1, p2), Diff.diff(b1, b2))) self = .Copy(.Assign(n2, Diff(v1, v2)))
case let (.Assign(n1, v1), .Assign(n2, v2)) where n1 == n2: case let (.Variable(n1), .Variable(n2)) where n1 == n2:
self = .Copy(.Assign(n2, Diff(v1, v2))) self = .Copy(.Variable(n2))
case let (.Variable(n1), .Variable(n2)) where n1 == n2: case let (.Literal(v1), .Literal(v2)) where v1 == v2:
self = .Copy(.Variable(n2)) self = .Copy(.Literal(v2))
case let (.Literal(v1), .Literal(v2)) where v1 == v2: case let (.Group(n1, v1), .Group(n2, v2)):
self = .Copy(.Literal(v2)) self = .Copy(.Group(Diff(n1, n2), Diff.diff(v1, v2)))
case let (.Group(n1, v1), .Group(n2, v2)):
self = .Copy(.Group(Diff(n1, n2), Diff.diff(v1, v2)))
default:
self = .Patch(Term(a), Term(b))
}
default: default:
self = Patch(a, b) self = .Patch(a, b)
} }
} }

View File

@ -1,13 +1,5 @@
public func == (left: Term, right: Term) -> Bool { public func == (left: Term, right: Term) -> Bool {
switch (left, right) { return left.syntax == right.syntax
case (.Empty, .Empty):
return true
case let (.Roll(s), .Roll(t)):
return s == t
default:
return false
}
} }
public func == <F: Equatable> (left: Syntax<F>, right: Syntax<F>) -> Bool { public func == <F: Equatable> (left: Syntax<F>, right: Syntax<F>) -> Bool {

View File

@ -3,13 +3,17 @@ public enum Term: CustomDebugStringConvertible, CustomDocConvertible, CustomStri
self = .Roll(out) self = .Roll(out)
} }
case Empty
indirect case Roll(Syntax<Term>) indirect case Roll(Syntax<Term>)
public var syntax: Syntax<Term> {
switch self {
case let .Roll(syntax):
return syntax
}
}
public var debugDescription: String { public var debugDescription: String {
switch self { switch self {
case .Empty:
return ".Empty"
case let .Roll(s): case let .Roll(s):
return s.debugDescription return s.debugDescription
} }
@ -17,8 +21,6 @@ public enum Term: CustomDebugStringConvertible, CustomDocConvertible, CustomStri
public var doc: Doc { public var doc: Doc {
switch self { switch self {
case .Empty:
return .Empty
case let .Roll(s): case let .Roll(s):
return s.doc return s.doc
} }