Skip to content

Commit

Permalink
refactor: nuke a ton of clones
Browse files Browse the repository at this point in the history
We don't do that here anymore, we roll with references now B-)

Signed-off-by: Prajwal S N <[email protected]>
  • Loading branch information
snprajwal committed Dec 28, 2023
1 parent e29ffef commit baf6bfe
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 244 deletions.
28 changes: 14 additions & 14 deletions lost_compile/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,42 @@ impl Env {
}))
}

pub fn set(&mut self, name: String, value: Type) {
pub fn set(&mut self, name: &str, value: Type) {
debug!("Set {name} -> {value:?}");
self.values.insert(name, value);
self.values.insert(name.to_string(), value);
}

pub fn get(&self, name: String) -> Result<Type, Exception> {
pub fn get(&self, name: &str) -> Result<Type, Exception> {
debug!("Get {name}");
if let Some(value) = self.values.get(&name) {
if let Some(value) = self.values.get(name) {
return Ok(value.clone());
}
if let Some(parent) = &self.parent {
debug!("Get {name} from parent");
return parent.borrow().get(name);
}
Err(runtime_error(ErrorMsg::UndefinedVar, name))
Err(runtime_error(ErrorMsg::UndefinedVar, &name))
}

pub fn assign(&mut self, name: String, value: Type) -> Result<(), Exception> {
pub fn assign(&mut self, name: &str, value: Type) -> Result<(), Exception> {
debug!("Assign {name} -> {value:?})");
if self.values.contains_key(&name) {
if self.values.contains_key(name) {
return Ok(self.set(name, value));
}
if let Some(parent) = &mut self.parent {
debug!("Assign {name} in parent");
return parent.borrow_mut().assign(name, value);
}
Err(runtime_error(ErrorMsg::UndefinedVar, name))
Err(runtime_error(ErrorMsg::UndefinedVar, &name))
}

pub fn get_at_depth(&self, name: String, depth: usize) -> Result<Type, Exception> {
pub fn get_at_depth(&self, name: &str, depth: usize) -> Result<Type, Exception> {
debug!("Get {name} at depth {depth}");
if depth == 0 {
if let Some(value) = self.values.get(&name) {
if let Some(value) = self.values.get(name) {
return Ok(value.clone());
}
return Err(runtime_error(ErrorMsg::MisresolvedVar, name));
return Err(runtime_error(ErrorMsg::MisresolvedVar, &name));
}
self.parent
.as_ref()
Expand All @@ -74,16 +74,16 @@ impl Env {

pub fn assign_at_depth(
&mut self,
name: String,
name: &str,
value: Type,
depth: usize,
) -> Result<(), Exception> {
debug!("Set {name} -> {value} at depth {depth}");
if depth == 0 {
if self.values.contains_key(&name) {
if self.values.contains_key(name) {
return Ok(self.set(name, value));
}
return Err(runtime_error(ErrorMsg::MisresolvedVar, name));
return Err(runtime_error(ErrorMsg::MisresolvedVar, &name));
}
self.parent
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions lost_compile/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ impl Display for ErrorMsg {
}
}

pub fn runtime_error(msg: ErrorMsg, val: String) -> Exception {
pub fn runtime_error(msg: ErrorMsg, val: &str) -> Exception {
Exception::Error(format!("Runtime error: {} {}", msg, val))
}

pub fn resolution_error(msg: ErrorMsg, val: String) -> Exception {
pub fn resolution_error(msg: ErrorMsg, val: &str) -> Exception {
Exception::Error(
format!("Resolution error: {} {}", msg, val)
.trim()
Expand Down
Loading

0 comments on commit baf6bfe

Please sign in to comment.