blob: 11f466e6a437e9f003ce58a91b93cdb2f900ff0c (
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
|
package bjc.dicelang.v1;
import java.util.Map;
/**
* A dice expression that refers to a variable bound in a mutable enviroment
*
* @author ben
*
*/
public class ReferenceDiceExpression implements IDiceExpression {
/*
* The enviroment to do variable dereferencing against
*/
private Map<String, IDiceExpression> enviroment;
/*
* The name of the bound variable
*/
private String name;
/**
* Create a new reference dice expression referring to the given name in
* an enviroment
*
* @param nme
* The name of the bound variable
* @param env
* The enviroment to resolve the variable against
*/
public ReferenceDiceExpression(String nme, Map<String, IDiceExpression> env) {
this.name = nme;
this.enviroment = env;
}
/**
* Get the name of the referenced variable
*
* @return the name of the referenced variable
*/
public String getName() {
return name;
}
@Override
public int roll() {
if(!enviroment.containsKey(name))
throw new UnsupportedOperationException("Attempted to reference undefined variable " + name);
return enviroment.get(name).roll();
}
@Override
public String toString() {
if(enviroment.containsKey(name)) return enviroment.get(name).toString() + "(bound to " + name + ")";
return name + "(unbound)";
}
}
|