feat: reference variables

This commit is contained in:
Artemy 2022-08-14 23:32:54 +03:00
parent 983819830b
commit f1259a6b1b

View file

@ -119,6 +119,14 @@ impl Interpreter {
self.error("Unsupported data type for the `var` argument, must be a string"); self.error("Unsupported data type for the `var` argument, must be a string");
} }
}, },
"ref" => match value {
Value::String(value) => {
return self.variable_reference(value);
}
_ => {
self.error("Unsupported data type for the `ref` argument, must be a string");
}
},
"isExist" => match value { "isExist" => match value {
Value::String(value) => { Value::String(value) => {
return Value::Bool(self.var_exists(value)); return Value::Bool(self.var_exists(value));
@ -437,6 +445,18 @@ impl Interpreter {
} }
} }
fn variable_reference(&mut self, name: &String) -> Value {
if self.var_exists(name) {
return json!({ "var": name });
} else {
self.error(&format!(
"The variable {} does not exist, the reference is invalid",
name
));
}
Value::Null
}
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];