Merge pull request 'change/isize-as-int' (#4) from change/isize-as-int into master

Reviewed-on: https://git.ablecorp.us:443/AbleScript/able-script/pulls/4
This commit is contained in:
Erin 2022-02-13 22:17:13 +00:00 committed by ondra05
commit 0134448472
5 changed files with 62 additions and 62 deletions

View file

@ -1,4 +1,4 @@
pub fn char2num(character: char) -> i32 { pub fn char2num(character: char) -> isize {
match character { match character {
'Z' => -26, 'Z' => -26,
'Y' => -25, 'Y' => -25,
@ -66,7 +66,7 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn str_to_base55() { fn str_to_base55() {
let chrs: Vec<i32> = "AbleScript".chars().map(char2num).collect(); let chrs: Vec<isize> = "AbleScript".chars().map(char2num).collect();
assert_eq!(chrs, &[-1, 2, 12, 5, -19, 3, 18, 9, 16, 20]); assert_eq!(chrs, &[-1, 2, 12, 5, -19, 3, 18, 9, 16, 20]);
} }
} }

View file

@ -21,7 +21,7 @@ pub fn ablescript_consts() -> HashMap<String, Variable> {
("GRAVITY", Int(10)), // Earth surface gravity, m/s ("GRAVITY", Int(10)), // Earth surface gravity, m/s
("RNG", Int(12)), // Kixiron#5289 Randomly rolled dice ("RNG", Int(12)), // Kixiron#5289 Randomly rolled dice
("STD_RNG", Int(4)), // The standard random number is 4 (https://xkcd.com/221/) ("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 ("INTERESSANT", Int(114514)), // HTGAzureX1212.#5959 intéressant number
("FUNNY", Int(69)), // HTGAzureX1212.#5959 funny number ("FUNNY", Int(69)), // HTGAzureX1212.#5959 funny number
( (
@ -44,4 +44,4 @@ pub fn ablescript_consts() -> HashMap<String, Variable> {
map map
} }
pub const ANSWER: i32 = 42; pub const ANSWER: isize = 42;

View file

@ -224,7 +224,7 @@ impl ExecEnv {
instructions: code.to_owned(), instructions: code.to_owned(),
tape_len: tape_len tape_len: tape_len
.as_ref() .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))?, .unwrap_or(Ok(crate::brian::DEFAULT_TAPE_SIZE_LIMIT))?,
}), }),
); );
@ -273,7 +273,7 @@ impl ExecEnv {
let mut value = 0; let mut value = 0;
for _ in 0..READ_BITS { for _ in 0..READ_BITS {
value <<= 1; value <<= 1;
value += self.get_bit()? as i32; value += self.get_bit()? as isize;
} }
self.assign(assignable, Value::Int(value))?; self.assign(assignable, Value::Int(value))?;
@ -644,7 +644,7 @@ mod tests {
env.eval_expr(&Expr { env.eval_expr(&Expr {
kind: ExprKind::BinOp { kind: ExprKind::BinOp {
lhs: Box::new(Expr { lhs: Box::new(Expr {
kind: ExprKind::Literal(Value::Int(i32::MAX)), kind: ExprKind::Literal(Value::Int(isize::MAX)),
span: 1..1, span: 1..1,
}), }),
rhs: Box::new(Expr { rhs: Box::new(Expr {

View file

@ -134,7 +134,7 @@ pub enum Token {
/// Integer /// Integer
#[regex(r"-?[0-9]+", get_value)] #[regex(r"-?[0-9]+", get_value)]
Integer(i32), Integer(isize),
/// A C-complaint identifier /// A C-complaint identifier
#[regex(r"[a-zA-Z_][a-zA-Z_0-9]*", get_ident)] #[regex(r"[a-zA-Z_][a-zA-Z_0-9]*", get_ident)]

View file

@ -127,7 +127,7 @@ pub type Cart = HashMap<Value, Rc<RefCell<Value>>>;
pub enum Value { pub enum Value {
Nul, Nul,
Str(String), Str(String),
Int(i32), Int(isize),
Bool(bool), Bool(bool),
Abool(Abool), Abool(Abool),
Functio(Functio), Functio(Functio),
@ -163,12 +163,12 @@ impl Value {
/// any IO errors will cause a panic. /// any IO errors will cause a panic.
pub fn bf_write(&self, stream: &mut impl Write) { pub fn bf_write(&self, stream: &mut impl Write) {
stream stream
.write_all(&[self.clone().into_i32() as u8]) .write_all(&[self.clone().into_isize() as u8])
.expect("Failed to write to Brainfuck input"); .expect("Failed to write to Brainfuck input");
} }
/// Coerce a value to an integer. /// Coerce a value to an integer.
pub fn into_i32(self) -> i32 { 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::Bool(b) => b as _,
@ -176,32 +176,34 @@ impl Value {
Functio::Bf { Functio::Bf {
instructions, instructions,
tape_len, tape_len,
} => instructions.into_iter().map(|x| x as i32).sum::<i32>() * tape_len as i32, } => {
instructions.into_iter().map(|x| x as isize).sum::<isize>() * tape_len as isize
}
Functio::Able { params, body } => { Functio::Able { params, body } => {
params params
.into_iter() .into_iter()
.map(|x| x.bytes().map(|x| x as i32).sum::<i32>()) .map(|x| x.bytes().map(|x| x as isize).sum::<isize>())
.sum::<i32>() .sum::<isize>()
+ body.len() as i32 + body.len() as isize
} }
Functio::Builtin(b) => (b.fn_addr() + b.arity) as _, Functio::Builtin(b) => (b.fn_addr() + b.arity) as _,
Functio::Chain { functios, kind } => { Functio::Chain { functios, kind } => {
let (lf, rf) = *functios; let (lf, rf) = *functios;
Value::Functio(lf).into_i32() Value::Functio(lf).into_isize()
+ Value::Functio(rf).into_i32() + Value::Functio(rf).into_isize()
* match kind { * match kind {
FunctioChainKind::Equal => -1, FunctioChainKind::Equal => -1,
FunctioChainKind::ByArity => 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::Int(i) => i,
Value::Nul => consts::ANSWER, Value::Nul => consts::ANSWER,
Value::Str(text) => text.parse().unwrap_or(consts::ANSWER), Value::Str(text) => text.parse().unwrap_or(consts::ANSWER),
Value::Cart(c) => c Value::Cart(c) => c
.into_iter() .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(), .sum(),
} }
} }
@ -261,14 +263,14 @@ impl Value {
) )
.into_abool(), .into_abool(),
Functio::Able { params, body } => { Functio::Able { params, body } => {
let str_to_i32 = let str_to_isize =
|x: String| -> i32 { x.as_bytes().into_iter().map(|x| *x as i32).sum() }; |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 params: isize = params.into_iter().map(str_to_isize).sum();
let body: i32 = body let body: isize = body
.into_iter() .into_iter()
.map(|x| format!("{:?}", x)) .map(|x| format!("{:?}", x))
.map(str_to_i32) .map(str_to_isize)
.sum(); .sum();
Value::Int((params + body) % 3 - 1).into_abool() Value::Int((params + body) % 3 - 1).into_abool()
@ -342,7 +344,7 @@ impl Value {
Value::Cart(c) => { Value::Cart(c) => {
let kind = if let Some(114514) = c let kind = if let Some(114514) = c
.get(&Value::Str("1452251871514141792252515212116".to_owned())) .get(&Value::Str("1452251871514141792252515212116".to_owned()))
.map(|x| x.borrow().to_owned().into_i32()) .map(|x| x.borrow().to_owned().into_isize())
{ {
FunctioChainKind::Equal FunctioChainKind::Equal
} else { } else {
@ -373,7 +375,7 @@ impl Value {
.enumerate() .enumerate()
.map(|(i, x)| { .map(|(i, x)| {
( (
Value::Int(i as i32 + 1), Value::Int(i as isize + 1),
Rc::new(RefCell::new(Value::Str(x.to_string()))), Rc::new(RefCell::new(Value::Str(x.to_string()))),
) )
}) })
@ -388,7 +390,7 @@ impl Value {
.enumerate() .enumerate()
.map(|(i, x)| { .map(|(i, x)| {
( (
Value::Int(i as i32 + 1), Value::Int(i as isize + 1),
Rc::new(RefCell::new(Value::Str(x))), Rc::new(RefCell::new(Value::Str(x))),
) )
}) })
@ -399,7 +401,7 @@ impl Value {
.enumerate() .enumerate()
.map(|(i, x)| { .map(|(i, x)| {
( (
Value::Int(i as i32 + 1), Value::Int(i as isize + 1),
Rc::new(RefCell::new(Value::Str(format!("{:?}", x)))), Rc::new(RefCell::new(Value::Str(format!("{:?}", x)))),
) )
}) })
@ -427,7 +429,7 @@ impl Value {
.enumerate() .enumerate()
.map(|(i, x)| { .map(|(i, x)| {
( (
Value::Int(i as i32 + 1), Value::Int(i as isize + 1),
Rc::new(RefCell::new( Rc::new(RefCell::new(
char::from_u32(x as u32) char::from_u32(x as u32)
.map(|x| Value::Str(x.to_string())) .map(|x| Value::Str(x.to_string()))
@ -480,7 +482,7 @@ impl Value {
} }
/// Get a lenght of a value /// Get a lenght of a value
pub fn len(&self) -> i32 { pub fn len(&self) -> isize {
match self { match self {
Value::Nul => 0, Value::Nul => 0,
Value::Str(s) => s.len() as _, Value::Str(s) => s.len() as _,
@ -508,22 +510,20 @@ impl Value {
tape_len, tape_len,
} => (instructions.len() + tape_len) as _, } => (instructions.len() + tape_len) as _,
Functio::Able { params, body } => (params.len() + format!("{:?}", body).len()) as _, Functio::Able { params, body } => (params.len() + format!("{:?}", body).len()) as _,
Functio::Builtin(b) => { Functio::Builtin(b) => (std::mem::size_of_val(b.function.as_ref()) + b.arity) as _,
(std::mem::size_of_val(b.function.as_ref()) + b.arity) as i32
}
Functio::Chain { functios, kind } => { Functio::Chain { functios, kind } => {
let (lhs, rhs) = *functios.clone(); let (lhs, rhs) = *functios.clone();
match kind { match kind {
FunctioChainKind::Equal => { FunctioChainKind::Equal => {
Value::Int(Value::Functio(lhs).into_i32()) Value::Int(Value::Functio(lhs).into_isize())
+ Value::Int(Value::Functio(rhs).into_i32()) + Value::Int(Value::Functio(rhs).into_isize())
} }
FunctioChainKind::ByArity => { FunctioChainKind::ByArity => {
Value::Int(Value::Functio(lhs).into_i32()) Value::Int(Value::Functio(lhs).into_isize())
* Value::Int(Value::Functio(rhs).into_i32()) * Value::Int(Value::Functio(rhs).into_isize())
} }
} }
.into_i32() .into_isize()
} }
Functio::Eval(s) => s.len() as _, Functio::Eval(s) => s.len() as _,
}, },
@ -540,17 +540,17 @@ impl ops::Add for Value {
Value::Nul => match rhs { Value::Nul => match rhs {
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_i32()) + rhs, Value::Int(_) => Value::Int(self.into_isize()) + rhs,
Value::Bool(_) => Value::Bool(self.into_bool()) + 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.to_string())), 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::Bool(b) => Value::Bool(b || rhs.into_bool()),
Value::Abool(_) => { 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 { Value::Functio(f) => Value::Functio(Functio::Chain {
functios: Box::new((f, rhs.into_functio())), functios: Box::new((f, rhs.into_functio())),
@ -571,14 +571,14 @@ impl ops::Sub for Value {
Value::Nul => match rhs { Value::Nul => match rhs {
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_i32()) - rhs, Value::Int(_) => Value::Int(self.into_isize()) - rhs,
Value::Bool(_) => Value::Bool(self.into_bool()) - 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_i32())), Value::Int(i) => Value::Int(i.wrapping_sub(rhs.into_isize())),
Value::Bool(b) => Value::Bool(b ^ rhs.into_bool()), 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 {
@ -630,14 +630,14 @@ impl ops::Sub for Value {
params: lhs_params, params: lhs_params,
body: lhs_body, body: lhs_body,
}) })
.into_i32() .into_isize()
- Value::Functio(rhs).into_i32(), - Value::Functio(rhs).into_isize(),
) )
.into_functio(), .into_functio(),
}, },
Functio::Builtin(b) => { Functio::Builtin(b) => {
let arity = b.arity; 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( Functio::Builtin(BuiltinFunctio::new(
move |args| { move |args| {
@ -692,17 +692,17 @@ impl ops::Mul for Value {
Value::Nul => match rhs { Value::Nul => match rhs {
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_i32()) * rhs, Value::Int(_) => Value::Int(self.into_isize()) * rhs,
Value::Bool(_) => Value::Bool(self.into_bool()) * 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_i32() as usize)), Value::Str(s) => Value::Str(s.repeat(rhs.into_isize() as usize)),
Value::Int(i) => Value::Int(i.wrapping_mul(rhs.into_i32())), Value::Int(i) => Value::Int(i.wrapping_mul(rhs.into_isize())),
Value::Bool(b) => Value::Bool(b && rhs.into_bool()), Value::Bool(b) => Value::Bool(b && rhs.into_bool()),
Value::Abool(_) => { 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 { Value::Functio(f) => Value::Functio(Functio::Chain {
functios: Box::new((f, rhs.into_functio())), functios: Box::new((f, rhs.into_functio())),
@ -735,7 +735,7 @@ impl ops::Div for Value {
Value::Nul => match rhs { Value::Nul => match rhs {
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_i32()) / rhs, Value::Int(_) => Value::Int(self.into_isize()) / rhs,
Value::Bool(_) => Value::Bool(self.into_bool()) / 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,
@ -746,13 +746,13 @@ impl ops::Div for Value {
.enumerate() .enumerate()
.map(|(i, x)| { .map(|(i, x)| {
( (
Value::Int(i as i32 + 1), Value::Int(i as isize + 1),
Rc::new(RefCell::new(Value::Str(x.to_owned()))), Rc::new(RefCell::new(Value::Str(x.to_owned()))),
) )
}) })
.collect(), .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, 0 => consts::ANSWER,
x => x, x => x,
})), })),
@ -763,7 +763,7 @@ impl ops::Div for Value {
instructions, instructions,
tape_len, tape_len,
} => { } => {
let fraction = 1.0 / rhs.into_i32() as f64; let fraction = 1.0 / rhs.into_isize() as f64;
let len = instructions.len(); let len = instructions.len();
Functio::Bf { Functio::Bf {
instructions: instructions instructions: instructions
@ -774,7 +774,7 @@ impl ops::Div for Value {
} }
} }
Functio::Able { params, body } => { 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(); let len = body.len();
Functio::Able { Functio::Able {
params, params,
@ -785,7 +785,7 @@ impl ops::Div for Value {
} }
} }
Functio::Builtin(b) => Functio::Builtin(BuiltinFunctio { Functio::Builtin(b) => Functio::Builtin(BuiltinFunctio {
arity: b.arity + rhs.into_i32() as usize, arity: b.arity + rhs.into_isize() as usize,
..b ..b
}), }),
Functio::Chain { functios, kind } => { Functio::Chain { functios, kind } => {
@ -799,14 +799,14 @@ impl ops::Div for Value {
} }
} }
Functio::Eval(s) => { 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(); let len = s.len();
Functio::Eval(s.chars().take((len as f64 * fraction) as usize).collect()) Functio::Eval(s.chars().take((len as f64 * fraction) as usize).collect())
} }
}), }),
Value::Cart(c) => { Value::Cart(c) => {
let cart_len = c.len(); let cart_len = c.len();
let chunk_len = rhs.into_i32() as usize; let chunk_len = rhs.into_isize() as usize;
Value::Cart( Value::Cart(
c.into_iter() c.into_iter()
@ -815,7 +815,7 @@ impl ops::Div for Value {
.enumerate() .enumerate()
.map(|(k, v)| { .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()))), Rc::new(RefCell::new(Value::Cart(v.iter().cloned().collect()))),
) )
}) })
@ -896,7 +896,7 @@ impl PartialEq for Value {
match self { match self {
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_i32(), Value::Int(i) => *i == other.into_isize(),
Value::Bool(b) => *b == other.into_bool(), 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(),
@ -921,10 +921,10 @@ 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_i32())), Value::Int(i) => Some(i.cmp(&other.into_isize())),
Value::Bool(b) => Some(b.cmp(&other.into_bool())), 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_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())), Value::Cart(c) => Some(c.len().cmp(&other.into_cart().len())),
} }
} }