summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/utils/dice/ReferenceDiceExpression.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 08:44:54 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-03-28 08:44:54 -0400
commit78d9c539e25f16fd15f06c2b2c48c0ad37a21540 (patch)
tree52d5db559d390c2fb83d4a4cd3bbfa4344c482e8 /dice-lang/src/bjc/utils/dice/ReferenceDiceExpression.java
parent1a020352bdcbb6b5279b703f93e42a1eb7071e36 (diff)
Imported dice stuff from general utils into dedicated project
Diffstat (limited to 'dice-lang/src/bjc/utils/dice/ReferenceDiceExpression.java')
-rw-r--r--dice-lang/src/bjc/utils/dice/ReferenceDiceExpression.java70
1 files changed, 70 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/utils/dice/ReferenceDiceExpression.java b/dice-lang/src/bjc/utils/dice/ReferenceDiceExpression.java
new file mode 100644
index 0000000..d38e0f9
--- /dev/null
+++ b/dice-lang/src/bjc/utils/dice/ReferenceDiceExpression.java
@@ -0,0 +1,70 @@
+package bjc.utils.dice;
+
+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> env;
+
+ /**
+ * The name of the bound variable
+ */
+ private String name;
+
+ /**
+ * Create a new reference dice expression referring to the given name
+ * in an enviroment
+ *
+ * @param name
+ * The name of the bound variable
+ * @param env
+ * The enviroment to resolve the variable against
+ */
+ public ReferenceDiceExpression(String name,
+ Map<String, IDiceExpression> env) {
+ this.name = name;
+ this.env = env;
+ }
+
+ /**
+ * Get the name of the referenced variable
+ *
+ * @return the name of the referenced variable
+ */
+ public String getName() {
+ return name;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see bjc.utils.dice.IDiceExpression#roll()
+ */
+ @Override
+ public int roll() {
+ return env.get(name).roll();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ if (env.containsKey(name)) {
+ return env.get(name).toString();
+ } else {
+ return name;
+ }
+ }
+}