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

import bjc.utils.data.IHolder;
import bjc.utils.data.Identity;
import bjc.utils.funcdata.ITree;
import bjc.utils.funcdata.TopDownTransformResult;
import bjc.utils.funcdata.Tree;

import bjc.dicelang.ast.nodes.DiceASTType;
import bjc.dicelang.ast.nodes.IDiceASTNode;
import bjc.dicelang.ast.nodes.OperatorDiceNode;

/**
 * Condenses chained operations into a single level
 * 
 * @author ben
 *
 */
public class OperationCondenser {
	/**
	 * Condense chained similiar operations into a single level
	 * 
	 * @param ast
	 *            The AST to condense
	 * @return The condensed AST
	 */
	public static ITree<IDiceASTNode> condense(ITree<IDiceASTNode> ast) {
		return ast.topDownTransform(OperationCondenser::pickNode,
				OperationCondenser::doCondense);
	}

	private static ITree<IDiceASTNode> doCondense(
			ITree<IDiceASTNode> ast) {
		OperatorDiceNode operation = ast
				.transformHead((node) -> (OperatorDiceNode) node);

		IHolder<Boolean> canCondense = new Identity<>(true);

		ast.doForChildren((child) -> {
			if (canCondense.getValue()) {
				canCondense.replace(child.transformHead((node) -> {
					if (node.getType() == DiceASTType.OPERATOR) {
						if (operation.equals(node)) {
							return true;
						}

						return false;
					}

					return true;
				}));
			}
		});

		if (!canCondense.getValue()) {
			return ast;
		}

		ITree<IDiceASTNode> condensedAST = new Tree<>(operation);

		ast.doForChildren((child) -> {
			if (child.getHead().getType() == DiceASTType.OPERATOR) {
				child.doForChildren((subChild) -> {
					condensedAST.addChild(subChild);
				});
			} else {
				condensedAST.addChild(child);
			}
		});

		return condensedAST;
	}

	private static TopDownTransformResult pickNode(IDiceASTNode node) {
		switch (node.getType()) {
			case LITERAL:
				return TopDownTransformResult.SKIP;
			case OPERATOR:
				return pickOperator((OperatorDiceNode) node);
			case VARIABLE:
				return TopDownTransformResult.SKIP;
			default:
				throw new UnsupportedOperationException(
						"Attempted to traverse unknown node type " + node);
		}
	}

	private static TopDownTransformResult pickOperator(
			OperatorDiceNode node) {
		switch (node) {
			case ADD:
			case MULTIPLY:
			case SUBTRACT:
			case DIVIDE:
			case COMPOUND:
				return TopDownTransformResult.PUSHDOWN;
			case ARRAY:
			case ASSIGN:
			case GROUP:
			case LET:
				return TopDownTransformResult.PASSTHROUGH;
			default:
				throw new UnsupportedOperationException(
						"Attempted to traverse unknown operator " + node);
		}
	}
}