summaryrefslogtreecommitdiff
path: root/dice-lang/src
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 14:02:12 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 14:02:12 -0400
commit9edd92f9a98762685aadc94ce9a59c7ac16cc76f (patch)
tree5ca14bc50fac73ff0de4a0a57755da41f3b095c8 /dice-lang/src
parent62e94ef994a59e87543445bb3c0ce0a37017a70a (diff)
Implemented support for conjoined tokens
Diffstat (limited to 'dice-lang/src')
-rw-r--r--dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java31
1 files changed, 28 insertions, 3 deletions
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 5f16554..d0b970a 100644
--- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java
+++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java
@@ -1,9 +1,14 @@
package bjc.dicelang.ast;
+import java.util.Deque;
+import java.util.LinkedList;
+
import org.apache.commons.lang3.StringUtils;
+import bjc.utils.data.Pair;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.FunctionalStringTokenizer;
+import bjc.utils.funcutils.ListUtils;
import bjc.utils.parserutils.AST;
import bjc.utils.parserutils.ShuntingYard;
import bjc.utils.parserutils.TreeConstructor;
@@ -39,10 +44,30 @@ public class DiceASTParser {
* @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> tokens =
+ FunctionalStringTokenizer.fromString(exp).toList((s) -> s);
+
+ Deque<Pair<String, String>> ops = new LinkedList<>();
+
+ ops.add(new Pair<>("+", "\\+"));
+ ops.add(new Pair<>("-", "-"));
+ ops.add(new Pair<>("*", "\\*"));
+ ops.add(new Pair<>("/", "/"));
+ ops.add(new Pair<>(":=", ":="));
+
+ FunctionalList<String> semiExpandedTokens =
+ ListUtils.splitTokens(tokens, ops);
+
+ ops = new LinkedList<>();
+
+ ops.add(new Pair<>("(", "\\("));
+ ops.add(new Pair<>(")", "\\)"));
+
+ FunctionalList<String> fullyExpandedTokens =
+ ListUtils.deAffixTokens(semiExpandedTokens, ops);
- FunctionalList<String> shunted = yard.postfix(tokens, (s) -> s);
+ FunctionalList<String> shunted =
+ yard.postfix(fullyExpandedTokens, (s) -> s);
AST<String> rawAST = TreeConstructor.constructTree(shunted,
this::isOperator, (op) -> false, null);