summaryrefslogtreecommitdiff
path: root/dice-lang/src/main/java/bjc/dicelang/ast/nodes/DiceLiteralNode.java
blob: 82c764d86e4386cd00f66ee03cd6b44c86bf4b39 (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
package bjc.dicelang.ast.nodes;

import bjc.dicelang.IDiceExpression;

/**
 * Represents a literal backed by a dice expression
 * 
 * @author ben
 *
 */
public class DiceLiteralNode implements ILiteralDiceNode {
	private IDiceExpression expression;

	/**
	 * Create a new literal from an expression
	 * 
	 * @param exp
	 *            The expression to attempt to create a literal from
	 */
	public DiceLiteralNode(IDiceExpression exp) {
		expression = exp;
	}

	/**
	 * Check if this node can be optimized to a constant
	 * 
	 * @return Whether or not this node can be optimized to a constant
	 * @see bjc.dicelang.IDiceExpression#canOptimize()
	 */
	public boolean canOptimize() {
		return expression.canOptimize();
	}

	@Override
	public DiceLiteralType getLiteralType() {
		return DiceLiteralType.DICE;
	}

	/**
	 * Return a value from the expression being represented
	 * 
	 * @return A value from the expression being represented
	 */
	public int getValue() {
		return expression.roll();
	}

	/**
	 * Optimize this node to a constant if possible
	 * 
	 * @return This node in constant form if possible
	 * @see bjc.dicelang.IDiceExpression#optimize()
	 */
	public int optimize() {
		return expression.optimize();
	}
}