replaced some Strings with SmolStr

This commit is contained in:
Moritz Gmeiner 2024-09-03 16:56:22 +02:00
commit 947949601f
4 changed files with 10 additions and 11 deletions

View file

@ -29,7 +29,7 @@ impl LoxClass {
// if class has an init method: insert an implicit "return this;" at its end
if let Some(Value::Function(init)) = methods.get("init") {
let name = init.name().to_owned();
let name = init.name().clone();
let closure = init.closure().clone();
let param_names = init.param_names().to_vec();
let mut body = init.body().clone();
@ -74,7 +74,7 @@ impl LoxClass {
self.superclass.clone()
}
pub fn name(&self) -> &str {
pub fn name(&self) -> &SmolStr {
&self.name
}

View file

@ -28,7 +28,7 @@ pub enum RuntimeError {
NotCallable { callee: Value },
#[error("{name}() takes {arity} args, but {given} were given.")]
WrongArity {
name: String,
name: SmolStr,
arity: usize,
given: usize,
},

View file

@ -4,11 +4,10 @@ use std::rc::Rc;
use rlox2_frontend::parser::Stmt;
use smol_str::SmolStr;
use crate::environment::{ClosureScope, HeapedValue};
use super::environment::Environment;
use super::interpreter::EvalResult;
use super::{Runtime, Value};
use crate::environment::{ClosureScope, HeapedValue};
#[derive(Debug, Clone)]
pub struct LoxFunction {
@ -35,7 +34,7 @@ impl LoxFunction {
Rc::new(fun)
}
pub fn name(&self) -> &str {
pub fn name(&self) -> &SmolStr {
&self.name
}
@ -85,13 +84,13 @@ pub type ExternFunClosure = fn(Vec<Value>, &mut Environment) -> EvalResult<Value
#[derive(Clone)]
pub struct LoxExternFunction {
name: String,
name: SmolStr,
arity: usize,
closure: ExternFunClosure,
}
impl LoxExternFunction {
pub fn new(name: impl Into<String>, arity: usize, closure: ExternFunClosure) -> Self {
pub fn new(name: impl Into<SmolStr>, arity: usize, closure: ExternFunClosure) -> Self {
let name = name.into();
LoxExternFunction {
@ -101,7 +100,7 @@ impl LoxExternFunction {
}
}
pub fn name(&self) -> &str {
pub fn name(&self) -> &SmolStr {
&self.name
}

View file

@ -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().into();
let name = fun.name().clone();
methods.insert(name, method);
}
}
@ -322,7 +322,7 @@ pub fn call_fun(fun: Rc<LoxFunction>, env: &mut Environment) -> EvalResult<Value
env.enter_scope()?;
env.define(fun.name(), Value::Function(Rc::clone(&fun)));
env.define(fun.name().clone(), Value::Function(Rc::clone(&fun)));
env.insert_closure(fun.closure().clone());