Implemented coercions for built-in functios

pull/3/head
ondra05 2022-02-12 22:10:30 +01:00
parent 013c324a8e
commit ebd8a28a83
1 changed files with 26 additions and 5 deletions

View File

@ -87,6 +87,10 @@ impl BuiltinFunctio {
pub fn call(&self, args: &[Rc<RefCell<Value>>]) -> 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<H: std::hash::Hasher>(&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::<i32>()
+ 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 {