Merge pull request #64 from chriseth/display_analyzed

Display for analyzed data.
This commit is contained in:
chriseth
2023-02-28 17:16:17 +01:00
committed by GitHub
3 changed files with 79 additions and 1 deletions

75
src/analyzer/display.rs Normal file
View File

@@ -0,0 +1,75 @@
use std::fmt::{Display, Formatter, Result};
use super::*;
impl Display for Identity {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self.kind {
IdentityKind::Polynomial => {
let expression = self.left.selector.as_ref().unwrap();
if let Expression::BinaryOperation(left, BinaryOperator::Sub, right) = expression {
write!(f, "{left} = {right};")
} else {
write!(f, "{expression} = 0;")
}
}
IdentityKind::Plookup => write!(f, "{} in {};", self.left, self.right),
IdentityKind::Permutation => write!(f, "{} is {};", self.left, self.right),
IdentityKind::Connect => write!(f, "{} connect {};", self.left, self.right),
}
}
}
impl Display for SelectedExpressions {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"{}{{ {} }}",
self.selector
.as_ref()
.map(|s| format!("{s} "))
.unwrap_or_default(),
format_expressions(&self.expressions)
)
}
}
impl Display for Expression {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Expression::Constant(name) => write!(f, "{name}"),
Expression::PolynomialReference(reference) => write!(f, "{reference}"),
Expression::PublicReference(name) => write!(f, "{name}"),
Expression::Number(value) => write!(f, "{value}"),
Expression::String(value) => write!(f, "\"{value}\""), // TODO quote?
Expression::Tuple(items) => write!(f, "({})", format_expressions(items)),
Expression::BinaryOperation(left, op, right) => write!(f, "({left} {op} {right})"),
Expression::UnaryOperation(op, exp) => write!(f, "{op}{exp}"),
Expression::FunctionCall(fun, args) => write!(f, "{fun}({})", format_expressions(args)),
Expression::LocalVariableReference(index) => write!(f, "${index}"),
}
}
}
fn format_expressions(expressions: &[Expression]) -> String {
expressions
.iter()
.map(|e| format!("{e}"))
.collect::<Vec<_>>()
.join(", ")
}
impl Display for PolynomialReference {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"{}{}{}",
self.name,
self.index
.as_ref()
.map(|s| format!("[{s}]"))
.unwrap_or_default(),
if self.next { "'" } else { "" }
)
}
}

View File

@@ -1,3 +1,4 @@
pub mod display;
pub mod pil_analyzer;
use std::collections::HashMap;

View File

@@ -148,7 +148,8 @@ where
}
IdentityKind::Plookup => self.process_plookup(identity),
_ => Ok(vec![]),
};
}
.map_err(|err| format!("No progress on {identity}:\n {err}"));
self.handle_eval_result(result);
}
if !self.progress {
@@ -543,6 +544,7 @@ where
e.coefficients
.iter()
.enumerate()
.filter(|(_, &c)| c != 0)
.map(|(i, c)| format!("{} * {c}", self.committed_names[i]))
.chain(e.constant_value().map(|v| format!("{v}")))
.collect::<Vec<_>>()