leo/compiler/src/compiler.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

use crate::{ast, Program, ResolvedProgram};
2020-04-25 11:47:10 +03:00
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Field, PrimeField},
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem},
};
use from_pest::FromPest;
2020-04-28 00:36:05 +03:00
use std::{fs, marker::PhantomData, path::PathBuf};
2020-04-25 11:47:10 +03:00
#[derive(Clone)]
2020-04-25 11:47:10 +03:00
pub struct Compiler<F: Field + PrimeField> {
main_file_path: PathBuf,
_engine: PhantomData<F>,
}
impl<F: Field + PrimeField> Compiler<F> {
pub fn init(main_file_path: PathBuf) -> Self {
2020-04-28 00:36:05 +03:00
Self {
main_file_path,
_engine: PhantomData,
}
2020-04-25 11:47:10 +03:00
}
}
impl<F: Field + PrimeField> ConstraintSynthesizer<F> for Compiler<F> {
fn generate_constraints<CS: ConstraintSystem<F>>(
self,
cs: &mut CS,
) -> Result<(), SynthesisError> {
// Read in the main file as string
let unparsed_file = fs::read_to_string(&self.main_file_path).expect("cannot read file");
// Parse the file using leo.pest
let mut file = ast::parse(&unparsed_file).expect("unsuccessful parse");
// Build the abstract syntax tree
let syntax_tree = ast::File::from_pest(&mut file).expect("infallible");
log::debug!("{:#?}", syntax_tree);
2020-04-25 11:47:10 +03:00
let program = Program::<'_, F>::from(syntax_tree);
log::info!(" compiled: {:#?}", program);
2020-04-25 11:47:10 +03:00
let program = program.name("simple".into());
ResolvedProgram::generate_constraints(cs, program);
2020-04-25 11:47:10 +03:00
Ok(())
}
2020-04-28 00:36:05 +03:00
}