From 9b207cbbf8599cfc8304e3e7857a1aec770c5142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domenic=20Wu=CC=88thrich?= Date: Mon, 15 Jan 2018 16:13:19 +0100 Subject: [PATCH] Improved terminal input and printing Multiple calculations can now be calculated after each other, quit with "q" or "quit" Removed RPN printing from terminal --- Calculator/main.swift | 18 +++++++++++++----- Interpreter/Interpreter.swift | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Calculator/main.swift b/Calculator/main.swift index c54202a..f5f089c 100644 --- a/Calculator/main.swift +++ b/Calculator/main.swift @@ -8,12 +8,20 @@ import Foundation -let text = readLine() +let quit: [String] = ["q", "quit"] -if let text = text { +while true { + + print("> ", separator: "", terminator: "") + + let txt = readLine() + + guard let text = txt, !quit.contains(text.lowercased()) else { + break + } + let interpreter = Interpreter(text) + print(">> = ", separator: "", terminator: "") print(interpreter.interpret()) -} else { - fatalError("Error: no input!") + print("") } - diff --git a/Interpreter/Interpreter.swift b/Interpreter/Interpreter.swift index ce00115..f4a3166 100644 --- a/Interpreter/Interpreter.swift +++ b/Interpreter/Interpreter.swift @@ -105,8 +105,8 @@ class Interpreter { */ public func interpret() -> Int { let tree = self.parser.parse() - let rpn = RPN(tree) - rpn.print() + //let rpn = RPN(tree) + //rpn.print() return self.eval(tree) }