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 | |
| parent | 67fee39e6dd22fce8dfaa800f0a5ddbe0ede0be3 (diff) | |
Some minor cleanliness, and beginning work on a language description.
7 files changed, 116 insertions, 38 deletions
diff --git a/dice-lang/lang-desc.md b/dice-lang/lang-desc.md new file mode 100644 index 0000000..1d3fdd1 --- /dev/null +++ b/dice-lang/lang-desc.md @@ -0,0 +1,59 @@ +# Dice-Lang Language Description +Dice lang was originally just a program for rolling +patterns of dice. However, through some effort +and pushing a shunting-yard parser farther than +it probably should have gone, it became a language. +It's still missing some things, but its getting there + +## Basic Syntax +You can use it like a 4-function calculator. +``` +1+1 +-> 2 +1+1 +2+2*2+2 +-> 8 +``` +However, we don't support floating point numbers or math +``` +1.1 +-> ERROR: Floating point literals are not supported +10/3 +-> 3 +``` +We do, however, support dice literals +``` +1d6 +-> 6 +1d6 +-> 3 +``` +These can be treated as numbers, but won't get turned into +numbers until you actually ask them to turn into numbers. + +## Variables and Assignment +There are variables, and you can assign things to them +``` +test := 1 +-> 1 +``` +When you assign a variable, its current value is mentioned. +To make sure that dice behave correctly, you can bind +them to a variable +``` +die := 1d6 +-> 5 +die +-> 3 +``` +There exists a meta-variable 'last' whose value is always the +result of the last expression. +``` +test := 1d6*2d8 +-> 9 +last +-> 30 +``` +We also have let, for binding things in the context of an +expression. However, let isn't quite working at the moment +## Arrays diff --git a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java index 1716698..c04318f 100644 --- a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java +++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java @@ -1,5 +1,6 @@ package bjc.dicelang.examples; +import java.util.InputMismatchException; import java.util.Scanner; import bjc.utils.funcdata.FunctionalMap; @@ -120,8 +121,9 @@ public class DiceASTLanguageTest { try { builtAST = DiceASTParser.createFromString(preparedTokens); - } catch (IllegalStateException isex) { - System.out.println("ERROR: " + isex.getLocalizedMessage()); + } catch (InputMismatchException | IllegalStateException + | UnsupportedOperationException ex) { + System.out.println("ERROR: " + ex.getLocalizedMessage()); currentLine = getNextCommand(inputSource, commandNumber); @@ -139,18 +141,19 @@ public class DiceASTLanguageTest { IResult sampleRoll; - try { - sampleRoll = DiceASTEvaluator.evaluateAST(transformedAST, - enviroment); - - enviroment.put("last", transformedAST); - } catch (UnsupportedOperationException usex) { - System.out.println("ERROR: " + usex.getLocalizedMessage()); - - currentLine = getNextCommand(inputSource, commandNumber); + // try { + sampleRoll = DiceASTEvaluator.evaluateAST(transformedAST, + enviroment); - continue; - } + enviroment.put("last", transformedAST); + /* + * } catch (UnsupportedOperationException usex) { + * System.out.println("ERROR: " + usex.getLocalizedMessage()); + * + * currentLine = getNextCommand(inputSource, commandNumber); + * + * continue; } + */ System.out.println("\t\tSample Roll: " + sampleRoll); 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; } } |
