add function module

This commit is contained in:
collin 2020-07-08 00:00:12 -07:00
parent c8a93073c1
commit ba239921b9
4 changed files with 62 additions and 46 deletions

View File

@ -55,52 +55,6 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
Ok(result_value) Ok(result_value)
} }
fn enforce_function_call_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
file_scope: String,
function_scope: String,
expected_types: &Vec<Type>,
function: Box<Expression>,
arguments: Vec<Expression>,
span: Span,
) -> Result<ConstrainedValue<F, G>, 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( pub(crate) fn enforce_number_implicit(
expected_types: &Vec<Type>, expected_types: &Vec<Type>,
value: String, value: String,

View File

@ -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<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub fn enforce_function_call_expression<CS: ConstraintSystem<F>>(
&mut self,
cs: &mut CS,
file_scope: String,
function_scope: String,
expected_types: &Vec<Type>,
function: Box<Expression>,
arguments: Vec<Expression>,
span: Span,
) -> Result<ConstrainedValue<F, G>, 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))),
}
}
}

View File

@ -0,0 +1,2 @@
pub mod function;
pub use self::function::*;

View File

@ -15,6 +15,9 @@ pub use self::conditional::*;
pub mod expression; pub mod expression;
pub use self::expression::*; pub use self::expression::*;
pub mod function;
pub use self::function::*;
pub mod logical; pub mod logical;
pub use self::logical::*; pub use self::logical::*;