From e7413128ff4e376997de6e94e4bea5eca14811ef Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 27 Oct 2016 21:56:18 -0400 Subject: Moved examples --- .../src/bjc/dicelang/ReferenceDiceExpression.java | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 dice-lang/src/bjc/dicelang/ReferenceDiceExpression.java (limited to 'dice-lang/src/bjc/dicelang/ReferenceDiceExpression.java') diff --git a/dice-lang/src/bjc/dicelang/ReferenceDiceExpression.java b/dice-lang/src/bjc/dicelang/ReferenceDiceExpression.java new file mode 100644 index 0000000..483545b --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ReferenceDiceExpression.java @@ -0,0 +1,76 @@ +package bjc.dicelang; + +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 enviroment; + + /** + * The name of the bound variable + */ + private String variableName; + + /** + * 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 env) { + this.variableName = name; + this.enviroment = env; + } + + /** + * Get the name of the referenced variable + * + * @return the name of the referenced variable + */ + public String getName() { + return variableName; + } + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ + @Override + public int roll() { + if (!enviroment.containsKey(variableName)) { + throw new UnsupportedOperationException( + "Attempted to reference undefined variable " + + variableName); + } + + return enviroment.get(variableName).roll(); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + if (enviroment.containsKey(variableName)) { + return enviroment.get(variableName).toString(); + } + + return variableName; + } +} \ No newline at end of file -- cgit v1.2.3