Removed booleans

This commit is contained in:
Erin 2022-03-30 20:55:05 +02:00 committed by ondra05
parent 04b44fb01e
commit 8d24ad5528
3 changed files with 26 additions and 70 deletions

View file

@ -31,8 +31,6 @@ pub fn ablescript_consts() -> HashMap<String, Variable> {
("OCTOTHORPE", Str("#".to_owned())), // It's an octothorpe ("OCTOTHORPE", Str("#".to_owned())), // It's an octothorpe
("ANSWER", Int(ANSWER)), ("ANSWER", Int(ANSWER)),
("nul", Nul), ("nul", Nul),
("true", Bool(true)),
("false", Bool(false)),
("always", Abool(crate::variables::Abool::Always)), ("always", Abool(crate::variables::Abool::Always)),
("sometimes", Abool(crate::variables::Abool::Sometimes)), ("sometimes", Abool(crate::variables::Abool::Sometimes)),
("never", Abool(crate::variables::Abool::Never)), ("never", Abool(crate::variables::Abool::Never)),

View file

@ -164,10 +164,10 @@ impl ExecEnv {
Subtract => lhs - rhs, Subtract => lhs - rhs,
Multiply => lhs * rhs, Multiply => lhs * rhs,
Divide => lhs / rhs, Divide => lhs / rhs,
Greater => Value::Bool(lhs > rhs), Greater => Value::Abool((lhs > rhs).into()),
Less => Value::Bool(lhs < rhs), Less => Value::Abool((lhs < rhs).into()),
Equal => Value::Bool(lhs == rhs), Equal => Value::Abool((lhs == rhs).into()),
NotEqual => Value::Bool(lhs != rhs), NotEqual => Value::Abool((lhs != rhs).into()),
} }
} }
Not(expr) => !self.eval_expr(expr)?, Not(expr) => !self.eval_expr(expr)?,
@ -249,7 +249,7 @@ impl ExecEnv {
); );
} }
StmtKind::If { cond, body } => { StmtKind::If { cond, body } => {
if self.eval_expr(cond)?.into_bool() { if self.eval_expr(cond)?.into_abool().to_bool() {
return self.eval_stmts_hs(&body.block, true); return self.eval_stmts_hs(&body.block, true);
} }
} }
@ -459,10 +459,7 @@ impl ExecEnv {
Ok(()) Ok(())
} }
fn deinterlace( fn deinterlace(args: &[ValueRef], arities: (usize, usize)) -> (Vec<ValueRef>, Vec<ValueRef>) {
args: &[ValueRef],
arities: (usize, usize),
) -> (Vec<ValueRef>, Vec<ValueRef>) {
let n_alternations = usize::min(arities.0, arities.1); let n_alternations = usize::min(arities.0, arities.1);
let (extra_l, extra_r) = match Ord::cmp(&arities.0, &arities.1) { let (extra_l, extra_r) = match Ord::cmp(&arities.0, &arities.1) {
Ordering::Less => (0, arities.1 - arities.0), Ordering::Less => (0, arities.1 - arities.0),
@ -630,8 +627,10 @@ mod tests {
#[test] #[test]
fn type_coercions() { fn type_coercions() {
// The sum of an integer and a boolean causes a boolean // The sum of an integer and an aboolean causes an aboolean
// coercion. // coercion.
use crate::variables::Abool;
let env = ExecEnv::new(); let env = ExecEnv::new();
assert_eq!( assert_eq!(
env.eval_expr(&Expr { env.eval_expr(&Expr {
@ -641,7 +640,7 @@ mod tests {
span: 1..1, span: 1..1,
}), }),
rhs: Box::new(Expr { rhs: Box::new(Expr {
kind: ExprKind::Literal(Value::Bool(true)), kind: ExprKind::Literal(Value::Abool(Abool::Always)),
span: 1..1, span: 1..1,
}), }),
kind: crate::ast::BinOpKind::Add, kind: crate::ast::BinOpKind::Add,

View file

@ -21,6 +21,15 @@ pub enum Abool {
Always = 1, Always = 1,
} }
impl Abool {
pub fn to_bool(&self) -> bool {
match self {
Self::Always => true,
Self::Sometimes if rand::random() => true,
_ => false,
}
}
}
impl Display for Abool { impl Display for Abool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
@ -31,12 +40,12 @@ impl Display for Abool {
} }
} }
impl From<Abool> for bool { impl From<bool> for Abool {
fn from(val: Abool) -> Self { fn from(b: bool) -> Self {
match val { if b {
Abool::Never => false, Abool::Always
Abool::Always => true, } else {
Abool::Sometimes => rand::thread_rng().gen(), // NOTE(Able): This is amazing and should be applied anywhere abooleans exist Abool::Never
} }
} }
} }
@ -135,7 +144,6 @@ pub enum Value {
Nul, Nul,
Str(String), Str(String),
Int(isize), Int(isize),
Bool(bool),
Abool(Abool), Abool(Abool),
Functio(Functio), Functio(Functio),
Cart(Cart), Cart(Cart),
@ -154,7 +162,6 @@ impl Hash for Value {
Value::Nul => (), Value::Nul => (),
Value::Str(v) => v.hash(state), Value::Str(v) => v.hash(state),
Value::Int(v) => v.hash(state), Value::Int(v) => v.hash(state),
Value::Bool(v) => v.hash(state),
Value::Abool(v) => v.to_string().hash(state), Value::Abool(v) => v.to_string().hash(state),
Value::Functio(statements) => statements.hash(state), Value::Functio(statements) => statements.hash(state),
Value::Cart(_) => self.to_string().hash(state), Value::Cart(_) => self.to_string().hash(state),
@ -178,7 +185,6 @@ impl Value {
pub fn into_isize(self) -> isize { pub fn into_isize(self) -> isize {
match self { match self {
Value::Abool(a) => a as _, Value::Abool(a) => a as _,
Value::Bool(b) => b as _,
Value::Functio(f) => match f { Value::Functio(f) => match f {
Functio::Bf { Functio::Bf {
instructions, instructions,
@ -215,23 +221,6 @@ impl Value {
} }
} }
/// Coerce a value to a boolean.
pub fn into_bool(self) -> bool {
match self {
Value::Abool(b) => b.into(),
Value::Bool(b) => b,
Value::Functio(_) => true,
Value::Int(x) => x != 0,
Value::Nul => false,
Value::Str(s) => match s.to_lowercase().as_str() {
"false" | "no" | "🇳🇴" => false,
"true" | "yes" => true,
s => !s.is_empty(),
},
Value::Cart(c) => !c.is_empty(),
}
}
/// Coerce a value to an aboolean. /// Coerce a value to an aboolean.
pub fn into_abool(self) -> Abool { pub fn into_abool(self) -> Abool {
match self { match self {
@ -253,13 +242,6 @@ impl Value {
std::cmp::Ordering::Equal => Abool::Sometimes, std::cmp::Ordering::Equal => Abool::Sometimes,
std::cmp::Ordering::Greater => Abool::Always, std::cmp::Ordering::Greater => Abool::Always,
}, },
Value::Bool(b) => {
if b {
Abool::Always
} else {
Abool::Never
}
}
Value::Abool(a) => a, Value::Abool(a) => a,
Value::Functio(f) => match f { Value::Functio(f) => match f {
Functio::Bf { Functio::Bf {
@ -282,7 +264,7 @@ impl Value {
Value::Int((params + body) % 3 - 1).into_abool() Value::Int((params + body) % 3 - 1).into_abool()
} }
Functio::Builtin(b) => Value::Bool(b.fn_addr() % b.arity == 0).into_abool(), Functio::Builtin(b) => (b.fn_addr() % b.arity == 0).into(),
Functio::Chain { functios, kind } => { Functio::Chain { functios, kind } => {
let (lhs, rhs) = *functios; let (lhs, rhs) = *functios;
match kind { match kind {
@ -328,14 +310,6 @@ impl Value {
}, },
tape_len: crate::brian::DEFAULT_TAPE_SIZE_LIMIT, tape_len: crate::brian::DEFAULT_TAPE_SIZE_LIMIT,
}, },
Value::Bool(b) => Functio::Eval(
if b {
r#"loop{"Buy Able products!"print;}"#
} else {
""
}
.to_owned(),
),
Value::Abool(a) => Functio::Eval(match a { Value::Abool(a) => Functio::Eval(match a {
Abool::Never => "".to_owned(), Abool::Never => "".to_owned(),
Abool::Sometimes => { Abool::Sometimes => {
@ -388,7 +362,6 @@ impl Value {
}) })
.collect(), .collect(),
Value::Int(i) => Value::Str(i.to_string()).into_cart(), Value::Int(i) => Value::Str(i.to_string()).into_cart(),
Value::Bool(b) => Value::Str(b.to_string()).into_cart(),
Value::Abool(a) => Value::Str(a.to_string()).into_cart(), Value::Abool(a) => Value::Str(a.to_string()).into_cart(),
Value::Functio(f) => match f { Value::Functio(f) => match f {
Functio::Able { params, body } => { Functio::Able { params, body } => {
@ -487,8 +460,6 @@ impl Value {
Value::Nul => 0, Value::Nul => 0,
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::Bool(b) if *b => -2,
Value::Bool(_) => 2,
Value::Abool(a) => match a { Value::Abool(a) => match a {
Abool::Never => -3, Abool::Never => -3,
Abool::Sometimes => { Abool::Sometimes => {
@ -541,14 +512,12 @@ impl ops::Add for Value {
Value::Nul => Value::Nul, Value::Nul => Value::Nul,
Value::Str(_) => Value::Str(self.to_string()) + rhs, Value::Str(_) => Value::Str(self.to_string()) + rhs,
Value::Int(_) => Value::Int(self.into_isize()) + rhs, Value::Int(_) => Value::Int(self.into_isize()) + rhs,
Value::Bool(_) => Value::Bool(self.into_bool()) + rhs,
Value::Abool(_) => Value::Abool(self.into_abool()) + rhs, Value::Abool(_) => Value::Abool(self.into_abool()) + rhs,
Value::Functio(_) => Value::Functio(self.into_functio()) + rhs, Value::Functio(_) => Value::Functio(self.into_functio()) + rhs,
Value::Cart(_) => Value::Cart(self.into_cart()) + rhs, Value::Cart(_) => Value::Cart(self.into_cart()) + rhs,
}, },
Value::Str(s) => Value::Str(format!("{s}{rhs}")), Value::Str(s) => Value::Str(format!("{s}{rhs}")),
Value::Int(i) => Value::Int(i.wrapping_add(rhs.into_isize())), Value::Int(i) => Value::Int(i.wrapping_add(rhs.into_isize())),
Value::Bool(b) => Value::Bool(b || rhs.into_bool()),
Value::Abool(_) => { Value::Abool(_) => {
Value::Abool(Value::Int(self.into_isize().max(rhs.into_isize())).into_abool()) Value::Abool(Value::Int(self.into_isize().max(rhs.into_isize())).into_abool())
} }
@ -572,14 +541,12 @@ impl ops::Sub for Value {
Value::Nul => Value::Nul, Value::Nul => Value::Nul,
Value::Str(_) => Value::Str(self.to_string()) - rhs, Value::Str(_) => Value::Str(self.to_string()) - rhs,
Value::Int(_) => Value::Int(self.into_isize()) - rhs, Value::Int(_) => Value::Int(self.into_isize()) - rhs,
Value::Bool(_) => Value::Bool(self.into_bool()) - rhs,
Value::Abool(_) => Value::Abool(self.into_abool()) - rhs, Value::Abool(_) => Value::Abool(self.into_abool()) - rhs,
Value::Functio(_) => Value::Functio(self.into_functio()) - rhs, Value::Functio(_) => Value::Functio(self.into_functio()) - rhs,
Value::Cart(_) => Value::Cart(self.into_cart()) - rhs, Value::Cart(_) => Value::Cart(self.into_cart()) - rhs,
}, },
Value::Str(s) => Value::Str(s.replace(&rhs.to_string(), "")), Value::Str(s) => Value::Str(s.replace(&rhs.to_string(), "")),
Value::Int(i) => Value::Int(i.wrapping_sub(rhs.into_isize())), Value::Int(i) => Value::Int(i.wrapping_sub(rhs.into_isize())),
Value::Bool(b) => Value::Bool(b ^ rhs.into_bool()),
Value::Abool(_) => (self.clone() + rhs.clone()) * !(self * rhs), Value::Abool(_) => (self.clone() + rhs.clone()) * !(self * rhs),
Value::Functio(f) => Value::Functio(match f { Value::Functio(f) => Value::Functio(match f {
Functio::Bf { Functio::Bf {
@ -691,14 +658,12 @@ impl ops::Mul for Value {
Value::Nul => Value::Nul, Value::Nul => Value::Nul,
Value::Str(_) => Value::Str(self.to_string()) * rhs, Value::Str(_) => Value::Str(self.to_string()) * rhs,
Value::Int(_) => Value::Int(self.into_isize()) * rhs, Value::Int(_) => Value::Int(self.into_isize()) * rhs,
Value::Bool(_) => Value::Bool(self.into_bool()) * rhs,
Value::Abool(_) => Value::Abool(self.into_abool()) * rhs, Value::Abool(_) => Value::Abool(self.into_abool()) * rhs,
Value::Functio(_) => Value::Functio(self.into_functio()) * rhs, Value::Functio(_) => Value::Functio(self.into_functio()) * rhs,
Value::Cart(_) => Value::Cart(self.into_cart()) * rhs, Value::Cart(_) => Value::Cart(self.into_cart()) * rhs,
}, },
Value::Str(s) => Value::Str(s.repeat(rhs.into_isize() as usize)), Value::Str(s) => Value::Str(s.repeat(rhs.into_isize() as usize)),
Value::Int(i) => Value::Int(i.wrapping_mul(rhs.into_isize())), Value::Int(i) => Value::Int(i.wrapping_mul(rhs.into_isize())),
Value::Bool(b) => Value::Bool(b && rhs.into_bool()),
Value::Abool(_) => { Value::Abool(_) => {
Value::Abool(Value::Int(self.into_isize().min(rhs.into_isize())).into_abool()) Value::Abool(Value::Int(self.into_isize().min(rhs.into_isize())).into_abool())
} }
@ -734,7 +699,6 @@ impl ops::Div for Value {
Value::Nul => Value::Nul, Value::Nul => Value::Nul,
Value::Str(_) => Value::Str(self.to_string()) / rhs, Value::Str(_) => Value::Str(self.to_string()) / rhs,
Value::Int(_) => Value::Int(self.into_isize()) / rhs, Value::Int(_) => Value::Int(self.into_isize()) / rhs,
Value::Bool(_) => Value::Bool(self.into_bool()) / rhs,
Value::Abool(_) => Value::Abool(self.into_abool()) / rhs, Value::Abool(_) => Value::Abool(self.into_abool()) / rhs,
Value::Functio(_) => Value::Functio(self.into_functio()) / rhs, Value::Functio(_) => Value::Functio(self.into_functio()) / rhs,
Value::Cart(_) => Value::Cart(self.into_cart()) / rhs, Value::Cart(_) => Value::Cart(self.into_cart()) / rhs,
@ -754,7 +718,6 @@ impl ops::Div for Value {
0 => consts::ANSWER, 0 => consts::ANSWER,
x => x, x => x,
})), })),
Value::Bool(b) => Value::Bool(!b || rhs.into_bool()),
Value::Abool(_) => !self + rhs, Value::Abool(_) => !self + rhs,
Value::Functio(f) => Value::Functio(match f { Value::Functio(f) => Value::Functio(match f {
Functio::Bf { Functio::Bf {
@ -832,7 +795,6 @@ impl ops::Not for Value {
Value::Nul => Value::Nul, Value::Nul => Value::Nul,
Value::Str(s) => Value::Str(s.chars().rev().collect()), Value::Str(s) => Value::Str(s.chars().rev().collect()),
Value::Int(i) => Value::Int(i.swap_bytes()), Value::Int(i) => Value::Int(i.swap_bytes()),
Value::Bool(b) => Value::Bool(!b),
Value::Abool(a) => Value::Abool(match a { Value::Abool(a) => Value::Abool(match a {
Abool::Never => Abool::Always, Abool::Never => Abool::Always,
Abool::Sometimes => Abool::Sometimes, Abool::Sometimes => Abool::Sometimes,
@ -895,7 +857,6 @@ impl PartialEq for Value {
Value::Nul => matches!(other, Value::Nul), Value::Nul => matches!(other, Value::Nul),
Value::Str(s) => *s == other.to_string(), Value::Str(s) => *s == other.to_string(),
Value::Int(i) => *i == other.into_isize(), Value::Int(i) => *i == other.into_isize(),
Value::Bool(b) => *b == other.into_bool(),
Value::Abool(a) => *a == other.into_abool(), Value::Abool(a) => *a == other.into_abool(),
Value::Functio(f) => *f == other.into_functio(), Value::Functio(f) => *f == other.into_functio(),
Value::Cart(c) => *c == other.into_cart(), Value::Cart(c) => *c == other.into_cart(),
@ -920,7 +881,6 @@ impl PartialOrd for Value {
} }
Value::Str(s) => Some(s.cmp(&other.to_string())), Value::Str(s) => Some(s.cmp(&other.to_string())),
Value::Int(i) => Some(i.cmp(&other.into_isize())), Value::Int(i) => Some(i.cmp(&other.into_isize())),
Value::Bool(b) => Some(b.cmp(&other.into_bool())),
Value::Abool(a) => a.partial_cmp(&other.into_abool()), Value::Abool(a) => a.partial_cmp(&other.into_abool()),
Value::Functio(_) => self.clone().into_isize().partial_cmp(&other.into_isize()), Value::Functio(_) => self.clone().into_isize().partial_cmp(&other.into_isize()),
Value::Cart(c) => Some(c.len().cmp(&other.into_cart().len())), Value::Cart(c) => Some(c.len().cmp(&other.into_cart().len())),
@ -934,7 +894,6 @@ impl Display for Value {
Value::Nul => write!(f, "nul"), Value::Nul => write!(f, "nul"),
Value::Str(v) => write!(f, "{}", v), Value::Str(v) => write!(f, "{}", v),
Value::Int(v) => write!(f, "{}", v), Value::Int(v) => write!(f, "{}", v),
Value::Bool(v) => write!(f, "{}", v),
Value::Abool(v) => write!(f, "{}", v), Value::Abool(v) => write!(f, "{}", v),
Value::Functio(v) => match v { Value::Functio(v) => match v {
Functio::Bf { Functio::Bf {