mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-29 22:36:05 +03:00
add function module
This commit is contained in:
parent
c8a93073c1
commit
ba239921b9
@ -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,
|
||||||
|
57
compiler/src/expression/function/function.rs
Normal file
57
compiler/src/expression/function/function.rs
Normal 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))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
compiler/src/expression/function/mod.rs
Normal file
2
compiler/src/expression/function/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod function;
|
||||||
|
pub use self::function::*;
|
@ -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::*;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user