replaced Box<str> with SmolStr

This commit is contained in:
Moritz Gmeiner 2024-09-02 05:19:30 +02:00
commit da6a820638
21 changed files with 137 additions and 85 deletions

View file

@ -12,6 +12,7 @@ path = "../frontend"
glob = "0.3"
[dependencies]
thiserror = "1"
itertools = "0.13"
rustc-hash = "2"
itertools = "0.13.0"
rustc-hash = "2.0.0"
smol_str = "0.2.2"
thiserror = "1.0.63"

View file

@ -3,6 +3,7 @@ use std::rc::Rc;
use rlox2_frontend::parser::{Expr, Stmt};
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
use crate::{LoxFunction, LoxReference, Value};
@ -10,7 +11,7 @@ use crate::{LoxFunction, LoxReference, Value};
pub struct LoxClass {
superclass: Option<Rc<LoxClass>>,
name: Box<str>,
name: SmolStr,
methods: FxHashMap<String, Value>,
}
@ -18,7 +19,7 @@ pub struct LoxClass {
/// 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<Box<str>>,
name: impl Into<SmolStr>,
methods: FxHashMap<String, Value>,
superclass: Option<Rc<LoxClass>>,
) -> Rc<Self> {

View file

@ -4,6 +4,7 @@ use std::io::{Read, Write};
use std::rc::Rc;
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
use crate::error::RuntimeError;
@ -89,8 +90,8 @@ impl Display for ScopedValue {
}
}
type Scope = FxHashMap<Box<str>, ScopedValue>;
pub type ClosureScope = FxHashMap<Box<str>, HeapedValue>;
type Scope = FxHashMap<SmolStr, ScopedValue>;
pub type ClosureScope = FxHashMap<SmolStr, HeapedValue>;
#[derive(Debug)]
pub struct Environment<'a> {
@ -128,7 +129,7 @@ impl<'a> Environment<'a> {
std::mem::take(&mut self.arg_stack)
}
pub fn globals(&self) -> &FxHashMap<Box<str>, Value> {
pub fn globals(&self) -> &FxHashMap<SmolStr, Value> {
self.runtime.globals()
}
@ -219,7 +220,7 @@ impl<'a> Environment<'a> {
}) */
}
pub fn define(&mut self, name: impl Into<Box<str>>, value: Value) {
pub fn define(&mut self, name: impl Into<SmolStr>, value: Value) {
if self.has_scope() {
let name = name.into();
self.current_scope_mut()
@ -246,7 +247,7 @@ impl<'a> Environment<'a> {
})
}
pub fn collect_closure(&mut self, closure_vars: &[(Box<str>, usize)]) -> ClosureScope {
pub fn collect_closure(&mut self, closure_vars: &[(SmolStr, usize)]) -> ClosureScope {
let mut closure_scope = ClosureScope::default();
for (name, level) in closure_vars {

View file

@ -3,6 +3,7 @@ use std::rc::Rc;
use rlox2_frontend::lexer::LexerError;
use rlox2_frontend::parser::{BinaryOp, ParserError, UnaryOp};
use smol_str::SmolStr;
use thiserror::Error;
use crate::Value;
@ -45,7 +46,7 @@ pub enum RuntimeError {
#[error("only objects have attributes")]
InvalidSetTarget,
#[error("class {0} has no property {name}", class.name())]
UndefinedAttribute { class: Rc<LoxClass>, name: Box<str> },
UndefinedAttribute { class: Rc<LoxClass>, name: SmolStr },
#[error("{value} is not a valid superclass")]
InvalidSuperclass { value: Value },
#[error("stack overflow")]

View file

@ -2,6 +2,7 @@ use std::fmt::{Debug, Display};
use std::rc::Rc;
use rlox2_frontend::parser::Stmt;
use smol_str::SmolStr;
use crate::environment::{ClosureScope, HeapedValue};
@ -11,17 +12,17 @@ use super::{Runtime, Value};
#[derive(Debug, Clone)]
pub struct LoxFunction {
name: Box<str>,
name: SmolStr,
closure: ClosureScope,
param_names: Vec<Box<str>>,
param_names: Vec<SmolStr>,
body: Box<Stmt>,
}
impl LoxFunction {
pub fn new(
name: impl Into<Box<str>>,
name: impl Into<SmolStr>,
closure: ClosureScope,
param_names: Vec<Box<str>>,
param_names: Vec<SmolStr>,
body: Stmt,
) -> Rc<Self> {
let name = name.into();
@ -43,7 +44,7 @@ impl LoxFunction {
&self.closure
}
pub fn param_names(&self) -> &[Box<str>] {
pub fn param_names(&self) -> &[SmolStr] {
&self.param_names
}
@ -55,7 +56,7 @@ impl LoxFunction {
self.param_names().len()
}
pub fn inject_closed_var(&mut self, name: impl Into<Box<str>>, value: Value) {
pub fn inject_closed_var(&mut self, name: impl Into<SmolStr>, value: Value) {
let name = name.into();
let heaped_value = HeapedValue::new(value);
self.closure.insert(name, heaped_value);

View file

@ -154,7 +154,8 @@ impl Eval for Expr {
if let Value::Object(object) = target {
object.get(name).ok_or_else(|| {
let class = object.class();
let name = name.to_owned();
let name = name.clone();
RuntimeError::UndefinedAttribute { class, name }
})
} else {

View file

@ -1,3 +1,5 @@
use smol_str::SmolStr;
use crate::error::RuntimeError;
use super::function::{ExternFunClosure, LoxExternFunction};
@ -47,7 +49,7 @@ fn clock() -> LoxExternFunction {
fn print_globals() -> LoxExternFunction {
let closure: ExternFunClosure = |args, env| {
assert_eq!(args.len(), 0);
let mut globals: Vec<(&Box<str>, &Value)> = env.globals().iter().collect();
let mut globals: Vec<(&SmolStr, &Value)> = env.globals().iter().collect();
globals.sort_by_key(|&(name, _value)| name);

View file

@ -3,6 +3,7 @@ use std::fmt::Display;
use std::rc::Rc;
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
use crate::{LoxClass, Value};
@ -12,7 +13,7 @@ use crate::{LoxClass, Value};
struct LoxObject {
class: Rc<LoxClass>,
attrs: FxHashMap<Box<str>, Value>,
attrs: FxHashMap<SmolStr, Value>,
}
impl LoxObject {
@ -35,7 +36,7 @@ impl LoxObject {
self.class.get_method(name, this)
}
fn set(&mut self, name: impl Into<Box<str>>, value: Value) {
fn set(&mut self, name: impl Into<SmolStr>, value: Value) {
let name = name.into();
self.attrs.insert(name, value);
}
@ -75,7 +76,7 @@ impl LoxReference {
}
}
pub fn set(&mut self, name: impl Into<Box<str>>, value: Value) {
pub fn set(&mut self, name: impl Into<SmolStr>, value: Value) {
unsafe {
let ptr = self.inner.get();
let object = &mut *ptr;

View file

@ -1,12 +1,13 @@
use rlox2_frontend::parser::{Expr, Stmt};
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
use crate::Runtime;
use crate::{LoxError, ResolverError};
/*====================================================================================================================*/
type ResolverScope = FxHashMap<Box<str>, ResolveStatus>;
type ResolverScope = FxHashMap<SmolStr, ResolveStatus>;
type ResolverFrame = Vec<ResolverScope>;
type ResolverResult<T> = Result<T, ResolverError>;
@ -37,11 +38,11 @@ struct Resolver {
// local_scopes: Vec<ResolverScope>,
frames: Vec<ResolverFrame>,
closure_vars: FxHashMap<Box<str>, usize>,
closure_vars: FxHashMap<SmolStr, usize>,
}
impl Resolver {
fn new(global_names: impl Iterator<Item = Box<str>>) -> Self {
fn new(global_names: impl Iterator<Item = SmolStr>) -> Self {
let mut global_scope = ResolverScope::default();
for name in global_names {
@ -83,7 +84,7 @@ impl Resolver {
.expect("Tried to pop non-existant ResolverFrame");
}
fn declare(&mut self, name: impl Into<Box<str>>) -> ResolverResult<()> {
fn declare(&mut self, name: impl Into<SmolStr>) -> ResolverResult<()> {
let name = name.into();
if let Some(local_scope) = self.local_scopes_mut().last_mut() {
@ -100,7 +101,7 @@ impl Resolver {
Ok(())
}
fn define(&mut self, name: impl Into<Box<str>>) {
fn define(&mut self, name: impl Into<SmolStr>) {
let name = name.into();
if let Some(local_scope) = self.local_scopes_mut().last_mut() {

View file

@ -1,11 +1,12 @@
use smol_str::SmolStr;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ResolverError {
#[error("can't read variable {name} in its own initializer.")]
VarInOwnInitializer { name: Box<str> },
VarInOwnInitializer { name: SmolStr },
#[error("variable {name} not defined")]
UnresolvableVariable { name: Box<str> },
UnresolvableVariable { name: SmolStr },
#[error("return outside of function definition")]
ReturnOutsideFunction,
#[error("this outside of method")]
@ -13,5 +14,5 @@ pub enum ResolverError {
#[error("super outside of subclass method")]
SuperOutsideMethod,
#[error("local variable {name} declares multiple times")]
LocalMulipleDeclarations { name: Box<str> },
LocalMulipleDeclarations { name: SmolStr },
}

View file

@ -4,6 +4,7 @@ use std::io::{stdin, stdout, Read, Write};
use std::rc::Rc;
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
use crate::error::RuntimeError;
@ -11,7 +12,7 @@ use super::lox_std::init_std;
use super::Value;
pub struct Runtime {
globals: FxHashMap<Box<str>, Value>,
globals: FxHashMap<SmolStr, Value>,
in_stream: Rc<RefCell<dyn std::io::Read>>,
out_stream: Rc<RefCell<dyn std::io::Write>>,
@ -53,7 +54,7 @@ impl Runtime {
self.out_stream.as_ref().borrow_mut()
}
pub fn globals(&self) -> &FxHashMap<Box<str>, Value> {
pub fn globals(&self) -> &FxHashMap<SmolStr, Value> {
&self.globals
}
@ -66,7 +67,7 @@ impl Runtime {
})
}
pub fn define_global(&mut self, name: impl Into<Box<str>>, value: Value) {
pub fn define_global(&mut self, name: impl Into<SmolStr>, value: Value) {
let name = name.into();
self.globals.insert(name, value);
}

View file

@ -1,6 +1,8 @@
use std::fmt::{Debug, Display};
use std::rc::Rc;
use smol_str::SmolStr;
use crate::{LoxClass, LoxReference};
use super::function::LoxExternFunction;
@ -12,7 +14,7 @@ pub enum Value {
Class(Rc<LoxClass>),
Function(Rc<LoxFunction>),
ExternFunction(Rc<LoxExternFunction>),
String(Box<str>),
String(SmolStr),
Number(f64),
Bool(bool),
#[default]
@ -34,7 +36,7 @@ impl Value {
Value::ExternFunction(fun)
}
pub fn string(s: impl Into<Box<str>>) -> Self {
pub fn string(s: impl Into<SmolStr>) -> Self {
let s = s.into();
Value::String(s)
}