diff options
| author | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-02-27 10:08:50 -0500 |
|---|---|---|
| committer | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-02-27 10:08:50 -0500 |
| commit | 79ee129fc0d36ad10bceb942262f2842419c030c (patch) | |
| tree | d1298fdb8b81726f4b9012d7a29c3029a55a3aa7 /dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java | |
| parent | c50a0744269ce22604c0604cc69e6d5e5ce8a3fc (diff) | |
Pacakge reorganization
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java b/dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java new file mode 100644 index 0000000..bb7ce02 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/v1/ReferenceDiceExpression.java @@ -0,0 +1,66 @@ +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)"; + } +} |
