Implemented all coercions for undefined

This commit is contained in:
Erin 2022-05-06 21:08:16 +02:00 committed by ondra05
parent 9f91a72b01
commit bb6888c22d

View file

@ -184,6 +184,12 @@ impl Value {
/// Coerce a value to an integer. /// Coerce a value to an integer.
pub fn into_isize(self) -> isize { pub fn into_isize(self) -> isize {
match self { 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::Abool(a) => a as _,
Value::Functio(f) => match f { Value::Functio(f) => match f {
Functio::Bf { Functio::Bf {
@ -211,12 +217,6 @@ impl Value {
} }
Functio::Eval(code) => code.bytes().map(|x| x as isize).sum(), 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 Value::Cart(c) => c
.into_iter() .into_iter()
.map(|(i, v)| i.into_isize() * v.borrow().clone().into_isize()) .map(|(i, v)| i.into_isize() * v.borrow().clone().into_isize())
@ -292,11 +292,10 @@ impl Value {
/// Coerce a value to a functio. /// Coerce a value to a functio.
pub fn into_functio(self) -> Functio { pub fn into_functio(self) -> Functio {
match self { match self {
Value::Nul => Functio::Able { Value::Nul | Value::Undefined => Functio::Able {
body: vec![], body: vec![],
params: vec![], params: vec![],
}, },
Value::Undefined => todo!("undefined -> functio coercion"),
Value::Str(s) => Functio::Eval(s), Value::Str(s) => Functio::Eval(s),
Value::Int(i) => Functio::Bf { Value::Int(i) => Functio::Bf {
instructions: { instructions: {
@ -350,7 +349,10 @@ impl Value {
pub fn into_cart(self) -> Cart { pub fn into_cart(self) -> Cart {
match self { match self {
Value::Nul => HashMap::new(), 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 Value::Str(s) => s
.chars() .chars()
.enumerate() .enumerate()
@ -458,7 +460,7 @@ impl Value {
pub fn length(&self) -> isize { pub fn length(&self) -> isize {
match self { match self {
Value::Nul => 0, Value::Nul => 0,
Value::Undefined => todo!("undefined length"), Value::Undefined => -1,
Value::Str(s) => s.len() as _, Value::Str(s) => s.len() as _,
Value::Int(i) => i.count_zeros() as _, Value::Int(i) => i.count_zeros() as _,
Value::Abool(a) => match a { Value::Abool(a) => match a {