Implemented all coercions for undefined

trunk
ondra05 2022-05-06 21:08:16 +02:00
parent 97d6edcc57
commit 7368a3afd1
1 changed files with 12 additions and 10 deletions

View File

@ -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 {