Use FnMut for callback.

This commit is contained in:
chriseth
2023-02-27 14:31:54 +01:00
parent b840c1db36
commit addcc54516
3 changed files with 23 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
use std::{env, fs, path::Path};
use powdr::compiler::no_callback;
fn main() {
if env::args().nth(1).unwrap() == "--asm" {
let file_name = env::args().nth(2).unwrap();
@@ -16,6 +18,6 @@ fn main() {
Err(err) => err.output_to_stderr(),
}
} else {
powdr::compiler::compile_pil(Path::new(&env::args().nth(1).unwrap()), None);
powdr::compiler::compile_pil(Path::new(&env::args().nth(1).unwrap()), no_callback());
}
}

View File

@@ -15,7 +15,7 @@ pub fn generate<'a>(
analyzed: &'a Analyzed,
degree: &ConstantNumberType,
constants: &[(&String, Vec<ConstantNumberType>)],
query_callback: Option<fn(&str) -> Option<ConstantNumberType>>,
query_callback: Option<impl FnMut(&str) -> Option<ConstantNumberType>>,
) -> Vec<(&'a String, Vec<ConstantNumberType>)> {
let polys: Vec<WitnessColumn> = analyzed
.committed_polys_in_source_order()
@@ -61,10 +61,13 @@ impl<'a> WitnessColumn<'a> {
}
}
struct Evaluator<'a> {
struct Evaluator<'a, QueryCallback>
where
QueryCallback: FnMut(&'a str) -> Option<ConstantNumberType>,
{
analyzed: &'a Analyzed,
constants: HashMap<&'a String, &'a Vec<ConstantNumberType>>,
query_callback: Option<fn(&str) -> Option<ConstantNumberType>>,
query_callback: Option<QueryCallback>,
/// Maps the committed polynomial names to their IDs internal to this component
/// and optional parameter and query string.
committed: HashMap<&'a String, &'a WitnessColumn<'a>>,
@@ -76,12 +79,15 @@ struct Evaluator<'a> {
next_row: usize,
}
impl<'a> Evaluator<'a> {
impl<'a, QueryCallback> Evaluator<'a, QueryCallback>
where
QueryCallback: FnMut(&str) -> Option<ConstantNumberType>,
{
pub fn new(
analyzed: &'a Analyzed,
constants: &'a [(&String, Vec<ConstantNumberType>)],
committed: &'a Vec<WitnessColumn<'a>>,
query_callback: Option<fn(&str) -> Option<ConstantNumberType>>,
query_callback: Option<QueryCallback>,
) -> Self {
Evaluator {
analyzed,
@@ -107,12 +113,12 @@ impl<'a> Evaluator<'a> {
let mut progress = false;
// TODO also use lookups, not only polynomial identities
if let Some(query_callback) = self.query_callback {
if self.query_callback.is_some() {
for column in self.committed.values() {
if self.next[column.id].is_none() && column.query.is_some() {
if let Some(value) =
query_callback(&self.interpolate_query(column.query.unwrap()))
{
let query = self.interpolate_query(column.query.unwrap());
let result = self.query_callback.as_mut().unwrap()(&query);
if let Some(value) = result {
self.next[column.id] = Some(value);
progress = true;
}

View File

@@ -6,11 +6,15 @@ use analyzer::ConstantNumberType;
use crate::{analyzer, commit_evaluator, constant_evaluator, json_exporter};
pub fn no_callback() -> Option<fn(&str) -> Option<ConstantNumberType>> {
None
}
/// Compiles a .pil file to its json form and also tries to generate
/// constants and committed polynomials.
pub fn compile_pil(
pil_file: &Path,
query_callback: Option<fn(&str) -> Option<ConstantNumberType>>,
query_callback: Option<impl FnMut(&str) -> Option<ConstantNumberType>>,
) {
let analyzed = analyzer::analyze(pil_file);
let (constants, degree) = constant_evaluator::generate(&analyzed);