diff --git a/compiler/src/expression/expression.rs b/compiler/src/expression/expression.rs index 1afdd6bc43..588348edd5 100644 --- a/compiler/src/expression/expression.rs +++ b/compiler/src/expression/expression.rs @@ -4,7 +4,7 @@ use crate::{ arithmetic::*, errors::ExpressionError, logical::*, - program::{new_scope, ConstrainedProgram}, + program::ConstrainedProgram, relational::*, value::{boolean::input::new_bool_constant, ConstrainedValue}, Address, @@ -12,7 +12,7 @@ use crate::{ GroupType, Integer, }; -use leo_types::{Expression, Identifier, Span, Type}; +use leo_types::{Expression, Span, Type}; use snarkos_models::{ curves::{Field, PrimeField}, @@ -20,41 +20,6 @@ use snarkos_models::{ }; impl> ConstrainedProgram { - /// Enforce a variable expression by getting the resolved value - pub(crate) fn evaluate_identifier( - &mut self, - file_scope: String, - function_scope: String, - expected_types: &Vec, - unresolved_identifier: Identifier, - ) -> Result, ExpressionError> { - // Evaluate the identifier name in the current function scope - let variable_name = new_scope(function_scope.clone(), unresolved_identifier.to_string()); - let identifier_name = new_scope(file_scope, unresolved_identifier.to_string()); - - let mut result_value = if let Some(value) = self.get(&variable_name) { - // Reassigning variable to another variable - value.clone() - } else if let Some(value) = self.get(&identifier_name) { - // Check global scope (function and circuit names) - value.clone() - } else if let Some(value) = self.get(&unresolved_identifier.name) { - // Check imported file scope - value.clone() - } else if expected_types.contains(&Type::Address) { - // If we expect an address type, try to return an address - let address = Address::new(unresolved_identifier.name, unresolved_identifier.span)?; - - return Ok(ConstrainedValue::Address(address)); - } else { - return Err(ExpressionError::undefined_identifier(unresolved_identifier)); - }; - - result_value.resolve_type(expected_types, unresolved_identifier.span.clone())?; - - Ok(result_value) - } - pub(crate) fn enforce_number_implicit( expected_types: &Vec, value: String, diff --git a/compiler/src/expression/identifier/identifier.rs b/compiler/src/expression/identifier/identifier.rs new file mode 100644 index 0000000000..1949fe7aaf --- /dev/null +++ b/compiler/src/expression/identifier/identifier.rs @@ -0,0 +1,49 @@ +//! Methods to enforce constraints on expressions in a compiled Leo program. + +use crate::{ + errors::ExpressionError, + program::{new_scope, ConstrainedProgram}, + value::ConstrainedValue, + Address, + GroupType, +}; +use leo_types::{Identifier, Type}; + +use snarkos_models::curves::{Field, PrimeField}; + +impl> ConstrainedProgram { + /// Enforce a variable expression by getting the resolved value + pub fn evaluate_identifier( + &mut self, + file_scope: String, + function_scope: String, + expected_types: &Vec, + unresolved_identifier: Identifier, + ) -> Result, ExpressionError> { + // Evaluate the identifier name in the current function scope + let variable_name = new_scope(function_scope.clone(), unresolved_identifier.to_string()); + let identifier_name = new_scope(file_scope, unresolved_identifier.to_string()); + + let mut result_value = if let Some(value) = self.get(&variable_name) { + // Reassigning variable to another variable + value.clone() + } else if let Some(value) = self.get(&identifier_name) { + // Check global scope (function and circuit names) + value.clone() + } else if let Some(value) = self.get(&unresolved_identifier.name) { + // Check imported file scope + value.clone() + } else if expected_types.contains(&Type::Address) { + // If we expect an address type, try to return an address + let address = Address::new(unresolved_identifier.name, unresolved_identifier.span)?; + + return Ok(ConstrainedValue::Address(address)); + } else { + return Err(ExpressionError::undefined_identifier(unresolved_identifier)); + }; + + result_value.resolve_type(expected_types, unresolved_identifier.span.clone())?; + + Ok(result_value) + } +} diff --git a/compiler/src/expression/identifier/mod.rs b/compiler/src/expression/identifier/mod.rs new file mode 100644 index 0000000000..294cb98b19 --- /dev/null +++ b/compiler/src/expression/identifier/mod.rs @@ -0,0 +1,2 @@ +pub mod identifier; +pub use self::identifier::*; diff --git a/compiler/src/expression/mod.rs b/compiler/src/expression/mod.rs index 471c140aeb..b543d9bd09 100644 --- a/compiler/src/expression/mod.rs +++ b/compiler/src/expression/mod.rs @@ -18,6 +18,9 @@ pub use self::expression::*; pub mod function; pub use self::function::*; +pub mod identifier; +pub use self::identifier::*; + pub mod logical; pub use self::logical::*;