implement Into for expressions to reduce verbosity
This commit is contained in:
parent
88f69dba68
commit
914b21daa3
3 changed files with 44 additions and 36 deletions
|
@ -206,6 +206,18 @@ pub enum AnnotatedExpression<T: Field> {
|
|||
FieldElement(FieldElementExpression<T>),
|
||||
}
|
||||
|
||||
impl<T: Field> From<BooleanExpression<T>> for AnnotatedExpression<T> {
|
||||
fn from(e: BooleanExpression<T>) -> AnnotatedExpression<T> {
|
||||
AnnotatedExpression::Boolean(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field> From<FieldElementExpression<T>> for AnnotatedExpression<T> {
|
||||
fn from(e: FieldElementExpression<T>) -> AnnotatedExpression<T> {
|
||||
AnnotatedExpression::FieldElement(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field> fmt::Display for AnnotatedExpression<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
|
@ -464,8 +476,8 @@ impl<T: Field> fmt::Debug for FieldElementExpression<T> {
|
|||
impl<T: Field> AnnotatedExpression<T> {
|
||||
pub fn apply_substitution(&self, substitution: &Substitution) -> AnnotatedExpression<T> {
|
||||
match self {
|
||||
AnnotatedExpression::Boolean(e) => AnnotatedExpression::Boolean(e.apply_substitution(substitution)),
|
||||
AnnotatedExpression::FieldElement(e) => AnnotatedExpression::FieldElement(e.apply_substitution(substitution)),
|
||||
AnnotatedExpression::Boolean(e) => e.apply_substitution(substitution).into(),
|
||||
AnnotatedExpression::FieldElement(e) => e.apply_substitution(substitution).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,30 +90,26 @@ impl Flattener {
|
|||
statements: vec![
|
||||
AnnotatedStatement::Definition(
|
||||
Variable::from("condition_as_field"),
|
||||
AnnotatedExpression::FieldElement(
|
||||
FieldElementExpression::FunctionCall(
|
||||
"_bool_to_field".to_string(),
|
||||
vec![
|
||||
AnnotatedExpression::FieldElement(
|
||||
FieldElementExpression::Identifier("condition".to_string()))
|
||||
]
|
||||
)
|
||||
)
|
||||
FieldElementExpression::FunctionCall(
|
||||
"_bool_to_field".to_string(),
|
||||
vec![
|
||||
AnnotatedExpression::FieldElement(
|
||||
FieldElementExpression::Identifier("condition".to_string()))
|
||||
]
|
||||
).into()
|
||||
),
|
||||
AnnotatedStatement::Return(
|
||||
vec![
|
||||
AnnotatedExpression::FieldElement(
|
||||
FieldElementExpression::Add(
|
||||
box FieldElementExpression::Mult(
|
||||
box FieldElementExpression::Identifier("condition_as_field".to_string()),
|
||||
box FieldElementExpression::Identifier("consequence".to_string()),
|
||||
),
|
||||
box FieldElementExpression::Mult(
|
||||
box FieldElementExpression::Identifier("condition_as_field".to_string()),
|
||||
box FieldElementExpression::Identifier("alternative".to_string())
|
||||
)
|
||||
FieldElementExpression::Add(
|
||||
box FieldElementExpression::Mult(
|
||||
box FieldElementExpression::Identifier("condition_as_field".to_string()),
|
||||
box FieldElementExpression::Identifier("consequence".to_string()),
|
||||
),
|
||||
box FieldElementExpression::Mult(
|
||||
box FieldElementExpression::Identifier("condition_as_field".to_string()),
|
||||
box FieldElementExpression::Identifier("alternative".to_string())
|
||||
)
|
||||
)
|
||||
).into()
|
||||
]
|
||||
)
|
||||
],
|
||||
|
@ -616,7 +612,7 @@ impl Flattener {
|
|||
statements_flattened,
|
||||
&"_if_else_field".to_string(),
|
||||
1,
|
||||
&vec![AnnotatedExpression::Boolean(cond_true), AnnotatedExpression::FieldElement(consequent), AnnotatedExpression::FieldElement(alternative)],
|
||||
&vec![cond_true.into(), consequent.into(), alternative.into()],
|
||||
).expressions[0].clone()
|
||||
},
|
||||
FieldElementExpression::FunctionCall(ref id, ref param_expressions) => {
|
||||
|
|
|
@ -236,8 +236,8 @@ impl Checker {
|
|||
// check that `id` is defined in the scope
|
||||
match self.scope.iter().find(|v| v.id.id == variable) {
|
||||
Some(v) => match v.clone().id._type {
|
||||
Type::Boolean => Ok(AnnotatedExpression::Boolean(BooleanExpression::Identifier(variable))),
|
||||
Type::FieldElement => Ok(AnnotatedExpression::FieldElement(FieldElementExpression::Identifier(variable))),
|
||||
Type::Boolean => Ok(BooleanExpression::Identifier(variable).into()),
|
||||
Type::FieldElement => Ok(FieldElementExpression::Identifier(variable).into()),
|
||||
},
|
||||
None => Err(Error { message: format!("{} is undefined", variable.to_string()) }),
|
||||
}
|
||||
|
@ -248,7 +248,7 @@ impl Checker {
|
|||
|
||||
match (e1_checked, e2_checked) {
|
||||
(AnnotatedExpression::FieldElement(e1), AnnotatedExpression::FieldElement(e2)) => {
|
||||
Ok(AnnotatedExpression::FieldElement(FieldElementExpression::Add(box e1, box e2)))
|
||||
Ok(FieldElementExpression::Add(box e1, box e2).into())
|
||||
}
|
||||
(t1, t2) => Err(Error { message: format!("Expected only field elements, found {:?}, {:?}", t1.get_type(), t2.get_type()) }),
|
||||
}
|
||||
|
@ -259,7 +259,7 @@ impl Checker {
|
|||
|
||||
match (e1_checked, e2_checked) {
|
||||
(AnnotatedExpression::FieldElement(e1), AnnotatedExpression::FieldElement(e2)) => {
|
||||
Ok(AnnotatedExpression::FieldElement(FieldElementExpression::Sub(box e1, box e2)))
|
||||
Ok(FieldElementExpression::Sub(box e1, box e2).into())
|
||||
}
|
||||
(t1, t2) => Err(Error { message: format!("Expected only field elements, found {:?}, {:?}", t1.get_type(), t2.get_type()) }),
|
||||
}
|
||||
|
@ -270,7 +270,7 @@ impl Checker {
|
|||
|
||||
match (e1_checked, e2_checked) {
|
||||
(AnnotatedExpression::FieldElement(e1), AnnotatedExpression::FieldElement(e2)) => {
|
||||
Ok(AnnotatedExpression::FieldElement(FieldElementExpression::Mult(box e1, box e2)))
|
||||
Ok(FieldElementExpression::Mult(box e1, box e2).into())
|
||||
}
|
||||
(t1, t2) => Err(Error { message: format!("Expected only field elements, found {:?}, {:?}", t1.get_type(), t2.get_type()) }),
|
||||
}
|
||||
|
@ -281,7 +281,7 @@ impl Checker {
|
|||
|
||||
match (e1_checked, e2_checked) {
|
||||
(AnnotatedExpression::FieldElement(e1), AnnotatedExpression::FieldElement(e2)) => {
|
||||
Ok(AnnotatedExpression::FieldElement(FieldElementExpression::Div(box e1, box e2)))
|
||||
Ok(FieldElementExpression::Div(box e1, box e2).into())
|
||||
}
|
||||
(t1, t2) => Err(Error { message: format!("Expected only field elements, found {:?}, {:?}", t1.get_type(), t2.get_type()) }),
|
||||
}
|
||||
|
@ -304,12 +304,12 @@ impl Checker {
|
|||
|
||||
match (condition_checked, consequence_checked, alternative_checked) {
|
||||
(condition, AnnotatedExpression::FieldElement(consequence), AnnotatedExpression::FieldElement(alternative)) => {
|
||||
Ok(AnnotatedExpression::FieldElement(FieldElementExpression::IfElse(box condition, box consequence, box alternative)))
|
||||
Ok(FieldElementExpression::IfElse(box condition, box consequence, box alternative).into())
|
||||
},
|
||||
_ => panic!("id else only for bool fe fe")
|
||||
}
|
||||
},
|
||||
Expression::Number(n) => Ok(AnnotatedExpression::FieldElement(FieldElementExpression::Number(n))),
|
||||
Expression::Number(n) => Ok(FieldElementExpression::Number(n).into()),
|
||||
Expression::FunctionCall(ref fun_id, ref arguments) => {
|
||||
// check the arguments
|
||||
let mut arguments_checked = vec![];
|
||||
|
@ -330,7 +330,7 @@ impl Checker {
|
|||
// the return count has to be 1
|
||||
if f.signature.outputs.len() == 1 {
|
||||
match f.signature.outputs[0] {
|
||||
Type::FieldElement => return Ok(AnnotatedExpression::FieldElement(FieldElementExpression::FunctionCall(f.id, arguments_checked))),
|
||||
Type::FieldElement => return Ok(FieldElementExpression::FunctionCall(f.id, arguments_checked).into()),
|
||||
_ => panic!("cannot return booleans")
|
||||
}
|
||||
}
|
||||
|
@ -448,7 +448,7 @@ mod tests {
|
|||
Ok(
|
||||
AnnotatedStatement::Definition(
|
||||
Variable::from("a"),
|
||||
AnnotatedExpression::FieldElement(FieldElementExpression::Identifier(String::from("b")))
|
||||
FieldElementExpression::Identifier(String::from("b")).into()
|
||||
)
|
||||
)
|
||||
);
|
||||
|
@ -646,7 +646,7 @@ mod tests {
|
|||
|
||||
for_statements_checked.push(AnnotatedStatement::Definition(
|
||||
Variable::from("a"),
|
||||
AnnotatedExpression::FieldElement(FieldElementExpression::Identifier(String::from("i")))
|
||||
FieldElementExpression::Identifier(String::from("i")).into()
|
||||
));
|
||||
|
||||
foo_statements_checked.push(AnnotatedStatement::For(
|
||||
|
@ -921,10 +921,10 @@ mod tests {
|
|||
AnnotatedExpressionList::FunctionCall("foo".to_string(), vec![], vec![Type::FieldElement, Type::FieldElement])
|
||||
),
|
||||
AnnotatedStatement::Return(vec![
|
||||
AnnotatedExpression::FieldElement(FieldElementExpression::Add(
|
||||
FieldElementExpression::Add(
|
||||
box FieldElementExpression::Identifier("a".to_string()),
|
||||
box FieldElementExpression::Identifier("b".to_string())
|
||||
))]
|
||||
).into()]
|
||||
)
|
||||
];
|
||||
|
||||
|
|
Loading…
Reference in a new issue