From 6f663839a7ddaf53837cfea101ffe27e4fcf2d0c Mon Sep 17 00:00:00 2001 From: Erin Date: Wed, 30 Mar 2022 20:55:05 +0200 Subject: [PATCH 1/4] Removed booleans --- ablescript/src/consts.rs | 2 - ablescript/src/interpret.rs | 21 +++++------ ablescript/src/variables.rs | 73 ++++++++----------------------------- 3 files changed, 26 insertions(+), 70 deletions(-) diff --git a/ablescript/src/consts.rs b/ablescript/src/consts.rs index edc49685..a1042ba1 100644 --- a/ablescript/src/consts.rs +++ b/ablescript/src/consts.rs @@ -31,8 +31,6 @@ pub fn ablescript_consts() -> HashMap { ("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)), diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index 95aabe10..452febac 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -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, Vec) { + fn deinterlace(args: &[ValueRef], arities: (usize, usize)) -> (Vec, Vec) { 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, diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index 22b4aa8a..b30601ab 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -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 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 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 { From c1c8f17c8d4478036bb862fc33803324ebaaffa0 Mon Sep 17 00:00:00 2001 From: Erin Date: Wed, 30 Mar 2022 20:56:59 +0200 Subject: [PATCH 2/4] Improved type coercions --- ablescript/src/variables.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index b30601ab..2f93eb51 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -226,16 +226,10 @@ impl Value { 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, From a3d2922f7eba0956b3825431a465e5555100743d Mon Sep 17 00:00:00 2001 From: Erin Date: Wed, 30 Mar 2022 21:03:17 +0200 Subject: [PATCH 3/4] fixed tests --- ablescript/src/interpret.rs | 2 +- ablescript/src/parser.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index 452febac..caed5f8b 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -751,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(); diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index 59a85810..4b3a070b 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -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, From d890d7cfff9037a416e23ef9d032e44d67522163 Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 2 Apr 2022 01:06:47 +0200 Subject: [PATCH 4/4] Simplified base 55 --- ablescript/src/base_55.rs | 59 ++++----------------------------------- 1 file changed, 5 insertions(+), 54 deletions(-) diff --git a/ablescript/src/base_55.rs b/ablescript/src/base_55.rs index 3cabcb10..9849fb6e 100644 --- a/ablescript/src/base_55.rs +++ b/ablescript/src/base_55.rs @@ -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, } }