mirror of
https://github.com/artegoser/ONLang
synced 2024-12-23 09:33:44 +03:00
perf: improve variable deleteing
This commit is contained in:
parent
77fbdbc3f7
commit
e7b4a02ab7
1 changed files with 12 additions and 7 deletions
|
@ -10,6 +10,7 @@ pub struct Interpreter {
|
||||||
vars: HashMap<String, Var>,
|
vars: HashMap<String, Var>,
|
||||||
pos: usize,
|
pos: usize,
|
||||||
scope: usize,
|
scope: usize,
|
||||||
|
scopes: Vec<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Interpreter {
|
impl Interpreter {
|
||||||
|
@ -19,6 +20,7 @@ impl Interpreter {
|
||||||
vars: HashMap::new(),
|
vars: HashMap::new(),
|
||||||
pos: 1,
|
pos: 1,
|
||||||
scope: 0,
|
scope: 0,
|
||||||
|
scopes: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +36,7 @@ impl Interpreter {
|
||||||
.expect("The program must be an array of commands")
|
.expect("The program must be an array of commands")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
self.scopes.push(Vec::new());
|
||||||
for command in arr {
|
for command in arr {
|
||||||
self.eval_node(command);
|
self.eval_node(command);
|
||||||
self.pos += 1;
|
self.pos += 1;
|
||||||
|
@ -302,6 +305,7 @@ impl Interpreter {
|
||||||
|
|
||||||
fn run_nodes(&mut self, arr: &Vec<Value>) -> String {
|
fn run_nodes(&mut self, arr: &Vec<Value>) -> String {
|
||||||
self.scope += 1;
|
self.scope += 1;
|
||||||
|
self.scopes.push(Vec::new());
|
||||||
for command in arr {
|
for command in arr {
|
||||||
let to_do = self.eval_node(command);
|
let to_do = self.eval_node(command);
|
||||||
match to_do {
|
match to_do {
|
||||||
|
@ -315,12 +319,11 @@ impl Interpreter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_last_scope(&mut self) {
|
fn delete_last_scope(&mut self) {
|
||||||
let vars = self.vars.clone().into_iter();
|
let vars = self.scopes[self.scope].clone();
|
||||||
for (k, v) in vars {
|
for name in vars {
|
||||||
if v.scope == self.scope {
|
self.delete(&name);
|
||||||
self.delete(&k);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
self.scopes.remove(self.scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn define(&mut self, vars: &Map<String, Value>) {
|
fn define(&mut self, vars: &Map<String, Value>) {
|
||||||
|
@ -330,21 +333,23 @@ impl Interpreter {
|
||||||
Value::Object(_) => {
|
Value::Object(_) => {
|
||||||
let value = self.eval_node(value);
|
let value = self.eval_node(value);
|
||||||
self.vars.insert(
|
self.vars.insert(
|
||||||
name.to_string(),
|
name.clone(),
|
||||||
Var {
|
Var {
|
||||||
scope: self.scope,
|
scope: self.scope,
|
||||||
body: value,
|
body: value,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
self.scopes[self.scope].push(name.clone())
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
self.vars.insert(
|
self.vars.insert(
|
||||||
name.to_string(),
|
name.clone(),
|
||||||
Var {
|
Var {
|
||||||
scope: self.scope,
|
scope: self.scope,
|
||||||
body: value.clone(),
|
body: value.clone(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
self.scopes[self.scope].push(name.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue