Skip to content

Commit 2766176

Browse files
committed
fix
1 parent 38460af commit 2766176

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

zenlang/src/vm/opcodes/vm_ret.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl VM {
1010
self.ret = self.stack.pop().unwrap();
1111
}
1212

13-
self.remove_scope();
13+
self.remove_environment();
1414

1515
if !self.call_stack.is_empty() {
1616
self.pc = self.call_stack.pop().unwrap();

zenlang/src/vm/opcodes/vm_vars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use alloc::string::*;
66

77
impl VM {
88
pub fn op_load_var(&mut self, name: &String) {
9-
if let Some(environ) = &self.environ {
9+
if let Some(environ) = self.environs.last() {
1010
let environ = &*environ.borrow();
1111
if let Some(value) = environ.get(name) {
1212
self.stack.push(value.clone());
@@ -43,7 +43,7 @@ impl VM {
4343
return;
4444
}
4545

46-
if let Some(environ) = &mut self.environ {
46+
if let Some(environ) = self.environs.last_mut() {
4747
let environ = &mut *environ.borrow_mut();
4848

4949
environ.create_if_doesnt_exist(name);

zenlang/src/vm/vm.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct VM {
1818
pub pc: u64,
1919
pub stack: Vec<Value>,
2020
pub call_stack: Vec<u64>,
21-
pub environ: Option<Rc<RefCell<Environment>>>,
21+
pub environs: Vec<Rc<RefCell<Environment>>>,
2222
pub error: String,
2323
pub ret: Value,
2424
pub platform: Option<Box<dyn Platform>>,
@@ -35,7 +35,7 @@ impl VM {
3535
pc: 0,
3636
stack: Vec::new(),
3737
call_stack: Vec::new(),
38-
environ: None,
38+
environs: Vec::new(),
3939
error: String::new(),
4040
ret: Value::Null(),
4141
platform: None,
@@ -78,7 +78,8 @@ impl VM {
7878
}
7979
}
8080

81-
self.environ = Some(Rc::new(RefCell::new(Environment::new())));
81+
self.environs
82+
.push(Rc::new(RefCell::new(Environment::new())));
8283

8384
if !self.error.is_empty() {
8485
self.halted = true;
@@ -149,21 +150,27 @@ impl VM {
149150

150151
pub(crate) fn add_environment(&mut self) {
151152
let new = Rc::new(RefCell::new(Environment::new()));
152-
if let Some(environ) = &self.environ {
153+
if let Some(environ) = self.environs.pop() {
153154
(&mut *new.borrow_mut()).parent = Some(environ.clone());
154155
}
155-
self.environ = Some(new);
156+
self.environs.push(new);
156157
}
157158

158-
pub(crate) fn remove_scope(&mut self) {
159-
if self.environ.is_none() {
160-
panic!("environ is None");
159+
pub(crate) fn remove_environment(&mut self) {
160+
if self.environs.len() == 0 {
161+
panic!("environs is empty");
161162
}
162163

163-
// Welcome to rust!
164-
self.environ = core::mem::take(
165-
&mut (&mut *core::mem::take(&mut self.environ).unwrap().borrow_mut()).parent,
166-
);
164+
let environ = self.environs.pop().unwrap();
165+
let environ = &mut *environ.borrow_mut();
166+
167+
if environ.parent.is_none() {
168+
self.environs.clear();
169+
return;
170+
}
171+
172+
self.environs
173+
.push(core::mem::take(&mut environ.parent).unwrap());
167174
}
168175

169176
pub fn get_function_name_from_pc(&mut self, pc: u64) -> Option<String> {

0 commit comments

Comments
 (0)