From 49d469992870996dca5fa24f18733c9d4f16f063 Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 12 Feb 2022 22:10:30 +0100 Subject: [PATCH] Implemented coercions for built-in functios --- ablescript/src/variables.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index 05157d4..cebd102 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -87,6 +87,10 @@ impl BuiltinFunctio { pub fn call(&self, args: &[Rc>]) -> Result<(), crate::error::ErrorKind> { (self.function)(args) } + + pub fn fn_addr(&self) -> usize { + Rc::as_ptr(&self.function) as *const () as _ + } } impl std::fmt::Debug for BuiltinFunctio { @@ -106,7 +110,7 @@ impl PartialEq for BuiltinFunctio { impl Hash for BuiltinFunctio { fn hash(&self, state: &mut H) { - (Rc::as_ptr(&self.function) as *const () as usize).hash(state); + self.fn_addr().hash(state); self.arity.hash(state); } } @@ -180,7 +184,7 @@ impl Value { .sum::() + body.len() as i32 } - Functio::Builtin(_) => todo!(), + Functio::Builtin(b) => b.fn_addr() as _, Functio::Chain { functios, kind } => { let (lf, rf) = *functios; Value::Functio(lf).into_i32() @@ -269,7 +273,7 @@ impl Value { Value::Int((params + body) % 3 - 1).into_abool() } - Functio::Builtin(_) => todo!(), + Functio::Builtin(b) => Value::Bool(b.fn_addr() % b.arity == 0).into_abool(), Functio::Chain { functios, kind } => { let (lhs, rhs) = *functios; match kind { @@ -439,7 +443,22 @@ impl Value { ); cart } - Functio::Builtin(_) => todo!(), + Functio::Builtin(b) => { + let mut cart = HashMap::new(); + cart.insert( + Value::Str("addr".to_owned()), + Rc::new(RefCell::new(Value::Cart( + Value::Int(b.fn_addr() as _).into_cart(), + ))), + ); + + cart.insert( + Value::Str("arity".to_owned()), + Rc::new(RefCell::new(Value::Int(b.arity as _))), + ); + + cart + } Functio::Chain { functios, kind } => { let (lhs, rhs) = *functios; match kind { @@ -489,7 +508,9 @@ impl Value { tape_len, } => (instructions.len() + tape_len) as _, Functio::Able { params, body } => (params.len() + format!("{:?}", body).len()) as _, - Functio::Builtin(_) => todo!(), + Functio::Builtin(b) => { + (std::mem::size_of_val(b.function.as_ref()) + b.arity) as i32 + } Functio::Chain { functios, kind } => { let (lhs, rhs) = *functios.clone(); match kind {