From ba239921b9b81ff1644c14b90185fbd9508cd53a Mon Sep 17 00:00:00 2001 From: collin Date: Wed, 8 Jul 2020 00:00:12 -0700 Subject: [PATCH] add function module --- compiler/src/expression/expression.rs | 46 ---------------- compiler/src/expression/function/function.rs | 57 ++++++++++++++++++++ compiler/src/expression/function/mod.rs | 2 + compiler/src/expression/mod.rs | 3 ++ 4 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 compiler/src/expression/function/function.rs create mode 100644 compiler/src/expression/function/mod.rs diff --git a/compiler/src/expression/expression.rs b/compiler/src/expression/expression.rs index 0db31dd5bb..1afdd6bc43 100644 --- a/compiler/src/expression/expression.rs +++ b/compiler/src/expression/expression.rs @@ -55,52 +55,6 @@ impl> ConstrainedProgram { Ok(result_value) } - fn enforce_function_call_expression>( - &mut self, - cs: &mut CS, - file_scope: String, - function_scope: String, - expected_types: &Vec, - function: Box, - arguments: Vec, - span: Span, - ) -> Result, ExpressionError> { - let function_value = self.enforce_expression( - cs, - file_scope.clone(), - function_scope.clone(), - expected_types, - *function.clone(), - )?; - - let (outer_scope, function_call) = function_value.extract_function(file_scope.clone(), span.clone())?; - - let name_unique = format!( - "function call {} {}:{}", - function_call.get_name(), - span.line, - span.start, - ); - - match self.enforce_function( - &mut cs.ns(|| name_unique), - outer_scope, - function_scope, - function_call, - arguments, - ) { - Ok(ConstrainedValue::Return(return_values)) => { - if return_values.len() == 1 { - Ok(return_values[0].clone()) - } else { - Ok(ConstrainedValue::Return(return_values)) - } - } - Ok(_) => Err(ExpressionError::function_no_return(function.to_string(), span)), - Err(error) => Err(ExpressionError::from(Box::new(error))), - } - } - pub(crate) fn enforce_number_implicit( expected_types: &Vec, value: String, diff --git a/compiler/src/expression/function/function.rs b/compiler/src/expression/function/function.rs new file mode 100644 index 0000000000..266f935110 --- /dev/null +++ b/compiler/src/expression/function/function.rs @@ -0,0 +1,57 @@ +//! Methods to enforce constraints on expressions in a compiled Leo program. + +use crate::{errors::ExpressionError, program::ConstrainedProgram, value::ConstrainedValue, GroupType}; +use leo_types::{Expression, Span, Type}; + +use snarkos_models::{ + curves::{Field, PrimeField}, + gadgets::r1cs::ConstraintSystem, +}; + +impl> ConstrainedProgram { + pub fn enforce_function_call_expression>( + &mut self, + cs: &mut CS, + file_scope: String, + function_scope: String, + expected_types: &Vec, + function: Box, + arguments: Vec, + span: Span, + ) -> Result, ExpressionError> { + let function_value = self.enforce_expression( + cs, + file_scope.clone(), + function_scope.clone(), + expected_types, + *function.clone(), + )?; + + let (outer_scope, function_call) = function_value.extract_function(file_scope.clone(), span.clone())?; + + let name_unique = format!( + "function call {} {}:{}", + function_call.get_name(), + span.line, + span.start, + ); + + match self.enforce_function( + &mut cs.ns(|| name_unique), + outer_scope, + function_scope, + function_call, + arguments, + ) { + Ok(ConstrainedValue::Return(return_values)) => { + if return_values.len() == 1 { + Ok(return_values[0].clone()) + } else { + Ok(ConstrainedValue::Return(return_values)) + } + } + Ok(_) => Err(ExpressionError::function_no_return(function.to_string(), span)), + Err(error) => Err(ExpressionError::from(Box::new(error))), + } + } +} diff --git a/compiler/src/expression/function/mod.rs b/compiler/src/expression/function/mod.rs new file mode 100644 index 0000000000..80945cf378 --- /dev/null +++ b/compiler/src/expression/function/mod.rs @@ -0,0 +1,2 @@ +pub mod function; +pub use self::function::*; diff --git a/compiler/src/expression/mod.rs b/compiler/src/expression/mod.rs index f83eaa3b05..471c140aeb 100644 --- a/compiler/src/expression/mod.rs +++ b/compiler/src/expression/mod.rs @@ -15,6 +15,9 @@ pub use self::conditional::*; pub mod expression; pub use self::expression::*; +pub mod function; +pub use self::function::*; + pub mod logical; pub use self::logical::*;