mirror of
https://github.com/artegoser/ONLang
synced 2024-12-23 09:33:44 +03:00
feat: check the existence
of variable before define
This commit is contained in:
parent
1aef3ef8d6
commit
c4c312cc88
2 changed files with 25 additions and 15 deletions
|
@ -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
|
||||
|
|
|
@ -285,14 +285,19 @@ impl Interpreter {
|
|||
|
||||
fn define(&mut self, vars: &Map<String, Value>) -> Value {
|
||||
for (name, value) in vars {
|
||||
match value {
|
||||
Value::Object(_) => {
|
||||
let value = self.eval_node(value);
|
||||
self.vars.insert(name.to_string(), value);
|
||||
}
|
||||
_ => {
|
||||
self.vars.insert(name.to_string(), value.clone());
|
||||
if !self.var_exists(&name) {
|
||||
match value {
|
||||
Value::Object(_) => {
|
||||
let value = self.eval_node(value);
|
||||
self.vars.insert(name.to_string(), value);
|
||||
}
|
||||
_ => {
|
||||
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));
|
||||
panic!();
|
||||
}
|
||||
} 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];
|
||||
|
|
Loading…
Add table
Reference in a new issue