fix: incorrect use of name scoped

This commit is contained in:
Artemy 2022-08-18 16:38:40 +03:00
parent 19fc8fa54c
commit d57d67a960

View file

@ -18,6 +18,16 @@ pub struct Interpreter {
}
impl Interpreter {
pub fn run(&mut self) {
self.enter_the_scope();
let length = self.commands.len();
for i in 0..length {
let command = &self.commands[i].clone();
self.eval_node(command);
self.pos = i;
}
self.exit_from_scope();
}
pub fn new(file_path: String) -> Self {
match Path::new(&file_path)
.extension()
@ -168,16 +178,6 @@ impl Interpreter {
println!("Converted");
}
pub fn run(&mut self) {
self.scopes.push(Vec::new());
let length = self.commands.len();
for i in 0..length {
let command = &self.commands[i].clone();
self.eval_node(command);
self.pos = i;
}
}
fn eval_node(&mut self, command: &Value) -> Value {
match command {
Value::Object(command) => {
@ -264,7 +264,8 @@ impl Interpreter {
},
"isExist" => match value {
Value::String(value) => {
return Value::Bool(self.var_exists(value));
let var_name = self.get_name_scoped_name(value, false);
return Value::Bool(self.var_exists(&var_name));
}
_ => {
self.error("Unsupported data type for the `isExist` argument, must be a string");
@ -506,6 +507,7 @@ impl Interpreter {
var_type: VarTypes::Function,
},
);
self.scopes[self.scope - 1].push(name);
} else {
self.error(&format!("The variable `{}` already exist, rename function", name));
}
@ -645,11 +647,11 @@ impl Interpreter {
}
fn delete_last_scope(&mut self) {
let vars = self.scopes[self.scope].clone();
let vars = self.scopes[self.scope - 1].clone();
for name in vars {
self.delete(&name, false);
self.delete(&name, true);
}
self.scopes.remove(self.scope);
self.scopes.remove(self.scope - 1);
}
fn define(&mut self, vars: &Map<String, Value>) {
@ -661,7 +663,7 @@ impl Interpreter {
fn get_name_scoped_name(&mut self, name: &String, to_create: bool) -> String {
let n_len = self.named_scopes.len();
if n_len > 0 {
let new_name = format!("{}.{name}", self.named_scopes[n_len - 1]);
let new_name = format!("{}.{name}", self.named_scopes.join("."));
if to_create {
new_name
} else {
@ -690,7 +692,7 @@ impl Interpreter {
var_type: VarTypes::Variable,
},
);
self.scopes[self.scope].push(name)
self.scopes[self.scope - 1].push(name);
}
_ => {
self.vars.insert(
@ -701,7 +703,7 @@ impl Interpreter {
var_type: VarTypes::Variable,
},
);
self.scopes[self.scope].push(name.clone())
self.scopes[self.scope - 1].push(name.clone());
}
}
} else {
@ -713,9 +715,8 @@ impl Interpreter {
}
}
fn delete(&mut self, var_name: &String, panic: bool) {
let var_name = self.get_name_scoped_name(var_name, false);
if self.var_exists(&var_name) {
self.vars.remove(&var_name);
if self.var_exists(var_name) {
self.vars.remove(var_name);
} else {
if panic {
self.error(&format!(
@ -751,11 +752,11 @@ impl Interpreter {
fn assign(&mut self, vars: &Map<String, Value>) {
for (name, value) in vars {
let name = self.get_name_scoped_name(name, false);
self.assign_var(&name, value);
}
}
fn assign_var(&mut self, name: &String, value: &Value) {
let name = self.get_name_scoped_name(name, false);
let scope = self.get_var_scope(&name);
match value {
Value::Object(_) => {
@ -782,8 +783,7 @@ impl Interpreter {
}
}
fn var_exists(&mut self, name: &String) -> bool {
let name = self.get_name_scoped_name(name, true);
match self.vars.get(&name) {
match self.vars.get(name) {
Some(_) => true,
None => false,
}