summaryrefslogtreecommitdiff
path: root/dice-lang/src/main/java/bjc/dicelang/ast/optimization/ConstantCollapser.java
blob: 6749cab91bcfbffef1ff77a1a6ce1d0df961fac0 (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
package bjc.dicelang.ast.optimization;

import bjc.utils.funcdata.IList;
import bjc.utils.funcdata.ITree;
import bjc.utils.funcdata.Tree;

import bjc.dicelang.ComplexDice;
import bjc.dicelang.ast.DiceASTUtils;
import bjc.dicelang.ast.nodes.IDiceASTNode;
import bjc.dicelang.ast.nodes.IntegerLiteralNode;
import bjc.dicelang.ast.nodes.OperatorDiceNode;

/**
 * Collapses operations with constants to constants
 * 
 * @author ben
 *
 */
public class ConstantCollapser implements IOptimizationPass {
	private static final ArithmeticCollapser	additionCollapser	= new ArithmeticCollapser(
			(left, right) -> left + right, OperatorDiceNode.ADD);

	private static final ArithmeticCollapser	divideCollapser		= new ArithmeticCollapser(
			(left, right) -> left / right, OperatorDiceNode.DIVIDE);

	private static final ArithmeticCollapser	multiplyCollapser	= new ArithmeticCollapser(
			(left, right) -> left * right, OperatorDiceNode.MULTIPLY);

	private static final ArithmeticCollapser	subtractCollapser	= new ArithmeticCollapser(
			(left, right) -> left - right, OperatorDiceNode.SUBTRACT);

	private static final ArithmeticCollapser	compoundCollapser	= new ArithmeticCollapser(
			(left, right) -> Integer.parseInt(
					Integer.toString(left) + Integer.toString(left)),
			OperatorDiceNode.SUBTRACT);

	@Override
	public ITree<IDiceASTNode> optimizeLeaf(IDiceASTNode leafNode) {
		// We don't do anything special here
		return new Tree<>(leafNode);
	}

	@Override
	public ITree<IDiceASTNode> optimizeOperator(IDiceASTNode operator,
			IList<ITree<IDiceASTNode>> children) {
		if (!operator.isOperator()) {
			return new Tree<>(operator, children);
		}

		switch ((OperatorDiceNode) operator) {
			case ADD:
				return additionCollapser.collapse(children);
			case DIVIDE:
				return divideCollapser.collapse(children);
			case MULTIPLY:
				return multiplyCollapser.collapse(children);
			case SUBTRACT:
				return subtractCollapser.collapse(children);
			case COMPOUND:
				return compoundCollapser.collapse(children);
			case GROUP:
				if (children.getSize() != 2) {
					return new Tree<>(operator, children);
				}

				ComplexDice dice = new ComplexDice(
						DiceASTUtils.literalToExpression(children.getByIndex(0)),
						DiceASTUtils.literalToExpression(children.getByIndex(1)));

				if (dice.canOptimize()) {
					return new Tree<>(
							new IntegerLiteralNode(dice.optimize()));
				}

				return new Tree<>(operator, children);
			case ARRAY:
				if (children.getSize() != 1) {
					return new Tree<>(operator, children);
				}

				return children.first();
			case ASSIGN:
			case LET:
			default:
				// We don't optimize these operators
				return new Tree<>(operator, children);
		}
	}
}