Add from_value variable constructor
This commit is contained in:
parent
ca4a9aba4c
commit
edce9dcc91
|
@ -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};
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
|
@ -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)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue