Evaluator/Environment
Add method to lookup the variable name via the witness index
use super::errors::RuntimeErrorKind;
use super::object::{Array, Object};
+use acir::native_types::Witness;
use noirc_frontend::hir::scope::{
ScopeForest as GenericScopeForest, ScopeTree as GenericScopeTree,
};
@@ -36,8 +37,31 @@ impl Environment {
pub fn get(&mut self, name: &String) -> Object {
let scope = self.0.current_scope_tree();
- scope.find_key(name).unwrap().clone()
+ scope.find(name).unwrap().clone()
}
+
+ // This method is somewhat of a hack, due to the fact that we do not map
+ // Witness indices to variable names.
+ pub fn find_with_value(&mut self, val: &Witness) -> Option<String> {
+ let mut found = None;
+ for scope in self.0.current_scope_tree().0.iter().rev() {
+ found = scope.0.iter().find_map(|(k, v)| match v {
+ Object::Null | Object::Array(_) | Object::Constants(_) | Object::Arithmetic(_) => {
+ None
+ }
+ Object::Integer(x) => {
+ // Integers are assumed to always be unit
+ (&x.witness == val).then(|| k)
+ }
+ Object::Linear(x) => (x.is_unit() && &x.witness == val).then(|| k),
+ });
+ if found.is_some() {
+ break;
+ }
+ }
+ found.cloned()
+ }
+
pub fn get_array(&mut self, name: &String) -> Result<Array, RuntimeErrorKind> {
let poly = self.get(name);
