Fixed stack overflow error caused by improper cleanup of block scoped variables
This commit is contained in:
@@ -91,6 +91,8 @@ impl<'a> VariableScope<'a> {
|
||||
pub fn scoped(parent: &'a VariableScope<'a>) -> Self {
|
||||
Self {
|
||||
parent: Option::Some(parent),
|
||||
temporary_vars: parent.temporary_vars.clone(),
|
||||
persistant_vars: parent.persistant_vars.clone(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
@@ -140,24 +142,32 @@ impl<'a> VariableScope<'a> {
|
||||
Ok(var_location)
|
||||
}
|
||||
|
||||
pub fn get_location_of(
|
||||
&mut self,
|
||||
var_name: impl Into<String>,
|
||||
) -> Result<VariableLocation, Error> {
|
||||
pub fn get_location_of(&self, var_name: impl Into<String>) -> Result<VariableLocation, Error> {
|
||||
let var_name = var_name.into();
|
||||
let var = self
|
||||
.var_lookup_table
|
||||
.get(var_name.as_str())
|
||||
.cloned()
|
||||
.ok_or(Error::UnknownVariable(var_name))?;
|
||||
|
||||
if let VariableLocation::Stack(inserted_at_offset) = var {
|
||||
Ok(VariableLocation::Stack(
|
||||
self.stack_offset - inserted_at_offset,
|
||||
))
|
||||
} else {
|
||||
Ok(var)
|
||||
// 1. Check this scope
|
||||
if let Some(var) = self.var_lookup_table.get(var_name.as_str()) {
|
||||
if let VariableLocation::Stack(inserted_at_offset) = var {
|
||||
// Return offset relative to CURRENT sp
|
||||
return Ok(VariableLocation::Stack(
|
||||
self.stack_offset - inserted_at_offset,
|
||||
));
|
||||
} else {
|
||||
return Ok(var.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Recursively check parent
|
||||
if let Some(parent) = self.parent {
|
||||
let loc = parent.get_location_of(var_name)?;
|
||||
|
||||
if let VariableLocation::Stack(parent_offset) = loc {
|
||||
return Ok(VariableLocation::Stack(parent_offset + self.stack_offset));
|
||||
}
|
||||
return Ok(loc);
|
||||
}
|
||||
|
||||
Err(Error::UnknownVariable(var_name))
|
||||
}
|
||||
|
||||
pub fn has_parent(&self) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user