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