mirror of
https://github.com/artegoser/ONLang
synced 2024-11-06 04:53:57 +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
|
- [ ] 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
|
||||||
|
|
|
@ -285,6 +285,7 @@ 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 {
|
||||||
|
if !self.var_exists(&name) {
|
||||||
match value {
|
match value {
|
||||||
Value::Object(_) => {
|
Value::Object(_) => {
|
||||||
let value = self.eval_node(value);
|
let value = self.eval_node(value);
|
||||||
|
@ -294,6 +295,10 @@ impl Interpreter {
|
||||||
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 => {
|
} else {
|
||||||
self.error(&format!("The variable {} does not exist", name));
|
self.error(&format!("The variable {} does not exist, use define", name));
|
||||||
panic!();
|
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];
|
||||||
|
|
Loading…
Reference in a new issue