Skip to content

Commit

Permalink
Remove forced inlining & apply Clippy's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
twolodzko committed Oct 17, 2024
1 parent 92500fd commit 3ec3eab
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 48 deletions.
11 changes: 3 additions & 8 deletions src/envir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ where
}

/// Create environment inheriting from the current one
#[inline]
pub fn branch(&self) -> Self {
Env(Some(Rc::new(RefCell::new(EnvContainer {
local: HashMap::new(),
Expand All @@ -33,14 +32,12 @@ where
}

/// Save key-value pair to the environment
#[inline]
pub fn insert(&mut self, key: &str, val: T) {
let mut env = self.unwrap().borrow_mut();
env.local.insert(key.to_string(), val);
}

/// Get (recursively) value associated with the key if available
#[inline]
pub fn get(&self, key: &str) -> Option<T> {
let env = self.unwrap().borrow();
if let Some(val) = env.local.get(key) {
Expand All @@ -53,11 +50,10 @@ where
}
}

/// Find (recursively) the enviroment that has some value associated with the key
#[inline]
/// Find (recursively) the environment that has some value associated with the key
pub fn find_env(&self, key: &str) -> Option<Self> {
let env = self.unwrap().borrow();
if env.local.get(key).is_some() {
if env.local.contains_key(key) {
Some(self.clone())
} else {
match env.parent {
Expand All @@ -68,7 +64,6 @@ where
}

/// Extract the internal Env container
#[inline]
fn unwrap(&self) -> &Rc<RefCell<EnvContainer<T>>> {
self.0.as_ref().unwrap()
}
Expand Down Expand Up @@ -97,7 +92,7 @@ where
}

// it is needed because otherwise we could be doing
// infinitelly recursive comparisons when
// infinitely recursive comparisons when
// object in Env holds clone of the Env itself
impl<T> PartialEq for Env<T> {
fn eq(&self, other: &Self) -> bool {
Expand Down
2 changes: 0 additions & 2 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub fn eval(sexpr: &Sexpr, env: &mut Env) -> FuncResult {
}
}

#[inline]
fn eval_list(list: &Args, env: &mut Env) -> TcoResult {
let head = match list.head() {
Some(head) => head,
Expand All @@ -40,7 +39,6 @@ fn eval_list(list: &Args, env: &mut Env) -> TcoResult {
}

/// Evaluate all the elements of the list but last, return last element unevaluated
#[inline]
pub fn eval_but_last(args: &Args, env: &mut Env) -> TcoResult {
let iter = &mut args.iter();
let mut last = iter.next().unwrap_or(&Sexpr::Nil);
Expand Down
9 changes: 0 additions & 9 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,16 @@ struct Pair<T> {
}

impl<T> Pair<T> {
#[inline]
fn new(this: T, next: MaybePair<T>) -> MaybePair<T> {
Some(Rc::new(Pair { this, next }))
}
}

impl<T> List<T> {
#[inline]
pub fn empty() -> Self {
List { head: None }
}

#[inline]
pub fn push_front(&self, elem: T) -> List<T> {
List {
head: Pair::new(elem, self.head.clone()),
Expand All @@ -51,39 +48,33 @@ impl<T> List<T> {
}

/// Extract first element of the list
#[inline]
pub fn head(&self) -> Option<&T> {
self.head.as_ref().map(|pair| &pair.this)
}

/// Extract list containing everyghing but the first element
#[inline]
pub fn tail(&self) -> Option<List<T>> {
let next = self.head.as_ref().and_then(|head| head.next.clone())?;
Some(List { head: Some(next) })
}

#[inline]
pub fn tail_or_empty(&self) -> List<T> {
self.tail().unwrap_or_default()
}

#[inline]
pub fn has_next(&self) -> bool {
match self.head.as_ref() {
None => false,
Some(head) => head.next.is_some(),
}
}

#[inline]
pub fn iter(&self) -> Iter<'_, T> {
Iter {
next: self.head.as_deref(),
}
}

#[inline]
pub fn is_empty(&self) -> bool {
self.head.is_none()
}
Expand Down
4 changes: 0 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ fn read_word(r: &mut impl Reader) -> Result<String, ReadError> {
Ok(chars.into_iter().collect())
}

#[inline]
fn is_special(c: char) -> bool {
matches!(c, '(' | ')' | '[' | ']' | '\'' | '`' | ',' | '"' | ';')
}

#[inline]
fn is_word_boundary(c: char) -> bool {
c.is_whitespace() || is_special(c)
}
Expand Down Expand Up @@ -137,7 +135,6 @@ fn read_special(r: &mut impl Reader, name: &str) -> Result<Sexpr, ReadError> {
Ok(Sexpr::List(list))
}

#[inline]
fn skip_whitespace(r: &mut impl Reader) -> Result<(), ReadError> {
loop {
match r.peek() {
Expand All @@ -154,7 +151,6 @@ fn skip_whitespace(r: &mut impl Reader) -> Result<(), ReadError> {
Ok(())
}

#[inline]
fn skip_line(r: &mut impl Reader) -> Result<(), ReadError> {
loop {
match r.next() {
Expand Down
2 changes: 0 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ impl StringReader {
}

impl Reader for StringReader {
#[inline]
fn peek(&mut self) -> Result<char, ReadError> {
self.cache.peek().ok_or(ReadError::EndOfInput).cloned()
}

#[inline]
fn next(&mut self) -> Result<char, ReadError> {
self.cache.next().ok_or(ReadError::EndOfInput)
}
Expand Down
6 changes: 1 addition & 5 deletions src/scheme/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ pub fn is_finite(args: &Args, env: &mut Env) -> FuncResult {
/// - when `args` is empty, return `init`
/// - when there is single element in `args`, return `func(init, arg)`
/// - for more elements, fold the list with `func(acc, arg)`
#[inline]
fn list_reduce(
args: &Args,
env: &mut Env,
Expand All @@ -129,7 +128,6 @@ fn list_reduce(
}

/// Use `partial_cmp` to compare subsequent values
#[inline]
fn cmp(args: &Args, env: &mut Env, order: std::cmp::Ordering) -> FuncResult {
let iter = &mut eval_iter(args, env);

Expand Down Expand Up @@ -217,13 +215,12 @@ impl ops::Rem<Sexpr> for Sexpr {
}

impl Sexpr {
#[inline]
fn div_euclid(self, rhs: Self) -> FuncResult {
op!(div_euclid | checked_div_euclid self rhs)
}

/// It is numeric, but NaN or infinite
#[inline]
pub fn is_nan(self) -> bool {
match self {
Sexpr::Float(num) => num.is_nan() || num.is_infinite(),
Expand All @@ -233,7 +230,6 @@ impl Sexpr {
}

/// It is numeric and finite
#[inline]
pub fn is_finite(self) -> bool {
match self {
Sexpr::Float(num) => num.is_finite(),
Expand Down
6 changes: 0 additions & 6 deletions src/scheme/special_forms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub fn lambda(args: &Args, env: &mut Env) -> FuncResult {
lambda_init(vars, &body, env)
}

#[inline]
pub fn lambda_init(vars: &List<Sexpr>, body: &List<Sexpr>, env: &mut Env) -> FuncResult {
let vars: Result<Vec<String>, Error<Sexpr>> = vars
.iter()
Expand Down Expand Up @@ -79,7 +78,6 @@ pub fn condfn(args: &Args, env: &mut Env) -> TcoResult {
}

pub fn define(args: &Args, env: &mut Env) -> FuncResult {
#[inline]
fn from_symbol(key: &str, rest: &List<Sexpr>, env: &mut Env) -> FuncResult {
if rest.has_next() {
return Err(Error::WrongArgNum);
Expand All @@ -90,7 +88,6 @@ pub fn define(args: &Args, env: &mut Env) -> FuncResult {
Ok(Sexpr::Nil)
}

#[inline]
fn from_list(list: &List<Sexpr>, rest: &List<Sexpr>, env: &mut Env) -> FuncResult {
let key = head_or_err(list).and_then(symbol_or_err)?;
let vars = list.tail_or_empty();
Expand Down Expand Up @@ -130,7 +127,6 @@ pub fn set(args: &Args, env: &mut Env) -> FuncResult {
}
}

#[inline]
fn eval_and_bind(
args: &Args,
eval_env: &mut Env,
Expand Down Expand Up @@ -177,7 +173,6 @@ pub fn let_star(args: &Args, env: &mut Env) -> TcoResult {
let_impl(bindings, &body, &mut local.clone(), local)
}

#[inline]
fn let_impl(bindings: &Args, body: &Args, call_env: &mut Env, eval_env: &mut Env) -> TcoResult {
bindings.iter().try_for_each(|elem| {
let binding = list_or_err(elem)?;
Expand All @@ -187,7 +182,6 @@ fn let_impl(bindings: &Args, body: &Args, call_env: &mut Env, eval_env: &mut Env
eval_but_last(body, eval_env)
}

#[inline]
fn named_let(key: &str, bindings: &Args, body: &Args, env: &mut Env) -> TcoResult {
let local = &mut env.branch();

Expand Down
7 changes: 0 additions & 7 deletions src/scheme/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::list::List;
use crate::types::{Args, Env, FuncResult, Sexpr};

/// Collect list of S-expressions to a space-separated string
#[inline]
pub fn stringify(args: &Args, env: &mut Env) -> Result<String, Error<Sexpr>> {
let string = eval_iter(args, env)
.map(|elem| elem.map(|sexpr| sexpr.to_string()))
Expand All @@ -13,7 +12,6 @@ pub fn stringify(args: &Args, env: &mut Env) -> Result<String, Error<Sexpr>> {
}

/// If `sexpr` is a list, return enclosed list, otherwise throw an error
#[inline]
pub fn list_or_err(sexpr: &Sexpr) -> Result<&List<Sexpr>, Error<Sexpr>> {
match sexpr {
Sexpr::List(list) => Ok(list),
Expand All @@ -22,7 +20,6 @@ pub fn list_or_err(sexpr: &Sexpr) -> Result<&List<Sexpr>, Error<Sexpr>> {
}

/// If `sexpr` is a symbol, return it's name, otherwise throw an error
#[inline]
pub fn symbol_or_err(sexpr: &Sexpr) -> Result<&String, Error<Sexpr>> {
match sexpr {
Sexpr::Symbol(name) => Ok(name),
Expand All @@ -31,12 +28,10 @@ pub fn symbol_or_err(sexpr: &Sexpr) -> Result<&String, Error<Sexpr>> {
}

/// Extract head of a list, for empty list throw an error
#[inline]
pub fn head_or_err(args: &Args) -> Result<&Sexpr, Error<Sexpr>> {
args.head().ok_or(Error::WrongArgNum)
}

#[inline]
pub fn one_arg_or_err(args: &Args) -> Result<&Sexpr, Error<Sexpr>> {
if args.has_next() {
return Err(Error::WrongArgNum);
Expand All @@ -45,13 +40,11 @@ pub fn one_arg_or_err(args: &Args) -> Result<&Sexpr, Error<Sexpr>> {
}

/// Evaluate first argument, raise error if more arguments were passed
#[inline]
pub fn eval_one_arg(args: &Args, env: &mut Env) -> FuncResult {
one_arg_or_err(args).and_then(|sexpr| eval(sexpr, env))
}

/// Initialize iterator that evaluates and returns each element of the list
#[inline]
pub fn eval_iter<'a>(
args: &'a List<Sexpr>,
env: &'a mut Env,
Expand Down
5 changes: 0 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,18 @@ pub struct Lambda {
}

impl Sexpr {
#[inline]
pub fn null() -> Sexpr {
Sexpr::List(List::empty())
}

#[inline]
pub fn symbol(name: &str) -> Sexpr {
Sexpr::Symbol(String::from(name))
}

#[inline]
pub fn lambda(vars: Vec<String>, body: List<Sexpr>, env: Env) -> Self {
Sexpr::Lambda(Box::new(Lambda { vars, body, env }))
}

#[inline]
pub fn is_true(&self) -> bool {
// see: https://www.scheme.com/tspl4/intro.html#./intro:s36
!matches!(self, Sexpr::False)
Expand Down Expand Up @@ -167,7 +163,6 @@ impl fmt::Display for Lambda {
}

impl From<Sexpr> for List<Sexpr> {
#[inline]
fn from(elem: Sexpr) -> Self {
List::empty().push_front(elem)
}
Expand Down

0 comments on commit 3ec3eab

Please sign in to comment.