diff --git a/interpreter/src/class.rs b/interpreter/src/class.rs index 420da8b..59eddf0 100644 --- a/interpreter/src/class.rs +++ b/interpreter/src/class.rs @@ -2,9 +2,9 @@ use std::fmt::Display; use std::rc::Rc; use rlox2_frontend::parser::{Expr, Stmt}; -use rustc_hash::FxHashMap; use smol_str::SmolStr; +use crate::value::Attrs; use crate::{LoxFunction, LoxReference, Value}; #[derive(Debug, Clone)] @@ -13,7 +13,7 @@ pub struct LoxClass { name: SmolStr, - methods: FxHashMap, + methods: Attrs, } /// Representation of a class in Lox. Always behind an Rc to ensure uniqueness. Should never be @@ -21,7 +21,7 @@ pub struct LoxClass { impl LoxClass { pub fn new( name: impl Into, - methods: FxHashMap, + methods: Attrs, superclass: Option>, ) -> Rc { let name = name.into(); @@ -41,11 +41,12 @@ impl LoxClass { } let new_init = Value::function(LoxFunction::new(name, closure, param_names, body)); - methods.insert("init".to_owned(), new_init); + + methods.insert("init".into(), new_init); } if let Some(ref superclass) = superclass { - let mut new_methods: FxHashMap = FxHashMap::default(); + let mut new_methods = Attrs::default(); // Rc is immutable, so we need to drain, change, and replace for (name, value) in methods { diff --git a/interpreter/src/interpreter.rs b/interpreter/src/interpreter.rs index 5dc06ee..d52737e 100644 --- a/interpreter/src/interpreter.rs +++ b/interpreter/src/interpreter.rs @@ -1,12 +1,12 @@ use std::rc::Rc; use rlox2_frontend::parser::{BinaryOp, Expr, Literal, LogicalOp, Stmt, UnaryOp}; -use rustc_hash::FxHashMap; use super::environment::Environment; use super::{LoxFunction, Runtime, Value}; use crate::error::RuntimeError; use crate::function::LoxExternFunction; +use crate::value::Attrs; use crate::{LoxClass, LoxReference}; pub type EvalResult = Result; @@ -206,7 +206,7 @@ impl Eval for Expr { None => None, }; - let mut methods: FxHashMap = FxHashMap::default(); + let mut methods = Attrs::default(); // this is the scope "this" will get injected in env.enter_scope()?; @@ -215,7 +215,7 @@ impl Eval for Expr { let method = method_expr.eval(env)?; if let Value::Function(ref fun) = method { - let name = fun.name().to_owned(); + let name = fun.name().into(); methods.insert(name, method); } } diff --git a/interpreter/src/object.rs b/interpreter/src/object.rs index f5b9168..e861ba3 100644 --- a/interpreter/src/object.rs +++ b/interpreter/src/object.rs @@ -5,6 +5,7 @@ use std::rc::Rc; use rustc_hash::FxHashMap; use smol_str::SmolStr; +use crate::value::Attrs; use crate::{LoxClass, Value}; /// This struct is private, since *nothing* is supposed to be handling an object directly, @@ -13,7 +14,7 @@ use crate::{LoxClass, Value}; struct LoxObject { class: Rc, - attrs: FxHashMap, + attrs: Attrs, } impl LoxObject { diff --git a/interpreter/src/value.rs b/interpreter/src/value.rs index 25e7fb2..71f9718 100644 --- a/interpreter/src/value.rs +++ b/interpreter/src/value.rs @@ -1,12 +1,15 @@ use std::fmt::{Debug, Display}; use std::rc::Rc; +use rustc_hash::FxHashMap; use smol_str::SmolStr; use super::function::LoxExternFunction; use super::LoxFunction; use crate::{LoxClass, LoxReference}; +pub type Attrs = FxHashMap; + #[derive(Debug, Default, Clone, PartialEq)] pub enum Value { Object(LoxReference),