diff --git a/ablescript/src/consts.rs b/ablescript/src/consts.rs index eb91949..3f4c419 100644 --- a/ablescript/src/consts.rs +++ b/ablescript/src/consts.rs @@ -1,6 +1,6 @@ //! Number constants. -use std::{cell::RefCell, collections::HashMap, rc::Rc}; +use std::collections::HashMap; use crate::variables::{Value, Variable}; @@ -11,19 +11,19 @@ pub fn ablescript_consts() -> HashMap { let mut map = HashMap::new(); for (name, value) in &[ - ("TAU", Int(6)), // Circumference / radius - ("PI", Int(3)), // Deprecated, do not use - ("EULER", Int(3)), // Mathematical constant e - ("MASS", Int(70)), // @Kev#6900's weight in kilograms - ("PHI", Int(2)), // Golden ratio - ("WUA", Int(1)), // 1 - ("EULERS_CONSTANT", Int(0)), // ??? - ("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/) + ("TAU", Int(6)), // Circumference / radius + ("PI", Int(3)), // Deprecated, do not use + ("EULER", Int(3)), // Mathematical constant e + ("MASS", Int(70)), // @Kev#6900's weight in kilograms + ("PHI", Int(2)), // Golden ratio + ("WUA", Int(1)), // 1 + ("EULERS_CONSTANT", Int(0)), // ??? + ("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(isize::max_value())), // The biggest number - ("INTERESSANT", Int(114514)), // HTGAzureX1212.#5959 intéressant number - ("FUNNY", Int(69)), // HTGAzureX1212.#5959 funny number + ("INTERESSANT", Int(114514)), // HTGAzureX1212.#5959 intéressant number + ("FUNNY", Int(69)), // HTGAzureX1212.#5959 funny number ( // Never gonna let you down "NEVERGONNAGIVEYOUUP", @@ -32,13 +32,7 @@ pub fn ablescript_consts() -> HashMap { ("OCTOTHORPE", Str("#".to_owned())), // It's an octothorpe ("ANSWER", Int(ANSWER)), ] { - map.insert( - (*name).to_owned(), - Variable { - melo: false, - value: Rc::new(RefCell::new(value.to_owned())), - }, - ); + map.insert((*name).to_owned(), Variable::from_value(value.to_owned())); } map diff --git a/ablescript/src/variables.rs b/ablescript/src/variables.rs index 198361c..552c067 100644 --- a/ablescript/src/variables.rs +++ b/ablescript/src/variables.rs @@ -263,8 +263,9 @@ impl Value { ) .into_abool(), Functio::Able { params, body } => { - let str_to_isize = - |x: String| -> isize { x.as_bytes().into_iter().map(|x| *x as isize).sum() }; + let str_to_isize = |x: String| -> isize { + x.as_bytes().into_iter().map(|x| *x as isize).sum() + }; let params: isize = params.into_iter().map(str_to_isize).sum(); let body: isize = body @@ -1006,3 +1007,12 @@ pub struct Variable { // pass-by-reference is used, therefore we use Rc here. pub value: Rc>, } + +impl Variable { + pub fn from_value(value: Value) -> Self { + Self { + melo: false, + value: Rc::new(RefCell::new(value)), + } + } +}