Don’t append variable ID if there is only one

This commit is contained in:
Patrick Lühne 2020-05-22 19:43:41 +02:00
parent 499fa0c667
commit 9b7895a032
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
2 changed files with 31 additions and 4 deletions

View File

@ -320,7 +320,11 @@ impl foliage::flavor::VariableDeclaration for VariableDeclaration
None => unreachable!("all variable IDs should be assigned at this point"), 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),
}
}, },
} }
} }

View File

@ -10,8 +10,8 @@ impl IDs
{ {
Self Self
{ {
program_variable_id: 0, program_variable_id: 1,
integer_variable_id: 0, 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) pub(crate) fn autoname_variables(formula: &mut crate::Formula)
{ {
// TODO: refactor, this is a bit hacky
reset_variable_names_in_formula(formula); 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);
} }