added type Attrs = FxHashMap<SmolStr, Value>

This commit is contained in:
Moritz Gmeiner 2024-09-03 16:53:46 +02:00
commit 943528a0db
4 changed files with 14 additions and 9 deletions

View file

@ -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<String, Value>,
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<SmolStr>,
methods: FxHashMap<String, Value>,
methods: Attrs,
superclass: Option<Rc<LoxClass>>,
) -> Rc<Self> {
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<String, Value> = FxHashMap::default();
let mut new_methods = Attrs::default();
// Rc<LoxFunction> is immutable, so we need to drain, change, and replace
for (name, value) in methods {

View file

@ -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<T> = Result<T, RuntimeError>;
@ -206,7 +206,7 @@ impl Eval for Expr {
None => None,
};
let mut methods: FxHashMap<String, Value> = 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);
}
}

View file

@ -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<LoxClass>,
attrs: FxHashMap<SmolStr, Value>,
attrs: Attrs,
}
impl LoxObject {

View file

@ -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<SmolStr, Value>;
#[derive(Debug, Default, Clone, PartialEq)]
pub enum Value {
Object(LoxReference),