Removed booleans
This commit is contained in:
parent
86e1391ea2
commit
6f663839a7
|
@ -31,8 +31,6 @@ pub fn ablescript_consts() -> HashMap<String, Variable> {
|
|||
("OCTOTHORPE", Str("#".to_owned())), // It's an octothorpe
|
||||
("ANSWER", Int(ANSWER)),
|
||||
("nul", Nul),
|
||||
("true", Bool(true)),
|
||||
("false", Bool(false)),
|
||||
("always", Abool(crate::variables::Abool::Always)),
|
||||
("sometimes", Abool(crate::variables::Abool::Sometimes)),
|
||||
("never", Abool(crate::variables::Abool::Never)),
|
||||
|
|
|
@ -164,10 +164,10 @@ impl ExecEnv {
|
|||
Subtract => lhs - rhs,
|
||||
Multiply => lhs * rhs,
|
||||
Divide => lhs / rhs,
|
||||
Greater => Value::Bool(lhs > rhs),
|
||||
Less => Value::Bool(lhs < rhs),
|
||||
Equal => Value::Bool(lhs == rhs),
|
||||
NotEqual => Value::Bool(lhs != rhs),
|
||||
Greater => Value::Abool((lhs > rhs).into()),
|
||||
Less => Value::Abool((lhs < rhs).into()),
|
||||
Equal => Value::Abool((lhs == rhs).into()),
|
||||
NotEqual => Value::Abool((lhs != rhs).into()),
|
||||
}
|
||||
}
|
||||
Not(expr) => !self.eval_expr(expr)?,
|
||||
|
@ -249,7 +249,7 @@ impl ExecEnv {
|
|||
);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -459,10 +459,7 @@ impl ExecEnv {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn deinterlace(
|
||||
args: &[ValueRef],
|
||||
arities: (usize, usize),
|
||||
) -> (Vec<ValueRef>, Vec<ValueRef>) {
|
||||
fn deinterlace(args: &[ValueRef], arities: (usize, usize)) -> (Vec<ValueRef>, Vec<ValueRef>) {
|
||||
let n_alternations = usize::min(arities.0, arities.1);
|
||||
let (extra_l, extra_r) = match Ord::cmp(&arities.0, &arities.1) {
|
||||
Ordering::Less => (0, arities.1 - arities.0),
|
||||
|
@ -630,8 +627,10 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
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.
|
||||
use crate::variables::Abool;
|
||||
|
||||
let env = ExecEnv::new();
|
||||
assert_eq!(
|
||||
env.eval_expr(&Expr {
|
||||
|
@ -641,7 +640,7 @@ mod tests {
|
|||
span: 1..1,
|
||||
}),
|
||||
rhs: Box::new(Expr {
|
||||
kind: ExprKind::Literal(Value::Bool(true)),
|
||||
kind: ExprKind::Literal(Value::Abool(Abool::Always)),
|
||||
span: 1..1,
|
||||
}),
|
||||
kind: crate::ast::BinOpKind::Add,
|
||||
|
|
|
@ -21,6 +21,15 @@ pub enum Abool {
|
|||
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 {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
|
@ -31,12 +40,12 @@ impl Display for Abool {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<Abool> for bool {
|
||||
fn from(val: Abool) -> Self {
|
||||
match val {
|
||||
Abool::Never => false,
|
||||
Abool::Always => true,
|
||||
Abool::Sometimes => rand::thread_rng().gen(), // NOTE(Able): This is amazing and should be applied anywhere abooleans exist
|
||||
impl From<bool> for Abool {
|
||||
fn from(b: bool) -> Self {
|
||||
if b {
|
||||
Abool::Always
|
||||
} else {
|
||||
Abool::Never
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -135,7 +144,6 @@ pub enum Value {
|
|||
Nul,
|
||||
Str(String),
|
||||
Int(isize),
|
||||
Bool(bool),
|
||||
Abool(Abool),
|
||||
Functio(Functio),
|
||||
Cart(Cart),
|
||||
|
@ -154,7 +162,6 @@ impl Hash for Value {
|
|||
Value::Nul => (),
|
||||
Value::Str(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::Functio(statements) => statements.hash(state),
|
||||
Value::Cart(_) => self.to_string().hash(state),
|
||||
|
@ -178,7 +185,6 @@ impl Value {
|
|||
pub fn into_isize(self) -> isize {
|
||||
match self {
|
||||
Value::Abool(a) => a as _,
|
||||
Value::Bool(b) => b as _,
|
||||
Value::Functio(f) => match f {
|
||||
Functio::Bf {
|
||||
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.
|
||||
pub fn into_abool(self) -> Abool {
|
||||
match self {
|
||||
|
@ -253,13 +242,6 @@ impl Value {
|
|||
std::cmp::Ordering::Equal => Abool::Sometimes,
|
||||
std::cmp::Ordering::Greater => Abool::Always,
|
||||
},
|
||||
Value::Bool(b) => {
|
||||
if b {
|
||||
Abool::Always
|
||||
} else {
|
||||
Abool::Never
|
||||
}
|
||||
}
|
||||
Value::Abool(a) => a,
|
||||
Value::Functio(f) => match f {
|
||||
Functio::Bf {
|
||||
|
@ -282,7 +264,7 @@ impl Value {
|
|||
|
||||
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 } => {
|
||||
let (lhs, rhs) = *functios;
|
||||
match kind {
|
||||
|
@ -328,14 +310,6 @@ impl Value {
|
|||
},
|
||||
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 {
|
||||
Abool::Never => "".to_owned(),
|
||||
Abool::Sometimes => {
|
||||
|
@ -388,7 +362,6 @@ impl Value {
|
|||
})
|
||||
.collect(),
|
||||
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::Functio(f) => match f {
|
||||
Functio::Able { params, body } => {
|
||||
|
@ -487,8 +460,6 @@ impl Value {
|
|||
Value::Nul => 0,
|
||||
Value::Str(s) => s.len() as _,
|
||||
Value::Int(i) => i.count_zeros() as _,
|
||||
Value::Bool(b) if *b => -2,
|
||||
Value::Bool(_) => 2,
|
||||
Value::Abool(a) => match a {
|
||||
Abool::Never => -3,
|
||||
Abool::Sometimes => {
|
||||
|
@ -541,14 +512,12 @@ impl ops::Add for Value {
|
|||
Value::Nul => Value::Nul,
|
||||
Value::Str(_) => Value::Str(self.to_string()) + 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::Functio(_) => Value::Functio(self.into_functio()) + rhs,
|
||||
Value::Cart(_) => Value::Cart(self.into_cart()) + rhs,
|
||||
},
|
||||
Value::Str(s) => Value::Str(format!("{s}{rhs}")),
|
||||
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::Int(self.into_isize().max(rhs.into_isize())).into_abool())
|
||||
}
|
||||
|
@ -572,14 +541,12 @@ impl ops::Sub for Value {
|
|||
Value::Nul => Value::Nul,
|
||||
Value::Str(_) => Value::Str(self.to_string()) - 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::Functio(_) => Value::Functio(self.into_functio()) - rhs,
|
||||
Value::Cart(_) => Value::Cart(self.into_cart()) - rhs,
|
||||
},
|
||||
Value::Str(s) => Value::Str(s.replace(&rhs.to_string(), "")),
|
||||
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::Functio(f) => Value::Functio(match f {
|
||||
Functio::Bf {
|
||||
|
@ -691,14 +658,12 @@ impl ops::Mul for Value {
|
|||
Value::Nul => Value::Nul,
|
||||
Value::Str(_) => Value::Str(self.to_string()) * 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::Functio(_) => Value::Functio(self.into_functio()) * rhs,
|
||||
Value::Cart(_) => Value::Cart(self.into_cart()) * rhs,
|
||||
},
|
||||
Value::Str(s) => Value::Str(s.repeat(rhs.into_isize() as usize)),
|
||||
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::Int(self.into_isize().min(rhs.into_isize())).into_abool())
|
||||
}
|
||||
|
@ -734,7 +699,6 @@ impl ops::Div for Value {
|
|||
Value::Nul => Value::Nul,
|
||||
Value::Str(_) => Value::Str(self.to_string()) / 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::Functio(_) => Value::Functio(self.into_functio()) / rhs,
|
||||
Value::Cart(_) => Value::Cart(self.into_cart()) / rhs,
|
||||
|
@ -754,7 +718,6 @@ impl ops::Div for Value {
|
|||
0 => consts::ANSWER,
|
||||
x => x,
|
||||
})),
|
||||
Value::Bool(b) => Value::Bool(!b || rhs.into_bool()),
|
||||
Value::Abool(_) => !self + rhs,
|
||||
Value::Functio(f) => Value::Functio(match f {
|
||||
Functio::Bf {
|
||||
|
@ -832,7 +795,6 @@ impl ops::Not for Value {
|
|||
Value::Nul => Value::Nul,
|
||||
Value::Str(s) => Value::Str(s.chars().rev().collect()),
|
||||
Value::Int(i) => Value::Int(i.swap_bytes()),
|
||||
Value::Bool(b) => Value::Bool(!b),
|
||||
Value::Abool(a) => Value::Abool(match a {
|
||||
Abool::Never => Abool::Always,
|
||||
Abool::Sometimes => Abool::Sometimes,
|
||||
|
@ -895,7 +857,6 @@ impl PartialEq for Value {
|
|||
Value::Nul => matches!(other, Value::Nul),
|
||||
Value::Str(s) => *s == other.to_string(),
|
||||
Value::Int(i) => *i == other.into_isize(),
|
||||
Value::Bool(b) => *b == other.into_bool(),
|
||||
Value::Abool(a) => *a == other.into_abool(),
|
||||
Value::Functio(f) => *f == other.into_functio(),
|
||||
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::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::Functio(_) => self.clone().into_isize().partial_cmp(&other.into_isize()),
|
||||
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::Str(v) => write!(f, "{}", v),
|
||||
Value::Int(v) => write!(f, "{}", v),
|
||||
Value::Bool(v) => write!(f, "{}", v),
|
||||
Value::Abool(v) => write!(f, "{}", v),
|
||||
Value::Functio(v) => match v {
|
||||
Functio::Bf {
|
||||
|
|
Loading…
Reference in a new issue