bean_script/
scope.rs

1use std::{any::Any, cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
2
3use crate::data::Data;
4use function::{CallScope, Function};
5
6use self::block_scope::IfState;
7
8pub mod block_scope;
9pub mod function;
10
11pub type ScopeRef = Rc<RefCell<dyn Scope>>;
12
13pub trait Scope: Debug {
14	fn has_function(&self, name: &str) -> bool;
15	fn get_function(&self, name: &str) -> Option<Function>;
16	fn set_function(&mut self, name: &str, function: Function);
17	fn delete_function(&mut self, name: &str);
18
19	fn parent(&self) -> Option<ScopeRef> {
20		None
21	}
22
23	fn get_call_scope(&self) -> Option<Rc<RefCell<CallScope>>> {
24		self.parent().map_or(None, |p| p.borrow().get_call_scope())
25	}
26	fn get_file_module(&self) -> Option<ScopeRef> {
27		self.parent().map_or(None, |p| p.borrow().get_file_module())
28	}
29	fn set_return_value(&mut self, value: Data);
30	fn set_if_state(&mut self, state: IfState);
31	fn get_if_state(&self) -> Option<IfState> {
32		None
33	}
34	fn get_function_list(&self) -> HashMap<String, Function>;
35
36	fn as_any(&self) -> &dyn Any;
37	fn as_mut(&mut self) -> &mut dyn Any;
38
39	fn to_string(&self) -> String {
40		String::from("[scope]")
41	}
42}