diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-27 22:45:03 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-27 22:45:03 -0400 |
| commit | f62abec2577d3745475581a19eff71dbb8c0494e (patch) | |
| tree | 2793192a9d393302b56783399dbc58fe5e220d4e /dice-lang/src/main/java | |
| parent | 67fee39e6dd22fce8dfaa800f0a5ddbe0ede0be3 (diff) | |
Some minor cleanliness, and beginning work on a language description.
Diffstat (limited to 'dice-lang/src/main/java')
5 files changed, 41 insertions, 25 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java b/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java index 0ee2127..acb1d4d 100644 --- a/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java +++ b/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java @@ -41,7 +41,7 @@ public interface IDiceExpression { } else if (StringUtils.containsInfixOperator(literalData, "d")) { // Handle groups of similiar dice return ComplexDice.fromString(literalData); - } else if (literalData.startsWith("d")) { + } else if (literalData.matches("\\Ad\\d+\\Z")) { // Handle people who put 'd6' instead of '1d6' return new Die(Integer.parseInt(literalData.substring(1))); } else { diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java b/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java index e481e5e..44904e5 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java @@ -24,23 +24,27 @@ final class ArithmeticCollapser implements IOperatorCollapser { // The operator to use to collapse operators private BinaryOperator<Integer> valueOp; + private int initialValue; + public ArithmeticCollapser(OperatorDiceNode type, - BinaryOperator<Integer> valueOp) { + BinaryOperator<Integer> valueOp, int initVal) { this.type = type; this.valueOp = valueOp; + this.initialValue = initVal; } @Override public IPair<IResult, ITree<IDiceASTNode>> apply( IList<IPair<IResult, ITree<IDiceASTNode>>> nodes) { IPair<IResult, ITree<IDiceASTNode>> initialState = new Pair<>( - new IntegerResult(0), new Tree<>(type)); + new IntegerResult(initialValue), new Tree<>(type)); BinaryOperator<IPair<IResult, ITree<IDiceASTNode>>> reducer = ( currentState, accumulatedState) -> { // Force evaluation of accumulated state to prevent // certain bugs from occuring - accumulatedState.merge((l, r) -> null); + // @TODO lets see if some of these bugs are fixed + // accumulatedState.merge((l, r) -> null); return reduceStates(accumulatedState, currentState); }; @@ -142,6 +146,7 @@ final class ArithmeticCollapser implements IOperatorCollapser { throw new UnsupportedOperationException( "Nested array operations not supported"); } + int elementInt = ((IntegerResult) element).getValue(); IResult combinedValue; diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java index d2c127f..2ed7adf 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java @@ -59,19 +59,19 @@ public class DiceASTEvaluator { operatorCollapsers.put(OperatorDiceNode.ADD, new ArithmeticCollapser(OperatorDiceNode.ADD, - (left, right) -> left + right)); + (left, right) -> left + right, 0)); operatorCollapsers.put(OperatorDiceNode.SUBTRACT, new ArithmeticCollapser(OperatorDiceNode.SUBTRACT, - (left, right) -> left - right)); + (left, right) -> left - right, 0)); operatorCollapsers.put(OperatorDiceNode.MULTIPLY, new ArithmeticCollapser(OperatorDiceNode.MULTIPLY, - (left, right) -> left * right)); + (left, right) -> left * right, 1)); operatorCollapsers.put(OperatorDiceNode.DIVIDE, new ArithmeticCollapser(OperatorDiceNode.DIVIDE, - (left, right) -> left / right)); + (left, right) -> left / right, 1)); operatorCollapsers.put(OperatorDiceNode.ASSIGN, (nodes) -> { return parseBinding(enviroment, nodes); @@ -82,7 +82,7 @@ public class DiceASTEvaluator { (left, right) -> { return Integer.parseInt(Integer.toString(left) + Integer.toString(right)); - })); + }, 0)); operatorCollapsers.put(OperatorDiceNode.GROUP, DiceASTEvaluator::parseGroup); @@ -304,7 +304,10 @@ public class DiceASTEvaluator { IMap<String, ITree<IDiceASTNode>> letEnviroment = enviroment .extend(); + System.out.println("Evaluating tree for bound values"); + evaluateAST(bindTree, letEnviroment); + IResult exprResult = evaluateAST(expressionTree, letEnviroment); IList<ITree<IDiceASTNode>> childrn = nodes diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java index bd6011e..0d57c57 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java @@ -49,6 +49,11 @@ public class DiceASTParser { } } + if (leafNode.matches("[+-]?\\d*\\.\\d+")) { + throw new InputMismatchException( + "Floating point literals are not supported"); + } + return new VariableDiceNode(leafNode); } @@ -87,9 +92,9 @@ public class DiceASTParser { IMap<String, Function<Deque<ITree<String>>, ITree<String>>> operators = new FunctionalMap<>(); operators.put("[", (queuedTrees) -> { - Tree<String> openTree = new Tree<>("["); + Tree<String> openArray = new Tree<>("["); - return openTree; + return openArray; }); operators.put("]", (queuedTrees) -> { diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/nodes/ILiteralDiceNode.java b/dice-lang/src/main/java/bjc/dicelang/ast/nodes/ILiteralDiceNode.java index 2105102..b94bcc8 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/nodes/ILiteralDiceNode.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/nodes/ILiteralDiceNode.java @@ -1,7 +1,5 @@ package bjc.dicelang.ast.nodes; -import org.apache.commons.lang3.StringUtils; - /** * Represents a literal of some type in the AST * @@ -18,20 +16,25 @@ public interface ILiteralDiceNode extends IDiceASTNode { * otherwise */ static DiceLiteralType getLiteralType(String tok) { - if (StringUtils.countMatches(tok, 'c') == 1 - && !tok.equalsIgnoreCase("c")) { + String diceGroupOrNumber = "[(?:\\d*d\\d+)(?:\\d+)]"; + + if (tok.matches("\\A" + diceGroupOrNumber + "?" + "c" + + diceGroupOrNumber + "\\Z")) { return DiceLiteralType.DICE; - } else if (StringUtils.countMatches(tok, 'd') == 1 - && !tok.equalsIgnoreCase("d")) { + } + + String diceGroup = "\\d*d\\d+\\"; + + if (tok.matches("\\A" + diceGroup + "Z")) { return DiceLiteralType.DICE; - } else { - try { - Integer.parseInt(tok); - return DiceLiteralType.INTEGER; - } catch (@SuppressWarnings("unused") NumberFormatException nfex) { - // We don't care about details - return null; - } + } + + try { + Integer.parseInt(tok); + return DiceLiteralType.INTEGER; + } catch (@SuppressWarnings("unused") NumberFormatException nfex) { + // We don't care about details + return null; } } |
