Skip to content

Commit

Permalink
Applying clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbor committed Oct 20, 2024
1 parent 7cf8dba commit c5d6b67
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/debruijn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Display for DeBruijnNode {
match self {
FreeVariable(name) => write!(f, "{}", name),
BoundVariable(i) => write!(f, "{}", i),
Abstraction(term) => write!(f, "{} {}", '\\', term.to_string()),
Abstraction(term) => write!(f, "\\ {}", term),
Application(term1, term2) => {
let s1 = if let DeBruijnNode::FreeVariable(name) = &**term1 {
name.to_string()
Expand All @@ -61,14 +61,14 @@ impl Display for DeBruijnNode {
} else if let DeBruijnNode::Application(_, _) = &**term1 {
term1.to_string()
} else {
format!("({})", term1.to_string())
format!("({})", term1)
};
let s2 = if let DeBruijnNode::FreeVariable(name) = &**term2 {
name.to_string()
} else if let DeBruijnNode::BoundVariable(i) = &**term2 {
format!("{}", i)
} else {
format!("({})", term2.to_string())
format!("({})", term2)
};
write!(f, "{} {}", s1, s2)
},
Expand Down
9 changes: 8 additions & 1 deletion src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub trait Environment: {
type Instant;

fn stdout(&mut self) -> &mut impl Write;
#[allow(dead_code)]
fn stderr(&mut self) -> &mut impl Write;
fn load(&self, file: &str) -> LashResult<String>;
fn now(&self) -> Self::Instant;
Expand All @@ -29,6 +30,12 @@ struct StdStdout {}
struct StdStderr {}

#[cfg(feature = "std")]
impl Default for StdEnvironment {
fn default() -> Self {
Self::new()
}
}

impl StdEnvironment {
pub fn new() -> Self {
StdEnvironment {
Expand All @@ -51,7 +58,7 @@ impl Environment for StdEnvironment {
}

fn load(&self, file: &str) -> LashResult<String> {
std::fs::read_to_string(&file)
std::fs::read_to_string(file)
.map_err(|e| LashError::new_file_error(file.into(), Some(e)))
}

Expand Down
1 change: 1 addition & 0 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use alloc::string::String;
use core::fmt;
use core::fmt::Write;
use core::str;
use core::str::FromStr;

use crate::error::*;
use crate::environment::*;
Expand Down
24 changes: 22 additions & 2 deletions src/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Macro {
term
},
DeBruijn => {
println!("{}", DeBruijnNode::from(terms[0].clone()).to_string());
println!("{}", DeBruijnNode::from(terms[0].clone()));
terms[0].clone()
},
Debug | Dbg => {
Expand Down Expand Up @@ -180,7 +180,27 @@ impl Macro {

impl Display for Macro {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
writeln!(f, "!{}", self)
use Macro::*;
let name = match self {
AlphaEq => "alphaeq",
CN => "cn",
CNormalize => "cnormalize",
Dbg => "dbg",
DeBruijn => "debruijn",
Debug => "debug",
Macros => "macros",
N => "n",
Normalize => "normalize",
R => "r",
Reduce => "reduce",
Resolve => "resolve",
Time => "time",
VN => "vn",
VNormalize => "vnormalize",
VR => "vr",
VReduce => "vreduce",
};
write!(f, "!{}", name)
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,7 @@ fn match_macro(s: Span) -> IResult<LambdaTree> {
};

let (rest, args) = opt(match_macro_args)(rest)?;
let lambdas = match args {
Some(lambdas) => lambdas,
None => Vec::new(),
};
let lambdas = args.unwrap_or_default();

Ok((rest, LambdaTree::new_macro(m, lambdas)))
}
Expand Down
23 changes: 14 additions & 9 deletions src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::String;
use core::fmt::Write;
use core::str::FromStr;

#[cfg(feature = "std")]
use colored::Colorize;
Expand All @@ -21,15 +22,6 @@ pub enum Strategy {
}

impl Strategy {
pub fn from_str(s: &str) -> Result<Self, ()> {
match s {
"applicative" => Ok(Self::Applicative),
"normal" => Ok(Self::Normal),
"callbyname" => Ok(Self::CallByName),
_ => Err(()),
}
}

pub fn normalize(&self, term: LambdaTree, verbose: bool, out: &mut impl Write) -> (LambdaTree, usize) {
let mut current = term;
let mut nreductions = 0;
Expand Down Expand Up @@ -262,3 +254,16 @@ impl Default for Strategy {
Self::Applicative
}
}

impl FromStr for Strategy {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"applicative" => Ok(Self::Applicative),
"normal" => Ok(Self::Normal),
"callbyname" => Ok(Self::CallByName),
_ => Err(()),
}
}
}

0 comments on commit c5d6b67

Please sign in to comment.