Skip to content

Commit 422ca10

Browse files
committed
Scope -> Environments
1 parent 116ba28 commit 422ca10

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
//! Scope
2-
//!
3-
//! Contains: Scope
1+
//! Environment
2+
use core::cell::RefCell;
43

54
use crate::value::Value;
5+
use alloc::rc::Rc;
66
use alloc::string::*;
77
use alloc::vec::*;
88

99
/// Scope
1010
///
1111
/// Contains variables values and names
1212
#[derive(Debug)]
13-
pub struct Scope {
13+
pub struct Environment {
1414
pub(crate) vars: Vec<(String, Value)>,
15+
pub parent: Option<Rc<RefCell<Environment>>>,
1516
}
1617

17-
impl Scope {
18-
pub fn new() -> Scope {
19-
return Scope { vars: Vec::new() };
18+
impl Environment {
19+
pub fn new() -> Environment {
20+
return Environment {
21+
vars: Vec::new(),
22+
parent: None,
23+
};
2024
}
2125

2226
/// Get a variable reference by a name
@@ -47,8 +51,8 @@ impl Scope {
4751
}
4852
}
4953

50-
impl Default for Scope {
54+
impl Default for Environment {
5155
fn default() -> Self {
52-
return Scope::new();
56+
return Environment::new();
5357
}
5458
}

zenlang/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#![no_std]
1212
pub mod ast;
1313
pub mod compiler;
14+
pub mod environment;
1415
pub mod interop;
1516
pub mod module;
1617
pub mod opcode;
1718
pub mod parser;
1819
pub mod platform;
19-
pub mod scope;
2020
pub mod stdlib;
2121
pub mod strong_u64;
2222
pub mod tokenizer;

zenlang/src/vm/opcodes/vm_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl VM {
1111
self.check_stack_overflow();
1212
self.pc = addr;
1313
self.pc.sub_low(1);
14-
self.add_scope();
14+
self.add_environment();
1515

1616
let start = self.bfas_stack_start.pop().unwrap();
1717
let end = self.bfas_stack_end.pop().unwrap();

zenlang/src/vm/opcodes/vm_vars.rs

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

77
impl VM {
88
pub fn op_load_var(&mut self, name: &String) {
9-
if let Some(scope) = self.scopes.last() {
10-
if let Some(value) = scope.get(name) {
9+
if let Some(environ) = &self.environ {
10+
let environ = &*environ.borrow();
11+
if let Some(value) = environ.get(name) {
1112
self.stack.push(value.clone());
12-
self.check_stack_overflow();
1313
return;
1414
}
1515
}
1616

1717
if let Some(value) = self.global_scope.get(name) {
1818
self.stack.push(value.clone());
19-
self.check_stack_overflow();
2019
return;
2120
}
2221

@@ -44,9 +43,11 @@ impl VM {
4443
return;
4544
}
4645

47-
if let Some(scope) = self.scopes.last_mut() {
48-
scope.create_if_doesnt_exist(name);
49-
if let Some(value) = scope.get_mut(name) {
46+
if let Some(environ) = &mut self.environ {
47+
let environ = &mut *environ.borrow_mut();
48+
49+
environ.create_if_doesnt_exist(name);
50+
if let Some(value) = environ.get_mut(name) {
5051
*value = store_value;
5152
return;
5253
}

zenlang/src/vm/vm.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use core::cell::RefCell;
2+
3+
use crate::environment::Environment;
14
use crate::module::Module;
25
use crate::platform::Platform;
3-
use crate::scope::Scope;
46
use crate::strong_u64::*;
57
use crate::value::*;
68
use alloc::boxed::*;
79
use alloc::format;
10+
use alloc::rc::*;
811
use alloc::string::*;
912
use alloc::vec::*;
1013

@@ -15,11 +18,11 @@ pub struct VM {
1518
pub pc: u64,
1619
pub stack: Vec<Value>,
1720
pub call_stack: Vec<u64>,
18-
pub scopes: Vec<Scope>,
21+
pub environ: Option<Rc<RefCell<Environment>>>,
1922
pub error: String,
2023
pub ret: Value,
2124
pub platform: Option<Box<dyn Platform>>,
22-
pub global_scope: Scope,
25+
pub global_scope: Environment,
2326
pub halted: bool,
2427
pub(crate) bfas_stack_start: Vec<i64>,
2528
pub(crate) bfas_stack_end: Vec<i64>,
@@ -32,11 +35,11 @@ impl VM {
3235
pc: 0,
3336
stack: Vec::new(),
3437
call_stack: Vec::new(),
35-
scopes: Vec::new(),
38+
environ: None,
3639
error: String::new(),
3740
ret: Value::Null(),
3841
platform: None,
39-
global_scope: Scope::new(),
42+
global_scope: Environment::new(),
4043
halted: false,
4144
bfas_stack_start: Vec::new(),
4245
bfas_stack_end: Vec::new(),
@@ -67,15 +70,15 @@ impl VM {
6770
if func.ctor {
6871
self.pc.set_low(func.addr as u32);
6972
self.pc.set_high((self.modules.len() - 1) as u32);
70-
self.add_scope();
73+
self.add_environment();
7174

7275
while !self.halted {
7376
if !self.step() {
7477
break;
7578
}
7679
}
7780

78-
self.scopes.clear();
81+
self.environ = Some(Rc::new(RefCell::new(Environment::new())));
7982

8083
if !self.error.is_empty() {
8184
self.halted = true;
@@ -127,7 +130,7 @@ impl VM {
127130
if function.name == entry_fn_name {
128131
self.pc.set_low(function.addr as u32);
129132
self.pc.set_high(i as u32);
130-
self.add_scope();
133+
self.add_environment();
131134
return Ok(());
132135
}
133136
}
@@ -144,12 +147,23 @@ impl VM {
144147
}
145148
}
146149

147-
pub(crate) fn add_scope(&mut self) {
148-
self.scopes.push(Scope::new());
150+
pub(crate) fn add_environment(&mut self) {
151+
let new = Rc::new(RefCell::new(Environment::new()));
152+
if let Some(environ) = &self.environ {
153+
(&mut *new.borrow_mut()).parent = Some(environ.clone());
154+
}
155+
self.environ = Some(new);
149156
}
150157

151158
pub(crate) fn remove_scope(&mut self) {
152-
self.scopes.pop();
159+
if self.environ.is_none() {
160+
panic!("environ is None");
161+
}
162+
163+
// Welcome to rust!
164+
self.environ = core::mem::take(
165+
&mut (&mut *core::mem::take(&mut self.environ).unwrap().borrow_mut()).parent,
166+
);
153167
}
154168

155169
pub fn get_function_name_from_pc(&mut self, pc: u64) -> Option<String> {
@@ -194,6 +208,7 @@ impl VM {
194208
let opcode = &opcodes[opcode_index as usize];
195209

196210
self.execute_opcode(opcode);
211+
self.check_stack_overflow();
197212

198213
self.modules[module_index].opcodes = opcodes;
199214

0 commit comments

Comments
 (0)