summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/utils/dice/ast/DiceASTParser.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 08:47:23 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 08:47:23 -0400
commit62e94ef994a59e87543445bb3c0ce0a37017a70a (patch)
treedfa1a488d3929e3f93e2a91669cdaf97c6d707f3 /dice-lang/src/bjc/utils/dice/ast/DiceASTParser.java
parent78d9c539e25f16fd15f06c2b2c48c0ad37a21540 (diff)
Renamed packages to suit updated project
Diffstat (limited to 'dice-lang/src/bjc/utils/dice/ast/DiceASTParser.java')
-rw-r--r--dice-lang/src/bjc/utils/dice/ast/DiceASTParser.java108
1 files changed, 0 insertions, 108 deletions
diff --git a/dice-lang/src/bjc/utils/dice/ast/DiceASTParser.java b/dice-lang/src/bjc/utils/dice/ast/DiceASTParser.java
deleted file mode 100644
index 38c514a..0000000
--- a/dice-lang/src/bjc/utils/dice/ast/DiceASTParser.java
+++ /dev/null
@@ -1,108 +0,0 @@
-package bjc.utils.dice.ast;
-
-import org.apache.commons.lang3.StringUtils;
-
-import bjc.utils.funcdata.FunctionalList;
-import bjc.utils.funcdata.FunctionalStringTokenizer;
-import bjc.utils.parserutils.AST;
-import bjc.utils.parserutils.ShuntingYard;
-import bjc.utils.parserutils.TreeConstructor;
-
-/**
- * Create an AST from a string expression
- *
- * @author ben
- *
- */
-public class DiceASTParser {
- /**
- * The yard to use for shunting expressions
- */
- private static ShuntingYard<String> yard;
-
- static {
- yard = new ShuntingYard<>();
-
- yard.addOp("d", 5); // dice operator: use for creating variable
- // size dice groups
- yard.addOp("c", 6); // compound operator: use for creating compound
- // dice from expressions
- yard.addOp(":=", 0); // binding operator: Bind a name to a variable
- // expression
- }
-
- /**
- * Build an AST from a string expression
- *
- * @param exp
- * The string to build from
- * @return An AST built from the passed in string
- */
- public AST<IDiceASTNode> buildAST(String exp) {
- FunctionalList<String> tokens = FunctionalStringTokenizer
- .fromString(exp).toList((s) -> s);
-
- FunctionalList<String> shunted = yard.postfix(tokens, (s) -> s);
-
- AST<String> rawAST = TreeConstructor.constructTree(shunted,
- this::isOperator, (op) -> false, null);
-
- AST<IDiceASTNode> bakedAST = rawAST.transmuteAST((tok) -> {
- if (isOperator(tok)) {
- return OperatorDiceNode.fromString(tok);
- } else if (isLiteral(tok)) {
- return new LiteralDiceNode(tok);
- } else {
- return new VariableDiceNode(tok);
- }
- });
-
- return bakedAST;
- }
-
- /**
- * Check if a token represents a literal
- *
- * @param tok
- * The token to check
- * @return Whether or not the token represents a literal
- */
- private static boolean isLiteral(String tok) {
- if (StringUtils.countMatches(tok, 'c') == 1
- && !tok.equalsIgnoreCase("c")) {
- return true;
- } else if (StringUtils.countMatches(tok, 'd') == 1
- && !tok.equalsIgnoreCase("d")) {
- return true;
- } else {
- try {
- Integer.parseInt(tok);
- return true;
- } catch (NumberFormatException nfx) {
- return false;
- }
- }
- }
-
- /**
- * Check if a token represents an operator
- *
- * @param tok
- * The token to check if it represents an operator
- * @return Whether or not the token represents an operator
- */
- private boolean isOperator(String tok) {
- switch (tok) {
- case ":=":
- case "+":
- case "-":
- case "*":
- case "/":
- case "c":
- case "d":
- return true;
- default:
- return false;
- }
- }
-}