Skip to content

Commit 4f22641

Browse files
authored
Merge pull request kodecocodes#365 from JaapWijnen/master
Migrate Shunting Yard to Swift 3
2 parents 0cccc4a + 2accb99 commit 4f22641

File tree

4 files changed

+218
-221
lines changed

4 files changed

+218
-221
lines changed

Shunting Yard/README.markdown

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Any mathematics we write is expressed in a notation known as *infix notation*. F
66

77
The operator is placed in between the operands, hence the expression is said to be in *infix* form. If you think about it, any expression that you write on a piece of paper will always be in infix form. This is what we humans understand.
88

9-
Multiplication has higher precedence than addition, so when the above expression is evaluated you would first multiply **B** and **C**, and then add the result to **A**. We humans can easily understand the precedence of operators, but a machine needs to be given instructions about each operator.
9+
Multiplication has higher precedence than addition, so when the above expression is evaluated you would first multiply **B** and **C**, and then add the result to **A**. We humans can easily understand the precedence of operators, but a machine needs to be given instructions about each operator.
1010

1111
To change the order of evaluation, we can use parentheses:
1212

@@ -108,4 +108,5 @@ We end up with the postfix expression:
108108

109109
[Shunting yard algorithm on Wikipedia](https://en.wikipedia.org/wiki/Shunting-yard_algorithm)
110110

111-
*Written for the Swift Algorithm Club by [Ali Hafizji](http://www.github.com/aliHafizji)*
111+
*Written for the Swift Algorithm Club by [Ali Hafizji](http://www.github.com/aliHafizji)*
112+
*Migrated to Swift 3 by Jaap Wijnen*

Shunting Yard/ShuntingYard.playground/Contents.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ public enum OperatorType: CustomStringConvertible {
1515

1616
public var description: String {
1717
switch self {
18-
case add:
18+
case .add:
1919
return "+"
20-
case subtract:
20+
case .subtract:
2121
return "-"
22-
case divide:
22+
case .divide:
2323
return "/"
24-
case multiply:
24+
case .multiply:
2525
return "*"
26-
case percent:
26+
case .percent:
2727
return "%"
28-
case exponent:
28+
case .exponent:
2929
return "^"
3030
}
3131
}
@@ -39,13 +39,13 @@ public enum TokenType: CustomStringConvertible {
3939

4040
public var description: String {
4141
switch self {
42-
case openBracket:
42+
case .openBracket:
4343
return "("
44-
case closeBracket:
44+
case .closeBracket:
4545
return ")"
46-
case Operator(let operatorToken):
46+
case .Operator(let operatorToken):
4747
return operatorToken.description
48-
case operand(let value):
48+
case .operand(let value):
4949
return "\(value)"
5050
}
5151
}
@@ -141,23 +141,23 @@ public struct Token: CustomStringConvertible {
141141
public class InfixExpressionBuilder {
142142
private var expression = [Token]()
143143

144-
public func addOperator(operatorType: OperatorType) -> InfixExpressionBuilder {
144+
public func addOperator(_ operatorType: OperatorType) -> InfixExpressionBuilder {
145145
expression.append(Token(operatorType: operatorType))
146146
return self
147147
}
148148

149-
public func addOperand(operand: Double) -> InfixExpressionBuilder {
149+
public func addOperand(_ operand: Double) -> InfixExpressionBuilder {
150150
expression.append(Token(operand: operand))
151151
return self
152152
}
153153

154154
public func addOpenBracket() -> InfixExpressionBuilder {
155-
expression.append(Token(tokenType: .OpenBracket))
155+
expression.append(Token(tokenType: .openBracket))
156156
return self
157157
}
158158

159159
public func addCloseBracket() -> InfixExpressionBuilder {
160-
expression.append(Token(tokenType: .CloseBracket))
160+
expression.append(Token(tokenType: .closeBracket))
161161
return self
162162
}
163163

@@ -168,7 +168,7 @@ public class InfixExpressionBuilder {
168168
}
169169

170170
// This returns the result of the shunting yard algorithm
171-
public func reversePolishNotation(expression: [Token]) -> String {
171+
public func reversePolishNotation(_ expression: [Token]) -> String {
172172

173173
var tokenStack = Stack<Token>()
174174
var reversePolishNotation = [Token]()
@@ -187,14 +187,14 @@ public func reversePolishNotation(expression: [Token]) -> String {
187187
}
188188

189189
case .Operator(let operatorToken):
190-
for tempToken in tokenStack.generate() {
190+
for tempToken in tokenStack.makeIterator() {
191191
if !tempToken.isOperator {
192192
break
193193
}
194194

195195
if let tempOperatorToken = tempToken.operatorToken {
196-
if operatorToken.associativity == .LeftAssociative && operatorToken <= tempOperatorToken
197-
|| operatorToken.associativity == .RightAssociative && operatorToken < tempOperatorToken {
196+
if operatorToken.associativity == .leftAssociative && operatorToken <= tempOperatorToken
197+
|| operatorToken.associativity == .rightAssociative && operatorToken < tempOperatorToken {
198198
reversePolishNotation.append(tokenStack.pop()!)
199199
} else {
200200
break
@@ -209,7 +209,7 @@ public func reversePolishNotation(expression: [Token]) -> String {
209209
reversePolishNotation.append(tokenStack.pop()!)
210210
}
211211

212-
return reversePolishNotation.map({token in token.description}).joinWithSeparator(" ")
212+
return reversePolishNotation.map({token in token.description}).joined(separator: " ")
213213
}
214214

215215

Shunting Yard/ShuntingYard.playground/Sources/Stack.swift

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
import Foundation
22

33
public struct Stack<T> {
4-
private var array = [T]()
5-
4+
fileprivate var array = [T]()
5+
66
public init() {
7-
array = []
7+
88
}
9-
9+
1010
public var isEmpty: Bool {
1111
return array.isEmpty
1212
}
13-
13+
1414
public var count: Int {
1515
return array.count
1616
}
17-
18-
public mutating func push(element: T) {
17+
18+
public mutating func push(_ element: T) {
1919
array.append(element)
2020
}
21-
21+
2222
public mutating func pop() -> T? {
23-
if isEmpty {
24-
return nil
25-
} else {
26-
return array.removeLast()
27-
}
23+
return array.popLast()
2824
}
29-
30-
public func peek() -> T? {
25+
26+
public var top: T? {
3127
return array.last
3228
}
3329
}
3430

35-
extension Stack: SequenceType {
36-
public func generate() -> AnyGenerator<T> {
31+
extension Stack: Sequence {
32+
public func makeIterator() -> AnyIterator<T> {
3733
var curr = self
38-
return anyGenerator {
34+
return AnyIterator {
3935
_ -> T? in
4036
return curr.pop()
4137
}

0 commit comments

Comments
 (0)