feat: check the existence

of variable before define
This commit is contained in:
Artemy 2022-08-08 17:28:04 +03:00
parent 1aef3ef8d6
commit c4c312cc88
2 changed files with 25 additions and 15 deletions

View file

@ -3,7 +3,6 @@
- [ ] Functions
- [ ] imports
- [ ] scopes of visibility
- [ ] parallel tasks `{parallel:[..tasks]}`
- [ ] types conversion
- [ ] methods for arrays
- [ ] methods for strings
@ -12,5 +11,6 @@
- [x] yaml support?
- [ ] command execution function
- [ ] function for random
- [ ] delete variable
and something else

View file

@ -285,6 +285,7 @@ impl Interpreter {
fn define(&mut self, vars: &Map<String, Value>) -> Value {
for (name, value) in vars {
if !self.var_exists(&name) {
match value {
Value::Object(_) => {
let value = self.eval_node(value);
@ -294,6 +295,10 @@ impl Interpreter {
self.vars.insert(name.to_string(), value.clone());
}
}
} else {
self.error(&format!("The variable {} already exist, use assign", name));
panic!()
}
}
Value::Null
}
@ -311,9 +316,8 @@ impl Interpreter {
fn assign(&mut self, vars: &Map<String, Value>) -> Value {
for (name, value) in vars {
let var = self.vars.get(name);
match var {
Some(_) => match value {
if self.var_exists(&name) {
match value {
Value::Object(_) => {
let value = self.eval_node(value);
self.vars.insert(name.to_string(), value);
@ -321,16 +325,22 @@ impl Interpreter {
_ => {
self.vars.insert(name.to_string(), value.clone());
}
},
None => {
self.error(&format!("The variable {} does not exist", name));
}
} else {
self.error(&format!("The variable {} does not exist, use define", name));
panic!();
}
}
}
Value::Null
}
fn var_exists(&self, name: &String) -> bool {
match self.vars.get(name) {
Some(_) => true,
None => false,
}
}
fn calc(&mut self, value: &Vec<Value>) -> Value {
let op1 = &value[0];
let operation = &value[1];