reduce complexity
This commit is contained in:
parent
56ef08597a
commit
c9a36ddcf5
3 changed files with 33 additions and 53 deletions
|
@ -151,8 +151,7 @@ pub enum Statement<T: Field> {
|
||||||
impl<T: Field> Statement<T> {
|
impl<T: Field> Statement<T> {
|
||||||
pub fn is_flattened(&self) -> bool {
|
pub fn is_flattened(&self) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
Statement::Definition(_, ref x) | Statement::MultipleDefinition(_, ref x) => x.is_flattened(),
|
Statement::Definition(_, ref x) | Statement::MultipleDefinition(_, ref x) | Statement::Return(ref x) => x.is_flattened(),
|
||||||
Statement::Return(ref x) => x.is_flattened(),
|
|
||||||
Statement::Compiler(..) => true,
|
Statement::Compiler(..) => true,
|
||||||
Statement::Condition(ref x, ref y) => {
|
Statement::Condition(ref x, ref y) => {
|
||||||
(x.is_linear() && y.is_flattened()) || (x.is_flattened() && y.is_linear())
|
(x.is_linear() && y.is_flattened()) || (x.is_flattened() && y.is_linear())
|
||||||
|
@ -291,7 +290,7 @@ impl<T: Field> Expression<T> {
|
||||||
Expression::FunctionCall(i.clone(), p.clone())
|
Expression::FunctionCall(i.clone(), p.clone())
|
||||||
},
|
},
|
||||||
Expression::List(ref exprs) => {
|
Expression::List(ref exprs) => {
|
||||||
Expression::List(exprs.iter().map(|e| e.apply_substitution(substitution)).collect::<Vec<_>>())
|
Expression::List(exprs.iter().map(|e| e.apply_substitution(substitution)).collect())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -372,7 +371,7 @@ impl<T: Field> Expression<T> {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Expression::List(ref exprs) => {
|
Expression::List(ref exprs) => {
|
||||||
exprs.into_iter().fold(true, |acc, x| acc && x.is_flattened())
|
exprs.into_iter().all(|x| x.is_flattened())
|
||||||
},
|
},
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
|
|
@ -433,7 +433,6 @@ impl Flattener {
|
||||||
FunctionCall(ref id, ref param_expressions) => {
|
FunctionCall(ref id, ref param_expressions) => {
|
||||||
for funct in functions_flattened {
|
for funct in functions_flattened {
|
||||||
if funct.id == *id && funct.arguments.len() == (*param_expressions).len() {
|
if funct.id == *id && funct.arguments.len() == (*param_expressions).len() {
|
||||||
|
|
||||||
// funct is now the called function
|
// funct is now the called function
|
||||||
|
|
||||||
// Idea: variables are given a prefix.
|
// Idea: variables are given a prefix.
|
||||||
|
@ -489,7 +488,7 @@ impl Flattener {
|
||||||
Statement::Return(x) => {
|
Statement::Return(x) => {
|
||||||
match x {
|
match x {
|
||||||
List(values) => {
|
List(values) => {
|
||||||
let new_values = values.into_iter().map(|x| x.apply_substitution(&replacement_map)).collect::<Vec<_>>();
|
let new_values: Vec<Expression<T>> = values.into_iter().map(|x| x.apply_substitution(&replacement_map)).collect();
|
||||||
if new_values.len() == 1 {
|
if new_values.len() == 1 {
|
||||||
return new_values[0].clone();
|
return new_values[0].clone();
|
||||||
}
|
}
|
||||||
|
@ -541,20 +540,15 @@ impl Flattener {
|
||||||
) {
|
) {
|
||||||
match *stat {
|
match *stat {
|
||||||
Statement::Return(ref exprs) => {
|
Statement::Return(ref exprs) => {
|
||||||
let exprs_subbed = exprs.clone().apply_substitution(&self.substitution);
|
let exprs_subbed = exprs.apply_substitution(&self.substitution);
|
||||||
let rhs = self.flatten_expression(
|
let rhs = self.flatten_expression(
|
||||||
functions_flattened,
|
functions_flattened,
|
||||||
arguments_flattened,
|
arguments_flattened,
|
||||||
statements_flattened,
|
statements_flattened,
|
||||||
exprs_subbed,
|
exprs_subbed,
|
||||||
);
|
);
|
||||||
|
|
||||||
match rhs.clone() {
|
statements_flattened.push(Statement::Return(rhs));
|
||||||
List(_) => {
|
|
||||||
statements_flattened.push(Statement::Return(rhs));
|
|
||||||
},
|
|
||||||
_ => panic!("")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Statement::Definition(ref id, ref expr) => {
|
Statement::Definition(ref id, ref expr) => {
|
||||||
let expr_subbed = expr.apply_substitution(&self.substitution);
|
let expr_subbed = expr.apply_substitution(&self.substitution);
|
||||||
|
@ -620,33 +614,28 @@ impl Flattener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ref s @ Statement::Compiler(..) => statements_flattened.push(s.clone()),
|
ref s @ Statement::Compiler(..) => statements_flattened.push(s.clone()),
|
||||||
Statement::MultipleDefinition(ref ids, ref e2) => {
|
Statement::MultipleDefinition(ref ids, ref rhs) => {
|
||||||
match *e2 {
|
let rhs_subbed = rhs.apply_substitution(&self.substitution);
|
||||||
FunctionCall(..) => {
|
let rhs_flattened = self.flatten_expression(
|
||||||
let expr_subbed = e2.apply_substitution(&self.substitution);
|
functions_flattened,
|
||||||
let rhs = self.flatten_expression(
|
arguments_flattened,
|
||||||
functions_flattened,
|
statements_flattened,
|
||||||
arguments_flattened,
|
rhs_subbed,
|
||||||
statements_flattened,
|
);
|
||||||
expr_subbed,
|
|
||||||
);
|
|
||||||
|
|
||||||
match rhs {
|
match rhs_flattened {
|
||||||
Expression::List(rhslist) => {
|
Expression::List(rhs_list) => {
|
||||||
for (i, id) in ids.into_iter().enumerate() {
|
for (i, id) in ids.into_iter().enumerate() {
|
||||||
let var = self.use_variable(&id);
|
let var = self.use_variable(&id);
|
||||||
// handle return of function call
|
// handle return of function call
|
||||||
let var_to_replace = self.get_latest_var_substitution(&id);
|
let var_to_replace = self.get_latest_var_substitution(&id);
|
||||||
if !(var == var_to_replace) && self.variables.contains(&var_to_replace) && !self.substitution.contains_key(&var_to_replace){
|
if !(var == var_to_replace) && self.variables.contains(&var_to_replace) && !self.substitution.contains_key(&var_to_replace){
|
||||||
self.substitution.insert(var_to_replace.clone().to_string(),var.clone());
|
self.substitution.insert(var_to_replace.clone().to_string(),var.clone());
|
||||||
}
|
}
|
||||||
statements_flattened.push(Statement::Definition(var, rhslist[i].clone()));
|
statements_flattened.push(Statement::Definition(var, rhs_list[i].clone()));
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => panic!("rhs should flatten to a List, semantics failed")
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => panic!("rhs should be a function call")
|
_ => panic!("rhs should flatten to a List, semantics failed")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -1119,29 +1119,21 @@ fn parse_statement<T: Field>(
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
(Token::Return, s1, p1) => match parse_expression_list(s1, p1) {
|
(Token::Return, s1, p1) => match parse_expression_list(s1, p1) {
|
||||||
Ok((Expression::List(xs), s2, p2)) => match next_token(&s2, &p2) {
|
Ok((e2, s2, p2)) => match next_token(&s2, &p2) {
|
||||||
(Token::InlineComment(_), ref s3, _) => {
|
(Token::InlineComment(_), ref s3, _) => {
|
||||||
assert_eq!(s3, "");
|
assert_eq!(s3, "");
|
||||||
Ok((Statement::Return(Expression::List(xs)), s2, p2))
|
Ok((Statement::Return(e2), s2, p2))
|
||||||
}
|
}
|
||||||
(Token::Unknown(ref t3), ref s3, _) if t3 == "" => {
|
(Token::Unknown(ref t3), ref s3, _) if t3 == "" => {
|
||||||
assert_eq!(s3, "");
|
assert_eq!(s3, "");
|
||||||
Ok((Statement::Return(Expression::List(xs)), s2, p2))
|
Ok((Statement::Return(e2), s2, p2))
|
||||||
}
|
}
|
||||||
(t4, _, p4) => Err(Error {
|
(t3, _, p3) => Err(Error {
|
||||||
expected: vec![
|
expected: vec![Token::Unknown("".to_string())],
|
||||||
Token::Add,
|
got: t3,
|
||||||
Token::Sub,
|
pos: p3,
|
||||||
Token::Pow,
|
|
||||||
Token::Mult,
|
|
||||||
Token::Div,
|
|
||||||
Token::Unknown("".to_string()),
|
|
||||||
],
|
|
||||||
got: t4,
|
|
||||||
pos: p4,
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
Ok(..) => unimplemented!(),
|
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
},
|
},
|
||||||
(Token::Def, _, p1) => Err(Error {
|
(Token::Def, _, p1) => Err(Error {
|
||||||
|
|
Loading…
Reference in a new issue