Skip to content

Commit

Permalink
end of bronze challenge of ch20
Browse files Browse the repository at this point in the history
  • Loading branch information
HaeSeongPark committed Mar 15, 2019
1 parent a544524 commit 3a881f3
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions 20ErrorHandling.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Cocoa

enum Token {
case number(Int)
//For Bronze Challenge
case minus
case plus
}

Expand Down Expand Up @@ -65,6 +67,10 @@ class Lexer {
case "+":
tokens.append(.plus)
advance()
// For Bronze Challenge
case "-":
tokens.append(.minus)
advance()
case " ":
// Just advance to ignore spaces
advance()
Expand Down Expand Up @@ -111,6 +117,8 @@ class Parser {
return value
case .plus:
throw Error.InvalidToken(token)
case .minus:
throw Error.InvalidToken(token)
}
}

Expand All @@ -125,6 +133,10 @@ class Parser {
// After a plus, we must get another number
let nextNumber = try getNumber()
value += nextNumber
// For Bronze Challenge
case .minus:
let nextNumber = try getNumber()
value -= nextNumber
case .number:
throw Error.InvalidToken(token)
}
Expand Down Expand Up @@ -159,7 +171,9 @@ func evaluate(_ input:String) {
print("An error ocurred: \(error)")
}
}
// For Bronze Challenge
evaluate("10 + 5 - 3 - 1") // should be 11

evaluate("10 + 3 + 5")
evaluate("10 + 3 5")
evaluate("10 + ")
evaluate("10 + 3 + 5") // should be 18
evaluate("10 + 3 5") // Invalid token during paring : number(5)
evaluate("10 + ") // Unexpected end of input during parsing

0 comments on commit 3a881f3

Please sign in to comment.