@Override
    public void run() {
        final Calculator calc = new Calculator();

        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8")));
        String line = readExpression(reader);

        while (!"exit".equalsIgnoreCase(line)) {
            try {
                if (isCommand(line)) {
                    CommandResult result = calc.executeCommand(new Command.Builder().parse(line).build());
                    if (!(result instanceof EmptyResult)) {
                        write(result.getStringRepresentation());
                    }
                } else {
                    calc.evaluate(line);
                    write(calc.getResult());
                }
            } catch (ExpressionExecuteException | FunctionParseException | UnknownCommandException |
                    CommandParseException ex) {
                ex.printStackTrace();
            }
            line = readExpression(reader);
        }
    }