diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-03 20:36:08 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-03 20:36:08 -0400 |
| commit | adea5713f3d6711885108e359813b4a62ffee98f (patch) | |
| tree | 1f30e9aa736d3e65a58bc3f7f195bd18f22cb309 /dice-lang/src/main/java/bjc/dicelang/ast/DiceASTDefinedChecker.java | |
| parent | 9658afb5b07d2b5a965dea322b0ad8fa3c16ce2d (diff) | |
Code maintenance and fixes
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/DiceASTDefinedChecker.java')
| -rw-r--r-- | dice-lang/src/main/java/bjc/dicelang/ast/DiceASTDefinedChecker.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTDefinedChecker.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTDefinedChecker.java new file mode 100644 index 0000000..247054a --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTDefinedChecker.java @@ -0,0 +1,58 @@ +package bjc.dicelang.ast; + +import java.util.Map; +import java.util.function.Consumer; + +import bjc.utils.data.IHolder; + +/** + * Check if the specified node references a particular variable + * + * @author ben + * + */ +public final class DiceASTDefinedChecker + implements Consumer<IDiceASTNode> { + /** + * This is true if the specified node references the set variable + */ + private IHolder<Boolean> referencesVariable; + + private Map<String, DiceASTExpression> enviroment; + + /** + * Create a new reference checker + * + * @param referencesVar + * The holder of whether the variable is referenced or not + * @param env + * The enviroment to check undefinedness against + */ + public DiceASTDefinedChecker(IHolder<Boolean> referencesVar, + Map<String, DiceASTExpression> env) { + this.referencesVariable = referencesVar; + this.enviroment = env; + } + + @Override + public void accept(IDiceASTNode astNode) { + referencesVariable.transform((bool) -> checkUndefined(astNode)); + } + + /** + * Check if a given AST node references an undefined variable + * + * @param astNode + * The node to check + * @return Whether or not the node directly the variable + */ + private boolean checkUndefined(IDiceASTNode astNode) { + if (astNode.getType() == DiceASTType.VARIABLE) { + VariableDiceNode node = (VariableDiceNode) astNode; + + return !enviroment.containsKey(node.getVariable()); + } else { + return false; + } + } +}
\ No newline at end of file |
