|
| 1 | +use crate::compiler::ast::vm_ir::Value; |
| 2 | +use crate::library::{register_library, LibModule, ModuleFunc}; |
| 3 | +use crate::runtime::RuntimeError; |
| 4 | +use smol_str::{SmolStr, ToSmolStr}; |
| 5 | + |
| 6 | +#[allow(clippy::unnecessary_wraps)] |
| 7 | +fn type_to_number(args: &[Value]) -> Result<Value, RuntimeError> { |
| 8 | + let auto = args.first().unwrap().clone(); |
| 9 | + if let Value::String(raw_str) = auto { |
| 10 | + let i = raw_str.as_str().parse::<i64>().unwrap(); |
| 11 | + Ok(Value::Int(i)) |
| 12 | + } else if let Value::Float(raw_float) = args.first().unwrap() { |
| 13 | + let i = raw_float.floor() as i64; |
| 14 | + Ok(Value::Int(i)) |
| 15 | + } else { |
| 16 | + Err(RuntimeError::TypeException( |
| 17 | + "to_number: auto not a string or float.".to_smolstr(), |
| 18 | + )) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn reg_to_number() -> ModuleFunc { |
| 23 | + ModuleFunc { |
| 24 | + name: SmolStr::new("to_number"), |
| 25 | + arity: 1, |
| 26 | + func: type_to_number, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[allow(clippy::unnecessary_wraps)] |
| 31 | +fn type_to_float(args: &[Value]) -> Result<Value, RuntimeError> { |
| 32 | + let auto = args.first().unwrap().clone(); |
| 33 | + if let Value::String(raw_str) = auto { |
| 34 | + let i = raw_str.as_str().parse::<f64>().unwrap(); |
| 35 | + Ok(Value::Float(i)) |
| 36 | + } else if let Value::Int(raw_number) = args.first().unwrap() { |
| 37 | + let i = *raw_number as f64; |
| 38 | + Ok(Value::Float(i)) |
| 39 | + } else { |
| 40 | + Err(RuntimeError::TypeException( |
| 41 | + "to_float: auto not a string or number.".to_smolstr(), |
| 42 | + )) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn reg_to_float() -> ModuleFunc { |
| 47 | + ModuleFunc { |
| 48 | + name: SmolStr::new("to_float"), |
| 49 | + arity: 1, |
| 50 | + func: type_to_float, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[allow(clippy::unnecessary_wraps)] |
| 55 | +fn type_check_type(args: &[Value]) -> Result<Value, RuntimeError> { |
| 56 | + let auto = args.first().unwrap().clone(); |
| 57 | + match auto { |
| 58 | + Value::String(_) => Ok(Value::String("string".to_smolstr())), |
| 59 | + Value::Float(_) => Ok(Value::String("float".to_smolstr())), |
| 60 | + Value::Int(_) => Ok(Value::String("number".to_smolstr())), |
| 61 | + Value::Bool(_) => Ok(Value::String("bool".to_smolstr())), |
| 62 | + Value::Array(..) => Ok(Value::String("array".to_smolstr())), |
| 63 | + Value::Ref(_) => Ok(Value::String("ref".to_smolstr())), |
| 64 | + Value::Null => Ok(Value::String("null".to_smolstr())), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +fn reg_check_type() -> ModuleFunc { |
| 69 | + ModuleFunc { |
| 70 | + name: SmolStr::new("check_type"), |
| 71 | + arity: 1, |
| 72 | + func: type_check_type, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +pub fn register_type_lib() { |
| 77 | + let mut type_lib = LibModule { |
| 78 | + name: SmolStr::new("type"), |
| 79 | + functions: vec![], |
| 80 | + }; |
| 81 | + type_lib.functions.push(reg_to_number()); |
| 82 | + type_lib.functions.push(reg_to_float()); |
| 83 | + type_lib.functions.push(reg_check_type()); |
| 84 | + register_library(type_lib); |
| 85 | +} |
0 commit comments