diff options
Diffstat (limited to 'dice-lang')
9 files changed, 132 insertions, 17 deletions
diff --git a/dice-lang/.settings/org.eclipse.core.resources.prefs b/dice-lang/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..d0482e7 --- /dev/null +++ b/dice-lang/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/lang-impl.html=UTF-8 diff --git a/dice-lang/lang-impl.html b/dice-lang/lang-impl.html new file mode 100644 index 0000000..62fddf6 --- /dev/null +++ b/dice-lang/lang-impl.html @@ -0,0 +1,13 @@ +<?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> + </head> + <body> + <h1 id="language-implementation-details">Language implementation details</h1> + <p>First, a command is read from the user, and + checked to see if it has any interpreter pragmas + in it. If so, the interpreter pragma is handled + and we move onto the next command.</p> + </body> +</html>
\ No newline at end of file diff --git a/dice-lang/lang-impl.md b/dice-lang/lang-impl.md new file mode 100644 index 0000000..c6c2ce2 --- /dev/null +++ b/dice-lang/lang-impl.md @@ -0,0 +1,23 @@ +# Language implementation details +First, a command is read from the user, and +checked to see if it has any interpreter pragmas +in it. If so, the interpreter pragma is handled +and we move onto the next command. + +Next, the command is prepared for parsing. +This involves 4 steps +1. Convert the command into tokens +2. Split operators from tokens. This means + converting tokens like 2+2 into the three tokens + 2, + and 2 +3. Deaffix tokens. This means deattaching brackets + and parenthesis from their attached tokens. +4. Remove blank tokens + +Next, is parsing. This is just a modified version +of the shunting-yard algorithm, with the +main modification being it properly handles +multiple nesting levels of parenthesis + +Then, the AST is created from the parsed +string.
\ No newline at end of file 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 c04318f..b331535 100644 --- a/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java +++ b/dice-lang/src/examples/java/bjc/dicelang/examples/DiceASTLanguageTest.java @@ -116,11 +116,26 @@ public class DiceASTLanguageTest { // Build an AST from the string expression ITree<IDiceASTNode> builtAST; + long time = System.nanoTime(); + IList<String> preparedTokens = DiceExpressionPreparer .prepareCommand(currentLine); + System.out.println("Command prepared in " + + (double) (System.nanoTime() - time) / 1000000000 + + " s"); + try { + time = System.nanoTime(); + builtAST = DiceASTParser.createFromString(preparedTokens); + + System.out + .println( + "Command parsed in " + + (double) (System.nanoTime() + - time) / 1000000000 + + " s"); } catch (InputMismatchException | IllegalStateException | UnsupportedOperationException ex) { System.out.println("ERROR: " + ex.getLocalizedMessage()); @@ -133,27 +148,41 @@ public class DiceASTLanguageTest { // Print out results System.out.println("\tParsed: " + builtAST.toString()); + time = System.nanoTime(); + ITree<IDiceASTNode> transformedAST = transformAST(builtAST, enviroment); + System.out.println("Command transformed in " + + (double) (System.nanoTime() - time) / 1000000000 + + " s"); + System.out .println("\tEvaluated: " + transformedAST.toString()); IResult sampleRoll; - // try { - sampleRoll = DiceASTEvaluator.evaluateAST(transformedAST, - enviroment); + try { + time = System.nanoTime(); - enviroment.put("last", transformedAST); - /* - * } catch (UnsupportedOperationException usex) { - * System.out.println("ERROR: " + usex.getLocalizedMessage()); - * - * currentLine = getNextCommand(inputSource, commandNumber); - * - * continue; } - */ + sampleRoll = DiceASTEvaluator.evaluateAST(transformedAST, + enviroment); + + System.out + .println( + "Command evaluated in " + + (double) (System.nanoTime() + - time) / 1000000000 + + " s"); + + 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/ast/ArithmeticCollapser.java b/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java index 44904e5..7537005 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/ArithmeticCollapser.java @@ -43,8 +43,7 @@ final class ArithmeticCollapser implements IOperatorCollapser { currentState, accumulatedState) -> { // Force evaluation of accumulated state to prevent // certain bugs from occuring - // @TODO lets see if some of these bugs are fixed - // accumulatedState.merge((l, r) -> null); + //accumulatedState.merge((l, r) -> null); return reduceStates(accumulatedState, currentState); }; @@ -94,6 +93,18 @@ final class ArithmeticCollapser implements IOperatorCollapser { private IPair<IResult, ITree<IDiceASTNode>> doArithmeticCollapse( IResult accumulatedValue, ITree<IDiceASTNode> accumulatedTree, IResult currentValue) { + if (accumulatedValue.getType() == ResultType.DUMMY + || currentValue.getType() == ResultType.DUMMY) { + DummyResult result = new DummyResult( + "Found dummy result with either accumulated dummy (" + + ((DummyResult) accumulatedValue).getData() + + ") or current dummy (" + + ((DummyResult) currentValue).getData() + + ")."); + + return new Pair<>(result, accumulatedTree); + } + boolean currentIsInt = currentValue .getType() == ResultType.INTEGER; boolean accumulatedIsInt = accumulatedValue 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 2ed7adf..cef2e19 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java @@ -42,7 +42,8 @@ public class DiceASTEvaluator { return result; } - throw new UnsupportedOperationException( + // Return a DummyResult to handle lets properly + return new DummyResult( "Attempted to deref unbound variable " + variableName); } @@ -88,6 +89,7 @@ public class DiceASTEvaluator { DiceASTEvaluator::parseGroup); operatorCollapsers.put(OperatorDiceNode.LET, (nodes) -> { + // @TODO Fix lets prematurely evaluating things return parseLet(enviroment, nodes); }); diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DummyResult.java b/dice-lang/src/main/java/bjc/dicelang/ast/DummyResult.java new file mode 100644 index 0000000..eeda874 --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DummyResult.java @@ -0,0 +1,31 @@ +package bjc.dicelang.ast; + +public class DummyResult implements IResult { + /* + * The reason why this result is a dummy + */ + private String dummyData; + + public DummyResult(String data) { + dummyData = data; + } + + @Override + public ResultType getType() { + return ResultType.DUMMY; + } + + /** + * Get the data in this dummy + * + * @return The reason why this result is a dummy + */ + public String getData() { + return dummyData; + } + + @Override + public String toString() { + return "Dummy with reason " + dummyData; + } +} diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/ResultType.java b/dice-lang/src/main/java/bjc/dicelang/ast/ResultType.java index d5e94b9..9e3b129 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/ResultType.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/ResultType.java @@ -14,5 +14,9 @@ public enum ResultType { /** * Represents a result that is an array */ - ARRAY; + ARRAY, + /** + * Represents something not to poke at + */ + DUMMY } diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java index 6749cab..35148fa 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java @@ -32,7 +32,7 @@ public class ConstantCollapser implements IOptimizationPass { private static final ArithmeticCollapser compoundCollapser = new ArithmeticCollapser( (left, right) -> Integer.parseInt( Integer.toString(left) + Integer.toString(left)), - OperatorDiceNode.SUBTRACT); + OperatorDiceNode.COMPOUND); @Override public ITree<IDiceASTNode> optimizeLeaf(IDiceASTNode leafNode) { |
