fix bug when redeclaring a variable 3 times in the same scope

This commit is contained in:
Mark 2023-10-27 14:35:10 +02:00
parent 1384222b59
commit 6f36ded38c
2 changed files with 4 additions and 1 deletions

View File

@ -59,11 +59,13 @@ pub type Info = info::Info<Local>;
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]
pub struct Local { pub struct Local {
vars: HashMap<String, (usize, usize)>, vars: HashMap<String, (usize, usize)>,
vars_count: usize,
} }
impl info::Local for Local { impl info::Local for Local {
type VariableIdentifier = String; type VariableIdentifier = String;
type VariableData = (usize, usize); type VariableData = (usize, usize);
fn init_var(&mut self, id: Self::VariableIdentifier, value: Self::VariableData) { fn init_var(&mut self, id: Self::VariableIdentifier, value: Self::VariableData) {
self.vars_count += 1;
self.vars.insert(id, value); self.vars.insert(id, value);
} }
fn get_var(&self, id: &Self::VariableIdentifier) -> Option<&Self::VariableData> { fn get_var(&self, id: &Self::VariableIdentifier) -> Option<&Self::VariableData> {

View File

@ -26,7 +26,7 @@ impl MersStatement for Variable {
self.var.clone(), self.var.clone(),
( (
info.scopes.len() - 1, info.scopes.len() - 1,
info.scopes.last().unwrap().vars.len(), info.scopes.last().unwrap().vars_count,
), ),
) )
} }
@ -35,6 +35,7 @@ impl MersStatement for Variable {
is_init: comp.is_init, is_init: comp.is_init,
is_ref: comp.is_init || self.is_ref, is_ref: comp.is_init || self.is_ref,
var: if let Some(v) = info.get_var(&self.var) { var: if let Some(v) = info.get_var(&self.var) {
eprintln!("Var '{}': {:?}", self.var, v);
*v *v
} else { } else {
return Err(format!("No variable named '{}' found!", self.var)); return Err(format!("No variable named '{}' found!", self.var));