From 653602b88e98fec3c28f5dbe2aba02e0333698db Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 5 Apr 2017 15:29:56 -0400 Subject: Move examples back --- .../java/bjc/utils/examples/ShuntTest.java | 37 ++++++ .../java/bjc/utils/examples/TreeConstructTest.java | 129 +++++++++++++++++++++ .../src/examples/java/bjc/utils/examples/test.tree | 13 +++ 3 files changed, 179 insertions(+) create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/ShuntTest.java create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/TreeConstructTest.java create mode 100644 BJC-Utils2/src/examples/java/bjc/utils/examples/test.tree diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/ShuntTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/ShuntTest.java new file mode 100644 index 0000000..e0c29fe --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/ShuntTest.java @@ -0,0 +1,37 @@ +package bjc.utils.examples; + +import bjc.utils.funcdata.FunctionalStringTokenizer; +import bjc.utils.funcdata.IList; +import bjc.utils.parserutils.ShuntingYard; + +import java.util.Scanner; + +/** + * Test of shunting yard + * + * @author ben + * + */ +public class ShuntTest { + /** + * Main method + * + * @param args + * Unused CLI args + */ + public static void main(String[] args) { + Scanner inputSource = new Scanner(System.in); + + System.out.print("Enter a expression to shunt: "); + String line = inputSource.nextLine(); + + ShuntingYard yard = new ShuntingYard<>(true); + + IList preTokens = new FunctionalStringTokenizer(line).toList(strang -> strang); + IList shuntedTokens = yard.postfix(preTokens, strang -> strang); + + System.out.println(shuntedTokens.toString()); + + inputSource.close(); + } +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/TreeConstructTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/TreeConstructTest.java new file mode 100644 index 0000000..8c945e9 --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/TreeConstructTest.java @@ -0,0 +1,129 @@ +package bjc.utils.examples; + +import bjc.utils.data.IPair; +import bjc.utils.data.ITree; +import bjc.utils.data.Pair; +import bjc.utils.data.Tree; +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.FunctionalStringTokenizer; +import bjc.utils.funcdata.IList; +import bjc.utils.funcdata.IMap; +import bjc.utils.funcutils.ListUtils; +import bjc.utils.funcutils.StringUtils; +import bjc.utils.parserutils.ShuntingYard; +import bjc.utils.parserutils.TreeConstructor; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.Scanner; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * Test of tree constructor + * + * @author ben + * + */ +public class TreeConstructTest { + private static final class OperatorPicker implements Predicate { + @Override + public boolean test(String token) { + if(StringUtils.containsOnly(token, "\\[")) return true; + else if(StringUtils.containsOnly(token, "\\]")) return true; + + switch(token) { + case "+": + case "-": + case "*": + case "/": + return true; + default: + return false; + } + } + } + + /** + * Main method + * + * @param args + * Unused CLI args + */ + @SuppressWarnings({ "resource", "deprecation" }) + public static void main(String[] args) { + Scanner inputSource = new Scanner(System.in); + + System.out.print("Enter a expression to parse: "); + String line = inputSource.nextLine(); + + IList tokens = new FunctionalStringTokenizer(line).toList(); + + ShuntingYard yard = new ShuntingYard<>(true); + + Deque> ops = new LinkedList<>(); + + ops.add(new Pair<>("+", "\\+")); + ops.add(new Pair<>("-", "-")); + ops.add(new Pair<>("*", "\\*")); + ops.add(new Pair<>("/", "/")); + ops.add(new Pair<>(":=", ":=")); + ops.add(new Pair<>("=>", "=>")); + + IList semiExpandedTokens = ListUtils.splitTokens(tokens, ops); + + ops = new LinkedList<>(); + ops.add(new Pair<>("(", "\\(")); + ops.add(new Pair<>(")", "\\)")); + ops.add(new Pair<>("[", "\\[")); + ops.add(new Pair<>("]", "\\]")); + + IList fullyExpandedTokens = ListUtils.deAffixTokens(semiExpandedTokens, ops); + fullyExpandedTokens.removeIf((strang) -> strang.equals("")); + + IList shuntedTokens = yard.postfix(fullyExpandedTokens, (token) -> token); + + System.out.println("Shunted: " + shuntedTokens.toString()); + + Predicate specialPicker = (operator) -> { + if(StringUtils.containsOnly(operator, "\\[")) return true; + else if(StringUtils.containsOnly(operator, "\\]")) return true; + + return false; + }; + + IMap>, ITree>> operators + = new FunctionalMap<>(); + + operators.put("[", (queuedTrees) -> { + return null; + }); + + operators.put("[", (queuedTrees) -> { + Tree openTree = new Tree<>("["); + + queuedTrees.push(openTree); + + return openTree; + }); + + operators.put("]", (queuedTrees) -> { + ITree arrayTree = new Tree<>("[]"); + + while(!queuedTrees.peek().getHead().equals("[")) { + arrayTree.addChild(queuedTrees.pop()); + } + + queuedTrees.push(arrayTree); + + return arrayTree; + }); + + ITree constructedTree = TreeConstructor.constructTree(shuntedTokens, + new OperatorPicker(), specialPicker, operators::get); + + System.out.println("AST: " + constructedTree.toString()); + + inputSource.close(); + } +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/test.tree b/BJC-Utils2/src/examples/java/bjc/utils/examples/test.tree new file mode 100644 index 0000000..795cc88 --- /dev/null +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/test.tree @@ -0,0 +1,13 @@ +test 1 + 1 + 1 + 2 + 2 + 1 + +simp 1 + 2 + 3 + 4 + 3 + 2 \ No newline at end of file -- cgit v1.2.3