diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-10-27 21:56:18 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-10-27 22:12:47 -0400 |
| commit | e7413128ff4e376997de6e94e4bea5eca14811ef (patch) | |
| tree | 0749e270fdb754d04dc223abd95d47436508047f /dice-lang/src/bjc/dicelang/BindingDiceExpression.java | |
| parent | e13a6981bd278c2cfc3b5ecb2517367b117f7a52 (diff) | |
Moved examples
Diffstat (limited to 'dice-lang/src/bjc/dicelang/BindingDiceExpression.java')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/BindingDiceExpression.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/BindingDiceExpression.java b/dice-lang/src/bjc/dicelang/BindingDiceExpression.java new file mode 100644 index 0000000..6a030e3 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/BindingDiceExpression.java @@ -0,0 +1,93 @@ +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 enviroment + * The enviroment to bind into + */ + public BindingDiceExpression(IDiceExpression left, + IDiceExpression right, + Map<String, IDiceExpression> enviroment) { + if (!(left instanceof ReferenceDiceExpression)) { + throw new UnsupportedOperationException( + "Error: Binding an expression to something that is not a variable reference," + + " or array thereof. is unsupported." + + " Problematic expression is " + left); + } + + String varName = ((ReferenceDiceExpression) left).getName(); + + initialize(varName, right, enviroment); + } + + /** + * Create a new dice expression binder + * + * @param name + * The name of the variable to bind + * @param expression + * The expression to bind to the variable + * @param enviroment + * The enviroment to bind it in + */ + public BindingDiceExpression(String name, IDiceExpression expression, + Map<String, IDiceExpression> enviroment) { + initialize(name, expression, enviroment); + } + + private void initialize(String name, IDiceExpression expr, + Map<String, IDiceExpression> enviroment) { + this.variableName = name; + this.expression = expr; + + enviroment.put(name, expr); + } + + /* + * (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() + "]"; + } +} |
