summaryrefslogtreecommitdiff
path: root/dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/DiceASTOptimizer.java
blob: dead812370f7c66f8b99f766dfdcd6244c13268f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package bjc.dicelang.old.ast.optimization;

import static bjc.dicelang.old.ast.nodes.DiceASTType.*;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;

import bjc.dicelang.IDiceExpression;
import bjc.dicelang.old.ast.nodes.DiceASTType;
import bjc.dicelang.old.ast.nodes.IDiceASTNode;
import bjc.dicelang.old.ast.nodes.LiteralDiceNode;
import bjc.dicelang.old.ast.nodes.OperatorDiceNode;
import bjc.utils.parserutils.AST;

/**
 * Optimize an AST
 * 
 * @author ben
 *
 */
public class DiceASTOptimizer {
	private static final class NestedArithmeticOperationCollapser
			implements BinaryOperator<AST<IDiceASTNode>> {
		private IDiceASTNode							type;
		private BiFunction<Integer, Integer, Integer>	valueCollapser;

		public NestedArithmeticOperationCollapser(IDiceASTNode type,
				BiFunction<Integer, Integer, Integer> valueCollapser) {
			this.type = type;
			this.valueCollapser = valueCollapser;
		}

		@Override
		public AST<IDiceASTNode> apply(AST<IDiceASTNode> leftAST,
				AST<IDiceASTNode> rightAST) {
			AST<IDiceASTNode> rightBranchOfLeftAST = leftAST
					.applyToRight((rightSideAST) -> rightSideAST);
			AST<IDiceASTNode> leftBranchOfLeftAST = leftAST
					.applyToRight((rightSideAST) -> rightSideAST);

			boolean leftContainsNestedConstant = DiceASTOptimizer
					.checkNodeType(rightBranchOfLeftAST, LITERAL)
					&& DiceASTOptimizer.isNodeConstant(leftAST);

			boolean isRightConstant = DiceASTOptimizer
					.checkNodeType(rightAST, LITERAL)
					&& DiceASTOptimizer.isNodeConstant(leftAST);

			if (leftContainsNestedConstant && isRightConstant) {
				int combinedValue = valueCollapser.apply(
						getNodeValue(rightBranchOfLeftAST),
						getNodeValue(rightAST));

				AST<IDiceASTNode> newRightBranch = new AST<>(
						new LiteralDiceNode(combinedValue));

				return new AST<>(type, leftBranchOfLeftAST,
						newRightBranch);
			}

			return new AST<>(type, leftAST, rightAST);
		}
	}

	private static final class ArithmeticOperationCollapser
			implements BinaryOperator<AST<IDiceASTNode>> {
		private IDiceASTNode							type;
		private BiFunction<Integer, Integer, Integer>	valueCollapser;
		private boolean									doSwap;

		public ArithmeticOperationCollapser(IDiceASTNode type,
				BiFunction<Integer, Integer, Integer> valueCollapser,
				boolean doSwap) {
			this.type = type;
			this.valueCollapser = valueCollapser;
			this.doSwap = doSwap;
		}

		@Override
		public AST<IDiceASTNode> apply(AST<IDiceASTNode> leftAST,
				AST<IDiceASTNode> rightAST) {
			boolean isLeftConstant = DiceASTOptimizer
					.checkNodeType(leftAST, LITERAL)
					&& DiceASTOptimizer.isNodeConstant(leftAST);

			boolean isRightConstant = DiceASTOptimizer
					.checkNodeType(rightAST, LITERAL)
					&& DiceASTOptimizer.isNodeConstant(leftAST);

			if (isLeftConstant) {
				if (isRightConstant) {
					int combinedValue = valueCollapser.apply(
							getNodeValue(leftAST), getNodeValue(rightAST));

					return new AST<>(new LiteralDiceNode(combinedValue));
				}

				if (doSwap) {
					return new AST<>(type, rightAST, leftAST);
				}
			}

			return new AST<>(type, leftAST, rightAST);
		}
	}

	private static Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> buildConstantCollapsers() {
		Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> operatorCollapsers = new HashMap<>();

		operatorCollapsers.put(OperatorDiceNode.ADD,
				new ArithmeticOperationCollapser(OperatorDiceNode.ADD,
						(leftVal, rightVal) -> leftVal + rightVal, true));

		operatorCollapsers.put(OperatorDiceNode.MULTIPLY,
				new ArithmeticOperationCollapser(OperatorDiceNode.MULTIPLY,
						(leftVal, rightVal) -> leftVal * rightVal, true));

		operatorCollapsers.put(OperatorDiceNode.SUBTRACT,
				new ArithmeticOperationCollapser(OperatorDiceNode.SUBTRACT,
						(leftVal, rightVal) -> leftVal - rightVal, false));

		operatorCollapsers.put(OperatorDiceNode.DIVIDE,
				new ArithmeticOperationCollapser(OperatorDiceNode.DIVIDE,
						(leftVal, rightVal) -> leftVal / rightVal, false));

		return operatorCollapsers;
	}

	private static Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> buildNestedConstantCollapsers() {
		Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> operatorCollapsers = new HashMap<>();

		operatorCollapsers.put(OperatorDiceNode.ADD,
				new NestedArithmeticOperationCollapser(
						OperatorDiceNode.ADD,
						(leftVal, rightVal) -> leftVal + rightVal));

		operatorCollapsers.put(OperatorDiceNode.MULTIPLY,
				new NestedArithmeticOperationCollapser(
						OperatorDiceNode.MULTIPLY,
						(leftVal, rightVal) -> leftVal * rightVal));

		return operatorCollapsers;
	}

	private static AST<IDiceASTNode> collapseLeaf(IDiceASTNode leaf) {
		// Can't optimize a simple reference
		if (leaf.getType() == VARIABLE) {
			return new AST<>(leaf);
		} else if (leaf.getType() == LITERAL) {
			LiteralDiceNode node = (LiteralDiceNode) leaf;

			return new AST<>(optimizeLiteral(node, node.toExpression()));
		} else {
			throw new UnsupportedOperationException(
					"Found leaf operator. This isn't supported");
		}
	}

	private static IDiceASTNode optimizeLiteral(LiteralDiceNode node,
			IDiceExpression leaf) {
		if (leaf.canOptimize()) {
			return new LiteralDiceNode(Integer.toString(leaf.optimize()));
		}

		return node;
	}

	private static AST<IDiceASTNode> finishTree(AST<IDiceASTNode> tree) {
		return tree;
	}

	/**
	 * Optimize a tree of expressions into a simpler form
	 * 
	 * @param tree
	 *            The tree to optimize
	 * @return The optimized tree
	 */
	public static AST<IDiceASTNode> optimizeTree(AST<IDiceASTNode> tree) {
		AST<IDiceASTNode> astWithConstantsFolded = tree.collapse(
				DiceASTOptimizer::collapseLeaf,
				buildConstantCollapsers()::get,
				DiceASTOptimizer::finishTree);

		AST<IDiceASTNode> astWithNestedConstantsFolded = astWithConstantsFolded
				.collapse(DiceASTOptimizer::collapseLeaf,
						buildNestedConstantCollapsers()::get,
						DiceASTOptimizer::finishTree);

		return astWithNestedConstantsFolded;
	}

	private static boolean checkNodeType(AST<IDiceASTNode> ast,
			DiceASTType type) {
		return ast.applyToHead((node) -> node.getType()) == type;
	}

	private static boolean isNodeConstant(AST<IDiceASTNode> ast) {
		return ast.applyToHead(
				(node) -> ((LiteralDiceNode) node).isConstant());
	}

	private static int getNodeValue(AST<IDiceASTNode> ast) {
		return ast.applyToHead(
				(node) -> ((LiteralDiceNode) node).toConstant());
	}
}