Value::Int is isize

pull/4/head
ondra05 2022-02-13 00:55:19 +01:00
parent a46b9ea910
commit 3dd91b0c97
4 changed files with 42 additions and 42 deletions

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

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,32 @@ 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
.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(),
}
}
@ -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())),
}
}