diff options
| -rw-r--r-- | dice-lang/src/bjc/dicelang/v2/DiceBox.java | 60 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java | 2 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java | 86 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/v2/Shunter.java | 2 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/v2/Token.java | 4 |
5 files changed, 78 insertions, 76 deletions
diff --git a/dice-lang/src/bjc/dicelang/v2/DiceBox.java b/dice-lang/src/bjc/dicelang/v2/DiceBox.java index 164530c..375e256 100644 --- a/dice-lang/src/bjc/dicelang/v2/DiceBox.java +++ b/dice-lang/src/bjc/dicelang/v2/DiceBox.java @@ -9,16 +9,16 @@ public class DiceBox { public interface Die { boolean canOptimize(); - int optimize(); + long optimize(); - int roll(); + long roll(); } public interface DieList { boolean canOptimize(); - int[] optimize(); + long[] optimize(); - int[] roll(); + long[] roll(); } public static class DieExpression { @@ -46,14 +46,14 @@ public class DiceBox { public String value() { if(isList) return Arrays.toString(list.roll()); - else return Integer.toString(scalar.roll()); + else return Long.toString(scalar.roll()); } } private static class ScalarDie implements Die { - private int val; + private long val; - public ScalarDie(int vl) { + public ScalarDie(long vl) { val = vl; } @@ -61,24 +61,24 @@ public class DiceBox { return true; } - public int optimize() { + public long optimize() { return val; } - public int roll() { + public long roll() { return val; } public String toString() { - return Integer.toString(val); + return Long.toString(val); } } private static class SimpleDie implements Die { - private int numDice; - private int diceSize; + private long numDice; + private long diceSize; - public SimpleDie(int nDice, int size) { + public SimpleDie(long nDice, long size) { numDice = nDice; diceSize = size; } @@ -88,15 +88,15 @@ public class DiceBox { else return false; } - public int optimize() { + public long optimize() { return numDice; } - public int roll() { - int total = 0; + public long roll() { + long total = 0; for(int i = 0; i < numDice; i++) { - total += rng.nextInt(diceSize) + 1; + total += (rng.nextLong() % numDice) + 1; } return total; @@ -120,12 +120,12 @@ public class DiceBox { return left.canOptimize() && right.canOptimize(); } - public int optimize() { - return Integer.parseInt(left.optimize() + "" + right.optimize()); + public long optimize() { + return Long.parseLong(left.optimize() + "" + right.optimize()); } - public int roll() { - return Integer.parseInt(left.roll() + "" + right.roll()); + public long roll() { + return Long.parseLong(left.roll() + "" + right.roll()); } public String toString() { @@ -150,10 +150,10 @@ public class DiceBox { } } - public int[] optimize() { - int[] ret = new int[numDice.optimize()]; + public long[] optimize() { + long[] ret = new long[(int)numDice.optimize()]; - int optSize = size.optimize(); + long optSize = size.optimize(); for(int i = 0; i < optSize; i++) { ret[i] = 1; @@ -162,10 +162,10 @@ public class DiceBox { return ret; } - public int[] roll() { - int num = numDice.roll(); + public long[] roll() { + int num = (int)numDice.roll(); - int[] ret = new int[num]; + long[] ret = new long[num]; for(int i = 0; i < num; i++) { ret[i] = size.roll(); @@ -183,14 +183,14 @@ public class DiceBox { if(!isValidExpression(exp)) return null; if(scalarDiePattern.matcher(exp).matches()) { - return new DieExpression(new ScalarDie(Integer.parseInt(exp))); + return new DieExpression(new ScalarDie(Long.parseLong(exp))); } else if(simpleDiePattern.matcher(exp).matches()) { String[] dieParts = exp.split("d"); if(dieParts[0].equals("")) { - return new DieExpression(new SimpleDie(1, Integer.parseInt(dieParts[1]))); + return new DieExpression(new SimpleDie(1, Long.parseLong(dieParts[1]))); } else { - return new DieExpression(new SimpleDie(Integer.parseInt(dieParts[0]), Integer.parseInt(dieParts[1]))); + return new DieExpression(new SimpleDie(Long.parseLong(dieParts[0]), Long.parseLong(dieParts[1]))); } } else if(compoundDiePattern.matcher(exp).matches()) { String[] dieParts = exp.split("c"); diff --git a/dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java b/dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java index 54bdaec..c827edf 100644 --- a/dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java +++ b/dice-lang/src/bjc/dicelang/v2/DiceLangConsole.java @@ -23,7 +23,7 @@ public class DiceLangConsole { System.out.printf("(%d) dice-lang> ", commandNumber); String comm = scn.nextLine(); - while(!comm.equals("quit")) { + while(!comm.equals("quit") && !comm.equals("exit")) { System.out.printf("\tRaw command: %s\n", comm); boolean success = eng.runCommand(comm); diff --git a/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java b/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java index 5a4ff26..2ab5030 100644 --- a/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java +++ b/dice-lang/src/bjc/dicelang/v2/DiceLangEngine.java @@ -33,14 +33,20 @@ public class DiceLangEngine { // Shunter for token postfixing private Shunter shunt; + // Tables for symbols private IMap<Integer, String> symTable; private IMap<Integer, String> stringLits; + private IMap<String, Token.Type> litTokens; + private final int MATH_PREC = 20; private final int DICE_PREC = 10; private final int EXPR_PREC = 0; public DiceLangEngine() { + symTable = new FunctionalMap<>(); + stringLits = new FunctionalMap<>(); + opExpansionTokens = new LinkedList<>(); opExpansionTokens.add(new Pair<>("+", "\\+")); @@ -64,6 +70,19 @@ public class DiceLangEngine { debugMode = true; shunt = new Shunter(); + + litTokens = new FunctionalMap<>(); + + litTokens.put("+", ADD); + litTokens.put("-", SUBTRACT); + litTokens.put("*", MULTIPLY); + litTokens.put("/", DIVIDE); + litTokens.put("//", IDIVIDE); + litTokens.put("dg", DICEGROUP); + litTokens.put("dc", DICECONCAT); + litTokens.put("dl", DICELIST); + litTokens.put("=>", LET); + litTokens.put(":=", BIND); } public boolean runCommand(String command) { @@ -136,45 +155,19 @@ public class DiceLangEngine { Token tk = Token.NIL_TOKEN; - switch(token) { - case "+": - tk = new Token(ADD); - break; - case "-": - tk = new Token(SUBTRACT); - break; - case "*": - tk = new Token(MULTIPLY); - break; - case "/": - tk = new Token(DIVIDE); - break; - case "//": - tk = new Token(IDIVIDE); - break; - case "dg": - tk = new Token(DICEGROUP); - break; - case "dc": - tk = new Token(DICECONCAT); - break; - case "dl": - tk = new Token(DICELIST); - break; - case "=>": - tk = new Token(LET); - break; - case ":=": - tk = new Token(BIND); - break; - case "(": - case ")": - case "[": - case "]": - tk = tokenizeGrouping(token); - break; - default: - tk = tokenizeLiteral(token, stringLts); + if(litTokens.containsKey(token)) { + tk = new Token(litTokens.get(token)); + } else { + switch(token) { + case "(": + case ")": + case "[": + case "]": + tk = tokenizeGrouping(token); + break; + default: + tk = tokenizeLiteral(token, stringLts); + } } return tk; @@ -203,14 +196,23 @@ public class DiceLangEngine { return tk; } - private Pattern intMatcher = Pattern.compile("\\A[\\-\\+]?\\d+\\Z"); - private Pattern stringLitMatcher = Pattern.compile("\\AstringLiteral\\d+\\Z"); + private Pattern intMatcher = Pattern.compile("\\A[\\-\\+]?\\d+\\Z"); + private Pattern hexadecimalMatcher = Pattern.compile("\\A[\\-\\+]?0x[0-9A-Fa-f]+\\Z"); + private Pattern flexadecimalMatcher = Pattern.compile("\\A[\\-\\+]?[0-9][0-9A-Za-z]+B\\d{1,2}\\Z"); + private Pattern stringLitMatcher = Pattern.compile("\\AstringLiteral(\\d+)\\Z"); private Token tokenizeLiteral(String token, IMap<String, String> stringLts) { Token tk = Token.NIL_TOKEN; if(intMatcher.matcher(token).matches()) { - tk = new Token(INT_LIT, Integer.parseInt(token)); + tk = new Token(INT_LIT, Long.parseLong(token)); + } else if(hexadecimalMatcher.matcher(token).matches()) { + String newToken = token.substring(0, 1) + token.substring(token.indexOf('x')); + + tk = new Token(INT_LIT, Long.parseLong(newToken.substring(2).toUpperCase(), 16)); + } else if(flexadecimalMatcher.matcher(token).matches()) { + tk = new Token(INT_LIT, Long.parseLong(token.substring(0, token.lastIndexOf('B')), + Integer.parseInt(token.substring(token.lastIndexOf('B') + 1)))); } else if(DoubleMatcher.floatingLiteral.matcher(token).matches()) { tk = new Token(FLOAT_LIT, Double.parseDouble(token)); } else if(DiceBox.isValidExpression(token)) { diff --git a/dice-lang/src/bjc/dicelang/v2/Shunter.java b/dice-lang/src/bjc/dicelang/v2/Shunter.java index 3edceeb..b0a822a 100644 --- a/dice-lang/src/bjc/dicelang/v2/Shunter.java +++ b/dice-lang/src/bjc/dicelang/v2/Shunter.java @@ -127,7 +127,7 @@ public class Shunter { while(currTk.type != OPAREN && currTk.intValue != tk.intValue) { if(opStack.isEmpty()) { System.out.printf("\tError: Could not find matching parenthesis" - + " with matching level %d", tk.intValue); + + " with matching level %d\n", tk.intValue); return false; } diff --git a/dice-lang/src/bjc/dicelang/v2/Token.java b/dice-lang/src/bjc/dicelang/v2/Token.java index e5fce00..9825dae 100644 --- a/dice-lang/src/bjc/dicelang/v2/Token.java +++ b/dice-lang/src/bjc/dicelang/v2/Token.java @@ -26,7 +26,7 @@ public class Token { // At most one of these is valid // based on the token type - public int intValue; + public long intValue; public double floatValue; public DiceBox.DieExpression diceValue; @@ -34,7 +34,7 @@ public class Token { type = typ; } - public Token(Type typ, int val) { + public Token(Type typ, long val) { this(typ); intValue = val; |
