diff --git a/src/error/ErrorManager.java b/src/error/ErrorManager.java index bd065a1..2a20dc6 100644 --- a/src/error/ErrorManager.java +++ b/src/error/ErrorManager.java @@ -3,7 +3,7 @@ package error; import java.text.MessageFormat; /** - * Prints error messages. + * Prints error messages and potentially terminates the application. */ public class ErrorManager { @@ -15,27 +15,13 @@ public class ErrorManager { public static final String ANSI_PURPLE = "\u001B[35m"; public static void generateError(LispException lispException) { - generateError(lispException.getMessage(), lispException.getSeverity()); - } - - /** - * Prints out the specified error message to the console and decides whether - * or not to terminate the currently running program. - * - * @param message - * the error message - * @param level - * the "criticality" level of the error - * @postcondition If level >= CRITICAL_LEVEL the currently - * running program has been terminated. - */ - public static void generateError(String message, int level) { - String color = (level >= CRITICAL_LEVEL) ? ANSI_PURPLE : ANSI_RED; - String formattedMessage = MessageFormat.format("{0}error: {1}{2}", color, message, ANSI_RESET); + String color = (lispException.getSeverity() >= CRITICAL_LEVEL) ? ANSI_PURPLE : ANSI_RED; + String formattedMessage = MessageFormat.format("{0}error: {1}{2}", color, lispException.getMessage(), + ANSI_RESET); System.out.println(formattedMessage); - if (level >= CRITICAL_LEVEL) { + if (lispException.getSeverity() >= CRITICAL_LEVEL) { System.exit(1); } } diff --git a/src/main/LispInterpreter.java b/src/main/LispInterpreter.java index 08dafe5..3236019 100644 --- a/src/main/LispInterpreter.java +++ b/src/main/LispInterpreter.java @@ -32,14 +32,14 @@ public class LispInterpreter { /** * Evaluate the S-expressions found in the file given as a command-line - * argument and print the results to the console. If no file name was - * given, retrieve the S-expressions from standard input. + * argument and print the results to the console. If no file name was given, + * retrieve the S-expressions from standard input. * * @param args - * the command-line arguments: - * + * the command-line arguments: + * */ public static void main(String[] args) { LispParser parser = null; @@ -51,7 +51,20 @@ public class LispInterpreter { try { parser = new LispParser(new FileInputStream(args[0]), args[0]); } catch (FileNotFoundException e) { - ErrorManager.generateError(e.getMessage(), ErrorManager.CRITICAL_LEVEL); + ErrorManager.generateError(new LispException() { + + private static final long serialVersionUID = 1L; + + @Override + public int getSeverity() { + return ErrorManager.CRITICAL_LEVEL; + } + + @Override + public String getMessage() { + return e.getMessage(); + } + }); } } else { // no file name was given, create a 'LispParser' on standard input @@ -63,7 +76,7 @@ public class LispInterpreter { System.out.print(PROMPT); } - while (! parser.isEof()) { + while (!parser.isEof()) { try { SExpression sexpr = parser.getNextSExpression(); String result = MessageFormat.format("{0}{1}{2}", ANSI_GREEN, EVAL.eval(sexpr), ANSI_RESET); @@ -75,14 +88,27 @@ public class LispInterpreter { ErrorManager.generateError(e); } catch (RuntimeException e) { LispInterpreter.erasePrompt(interactive); - ErrorManager.generateError(e.getMessage(), 0); + ErrorManager.generateError(new LispException() { + + private static final long serialVersionUID = 1L; + + @Override + public int getSeverity() { + return 0; + } + + @Override + public String getMessage() { + return e.getMessage(); + } + }); } if (interactive) { System.out.print(PROMPT); } } - + System.out.println(); }