1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 17:59:10 +03:00

Branch has only one child.

This commit is contained in:
Rob Rix 2015-10-01 14:08:13 -04:00
parent 0e7bc6b2b3
commit 8961ee9168
3 changed files with 15 additions and 16 deletions

View File

@ -100,7 +100,7 @@ public enum Diff: Comparable, CustomDebugStringConvertible, CustomDocConvertible
self = .Copy(.Leaf(v2)) self = .Copy(.Leaf(v2))
case let (.Branch(v1), .Branch(v2)): case let (.Branch(v1), .Branch(v2)):
self = .Copy(.Branch(Diff.diff(v1, v2))) self = .Copy(.Branch(Diff(v1, v2)))
default: default:
self = .Patch(a, b) self = .Patch(a, b)

View File

@ -9,7 +9,7 @@ private func equals<F, A: Equatable>(left: Syntax<F, A>, _ right: Syntax<F, A>,
case let (.Leaf(l1), .Leaf(l2)): case let (.Leaf(l1), .Leaf(l2)):
return l1 == l2 return l1 == l2
case let (.Branch(v1), .Branch(v2)): case let (.Branch(v1), .Branch(v2)):
return v1.count == v2.count && zip(v1, v2).reduce(true) { $0 && recur($1.0, $1.1) } return recur(v1, v2)
default: default:
return false return false
} }

View File

@ -29,15 +29,15 @@ public enum Term<A: Equatable>: CustomDebugStringConvertible, CustomDocConvertib
return Term(.Leaf(a)) return Term(.Leaf(a))
} }
public static func Branch(terms: [Term]) -> Term { public static func Branch(term: Term) -> Term {
return Term(.Branch(terms)) return Term(.Branch(term))
} }
} }
public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible { public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible {
case Empty case Empty
case Leaf(A) case Leaf(A)
case Branch([Recur]) case Branch(Recur)
public func map<T>(@noescape transform: Recur -> T) -> Syntax<T, A> { public func map<T>(@noescape transform: Recur -> T) -> Syntax<T, A> {
switch self { switch self {
@ -45,15 +45,15 @@ public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible
return .Empty return .Empty
case let .Leaf(n): case let .Leaf(n):
return .Leaf(n) return .Leaf(n)
case let .Branch(vs): case let .Branch(x):
return .Branch(vs.map(transform)) return .Branch(transform(x))
} }
} }
public func reduce<T>(initial: T, @noescape combine: (T, Recur) throws -> T) rethrows -> T { public func reduce<T>(initial: T, @noescape combine: (T, Recur) throws -> T) rethrows -> T {
switch self { switch self {
case let .Branch(xs): case let .Branch(x):
return try xs.reduce(initial, combine: combine) return try combine(initial, x)
default: default:
return initial return initial
@ -66,9 +66,8 @@ public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible
return ".Empty" return ".Empty"
case let .Leaf(n): case let .Leaf(n):
return ".Leaf(\(n))" return ".Leaf(\(n))"
case let .Branch(vs): case let .Branch(x):
let s = vs.map { String(reflecting: $0) }.joinWithSeparator(", ") return ".Branch(\(String(reflecting: x)))"
return ".Branch([ \(s) ])"
} }
} }
@ -78,8 +77,8 @@ public enum Syntax<Recur, A>: CustomDebugStringConvertible, CustomDocConvertible
return .Empty return .Empty
case let .Leaf(n): case let .Leaf(n):
return Doc(n) return Doc(n)
case let .Branch(vs): case let .Branch(x):
return vs.map(Doc.init).joinWithSeparator(",").bracket("{", "}") return Doc(x)
} }
} }
} }
@ -97,8 +96,8 @@ extension Syntax where A: Hashable {
return Hash("Empty") return Hash("Empty")
case let .Leaf(n): case let .Leaf(n):
return Hash("Leaf", Hash(n)) return Hash("Leaf", Hash(n))
case let .Branch(vs): case let .Branch(x):
return Hash("Branch", .Ordered(vs.map(recur))) return Hash("Branch", recur(x))
} }
} }
} }