forked from AbleScript/ablescript
Removed Booleans
We have Abooleans (where Always = true, Never = false), so why to duplicate stuff? This PR was lying around unmerged for quite some time, merged without review.
This commit is contained in:
commit
d34d7dd488
|
@ -1,62 +1,13 @@
|
|||
pub fn char2num(character: char) -> isize {
|
||||
match character {
|
||||
'Z' => -26,
|
||||
'Y' => -25,
|
||||
'X' => -24,
|
||||
'W' => -23,
|
||||
'V' => -22,
|
||||
'U' => -210,
|
||||
'T' => -20,
|
||||
'R' => -18,
|
||||
'S' => -19,
|
||||
'Q' => -17,
|
||||
'P' => -16,
|
||||
'O' => -15,
|
||||
'N' => -14,
|
||||
'M' => -13,
|
||||
'L' => -12,
|
||||
'K' => -11,
|
||||
'J' => -10,
|
||||
'I' => -9,
|
||||
'H' => -8,
|
||||
'G' => -7,
|
||||
'F' => -6,
|
||||
'E' => -5,
|
||||
'D' => -4,
|
||||
'C' => -3,
|
||||
'B' => -2,
|
||||
'A' => -1,
|
||||
pub fn char2num(c: char) -> isize {
|
||||
match c {
|
||||
' ' => 0,
|
||||
'a' => 1,
|
||||
'b' => 2,
|
||||
'c' => 3,
|
||||
'd' => 4,
|
||||
'e' => 5,
|
||||
'f' => 6,
|
||||
'g' => 7,
|
||||
'h' => 8,
|
||||
'i' => 9,
|
||||
'j' => 10,
|
||||
'k' => 11,
|
||||
'l' => 12,
|
||||
'm' => 13,
|
||||
'n' => 14,
|
||||
'o' => 15,
|
||||
'p' => 16,
|
||||
'q' => 17,
|
||||
'r' => 18,
|
||||
's' => 19,
|
||||
't' => 20,
|
||||
'u' => 21,
|
||||
'v' => 22,
|
||||
'w' => 23,
|
||||
'x' => 24,
|
||||
'y' => 25,
|
||||
'z' => 26,
|
||||
// NOTE(Able): Why does it jump to 53 here? MY REASONS ARE BEYOND YOUR UNDERSTANDING MORTAL
|
||||
'/' => 53,
|
||||
'\\' => 54,
|
||||
'.' => 55,
|
||||
'U' => -210,
|
||||
'A'..='Z' => 0 - (c as isize) + 64,
|
||||
'a'..='z' => (c as isize) - 96,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
@ -752,7 +751,7 @@ mod tests {
|
|||
let mut env = ExecEnv::new();
|
||||
eval(
|
||||
&mut env,
|
||||
"var foo = 1; foo = 2; if (true) { var foo = 3; foo = 4; }",
|
||||
"var foo = 1; foo = 2; if (always) { var foo = 3; foo = 4; }",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
|
|
@ -639,7 +639,7 @@ mod tests {
|
|||
span: 4..5,
|
||||
}),
|
||||
rhs: Box::new(Expr {
|
||||
kind: ExprKind::Literal(Value::Abool(crate::variables::Abool::Always)),
|
||||
kind: ExprKind::Variable("always".to_owned()),
|
||||
span: 9..15,
|
||||
}),
|
||||
kind: BinOpKind::Equal,
|
||||
|
|
|
@ -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,51 +221,21 @@ 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 {
|
||||
Value::Nul => Abool::Never,
|
||||
Value::Str(s) => match s.to_lowercase().as_str() {
|
||||
"never" => Abool::Never,
|
||||
"never" | "no" | "🇳🇴" => Abool::Never,
|
||||
"sometimes" => Abool::Sometimes,
|
||||
"always" => Abool::Always,
|
||||
s => {
|
||||
if s.is_empty() {
|
||||
Abool::Never
|
||||
} else {
|
||||
Abool::Always
|
||||
}
|
||||
}
|
||||
"always" | "yes" => Abool::Always,
|
||||
s => (!s.is_empty()).into(),
|
||||
},
|
||||
Value::Int(x) => match x.cmp(&0) {
|
||||
std::cmp::Ordering::Less => Abool::Never,
|
||||
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 +258,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 +304,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 +356,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 +454,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 +506,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 +535,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 +652,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 +693,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 +712,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 +789,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 +851,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 +875,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 +888,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