1
1
mirror of https://github.com/github/semantic.git synced 2024-12-03 22:54:16 +03:00
semantic/prototype/Doubt/Diff.swift

53 lines
1.3 KiB
Swift
Raw Normal View History

2015-09-11 18:34:15 +03:00
public enum Diff: CustomDocConvertible, Equatable {
2015-09-11 18:38:42 +03:00
case Patch(Fix, Fix)
2015-09-11 18:40:01 +03:00
indirect case Copy(Syntax<Diff>)
2015-07-18 22:43:49 +03:00
2015-09-11 18:34:15 +03:00
public var doc: Doc<Pretty> {
2015-07-18 22:43:49 +03:00
switch self {
case let .Patch(a, b):
return .Horizontal([
.Wrap(Pretty.Text("{-"), Pretty(a), Pretty.Text("-}")),
.Wrap(Pretty.Text("{+"), Pretty(b), Pretty.Text("+}"))
2015-07-18 22:43:49 +03:00
])
case let .Copy(a):
2015-09-11 18:40:01 +03:00
return a.doc
2015-07-18 22:43:49 +03:00
}
}
2015-09-11 18:34:15 +03:00
public var description: String {
2015-07-18 22:43:49 +03:00
switch self {
case let .Patch(a, b):
return ".Patch(\(a), \(b))"
case let .Copy(a):
2015-09-11 18:40:01 +03:00
return ".Copy(\(a))"
2015-07-18 22:43:49 +03:00
}
}
2015-09-11 18:38:42 +03:00
public init(_ a: Fix, _ b: Fix) {
switch (a.out, b.out) {
case let (.Apply(a, aa), .Apply(b, bb)):
2015-09-11 23:35:02 +03:00
// fixme: SES
2015-09-11 18:40:01 +03:00
self = .Copy(.Apply(Diff(a, b), Array(zip(aa, bb).lazy.map(Diff.init))))
2015-07-18 22:43:49 +03:00
case let (.Abstract(p1, b1), .Abstract(p2, b2)):
2015-09-11 18:40:01 +03:00
self = .Copy(.Abstract(Array(zip(p1, p2).lazy.map(Diff.init)), Diff(b1, b2)))
2015-07-18 22:43:49 +03:00
case let (.Assign(n1, v1), .Assign(n2, v2)) where n1 == n2:
2015-09-11 18:40:01 +03:00
self = .Copy(.Assign(n2, Diff(v1, v2)))
2015-07-18 22:43:49 +03:00
case let (.Variable(n1), .Variable(n2)) where n1 == n2:
2015-09-11 18:40:01 +03:00
self = .Copy(.Variable(n2))
2015-07-18 22:43:49 +03:00
case let (.Literal(v1), .Literal(v2)) where v1 == v2:
2015-09-11 18:40:01 +03:00
self = .Copy(.Literal(v2))
2015-07-18 22:43:49 +03:00
case let (.Group(n1, v1), .Group(n2, v2)):
2015-09-11 18:40:01 +03:00
self = .Copy(.Group(Diff(n1, n2), Array(zip(v1, v2).lazy.map(Diff.init))))
2015-07-18 22:43:49 +03:00
default:
self = .Patch(a, b)
}
2015-07-18 22:43:49 +03:00
}
}