diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-09-14 16:14:27 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-09-14 16:14:27 -0300 |
| commit | be125640128e741e964f65af0479dcb2f8002919 (patch) | |
| tree | e85b09ffaf029eeb72aa0b0c20e947f5c4a1d901 /BJC-Utils2/src/main/java/bjc/utils/ioutils/CLParameters.java | |
| parent | cb7671a1e5d171cc4002b07d832a5fd8afcc107e (diff) | |
More work on CL format
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils/CLParameters.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/ioutils/CLParameters.java | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/CLParameters.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/CLParameters.java new file mode 100644 index 0000000..e4bb6fb --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/CLParameters.java @@ -0,0 +1,109 @@ +package bjc.utils.ioutils; + +import java.util.ArrayList; +import java.util.List; + +import bjc.utils.esodata.Tape; + +/** + * Represents a set of parameters to a CL format directive. + * + * @author Benjamin Culkin + */ +public class CLParameters { + private String[] params; + + public CLParameters(String[] params) { + this.params = params; + } + + public int length() { + return params.length; + } + + /** + * Creates a set of parameters from an array of parameters. + * + * Mostly, this just fills in V and # parameters. + * + * @param params + * The parameters of the directive. + * @param dirParams + * The parameters of the format string. + * + * @return A set of CL parameters. + */ + public static CLParameters fromDirective(String[] params, Tape<Object> dirParams) { + List<String> parameters = new ArrayList<>(); + + for(String param : params) { + if(param.equalsIgnoreCase("V")) { + Object par = dirParams.item(); + boolean succ = dirParams.right(); + + if(par == null) { + throw new IllegalArgumentException("Expected a format parameter for V inline parameter"); + } + + if(par instanceof Number) { + int val = ((Number)par).intValue(); + + parameters.add(Integer.toString(val)); + } else if(par instanceof Character) { + char ch = ((Character)par); + + parameters.add(Character.toString(ch)); + } else { + throw new IllegalArgumentException("Incorrect type of parameter for V inline parameter"); + } + } else if(param.equals("#")) { + parameters.add(Integer.toString(dirParams.position())); + } else { + parameters.add(param); + } + } + + return new CLParameters(parameters.toArray(new String[0])); + } + + public char getCharDefault(int idx, String paramName, char directive, char def) { + if(!params[idx].equals("")) { + return getChar(idx, paramName, directive); + } + + return def; + } + + public char getChar(int idx, String paramName, char directive) { + String param = params[idx]; + + if(!param.startsWith("'")) { + throw new IllegalArgumentException(String.format("Invalid %s %s to %c directive", paramName, param, directive)); + } + + return param.charAt(1); + } + + public int getIntDefault(int idx, String paramName, char directive, int def) { + if(!params[idx].equals("")) { + + } + + return def; + } + + public int getInt(int idx, String paramName, char directive) { + String param = params[idx]; + + try { + return Integer.parseInt(param); + } catch (NumberFormatException nfex) { + String msg = String.format("Invalid %s %s to %c directive", paramName, param, directive); + + IllegalArgumentException iaex = new IllegalArgumentException(msg); + iaex.initCause(nfex); + + throw iaex; + } + } +} |
