mirror of
https://github.com/artegoser/ONLang
synced 2024-12-22 17:13:46 +03:00
feat: toNumber conversion
This commit is contained in:
parent
17bce621c6
commit
ef6c40215e
1 changed files with 33 additions and 8 deletions
|
@ -325,13 +325,20 @@ impl Interpreter {
|
||||||
self.error("Unsupported data type for the `arr` argument, must be an array");
|
self.error("Unsupported data type for the `arr` argument, must be an array");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"toString" => {
|
"toString" => match value {
|
||||||
return serde_json::Value::String(
|
Value::Object(_) => {
|
||||||
serde_json::to_string_pretty(&self.eval_node(value))
|
return Value::String(
|
||||||
.expect("Some error"),
|
serde_json::to_string_pretty(&self.eval_node(value))
|
||||||
)
|
.expect("Some error"),
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return serde_json::Value::String(
|
||||||
|
serde_json::to_string_pretty(&value).expect("Some error"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"toNumber" => return self.toNumber(value),
|
||||||
"obj" => match value {
|
"obj" => match value {
|
||||||
Value::Object(value) => {
|
Value::Object(value) => {
|
||||||
return self.calc_obj(value);
|
return self.calc_obj(value);
|
||||||
|
@ -387,7 +394,25 @@ impl Interpreter {
|
||||||
}
|
}
|
||||||
return Value::Null;
|
return Value::Null;
|
||||||
}
|
}
|
||||||
|
fn toNumber(&mut self, value: &Value) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Object(_) => {
|
||||||
|
let value = &self.eval_node(value);
|
||||||
|
return self.toNumber(value);
|
||||||
|
}
|
||||||
|
Value::String(value) => {
|
||||||
|
return json!(value.parse::<u32>().unwrap_or_else(|_| {
|
||||||
|
self.error("Cannot be converted to a number");
|
||||||
|
panic!()
|
||||||
|
},))
|
||||||
|
}
|
||||||
|
Value::Bool(value) => return json!(*value as i32),
|
||||||
|
_ => {
|
||||||
|
self.error("Cannot be converted to a number");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Null
|
||||||
|
}
|
||||||
fn import(&mut self, value: &Map<String, Value>) {
|
fn import(&mut self, value: &Map<String, Value>) {
|
||||||
let path = value
|
let path = value
|
||||||
.get("path")
|
.get("path")
|
||||||
|
|
Loading…
Add table
Reference in a new issue