From 0b703173f815ed16b2573f8acaad1ea672375538 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Wed, 8 Dec 2021 16:33:06 -0700 Subject: [PATCH] Function division --- ablescript/src/variables.rs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index e1f44597..4787f35d 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -107,9 +107,7 @@ impl Value { instructions, tape_len, } => (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::Eval(s) => s.len() as _, }, Value::Int(i) => i, @@ -456,7 +454,38 @@ impl ops::Div for Value { })), Value::Bool(b) => Value::Bool(!b || rhs.into_bool()), Value::Abool(_) => !self + rhs, - Value::Functio(_) => todo!(), + Value::Functio(f) => { + let fraction = 1.0 / rhs.into_i32() as f64; + Value::Functio(match f { + Functio::Bf { + instructions, + tape_len, + } => { + let len = instructions.len(); + Functio::Bf { + instructions: instructions + .into_iter() + .take((len as f64 * fraction) as usize) + .collect(), + tape_len, + } + } + Functio::Able { params, body } => { + let len = body.len(); + Functio::Able { + params, + body: body + .into_iter() + .take((len as f64 * fraction) as usize) + .collect(), + } + } + Functio::Eval(s) => { + let len = s.len(); + Functio::Eval(s.chars().take((len as f64 * fraction) as usize).collect()) + } + }) + } Value::Cart(c) => { let cart_len = c.len(); let chunk_len = rhs.into_i32() as usize;