mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 04:12:42 +00:00
replaced some Strings with SmolStr
This commit is contained in:
parent
943528a0db
commit
947949601f
4 changed files with 10 additions and 11 deletions
|
|
@ -29,7 +29,7 @@ impl LoxClass {
|
||||||
|
|
||||||
// if class has an init method: insert an implicit "return this;" at its end
|
// if class has an init method: insert an implicit "return this;" at its end
|
||||||
if let Some(Value::Function(init)) = methods.get("init") {
|
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 closure = init.closure().clone();
|
||||||
let param_names = init.param_names().to_vec();
|
let param_names = init.param_names().to_vec();
|
||||||
let mut body = init.body().clone();
|
let mut body = init.body().clone();
|
||||||
|
|
@ -74,7 +74,7 @@ impl LoxClass {
|
||||||
self.superclass.clone()
|
self.superclass.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &SmolStr {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ pub enum RuntimeError {
|
||||||
NotCallable { callee: Value },
|
NotCallable { callee: Value },
|
||||||
#[error("{name}() takes {arity} args, but {given} were given.")]
|
#[error("{name}() takes {arity} args, but {given} were given.")]
|
||||||
WrongArity {
|
WrongArity {
|
||||||
name: String,
|
name: SmolStr,
|
||||||
arity: usize,
|
arity: usize,
|
||||||
given: usize,
|
given: usize,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,10 @@ use std::rc::Rc;
|
||||||
use rlox2_frontend::parser::Stmt;
|
use rlox2_frontend::parser::Stmt;
|
||||||
use smol_str::SmolStr;
|
use smol_str::SmolStr;
|
||||||
|
|
||||||
use crate::environment::{ClosureScope, HeapedValue};
|
|
||||||
|
|
||||||
use super::environment::Environment;
|
use super::environment::Environment;
|
||||||
use super::interpreter::EvalResult;
|
use super::interpreter::EvalResult;
|
||||||
use super::{Runtime, Value};
|
use super::{Runtime, Value};
|
||||||
|
use crate::environment::{ClosureScope, HeapedValue};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct LoxFunction {
|
pub struct LoxFunction {
|
||||||
|
|
@ -35,7 +34,7 @@ impl LoxFunction {
|
||||||
Rc::new(fun)
|
Rc::new(fun)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &SmolStr {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,13 +84,13 @@ pub type ExternFunClosure = fn(Vec<Value>, &mut Environment) -> EvalResult<Value
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct LoxExternFunction {
|
pub struct LoxExternFunction {
|
||||||
name: String,
|
name: SmolStr,
|
||||||
arity: usize,
|
arity: usize,
|
||||||
closure: ExternFunClosure,
|
closure: ExternFunClosure,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LoxExternFunction {
|
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();
|
let name = name.into();
|
||||||
|
|
||||||
LoxExternFunction {
|
LoxExternFunction {
|
||||||
|
|
@ -101,7 +100,7 @@ impl LoxExternFunction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &SmolStr {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -215,7 +215,7 @@ impl Eval for Expr {
|
||||||
let method = method_expr.eval(env)?;
|
let method = method_expr.eval(env)?;
|
||||||
|
|
||||||
if let Value::Function(ref fun) = method {
|
if let Value::Function(ref fun) = method {
|
||||||
let name = fun.name().into();
|
let name = fun.name().clone();
|
||||||
methods.insert(name, method);
|
methods.insert(name, method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -322,7 +322,7 @@ pub fn call_fun(fun: Rc<LoxFunction>, env: &mut Environment) -> EvalResult<Value
|
||||||
|
|
||||||
env.enter_scope()?;
|
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());
|
env.insert_closure(fun.closure().clone());
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue