From ed1b3bfb43640b1958b1c9a3bdeb9804d7775c1a Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 6 May 2022 21:08:16 +0200 Subject: [PATCH] Implemented all coercions for undefined --- ablescript/src/variables.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index 91c3c77..37259b5 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -184,6 +184,12 @@ impl Value { /// Coerce a value to an integer. pub fn into_isize(self) -> isize { match self { + Value::Nul => consts::ANSWER, + Value::Undefined => rand::random(), + Value::Str(text) => text + .parse() + .unwrap_or_else(|_| text.chars().map(|cr| cr as isize).sum()), + Value::Int(i) => i, Value::Abool(a) => a as _, Value::Functio(f) => match f { Functio::Bf { @@ -211,12 +217,6 @@ impl Value { } Functio::Eval(code) => code.bytes().map(|x| x as isize).sum(), }, - Value::Int(i) => i, - Value::Nul => consts::ANSWER, - Value::Undefined => todo!("undefined -> isize coercion"), - Value::Str(text) => text - .parse() - .unwrap_or_else(|_| text.chars().map(|cr| cr as isize).sum()), Value::Cart(c) => c .into_iter() .map(|(i, v)| i.into_isize() * v.borrow().clone().into_isize()) @@ -292,11 +292,10 @@ impl Value { /// Coerce a value to a functio. pub fn into_functio(self) -> Functio { match self { - Value::Nul => Functio::Able { + Value::Nul | Value::Undefined => Functio::Able { body: vec![], params: vec![], }, - Value::Undefined => todo!("undefined -> functio coercion"), Value::Str(s) => Functio::Eval(s), Value::Int(i) => Functio::Bf { instructions: { @@ -350,7 +349,10 @@ impl Value { pub fn into_cart(self) -> Cart { match self { Value::Nul => HashMap::new(), - Value::Undefined => todo!("undefined -> cart coercion"), + Value::Undefined => [(2, 4), (4, 2)] + .into_iter() + .map(|(k, v)| (Value::Int(k), ValueRef::new(Value::Int(v)))) + .collect(), Value::Str(s) => s .chars() .enumerate() @@ -458,7 +460,7 @@ impl Value { pub fn length(&self) -> isize { match self { Value::Nul => 0, - Value::Undefined => todo!("undefined length"), + Value::Undefined => -1, Value::Str(s) => s.len() as _, Value::Int(i) => i.count_zeros() as _, Value::Abool(a) => match a {