diff options
Diffstat (limited to 'src/main/java/bjc/dicelang/scl/Errors.java')
| -rw-r--r-- | src/main/java/bjc/dicelang/scl/Errors.java | 85 |
1 files changed, 39 insertions, 46 deletions
diff --git a/src/main/java/bjc/dicelang/scl/Errors.java b/src/main/java/bjc/dicelang/scl/Errors.java index 8d3faff..ef20086 100644 --- a/src/main/java/bjc/dicelang/scl/Errors.java +++ b/src/main/java/bjc/dicelang/scl/Errors.java @@ -1,5 +1,7 @@ package bjc.dicelang.scl; +import java.util.Arrays; + /** * Repository for error messages. * @@ -12,11 +14,17 @@ package bjc.dicelang.scl; * This way of handling error messages is not easy to deal with. Something else * needs to be done, but I'm not sure what at the moment. * + * ADDENDA: 5/28/18 Ben Culkin + * The error messages were moved into ErrorKey, as well as checking how + * many arguments they expect. This is better than it was, but still mildly + * annoying. * */ public class Errors { /** - * The types of error message. + * The types of error/warning message. + * + * Error messages are marked by starting with EK, warnings start with WK * * @author EVE * @@ -26,38 +34,50 @@ public class Errors { /** * Attempted to switch to a non-existant stream */ - EK_STRM_NONEX, + EK_STRM_NONEX("\tERROR: Fell off the stream list, attempting to move %s", 1), /** * Can't delete the last stream */ - EK_STRM_LAST, + EK_STRM_LAST("\tERROR: Cannot delete last remaining stream", 0), /** * Unknown stream command */ - EK_STRM_INVCOM, + EK_STRM_INVCOM("\tERROR: Unknown stream control command %s\n", 1), + + /* SCL Warnings */ + WK_SCL_WRDFAIL("\tWARNING: Execution of word %s failed\n", 1), + /* SCL Errors */ /** * Unknown SCL token */ - EK_SCL_INVTOKEN, + EK_SCL_INVTOKEN("\tERROR: Unknown SCL token %s\n", 1), /** * Mismatched quote in SCL command */ - EK_SCL_MMQUOTE, + EK_SCL_MMQUOTE("\tERROR: Mismatched delimiter %s in SCL command\n", 1), /** * Stack underflow in SCL command */ - EK_SCL_SUNDERFLOW, + EK_SCL_SUNDERFLOW("\tERROR: Not enough items in stack for word %s (need at least %d)\n", 2), /** * Unknown word in SCL command */ - EK_SCL_UNWORD, + EK_SCL_UNWORD("\tERROR: Unknown word %s\n", 1), /** * Invalid argument to SCL command */ - EK_SCL_INVARG, - } + EK_SCL_INVARG("\tERROR: Invalid argument to SCL command\n", 0); + + public final String msg; + public final int argc; + + private ErrorKey(String message, int argcount) { + msg = message; + argc = argcount; + } + } /** * The mode for the type of error messages to print out. * @@ -86,54 +106,27 @@ public class Errors { * @param args * The arguments for the error. */ - public void printError(final ErrorKey key, final String... args) { + public void printError(final ErrorKey key, final Object... args) { switch(mode) { case WIZARD: - System.out.println("\t? " + key.ordinal()); + System.out.printf("\t? %d %s\n", key.ordinal(), Arrays.deepToString(args)); break; - case DEV: devError(key, args); break; - default: - System.out.println("\tERROR ERROR: Unknown error mode " + mode); + System.out.printf("\tERROR ERROR: Unknown error mode %s\n", mode); } } - private static void devError(final ErrorKey key, final String[] args) { - switch(key) { - case EK_STRM_NONEX: - System.out.printf("\tERROR: Attempted to switch to non-existent stream\n"); - break; - - case EK_STRM_LAST: - System.out.printf("\tERROR: Cannot delete last stream\n"); - break; - - case EK_STRM_INVCOM: - System.out.printf("\tERROR: Unknown stream control command %s\n", args[0]); - break; - - case EK_SCL_INVTOKEN: - System.out.printf("\tERROR: Unknown SCL token %s\n", args[0]); - break; - - case EK_SCL_MMQUOTE: - System.out.printf("\tERROR: Mismatched delimiter in SCL command\n"); - break; + private static void devError(final ErrorKey key, final Object[] args) { + if(args.length != key.argc) { + System.out.printf("\tERROR ERROR: Incorrect # of format arguments (got %d, expected %d)\n", args.length, key.argc); - case EK_SCL_SUNDERFLOW: - System.out.printf("\tERROR: Not enough items in stack for word %s\n", args[0]); - break; - - case EK_SCL_UNWORD: - System.out.printf("\tERROR: Unknown word %s\n", args[0]); - break; - - default: - System.out.printf("\tERROR ERROR: Unknown error key %s\n", key); + return; } + + System.out.printf(key.msg, args); } /** |
