Add from_value variable constructor

This commit is contained in:
Erin 2022-02-14 00:11:37 +01:00 committed by ondra05
parent ca4a9aba4c
commit edce9dcc91
2 changed files with 26 additions and 22 deletions

View file

@ -1,6 +1,6 @@
//! Number constants. //! Number constants.
use std::{cell::RefCell, collections::HashMap, rc::Rc}; use std::collections::HashMap;
use crate::variables::{Value, Variable}; use crate::variables::{Value, Variable};
@ -11,19 +11,19 @@ pub fn ablescript_consts() -> HashMap<String, Variable> {
let mut map = HashMap::new(); let mut map = HashMap::new();
for (name, value) in &[ for (name, value) in &[
("TAU", Int(6)), // Circumference / radius ("TAU", Int(6)), // Circumference / radius
("PI", Int(3)), // Deprecated, do not use ("PI", Int(3)), // Deprecated, do not use
("EULER", Int(3)), // Mathematical constant e ("EULER", Int(3)), // Mathematical constant e
("MASS", Int(70)), // @Kev#6900's weight in kilograms ("MASS", Int(70)), // @Kev#6900's weight in kilograms
("PHI", Int(2)), // Golden ratio ("PHI", Int(2)), // Golden ratio
("WUA", Int(1)), // 1 ("WUA", Int(1)), // 1
("EULERS_CONSTANT", Int(0)), // ??? ("EULERS_CONSTANT", Int(0)), // ???
("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(isize::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
( (
// Never gonna let you down // Never gonna let you down
"NEVERGONNAGIVEYOUUP", "NEVERGONNAGIVEYOUUP",
@ -32,13 +32,7 @@ pub fn ablescript_consts() -> HashMap<String, Variable> {
("OCTOTHORPE", Str("#".to_owned())), // It's an octothorpe ("OCTOTHORPE", Str("#".to_owned())), // It's an octothorpe
("ANSWER", Int(ANSWER)), ("ANSWER", Int(ANSWER)),
] { ] {
map.insert( map.insert((*name).to_owned(), Variable::from_value(value.to_owned()));
(*name).to_owned(),
Variable {
melo: false,
value: Rc::new(RefCell::new(value.to_owned())),
},
);
} }
map map

View file

@ -263,8 +263,9 @@ impl Value {
) )
.into_abool(), .into_abool(),
Functio::Able { params, body } => { Functio::Able { params, body } => {
let str_to_isize = let str_to_isize = |x: String| -> isize {
|x: String| -> isize { x.as_bytes().into_iter().map(|x| *x as isize).sum() }; x.as_bytes().into_iter().map(|x| *x as isize).sum()
};
let params: isize = params.into_iter().map(str_to_isize).sum(); let params: isize = params.into_iter().map(str_to_isize).sum();
let body: isize = body let body: isize = body
@ -1006,3 +1007,12 @@ pub struct Variable {
// pass-by-reference is used, therefore we use Rc here. // pass-by-reference is used, therefore we use Rc here.
pub value: Rc<RefCell<Value>>, pub value: Rc<RefCell<Value>>,
} }
impl Variable {
pub fn from_value(value: Value) -> Self {
Self {
melo: false,
value: Rc::new(RefCell::new(value)),
}
}
}