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
|
package bjc.dicelang;
import java.util.Map;
/**
* A variable expression that represents binding a variable to a name in an
* enviroment
*
* @author ben
*
*/
public class BindingDiceExpression implements IDiceExpression {
/**
* The expression being bound to a name
*/
private IDiceExpression expression;
/**
* The name to bind the expression to
*/
private String variableName;
/**
* Create a new dice expression binder from two expressions and an
* enviroment
*
* @param left
* The left side expression to get a name from. Must be a
* ReferenceDiceExpression
* @param right
* The right side to bind to the name
* @param env
* The enviroment to bind into
*/
public BindingDiceExpression(IDiceExpression left,
IDiceExpression right, Map<String, IDiceExpression> env) {
if (!(left instanceof ReferenceDiceExpression)) {
throw new UnsupportedOperationException(
"Binding to non-references is unsupported."
+ " Problematic expression is " + left);
} else {
String varName = ((ReferenceDiceExpression) left).getName();
initialize(varName, right, env);
}
}
/**
* Create a new dice expression binder
*
* @param name
* The name of the variable to bind
* @param exp
* The expression to bind to the variable
* @param env
* The enviroment to bind it in
*/
public BindingDiceExpression(String name, IDiceExpression exp,
Map<String, IDiceExpression> env) {
initialize(name, exp, env);
}
private void initialize(String name, IDiceExpression exp,
Map<String, IDiceExpression> env) {
this.variableName = name;
this.expression = exp;
env.put(name, exp);
}
/*
* (non-Javadoc)
*
* @see bjc.utils.dice.IDiceExpression#roll()
*/
@Override
public int roll() {
return expression.roll();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "assign[n=" + variableName + ", exp="
+ expression.toString() + "]";
}
}
|