From 9edd92f9a98762685aadc94ce9a59c7ac16cc76f Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 28 Mar 2016 14:02:12 -0400 Subject: Implemented support for conjoined tokens --- .../main/java/bjc/dicelang/ast/DiceASTParser.java | 31 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'dice-lang/src/main/java/bjc') 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 buildAST(String exp) { - FunctionalList tokens = FunctionalStringTokenizer - .fromString(exp).toList((s) -> s); + FunctionalList tokens = + FunctionalStringTokenizer.fromString(exp).toList((s) -> s); + + Deque> ops = new LinkedList<>(); + + ops.add(new Pair<>("+", "\\+")); + ops.add(new Pair<>("-", "-")); + ops.add(new Pair<>("*", "\\*")); + ops.add(new Pair<>("/", "/")); + ops.add(new Pair<>(":=", ":=")); + + FunctionalList semiExpandedTokens = + ListUtils.splitTokens(tokens, ops); + + ops = new LinkedList<>(); + + ops.add(new Pair<>("(", "\\(")); + ops.add(new Pair<>(")", "\\)")); + + FunctionalList fullyExpandedTokens = + ListUtils.deAffixTokens(semiExpandedTokens, ops); - FunctionalList shunted = yard.postfix(tokens, (s) -> s); + FunctionalList shunted = + yard.postfix(fullyExpandedTokens, (s) -> s); AST rawAST = TreeConstructor.constructTree(shunted, this::isOperator, (op) -> false, null); -- cgit v1.2.3