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
pull/5/head
ondra05 2022-02-13 22:17:13 +00:00
commit 23fcb800c4
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 {
'Z' => -26,
'Y' => -25,
@ -66,7 +66,7 @@ mod tests {
use super::*;
#[test]
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]);
}
}

View File

@ -21,7 +21,7 @@ pub fn ablescript_consts() -> HashMap<String, Variable> {
("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<String, Variable> {
map
}
pub const ANSWER: i32 = 42;
pub const ANSWER: isize = 42;

View File

@ -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))?,
}),
);
@ -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 {

View File

@ -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)]

View File

@ -127,7 +127,7 @@ pub type Cart = HashMap<Value, Rc<RefCell<Value>>>;
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,34 @@ impl Value {
Functio::Bf {
instructions,
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 } => {
params
.into_iter()
.map(|x| x.bytes().map(|x| x as i32).sum::<i32>())
.sum::<i32>()
+ body.len() as i32
.map(|x| x.bytes().map(|x| x as isize).sum::<isize>())
.sum::<isize>()
+ 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(),
}
}
@ -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()
@ -342,7 +344,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 {
@ -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,22 +510,20 @@ 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 {
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,
@ -746,13 +746,13 @@ 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()))),
)
})
.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()
@ -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()))),
)
})
@ -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())),
}
}