From f62abec2577d3745475581a19eff71dbb8c0494e Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 27 Jul 2016 22:45:03 -0400 Subject: Some minor cleanliness, and beginning work on a language description. --- .../main/java/bjc/dicelang/IDiceExpression.java | 2 +- .../java/bjc/dicelang/ast/ArithmeticCollapser.java | 11 +++++--- .../java/bjc/dicelang/ast/DiceASTEvaluator.java | 13 +++++---- .../main/java/bjc/dicelang/ast/DiceASTParser.java | 9 +++++-- .../bjc/dicelang/ast/nodes/ILiteralDiceNode.java | 31 ++++++++++++---------- 5 files changed, 41 insertions(+), 25 deletions(-) (limited to 'dice-lang/src/main/java/bjc') 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 valueOp; + private int initialValue; + public ArithmeticCollapser(OperatorDiceNode type, - BinaryOperator valueOp) { + BinaryOperator valueOp, int initVal) { this.type = type; this.valueOp = valueOp; + this.initialValue = initVal; } @Override public IPair> apply( IList>> nodes) { IPair> initialState = new Pair<>( - new IntegerResult(0), new Tree<>(type)); + new IntegerResult(initialValue), new Tree<>(type)); BinaryOperator>> 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> letEnviroment = enviroment .extend(); + System.out.println("Evaluating tree for bound values"); + evaluateAST(bindTree, letEnviroment); + IResult exprResult = evaluateAST(expressionTree, letEnviroment); IList> 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>, ITree>> operators = new FunctionalMap<>(); operators.put("[", (queuedTrees) -> { - Tree openTree = new Tree<>("["); + Tree 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; } } -- cgit v1.2.3