From c9a36ddcf5a6cc2872315e9170b07913534e77c7 Mon Sep 17 00:00:00 2001 From: schaeff Date: Mon, 22 Jan 2018 23:49:41 +0100 Subject: [PATCH] reduce complexity --- src/absy.rs | 7 +++---- src/flatten.rs | 57 ++++++++++++++++++++------------------------------ src/parser.rs | 22 +++++++------------ 3 files changed, 33 insertions(+), 53 deletions(-) diff --git a/src/absy.rs b/src/absy.rs index b39c4fd8..5ac994bb 100644 --- a/src/absy.rs +++ b/src/absy.rs @@ -151,8 +151,7 @@ pub enum Statement { impl Statement { pub fn is_flattened(&self) -> bool { match *self { - Statement::Definition(_, ref x) | Statement::MultipleDefinition(_, ref x) => x.is_flattened(), - Statement::Return(ref x) => x.is_flattened(), + Statement::Definition(_, ref x) | Statement::MultipleDefinition(_, ref x) | Statement::Return(ref x) => x.is_flattened(), Statement::Compiler(..) => true, Statement::Condition(ref x, ref y) => { (x.is_linear() && y.is_flattened()) || (x.is_flattened() && y.is_linear()) @@ -291,7 +290,7 @@ impl Expression { Expression::FunctionCall(i.clone(), p.clone()) }, Expression::List(ref exprs) => { - Expression::List(exprs.iter().map(|e| e.apply_substitution(substitution)).collect::>()) + Expression::List(exprs.iter().map(|e| e.apply_substitution(substitution)).collect()) } } } @@ -372,7 +371,7 @@ impl Expression { } }, Expression::List(ref exprs) => { - exprs.into_iter().fold(true, |acc, x| acc && x.is_flattened()) + exprs.into_iter().all(|x| x.is_flattened()) }, _ => false, } diff --git a/src/flatten.rs b/src/flatten.rs index 6775b00c..dd2b977a 100644 --- a/src/flatten.rs +++ b/src/flatten.rs @@ -433,7 +433,6 @@ impl Flattener { FunctionCall(ref id, ref param_expressions) => { for funct in functions_flattened { if funct.id == *id && funct.arguments.len() == (*param_expressions).len() { - // funct is now the called function // Idea: variables are given a prefix. @@ -489,7 +488,7 @@ impl Flattener { Statement::Return(x) => { match x { List(values) => { - let new_values = values.into_iter().map(|x| x.apply_substitution(&replacement_map)).collect::>(); + let new_values: Vec> = values.into_iter().map(|x| x.apply_substitution(&replacement_map)).collect(); if new_values.len() == 1 { return new_values[0].clone(); } @@ -541,20 +540,15 @@ impl Flattener { ) { match *stat { 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( functions_flattened, arguments_flattened, statements_flattened, exprs_subbed, ); - - match rhs.clone() { - List(_) => { - statements_flattened.push(Statement::Return(rhs)); - }, - _ => panic!("") - } + + statements_flattened.push(Statement::Return(rhs)); } Statement::Definition(ref id, ref expr) => { let expr_subbed = expr.apply_substitution(&self.substitution); @@ -620,33 +614,28 @@ impl Flattener { } } ref s @ Statement::Compiler(..) => statements_flattened.push(s.clone()), - Statement::MultipleDefinition(ref ids, ref e2) => { - match *e2 { - FunctionCall(..) => { - let expr_subbed = e2.apply_substitution(&self.substitution); - let rhs = self.flatten_expression( - functions_flattened, - arguments_flattened, - statements_flattened, - expr_subbed, - ); + Statement::MultipleDefinition(ref ids, ref rhs) => { + let rhs_subbed = rhs.apply_substitution(&self.substitution); + let rhs_flattened = self.flatten_expression( + functions_flattened, + arguments_flattened, + statements_flattened, + rhs_subbed, + ); - match rhs { - Expression::List(rhslist) => { - for (i, id) in ids.into_iter().enumerate() { - let var = self.use_variable(&id); - // handle return of function call - 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){ - self.substitution.insert(var_to_replace.clone().to_string(),var.clone()); - } - statements_flattened.push(Statement::Definition(var, rhslist[i].clone())); - } - }, - _ => panic!("rhs should flatten to a List, semantics failed") + match rhs_flattened { + Expression::List(rhs_list) => { + for (i, id) in ids.into_iter().enumerate() { + let var = self.use_variable(&id); + // handle return of function call + 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){ + self.substitution.insert(var_to_replace.clone().to_string(),var.clone()); + } + statements_flattened.push(Statement::Definition(var, rhs_list[i].clone())); } }, - _ => panic!("rhs should be a function call") + _ => panic!("rhs should flatten to a List, semantics failed") } }, } diff --git a/src/parser.rs b/src/parser.rs index 9726fd9d..7f1ac092 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1119,29 +1119,21 @@ fn parse_statement( }), }, (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, _) => { 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 == "" => { assert_eq!(s3, ""); - Ok((Statement::Return(Expression::List(xs)), s2, p2)) + Ok((Statement::Return(e2), s2, p2)) } - (t4, _, p4) => Err(Error { - expected: vec![ - Token::Add, - Token::Sub, - Token::Pow, - Token::Mult, - Token::Div, - Token::Unknown("".to_string()), - ], - got: t4, - pos: p4, + (t3, _, p3) => Err(Error { + expected: vec![Token::Unknown("".to_string())], + got: t3, + pos: p3, }), }, - Ok(..) => unimplemented!(), Err(err) => Err(err), }, (Token::Def, _, p1) => Err(Error {