forked from AbleScript/ablescript
Value::Int is isize
This commit is contained in:
parent
5b320259f6
commit
8c867c7899
|
@ -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;
|
||||||
|
|
|
@ -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))?,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
|
@ -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)]
|
||||||
|
|
|
@ -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,32 @@ 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 i32).sum::<i32>())
|
||||||
.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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -342,7 +342,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 {
|
||||||
|
@ -515,15 +515,15 @@ impl Value {
|
||||||
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,
|
||||||
|
@ -752,7 +752,7 @@ impl ops::Div for Value {
|
||||||
})
|
})
|
||||||
.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()
|
||||||
|
@ -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())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue