From 9b7895a0324ff185573bd46f79f1cb7f267f648e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 22 May 2020 19:43:41 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20append=20variable=20ID=20if=20t?= =?UTF-8?q?here=20is=20only=20one?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ast.rs | 6 +++++- src/utils/autoname_variables.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index e1e69e5..fb5069d 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -320,7 +320,11 @@ impl foliage::flavor::VariableDeclaration for VariableDeclaration None => unreachable!("all variable IDs should be assigned at this point"), }; - write!(formatter, "{}{}", variable_name_prefix, variable_id + 1) + match variable_id + { + 0 => write!(formatter, "{}", variable_name_prefix), + _ => write!(formatter, "{}{}", variable_name_prefix, variable_id), + } }, } } diff --git a/src/utils/autoname_variables.rs b/src/utils/autoname_variables.rs index e3ce754..9672c24 100644 --- a/src/utils/autoname_variables.rs +++ b/src/utils/autoname_variables.rs @@ -10,8 +10,8 @@ impl IDs { Self { - program_variable_id: 0, - integer_variable_id: 0, + program_variable_id: 1, + integer_variable_id: 1, } } } @@ -218,6 +218,29 @@ fn set_variable_names_in_formula(formula: &mut crate::Formula, ids: &mut IDs) pub(crate) fn autoname_variables(formula: &mut crate::Formula) { + // TODO: refactor, this is a bit hacky reset_variable_names_in_formula(formula); - set_variable_names_in_formula(formula, &mut IDs::new()); + + let mut ids = IDs::new(); + + set_variable_names_in_formula(formula, &mut ids); + + // If there only exists exactly one program variable (which incremented the ID from 1 to 2), + // give it the special ID 0 on the second run + ids.program_variable_id = match ids.program_variable_id + { + 2 => 0, + _ => 1, + }; + + // If there only exists exactly one integer variable (which incremented the ID from 1 to 2), + // give it the special ID 0 on the second run + ids.integer_variable_id = match ids.integer_variable_id + { + 2 => 0, + _ => 1, + }; + + reset_variable_names_in_formula(formula); + set_variable_names_in_formula(formula, &mut ids); }