From 3a881f317f856b35c422d69cef7351beda91e64d Mon Sep 17 00:00:00 2001 From: rhino Date: Fri, 15 Mar 2019 17:04:39 +0900 Subject: [PATCH] end of bronze challenge of ch20 --- 20ErrorHandling.playground/Contents.swift | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/20ErrorHandling.playground/Contents.swift b/20ErrorHandling.playground/Contents.swift index e2e11d4..b85d512 100644 --- a/20ErrorHandling.playground/Contents.swift +++ b/20ErrorHandling.playground/Contents.swift @@ -2,6 +2,8 @@ import Cocoa enum Token { case number(Int) + //For Bronze Challenge + case minus case plus } @@ -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() @@ -111,6 +117,8 @@ class Parser { return value case .plus: throw Error.InvalidToken(token) + case .minus: + throw Error.InvalidToken(token) } } @@ -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) } @@ -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