Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Migrate Shunting Yard to Swift 3
  • Loading branch information
JaapWijnen committed Jan 28, 2017
commit 2accb9907f434968e8806afe5637eeab69d4a0a9
5 changes: 3 additions & 2 deletions Shunting Yard/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Any mathematics we write is expressed in a notation known as *infix notation*. F

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.

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.
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.

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

Expand Down Expand Up @@ -108,4 +108,5 @@ We end up with the postfix expression:

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

*Written for the Swift Algorithm Club by [Ali Hafizji](http://www.github.com/aliHafizji)*
*Written for the Swift Algorithm Club by [Ali Hafizji](http://www.github.com/aliHafizji)*
*Migrated to Swift 3 by Jaap Wijnen*
38 changes: 19 additions & 19 deletions Shunting Yard/ShuntingYard.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public enum OperatorType: CustomStringConvertible {

public var description: String {
switch self {
case add:
case .add:
return "+"
case subtract:
case .subtract:
return "-"
case divide:
case .divide:
return "/"
case multiply:
case .multiply:
return "*"
case percent:
case .percent:
return "%"
case exponent:
case .exponent:
return "^"
}
}
Expand All @@ -39,13 +39,13 @@ public enum TokenType: CustomStringConvertible {

public var description: String {
switch self {
case openBracket:
case .openBracket:
return "("
case closeBracket:
case .closeBracket:
return ")"
case Operator(let operatorToken):
case .Operator(let operatorToken):
return operatorToken.description
case operand(let value):
case .operand(let value):
return "\(value)"
}
}
Expand Down Expand Up @@ -141,23 +141,23 @@ public struct Token: CustomStringConvertible {
public class InfixExpressionBuilder {
private var expression = [Token]()

public func addOperator(operatorType: OperatorType) -> InfixExpressionBuilder {
public func addOperator(_ operatorType: OperatorType) -> InfixExpressionBuilder {
expression.append(Token(operatorType: operatorType))
return self
}

public func addOperand(operand: Double) -> InfixExpressionBuilder {
public func addOperand(_ operand: Double) -> InfixExpressionBuilder {
expression.append(Token(operand: operand))
return self
}

public func addOpenBracket() -> InfixExpressionBuilder {
expression.append(Token(tokenType: .OpenBracket))
expression.append(Token(tokenType: .openBracket))
return self
}

public func addCloseBracket() -> InfixExpressionBuilder {
expression.append(Token(tokenType: .CloseBracket))
expression.append(Token(tokenType: .closeBracket))
return self
}

Expand All @@ -168,7 +168,7 @@ public class InfixExpressionBuilder {
}

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

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

case .Operator(let operatorToken):
for tempToken in tokenStack.generate() {
for tempToken in tokenStack.makeIterator() {
if !tempToken.isOperator {
break
}

if let tempOperatorToken = tempToken.operatorToken {
if operatorToken.associativity == .LeftAssociative && operatorToken <= tempOperatorToken
|| operatorToken.associativity == .RightAssociative && operatorToken < tempOperatorToken {
if operatorToken.associativity == .leftAssociative && operatorToken <= tempOperatorToken
|| operatorToken.associativity == .rightAssociative && operatorToken < tempOperatorToken {
reversePolishNotation.append(tokenStack.pop()!)
} else {
break
Expand All @@ -209,7 +209,7 @@ public func reversePolishNotation(expression: [Token]) -> String {
reversePolishNotation.append(tokenStack.pop()!)
}

return reversePolishNotation.map({token in token.description}).joinWithSeparator(" ")
return reversePolishNotation.map({token in token.description}).joined(separator: " ")
}


Expand Down
32 changes: 14 additions & 18 deletions Shunting Yard/ShuntingYard.playground/Sources/Stack.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import Foundation

public struct Stack<T> {
private var array = [T]()

fileprivate var array = [T]()
public init() {
array = []

}

public var isEmpty: Bool {
return array.isEmpty
}

public var count: Int {
return array.count
}

public mutating func push(element: T) {
public mutating func push(_ element: T) {
array.append(element)
}

public mutating func pop() -> T? {
if isEmpty {
return nil
} else {
return array.removeLast()
}
return array.popLast()
}

public func peek() -> T? {
public var top: T? {
return array.last
}
}

extension Stack: SequenceType {
public func generate() -> AnyGenerator<T> {
extension Stack: Sequence {
public func makeIterator() -> AnyIterator<T> {
var curr = self
return anyGenerator {
return AnyIterator {
_ -> T? in
return curr.pop()
}
Expand Down
Loading