This commit is contained in:
Protryon 2021-03-07 07:23:01 -08:00
parent 9156068801
commit 0f977707a9
4 changed files with 29 additions and 26 deletions

View File

@ -14,12 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use std::{cell::Cell};
use std::cell::Cell;
use leo_asg::*;
pub struct ConstantFolding<'a, 'b> {
program: &'b Program<'a>
program: &'b Program<'a>,
}
impl<'a, 'b> ExpressionVisitor<'a> for ConstantFolding<'a, 'b> {
@ -46,9 +46,7 @@ impl<'a, 'b> ProgramVisitor<'a> for ConstantFolding<'a, 'b> {}
impl<'a, 'b> AsgPass<'a> for ConstantFolding<'a, 'b> {
fn do_pass(asg: &Program<'a>) -> Result<(), FormattedError> {
let pass = ConstantFolding {
program: asg,
};
let pass = ConstantFolding { program: asg };
let mut director = VisitorDirector::new(pass);
director.visit_program(asg).ok();
Ok(())

View File

@ -16,10 +16,17 @@
//! Compiles a Leo program from a file path.
use crate::{CompilerOptions, GroupType, OutputBytes, OutputFile, constraints::{generate_constraints, generate_test_constraints}, errors::CompilerError};
use crate::{
constraints::{generate_constraints, generate_test_constraints},
errors::CompilerError,
CompilerOptions,
GroupType,
OutputBytes,
OutputFile,
};
use indexmap::IndexMap;
use leo_asg::{Asg, AsgPass, FormattedError};
pub use leo_asg::{new_context, AsgContext as Context, AsgContext};
use leo_asg::{Asg, AsgPass, FormattedError};
use leo_ast::{Input, LeoError, MainInput, Program};
use leo_input::LeoInputParser;
use leo_package::inputs::InputPairs;
@ -265,7 +272,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
///
pub fn compile_constraints<CS: ConstraintSystem<F>>(&self, cs: &mut CS) -> Result<OutputBytes, CompilerError> {
self.do_asg_passes().map_err(CompilerError::AsgPassError)?;
generate_constraints::<F, G, CS>(cs, &self.asg.as_ref().unwrap(), &self.program_input).map_err(|mut error| {
if let Some(path) = error.get_path().map(|x| x.to_string()) {
let content = match self.resolve_content(&path) {
@ -283,7 +290,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
///
pub fn compile_test_constraints(self, input_pairs: InputPairs) -> Result<(u32, u32), CompilerError> {
self.do_asg_passes().map_err(CompilerError::AsgPassError)?;
generate_test_constraints::<F, G>(
&self.asg.as_ref().unwrap(),
input_pairs,

View File

@ -27,7 +27,7 @@ use crate::{
FieldType,
GroupType,
};
use leo_asg::{ConstValue, Expression, Node, Span, expression::*};
use leo_asg::{expression::*, ConstValue, Expression, Node, Span};
use snarkvm_fields::PrimeField;
use snarkvm_gadgets::traits::utilities::boolean::Boolean;
@ -46,18 +46,18 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
ConstValue::Field(value) => ConstrainedValue::Field(FieldType::constant(value.to_string(), span)?),
ConstValue::Group(value) => ConstrainedValue::Group(G::constant(value, span)?),
ConstValue::Int(value) => ConstrainedValue::Integer(Integer::new(value)),
ConstValue::Tuple(values) =>
ConstrainedValue::Tuple(
values.iter()
.map(|x| self.enforce_const_value(cs, x, span))
.collect::<Result<Vec<_>, _>>()?
),
ConstValue::Array(values) =>
ConstrainedValue::Array(
values.iter()
.map(|x| self.enforce_const_value(cs, x, span))
.collect::<Result<Vec<_>, _>>()?
),
ConstValue::Tuple(values) => ConstrainedValue::Tuple(
values
.iter()
.map(|x| self.enforce_const_value(cs, x, span))
.collect::<Result<Vec<_>, _>>()?,
),
ConstValue::Array(values) => ConstrainedValue::Array(
values
.iter()
.map(|x| self.enforce_const_value(cs, x, span))
.collect::<Result<Vec<_>, _>>()?,
),
})
}
@ -75,9 +75,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
Expression::VariableRef(variable_ref) => self.evaluate_ref(variable_ref),
// Values
Expression::Constant(Constant { value, .. }) => {
self.enforce_const_value(cs, value, span)
}
Expression::Constant(Constant { value, .. }) => self.enforce_const_value(cs, value, span),
// Binary operations
Expression::Binary(BinaryExpression {

View File

@ -25,4 +25,4 @@ impl Default for CompilerOptions {
constant_folding_enabled: true,
}
}
}
}