Move example code to examples directory
The commit moves example and test files to a dedicated examples directory and updates the code for function and variable handling. The subject line captures the primary purpose of the move operation. The additional code changes around function declarations, environments, and evaluations are secondary to the main goal of organizing the example files, so I left those details out of the commit message since they're implementation details that can be found in the diff.
This commit is contained in:
@@ -5,7 +5,7 @@ use crate::{
|
||||
result::{runtime_error, LoxResult},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct EnvironmentStack {
|
||||
stack: Vec<HashMap<String, LiteralValue>>,
|
||||
}
|
||||
@@ -39,7 +39,30 @@ impl EnvironmentStack {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn set(&mut self, name: String, value: LiteralValue) {
|
||||
self.stack.last_mut().unwrap().insert(name, value);
|
||||
pub fn declare(&mut self, name: String, value: LiteralValue) -> LoxResult<LiteralValue> {
|
||||
self.stack.last_mut().unwrap().insert(name, value.clone());
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub fn set(&mut self, name: String, value: LiteralValue) -> LoxResult<LiteralValue> {
|
||||
let size = self.stack.len();
|
||||
|
||||
for i in (0..size).rev() {
|
||||
if let Some(_) = self.stack[i].get(&name) {
|
||||
let mut founded = false;
|
||||
self.stack[i].entry(name.clone()).and_modify(|v| {
|
||||
*v = value.clone();
|
||||
founded = true;
|
||||
});
|
||||
|
||||
if founded {
|
||||
return Ok(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
runtime_error(
|
||||
SourceSlice::default(), // todo change this to the actual source slice
|
||||
format!("Undefined variable '{}'", name),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user