diff --git a/prototype/Doubt/Diff.swift b/prototype/Doubt/Diff.swift index 9d4ac3fee..cde9e5d08 100644 --- a/prototype/Doubt/Diff.swift +++ b/prototype/Doubt/Diff.swift @@ -20,13 +20,13 @@ public enum Diff: Comparable, CustomDebugStringConvertible, CustomDocConvertible } } - public var doc: DOC { + public var doc: Doc { switch self { case .Empty: return .Empty case let .Patch(a, b): - return .bracket("{-", DOC(a), "-}") - <> .bracket("{+", DOC(b), "+}") + return .bracket("{-", Doc(a), "-}") + <> .bracket("{+", Doc(b), "+}") case let .Copy(a): return a.doc } diff --git a/prototype/Doubt/Doc.swift b/prototype/Doubt/Doc.swift index 856ec01df..f6b4f5a54 100644 --- a/prototype/Doubt/Doc.swift +++ b/prototype/Doubt/Doc.swift @@ -3,7 +3,7 @@ public enum Layout: CustomStringConvertible, Equatable { indirect case Text(String, Layout) indirect case Line(Int, Layout) - public init(width: Int, placed: Int, alternatives: Stream<(Int, DOC)>) { + public init(width: Int, placed: Int, alternatives: Stream<(Int, Doc)>) { switch alternatives { case .Nil: self = .Empty @@ -48,29 +48,29 @@ public enum Layout: CustomStringConvertible, Equatable { } } -public enum DOC: CustomDocConvertible, Equatable { +public enum Doc: CustomDocConvertible, Equatable { case Empty - indirect case Concat(DOC, DOC) - indirect case Union(DOC, DOC) + indirect case Concat(Doc, Doc) + indirect case Union(Doc, Doc) case Text(String) - indirect case Nest(Int, DOC) + indirect case Nest(Int, Doc) case Line public init(_ value: T) { self = (value as? CustomDocConvertible)?.doc ?? .Text(String(value)) } - public static func group(doc: DOC) -> DOC { + public static func group(doc: Doc) -> Doc { return Union(doc.flattened, doc) } - public static func bracket(l: String, _ x: DOC, _ r: String) -> DOC { + public static func bracket(l: String, _ x: Doc, _ r: String) -> Doc { return group(Concat(Text(l), Concat(Nest(2, Concat(Line, x)), Concat(Line, Text(r))))) } - public static func folddoc(docs: C, combine: (DOC, DOC) -> DOC) -> DOC { - func fold(docs: Stream) -> DOC { - switch docs { + public static func foldDoc(Docs: C, combine: (Doc, Doc) -> Doc) -> Doc { + func fold(Docs: Stream) -> Doc { + switch Docs { case .Nil: return .Empty case let .Cons(x, rest) where rest.value.isEmpty: @@ -79,24 +79,24 @@ public enum DOC: CustomDocConvertible, Equatable { return combine(x, fold(rest.value)) } } - return fold(Stream(sequence: docs)) + return fold(Stream(sequence: Docs)) } - public static func spread(docs: C) -> DOC { - return folddoc(docs, combine: <+>) + public static func spread(Docs: C) -> Doc { + return foldDoc(Docs, combine: <+>) } - public static func stack(docs: C) -> DOC { - return folddoc(docs, combine: ) + public static func stack(Docs: C) -> Doc { + return foldDoc(Docs, combine: ) } - public static func join(separator: String, _ docs: C) -> DOC { - return folddoc(docs) { + public static func join(separator: String, _ Docs: C) -> Doc { + return foldDoc(Docs) { $0 <> Text(separator) <+> $1 } } - public var flattened: DOC { + public var flattened: Doc { switch self { case .Empty, .Text: return self @@ -104,8 +104,8 @@ public enum DOC: CustomDocConvertible, Equatable { return .Concat(a.flattened, b.flattened) case let .Union(a, _): return a.flattened - case let .Nest(_, doc): - return doc.flattened + case let .Nest(_, Doc): + return Doc.flattened case .Line: return .Text(" ") } @@ -119,13 +119,13 @@ public enum DOC: CustomDocConvertible, Equatable { return Layout(width: width, placed: placed, alternatives: .pure((0, self))) } - public var doc: DOC { + public var doc: Doc { return self } } public protocol CustomDocConvertible: CustomStringConvertible { - var doc: DOC { get } + var doc: Doc { get } } extension CustomDocConvertible { @@ -135,15 +135,15 @@ extension CustomDocConvertible { } -public func <> (left: DOC, right: DOC) -> DOC { +public func <> (left: Doc, right: Doc) -> Doc { return .Concat(left, right) } -public func <+> (left: DOC, right: DOC) -> DOC { +public func <+> (left: Doc, right: Doc) -> Doc { return left <> .Text(" ") <> right } -public func (left: DOC, right: DOC) -> DOC { +public func (left: Doc, right: Doc) -> Doc { return left <> .Line <> right } diff --git a/prototype/Doubt/Equatable.swift b/prototype/Doubt/Equatable.swift index 5baf3fa5e..50287b281 100644 --- a/prototype/Doubt/Equatable.swift +++ b/prototype/Doubt/Equatable.swift @@ -55,7 +55,7 @@ public func == (left: Layout, right: Layout) -> Bool { } } -public func == (left: DOC, right: DOC) -> Bool { +public func == (left: Doc, right: Doc) -> Bool { switch (left, right) { case (.Empty, .Empty), (.Line, .Line): return true diff --git a/prototype/Doubt/Syntax.swift b/prototype/Doubt/Syntax.swift index b46a0f39a..6ccf59d2b 100644 --- a/prototype/Doubt/Syntax.swift +++ b/prototype/Doubt/Syntax.swift @@ -15,7 +15,7 @@ public enum Term: CustomDebugStringConvertible, CustomDocConvertible, CustomStri } } - public var doc: DOC { + public var doc: Doc { switch self { case .Empty: return .Empty @@ -182,25 +182,25 @@ public enum Syntax: CustomDebugStringConvertible, CustomDocConvertible } } - public var doc: DOC { + public var doc: Doc { switch self { case let .Apply(f, vs): - return DOC(f) <> .bracket("(", .join(", ", vs.map(DOC.init)), ")") + return Doc(f) <> .bracket("(", .join(", ", vs.map(Doc.init)), ")") case let .Abstract(parameters, body): - return .folddoc([ + return .foldDoc([ .Text("λ"), - .join(", ", parameters.map(DOC.init)), + .join(", ", parameters.map(Doc.init)), .Text("."), - .stack(body.map(DOC.init)) + .stack(body.map(Doc.init)) ], combine: <>) case let .Assign(n, v): - return .spread([ .Text(n), .Text("="), DOC(v) ]) + return .spread([ .Text(n), .Text("="), Doc(v) ]) case let .Variable(n): return .Text(n) case let .Literal(s): return .Text(s) case let .Group(n, vs): - return DOC(n) <> .bracket("{", .stack(vs.map(DOC.init)), "}") + return Doc(n) <> .bracket("{", .stack(vs.map(Doc.init)), "}") } } }