Classes - May now carry a reference to their superclass

This commit is contained in:
Emmanuel BENOîT 2023-01-15 18:13:51 +01:00
parent 3c062df202
commit 214cc6dc25

View file

@ -24,6 +24,7 @@ pub type ClassMemberKey = (ClassMemberKind, bool, String);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Class { pub struct Class {
name: String, name: String,
superclass: Option<ClassRef>,
members: HashMap<ClassMemberKey, Function>, members: HashMap<ClassMemberKey, Function>,
fields: RefCell<HashMap<String, Value>>, fields: RefCell<HashMap<String, Value>>,
} }
@ -34,7 +35,7 @@ pub type ClassRef = Rc<RefCell<Class>>;
/// An instance of a Lox class /// An instance of a Lox class
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Instance { pub struct Instance {
class: Rc<RefCell<Class>>, class: ClassRef,
fields: RefCell<HashMap<String, Value>>, fields: RefCell<HashMap<String, Value>>,
} }
@ -58,9 +59,14 @@ fn bind_method(method: &Function, this_value: Value) -> Function {
impl Class { impl Class {
/// Create a new class, specifying its name. /// Create a new class, specifying its name.
pub fn new(name: String, members: HashMap<ClassMemberKey, Function>) -> Self { pub fn new(
name: String,
superclass: Option<ClassRef>,
members: HashMap<ClassMemberKey, Function>,
) -> Self {
Self { Self {
name, name,
superclass,
members, members,
fields: RefCell::new(HashMap::default()), fields: RefCell::new(HashMap::default()),
} }