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

View file

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