diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-18 13:04:01 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-18 13:04:01 -0400 |
| commit | 24f3ce54983348e1aa0229d5c08b3fe99d739d40 (patch) | |
| tree | 948277e2519e2dffcb3fa2b2bf2ee649c21c2e3c /dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java | |
| parent | 9ce39956fa1702f157c347dc4b8807d9b5dd2185 (diff) | |
Added operation condensing
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java')
| -rw-r--r-- | dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java | 34 |
1 files changed, 32 insertions, 2 deletions
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 411212e..aeecb38 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTEvaluator.java @@ -67,9 +67,38 @@ public class DiceASTEvaluator { operatorCollapsers.put(OperatorDiceNode.GROUP, DiceASTEvaluator::parseGroup); + operatorCollapsers.put(OperatorDiceNode.LET, (nodes) -> { + return parseLet(enviroment, nodes); + }); + return operatorCollapsers; } + private static IPair<Integer, ITree<IDiceASTNode>> parseLet( + IFunctionalMap<String, ITree<IDiceASTNode>> enviroment, + IFunctionalList<IPair<Integer, ITree<IDiceASTNode>>> nodes) { + if (nodes.getSize() != 2) { + throw new UnsupportedOperationException( + "Can only use let with two expressions."); + } + + ITree<IDiceASTNode> bindTree = nodes.getByIndex(0).getRight(); + ITree<IDiceASTNode> expressionTree = + nodes.getByIndex(1).getRight(); + + IFunctionalMap<String, ITree<IDiceASTNode>> letEnviroment = + enviroment.extend(); + + evaluateAST(bindTree, letEnviroment); + int exprResult = evaluateAST(expressionTree, letEnviroment); + + IFunctionalList<ITree<IDiceASTNode>> childrn = + nodes.map((pair) -> pair.getRight()); + + return new Pair<>(exprResult, + new Tree<>(OperatorDiceNode.LET, childrn)); + } + /** * Evaluate the provided AST to a numeric value * @@ -121,8 +150,9 @@ public class DiceASTEvaluator { return result; } - // Value to allow for assignments - return 0; + throw new UnsupportedOperationException( + "Attempted to dereference unbound variable " + + variableName); } private static int evaluateLiteral(IDiceASTNode leafNode) { |
