Implemented coercions for built-in functios

This commit is contained in:
Erin 2022-02-12 22:10:30 +01:00 committed by ondra05
parent 1d1919976f
commit 49d4699928

View file

@ -87,6 +87,10 @@ impl BuiltinFunctio {
pub fn call(&self, args: &[Rc<RefCell<Value>>]) -> Result<(), crate::error::ErrorKind> { pub fn call(&self, args: &[Rc<RefCell<Value>>]) -> Result<(), crate::error::ErrorKind> {
(self.function)(args) (self.function)(args)
} }
pub fn fn_addr(&self) -> usize {
Rc::as_ptr(&self.function) as *const () as _
}
} }
impl std::fmt::Debug for BuiltinFunctio { impl std::fmt::Debug for BuiltinFunctio {
@ -106,7 +110,7 @@ impl PartialEq for BuiltinFunctio {
impl Hash for BuiltinFunctio { impl Hash for BuiltinFunctio {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { 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); self.arity.hash(state);
} }
} }
@ -180,7 +184,7 @@ impl Value {
.sum::<i32>() .sum::<i32>()
+ body.len() as i32 + body.len() as i32
} }
Functio::Builtin(_) => todo!(), Functio::Builtin(b) => b.fn_addr() as _,
Functio::Chain { functios, kind } => { Functio::Chain { functios, kind } => {
let (lf, rf) = *functios; let (lf, rf) = *functios;
Value::Functio(lf).into_i32() Value::Functio(lf).into_i32()
@ -269,7 +273,7 @@ impl Value {
Value::Int((params + body) % 3 - 1).into_abool() 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 } => { Functio::Chain { functios, kind } => {
let (lhs, rhs) = *functios; let (lhs, rhs) = *functios;
match kind { match kind {
@ -439,7 +443,22 @@ impl Value {
); );
cart 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 } => { Functio::Chain { functios, kind } => {
let (lhs, rhs) = *functios; let (lhs, rhs) = *functios;
match kind { match kind {
@ -489,7 +508,9 @@ impl Value {
tape_len, tape_len,
} => (instructions.len() + tape_len) as _, } => (instructions.len() + tape_len) as _,
Functio::Able { params, body } => (params.len() + format!("{:?}", body).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 } => { Functio::Chain { functios, kind } => {
let (lhs, rhs) = *functios.clone(); let (lhs, rhs) = *functios.clone();
match kind { match kind {