Finished up to and including chapter 16

This commit is contained in:
Moritz Gmeiner 2023-01-30 17:41:48 +01:00
commit b86985deaf
24 changed files with 1051 additions and 198 deletions

View file

@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::fmt::Display;
use std::rc::Rc;
use rlox2_frontend::parser::{Expr, Stmt};
use rustc_hash::FxHashMap;
use crate::{LoxFunction, LoxReference, Value};
@ -12,12 +12,16 @@ pub struct LoxClass {
name: String,
methods: HashMap<String, Value>,
methods: FxHashMap<String, Value>,
}
/// Representation of a class in Lox. Always behind an Rc to ensure uniqueness. Should never be handled raw.
impl LoxClass {
pub fn new(name: impl Into<String>, methods: HashMap<String, Value>, superclass: Option<Rc<LoxClass>>) -> Rc<Self> {
pub fn new(
name: impl Into<String>,
methods: FxHashMap<String, Value>,
superclass: Option<Rc<LoxClass>>,
) -> Rc<Self> {
let name = name.into();
let mut methods = methods;
@ -42,7 +46,7 @@ impl LoxClass {
}
if let Some(ref superclass) = superclass {
let mut new_methods: HashMap<String, Value> = HashMap::new();
let mut new_methods: FxHashMap<String, Value> = FxHashMap::default();
// Rc<LoxFunction> is immutable, so we need to drain, change, and replace
for (name, value) in methods {