1
1
mirror of https://github.com/github/semantic.git synced 2024-12-25 07:55:12 +03:00

Fix can be empty.

This commit is contained in:
Rob Rix 2015-09-15 14:07:35 -04:00
parent 64c3a6074b
commit cae54e2c95
3 changed files with 53 additions and 32 deletions

View File

@ -1,9 +1,12 @@
public enum Diff: CustomDocConvertible, Equatable {
case Empty
case Patch(Fix, Fix)
indirect case Copy(Syntax<Diff>)
public var doc: Doc {
switch self {
case .Empty:
return .Empty
case let .Patch(a, b):
return .Horizontal([
.Wrap(Doc.Text("{-"), Doc(a), Doc.Text("-}")),
@ -16,6 +19,8 @@ public enum Diff: CustomDocConvertible, Equatable {
public var description: String {
switch self {
case .Empty:
return ".Empty"
case let .Patch(a, b):
return ".Patch(\(a), \(b))"
case let .Copy(a):
@ -24,28 +29,37 @@ public enum Diff: CustomDocConvertible, Equatable {
}
public init(_ a: Fix, _ b: Fix) {
switch (a.out, b.out) {
case let (.Apply(a, aa), .Apply(b, bb)):
// fixme: SES
self = .Copy(.Apply(Diff(a, b), Array(zip(aa, bb).lazy.map(Diff.init))))
switch (a, b) {
case (.Empty, .Empty):
self = .Empty
case let (.Abstract(p1, b1), .Abstract(p2, b2)):
self = .Copy(.Abstract(Array(zip(p1, p2).lazy.map(Diff.init)), Diff(b1, b2)))
case let (.Roll(a), .Roll(b)):
switch (a, b) {
case let (.Apply(a, aa), .Apply(b, bb)):
// fixme: SES
self = .Copy(.Apply(Diff(a, b), Array(zip(aa, bb).lazy.map(Diff.init))))
case let (.Assign(n1, v1), .Assign(n2, v2)) where n1 == n2:
self = .Copy(.Assign(n2, Diff(v1, v2)))
case let (.Abstract(p1, b1), .Abstract(p2, b2)):
self = .Copy(.Abstract(Array(zip(p1, p2).lazy.map(Diff.init)), Diff(b1, b2)))
case let (.Variable(n1), .Variable(n2)) where n1 == n2:
self = .Copy(.Variable(n2))
case let (.Assign(n1, v1), .Assign(n2, v2)) where n1 == n2:
self = .Copy(.Assign(n2, Diff(v1, v2)))
case let (.Literal(v1), .Literal(v2)) where v1 == v2:
self = .Copy(.Literal(v2))
case let (.Variable(n1), .Variable(n2)) where n1 == n2:
self = .Copy(.Variable(n2))
case let (.Group(n1, v1), .Group(n2, v2)):
self = .Copy(.Group(Diff(n1, n2), Array(zip(v1, v2).lazy.map(Diff.init))))
case let (.Literal(v1), .Literal(v2)) where v1 == v2:
self = .Copy(.Literal(v2))
case let (.Group(n1, v1), .Group(n2, v2)):
self = .Copy(.Group(Diff(n1, n2), Array(zip(v1, v2).lazy.map(Diff.init))))
default:
self = .Patch(Fix(a), Fix(b))
}
default:
self = .Patch(a, b)
self = Patch(a, b)
}
}
}

View File

@ -1,5 +1,13 @@
public func == (left: Fix, right: Fix) -> Bool {
return left.out == right.out
switch (left, right) {
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 {

View File

@ -3,21 +3,25 @@ public enum Fix: CustomDebugStringConvertible, CustomDocConvertible, CustomStrin
self = .Roll(out)
}
case Empty
indirect case Roll(Syntax<Fix>)
public var out: Syntax<Fix> {
public var debugDescription: String {
switch self {
case .Empty:
return ".Empty"
case let .Roll(s):
return s
return s.debugDescription
}
}
public var debugDescription: String {
return cata { String(reflecting: $0) } (self)
}
public var doc: Doc {
return cata { (syntax: Syntax<Doc>) in syntax.doc } (self)
switch self {
case .Empty:
return .Empty
case let .Roll(s):
return s.doc
}
}
}
@ -51,10 +55,10 @@ public enum Syntax<Payload>: CustomDebugStringConvertible, CustomDocConvertible
public var debugDescription: String {
switch self {
case let .Apply(f, vs):
let s = vs.map { String($0) }.joinWithSeparator(", ")
let s = vs.map { String(reflecting: $0) }.joinWithSeparator(", ")
return ".Apply(\(f), [ \(s) ])"
case let .Abstract(parameters, body):
let s = parameters.map { String($0) }.joinWithSeparator(", ")
let s = parameters.map { String(reflecting: $0) }.joinWithSeparator(", ")
return ".Abstract([ \(s) ], \(body))"
case let .Assign(n, v):
return ".Assign(\(n), \(v))"
@ -63,8 +67,8 @@ public enum Syntax<Payload>: CustomDebugStringConvertible, CustomDocConvertible
case let .Literal(s):
return ".Literal(\(s))"
case let .Group(n, vs):
let s = vs.map { String($0) }.joinWithSeparator(", ")
return ".Group(\(n), [ \(s) ])"
let s = vs.map { String(reflecting: $0) }.joinWithSeparator(", ")
return ".Group(\(String(reflecting: n)), [ \(s) ])"
}
}
@ -96,8 +100,3 @@ public enum Syntax<Payload>: CustomDebugStringConvertible, CustomDocConvertible
}
}
}
func cata<T>(f: Syntax<T> -> T)(_ term: Fix) -> T {
return ({ $0.out } >>> { $0.map(cata(f)) } >>> f)(term)
}