From 8c867c789933e758a61851055a2be60f231945c9 Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 13 Feb 2022 00:55:19 +0100 Subject: [PATCH 1/3] Value::Int is isize --- ablescript/src/consts.rs | 4 +- ablescript/src/interpret.rs | 2 +- ablescript/src/lexer.rs | 2 +- ablescript/src/variables.rs | 76 ++++++++++++++++++------------------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/ablescript/src/consts.rs b/ablescript/src/consts.rs index 8eaeb51d..eb919490 100644 --- a/ablescript/src/consts.rs +++ b/ablescript/src/consts.rs @@ -21,7 +21,7 @@ pub fn ablescript_consts() -> HashMap { ("GRAVITY", Int(10)), // Earth surface gravity, m/s ("RNG", Int(12)), // Kixiron#5289 Randomly rolled dice ("STD_RNG", Int(4)), // The standard random number is 4 (https://xkcd.com/221/) - ("INF", Int(i32::max_value())), // The biggest number + ("INF", Int(isize::max_value())), // The biggest number ("INTERESSANT", Int(114514)), // HTGAzureX1212.#5959 intéressant number ("FUNNY", Int(69)), // HTGAzureX1212.#5959 funny number ( @@ -44,4 +44,4 @@ pub fn ablescript_consts() -> HashMap { map } -pub const ANSWER: i32 = 42; +pub const ANSWER: isize = 42; diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index af679d4b..d1eb20c8 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -224,7 +224,7 @@ impl ExecEnv { instructions: code.to_owned(), tape_len: tape_len .as_ref() - .map(|tape_len| self.eval_expr(tape_len).map(|v| v.into_i32() as usize)) + .map(|tape_len| self.eval_expr(tape_len).map(|v| v.into_isize() as usize)) .unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?, }), ); diff --git a/ablescript/src/lexer.rs b/ablescript/src/lexer.rs index a6c5940b..75200160 100644 --- a/ablescript/src/lexer.rs +++ b/ablescript/src/lexer.rs @@ -134,7 +134,7 @@ pub enum Token { /// Integer #[regex(r"-?[0-9]+", get_value)] - Integer(i32), + Integer(isize), /// A C-complaint identifier #[regex(r"[a-zA-Z_][a-zA-Z_0-9]*", get_ident)] diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index 805c54ef..cb5a9f65 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -127,7 +127,7 @@ pub type Cart = HashMap>>; pub enum Value { Nul, Str(String), - Int(i32), + Int(isize), Bool(bool), Abool(Abool), Functio(Functio), @@ -163,12 +163,12 @@ impl Value { /// any IO errors will cause a panic. pub fn bf_write(&self, stream: &mut impl Write) { stream - .write_all(&[self.clone().into_i32() as u8]) + .write_all(&[self.clone().into_isize() as u8]) .expect("Failed to write to Brainfuck input"); } /// Coerce a value to an integer. - pub fn into_i32(self) -> i32 { + pub fn into_isize(self) -> isize { match self { Value::Abool(a) => a as _, Value::Bool(b) => b as _, @@ -176,32 +176,32 @@ impl Value { Functio::Bf { instructions, tape_len, - } => instructions.into_iter().map(|x| x as i32).sum::() * tape_len as i32, + } => instructions.into_iter().map(|x| x as isize).sum::() * tape_len as isize, Functio::Able { params, body } => { params .into_iter() .map(|x| x.bytes().map(|x| x as i32).sum::()) - .sum::() - + body.len() as i32 + .sum::() + + body.len() as isize } Functio::Builtin(b) => (b.fn_addr() + b.arity) as _, Functio::Chain { functios, kind } => { let (lf, rf) = *functios; - Value::Functio(lf).into_i32() - + Value::Functio(rf).into_i32() + Value::Functio(lf).into_isize() + + Value::Functio(rf).into_isize() * match kind { FunctioChainKind::Equal => -1, FunctioChainKind::ByArity => 1, } } - Functio::Eval(code) => code.bytes().map(|x| x as i32).sum(), + Functio::Eval(code) => code.bytes().map(|x| x as isize).sum(), }, Value::Int(i) => i, Value::Nul => consts::ANSWER, Value::Str(text) => text.parse().unwrap_or(consts::ANSWER), Value::Cart(c) => c .into_iter() - .map(|(i, v)| i.into_i32() * v.borrow().clone().into_i32()) + .map(|(i, v)| i.into_isize() * v.borrow().clone().into_isize()) .sum(), } } @@ -342,7 +342,7 @@ impl Value { Value::Cart(c) => { let kind = if let Some(114514) = c .get(&Value::Str("1452251871514141792252515212116".to_owned())) - .map(|x| x.borrow().to_owned().into_i32()) + .map(|x| x.borrow().to_owned().into_isize()) { FunctioChainKind::Equal } else { @@ -515,15 +515,15 @@ impl Value { let (lhs, rhs) = *functios.clone(); match kind { FunctioChainKind::Equal => { - Value::Int(Value::Functio(lhs).into_i32()) - + Value::Int(Value::Functio(rhs).into_i32()) + Value::Int(Value::Functio(lhs).into_isize()) + + Value::Int(Value::Functio(rhs).into_isize()) } FunctioChainKind::ByArity => { - Value::Int(Value::Functio(lhs).into_i32()) - * Value::Int(Value::Functio(rhs).into_i32()) + Value::Int(Value::Functio(lhs).into_isize()) + * Value::Int(Value::Functio(rhs).into_isize()) } } - .into_i32() + .into_isize() } Functio::Eval(s) => s.len() as _, }, @@ -540,17 +540,17 @@ impl ops::Add for Value { Value::Nul => match rhs { Value::Nul => Value::Nul, Value::Str(_) => Value::Str(self.to_string()) + rhs, - Value::Int(_) => Value::Int(self.into_i32()) + 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.to_string())), - Value::Int(i) => Value::Int(i.wrapping_add(rhs.into_i32())), + 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_i32().max(rhs.into_i32())).into_abool()) + Value::Abool(Value::Int(self.into_isize().max(rhs.into_isize())).into_abool()) } Value::Functio(f) => Value::Functio(Functio::Chain { functios: Box::new((f, rhs.into_functio())), @@ -571,14 +571,14 @@ impl ops::Sub for Value { Value::Nul => match rhs { Value::Nul => Value::Nul, Value::Str(_) => Value::Str(self.to_string()) - rhs, - Value::Int(_) => Value::Int(self.into_i32()) - 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_i32())), + 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 { @@ -630,14 +630,14 @@ impl ops::Sub for Value { params: lhs_params, body: lhs_body, }) - .into_i32() - - Value::Functio(rhs).into_i32(), + .into_isize() + - Value::Functio(rhs).into_isize(), ) .into_functio(), }, Functio::Builtin(b) => { let arity = b.arity; - let resulting_arity = arity.saturating_sub(rhs.into_i32() as usize); + let resulting_arity = arity.saturating_sub(rhs.into_isize() as usize); Functio::Builtin(BuiltinFunctio::new( move |args| { @@ -692,17 +692,17 @@ impl ops::Mul for Value { Value::Nul => match rhs { Value::Nul => Value::Nul, Value::Str(_) => Value::Str(self.to_string()) * rhs, - Value::Int(_) => Value::Int(self.into_i32()) * 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_i32() as usize)), - Value::Int(i) => Value::Int(i.wrapping_mul(rhs.into_i32())), + 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_i32().min(rhs.into_i32())).into_abool()) + Value::Abool(Value::Int(self.into_isize().min(rhs.into_isize())).into_abool()) } Value::Functio(f) => Value::Functio(Functio::Chain { functios: Box::new((f, rhs.into_functio())), @@ -735,7 +735,7 @@ impl ops::Div for Value { Value::Nul => match rhs { Value::Nul => Value::Nul, Value::Str(_) => Value::Str(self.to_string()) / rhs, - Value::Int(_) => Value::Int(self.into_i32()) / 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, @@ -752,7 +752,7 @@ impl ops::Div for Value { }) .collect(), ), - Value::Int(i) => Value::Int(i.wrapping_div(match rhs.into_i32() { + Value::Int(i) => Value::Int(i.wrapping_div(match rhs.into_isize() { 0 => consts::ANSWER, x => x, })), @@ -763,7 +763,7 @@ impl ops::Div for Value { instructions, tape_len, } => { - let fraction = 1.0 / rhs.into_i32() as f64; + let fraction = 1.0 / rhs.into_isize() as f64; let len = instructions.len(); Functio::Bf { instructions: instructions @@ -774,7 +774,7 @@ impl ops::Div for Value { } } Functio::Able { params, body } => { - let fraction = 1.0 / rhs.into_i32() as f64; + let fraction = 1.0 / rhs.into_isize() as f64; let len = body.len(); Functio::Able { params, @@ -785,7 +785,7 @@ impl ops::Div for Value { } } Functio::Builtin(b) => Functio::Builtin(BuiltinFunctio { - arity: b.arity + rhs.into_i32() as usize, + arity: b.arity + rhs.into_isize() as usize, ..b }), Functio::Chain { functios, kind } => { @@ -799,14 +799,14 @@ impl ops::Div for Value { } } Functio::Eval(s) => { - let fraction = 1.0 / rhs.into_i32() as f64; + let fraction = 1.0 / rhs.into_isize() as f64; let len = s.len(); Functio::Eval(s.chars().take((len as f64 * fraction) as usize).collect()) } }), Value::Cart(c) => { let cart_len = c.len(); - let chunk_len = rhs.into_i32() as usize; + let chunk_len = rhs.into_isize() as usize; Value::Cart( c.into_iter() @@ -896,7 +896,7 @@ impl PartialEq for Value { match self { Value::Nul => matches!(other, Value::Nul), Value::Str(s) => *s == other.to_string(), - Value::Int(i) => *i == other.into_i32(), + 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(), @@ -921,10 +921,10 @@ impl PartialOrd for Value { } } Value::Str(s) => Some(s.cmp(&other.to_string())), - Value::Int(i) => Some(i.cmp(&other.into_i32())), + 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_i32().partial_cmp(&other.into_i32()), + Value::Functio(_) => self.clone().into_isize().partial_cmp(&other.into_isize()), Value::Cart(c) => Some(c.len().cmp(&other.into_cart().len())), } } From 5d29c8cf87bf2d0933531ba8c40bca18837e4575 Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 13 Feb 2022 00:55:51 +0100 Subject: [PATCH 2/3] fixed base 55 --- ablescript/src/base_55.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ablescript/src/base_55.rs b/ablescript/src/base_55.rs index a8222273..3cabcb10 100644 --- a/ablescript/src/base_55.rs +++ b/ablescript/src/base_55.rs @@ -1,4 +1,4 @@ -pub fn char2num(character: char) -> i32 { +pub fn char2num(character: char) -> isize { match character { 'Z' => -26, 'Y' => -25, @@ -66,7 +66,7 @@ mod tests { use super::*; #[test] fn str_to_base55() { - let chrs: Vec = "AbleScript".chars().map(char2num).collect(); + let chrs: Vec = "AbleScript".chars().map(char2num).collect(); assert_eq!(chrs, &[-1, 2, 12, 5, -19, 3, 18, 9, 16, 20]); } } From e79c98f2f6ba779af40897f333867db1fd5b2190 Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 13 Feb 2022 00:58:50 +0100 Subject: [PATCH 3/3] Fixed all errors --- ablescript/src/interpret.rs | 4 ++-- ablescript/src/variables.rs | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index d1eb20c8..a2239e27 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -273,7 +273,7 @@ impl ExecEnv { let mut value = 0; for _ in 0..READ_BITS { value <<= 1; - value += self.get_bit()? as i32; + value += self.get_bit()? as isize; } self.assign(assignable, Value::Int(value))?; @@ -644,7 +644,7 @@ mod tests { env.eval_expr(&Expr { kind: ExprKind::BinOp { lhs: Box::new(Expr { - kind: ExprKind::Literal(Value::Int(i32::MAX)), + kind: ExprKind::Literal(Value::Int(isize::MAX)), span: 1..1, }), rhs: Box::new(Expr { diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index cb5a9f65..198361c3 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -176,11 +176,13 @@ impl Value { Functio::Bf { instructions, tape_len, - } => instructions.into_iter().map(|x| x as isize).sum::() * tape_len as isize, + } => { + instructions.into_iter().map(|x| x as isize).sum::() * tape_len as isize + } Functio::Able { params, body } => { params .into_iter() - .map(|x| x.bytes().map(|x| x as i32).sum::()) + .map(|x| x.bytes().map(|x| x as isize).sum::()) .sum::() + body.len() as isize } @@ -261,14 +263,14 @@ impl Value { ) .into_abool(), Functio::Able { params, body } => { - let str_to_i32 = - |x: String| -> i32 { x.as_bytes().into_iter().map(|x| *x as i32).sum() }; + let str_to_isize = + |x: String| -> isize { x.as_bytes().into_iter().map(|x| *x as isize).sum() }; - let params: i32 = params.into_iter().map(str_to_i32).sum(); - let body: i32 = body + let params: isize = params.into_iter().map(str_to_isize).sum(); + let body: isize = body .into_iter() .map(|x| format!("{:?}", x)) - .map(str_to_i32) + .map(str_to_isize) .sum(); Value::Int((params + body) % 3 - 1).into_abool() @@ -373,7 +375,7 @@ impl Value { .enumerate() .map(|(i, x)| { ( - Value::Int(i as i32 + 1), + Value::Int(i as isize + 1), Rc::new(RefCell::new(Value::Str(x.to_string()))), ) }) @@ -388,7 +390,7 @@ impl Value { .enumerate() .map(|(i, x)| { ( - Value::Int(i as i32 + 1), + Value::Int(i as isize + 1), Rc::new(RefCell::new(Value::Str(x))), ) }) @@ -399,7 +401,7 @@ impl Value { .enumerate() .map(|(i, x)| { ( - Value::Int(i as i32 + 1), + Value::Int(i as isize + 1), Rc::new(RefCell::new(Value::Str(format!("{:?}", x)))), ) }) @@ -427,7 +429,7 @@ impl Value { .enumerate() .map(|(i, x)| { ( - Value::Int(i as i32 + 1), + Value::Int(i as isize + 1), Rc::new(RefCell::new( char::from_u32(x as u32) .map(|x| Value::Str(x.to_string())) @@ -480,7 +482,7 @@ impl Value { } /// Get a lenght of a value - pub fn len(&self) -> i32 { + pub fn len(&self) -> isize { match self { Value::Nul => 0, Value::Str(s) => s.len() as _, @@ -508,9 +510,7 @@ impl Value { tape_len, } => (instructions.len() + tape_len) as _, Functio::Able { params, body } => (params.len() + format!("{:?}", body).len()) as _, - Functio::Builtin(b) => { - (std::mem::size_of_val(b.function.as_ref()) + b.arity) as i32 - } + Functio::Builtin(b) => (std::mem::size_of_val(b.function.as_ref()) + b.arity) as _, Functio::Chain { functios, kind } => { let (lhs, rhs) = *functios.clone(); match kind { @@ -746,7 +746,7 @@ impl ops::Div for Value { .enumerate() .map(|(i, x)| { ( - Value::Int(i as i32 + 1), + Value::Int(i as isize + 1), Rc::new(RefCell::new(Value::Str(x.to_owned()))), ) }) @@ -815,7 +815,7 @@ impl ops::Div for Value { .enumerate() .map(|(k, v)| { ( - Value::Int(k as i32 + 1), + Value::Int(k as isize + 1), Rc::new(RefCell::new(Value::Cart(v.iter().cloned().collect()))), ) })