Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
strtok committed Apr 6, 2024
1 parent af499bd commit a194f15
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 75 deletions.
2 changes: 1 addition & 1 deletion marwood/examples/factorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
(factorial (- n 1) (* acc n)))))
"#;

vm.eval_text(&code).unwrap();
vm.eval_text(code).unwrap();

for it in 0..10 {
let (cell, _) = vm.eval_text(&format!("(factorial {})", it)).unwrap();
Expand Down
10 changes: 5 additions & 5 deletions marwood/src/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub fn named_to_char(text: &str) -> Option<char> {
match text {
"alarm" => Some(char::from_u32(ALARM).unwrap()),
"backspace" => Some(char::from_u32(BACKSPACE).unwrap()),
"delete" => Some(char::from_u32(DELETE)).unwrap(),
"escape" => Some(char::from_u32(ESCAPE)).unwrap(),
"null" => Some(char::from_u32(NULL)).unwrap(),
"return" => Some(char::from_u32(RETURN)).unwrap(),
"tab" => Some(char::from_u32(TAB)).unwrap(),
"delete" => char::from_u32(DELETE),
"escape" => char::from_u32(ESCAPE),
"null" => char::from_u32(NULL),
"return" => char::from_u32(RETURN),
"tab" => char::from_u32(TAB),
"space" => Some(' '),
"newline" => Some('\n'),
_ => None,
Expand Down
10 changes: 3 additions & 7 deletions marwood/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ impl Token {
/// # Safety
/// This method assumes the originally scanned &str be used, and
/// may panic otherwise.
pub fn span<'a, 'b>(&'a self, text: &'b str) -> &'b str {
pub fn span<'a>(&self, text: &'a str) -> &'a str {
&text[self.span.0..self.span.1]
}

/// span prefix
///
/// Given the originally scanned &str, extract any text before this
/// token.
pub fn span_prefix<'a, 'b>(&'a self, text: &'b str) -> &'b str {
pub fn span_prefix<'a>(&self, text: &'a str) -> &'a str {
&text[0..self.span.0]
}

Expand Down Expand Up @@ -246,11 +246,7 @@ fn scan_string(cur: &mut Peekable<CharIndices>) -> Result<Token, Error> {
terminated = true;
break;
}
if c == '\\' && !escaping {
escape_next = true;
} else {
escape_next = false;
}
escape_next = c == '\\' && !escaping;
cur.next();
}
// empty string
Expand Down
2 changes: 1 addition & 1 deletion marwood/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ impl Number {
Number::Fixnum(lhs) => match rhs {
Number::Fixnum(rhs) => Some((lhs / rhs).into()),
Number::BigInt(rhs) => Some((BigInt::from(*lhs) / &**rhs).into()),
Number::Float(rhs) => lhs.to_f64().map(|lhs| (lhs as f64 / rhs).trunc().into()),
Number::Float(rhs) => lhs.to_f64().map(|lhs| (lhs / rhs).trunc().into()),
Number::Rational(rhs) => {
if rhs.is_integer() {
Some((*lhs / rhs.to_i64().unwrap()).into())
Expand Down
4 changes: 1 addition & 3 deletions marwood/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ impl ReplHighlighter {
return false;
}
};
if index > 0 {
index -= 1;
}
index = index.saturating_sub(1);
matches!(
find_token_at_cursor(&tokens, index),
Some((
Expand Down
68 changes: 28 additions & 40 deletions marwood/src/vm/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,17 @@ fn pop_argc(vm: &mut Vm, min: usize, max: Option<usize>, proc: &str) -> Result<u
fn pop_number(vm: &mut Vm) -> Result<Number, Error> {
match vm.heap.get(vm.stack.pop()?) {
VCell::Number(num) => Ok(num),
vcell => {
return Err(InvalidSyntax(format!(
"{:#} is not a valid number",
vm.heap.get_as_cell(&vcell)
)))
}
vcell => Err(InvalidSyntax(format!(
"{:#} is not a valid number",
vm.heap.get_as_cell(&vcell)
))),
}
}

fn pop_integer(vm: &mut Vm) -> Result<Number, Error> {
match pop_number(vm) {
Ok(num) if num.is_integer() => Ok(num),
Ok(num) => return Err(InvalidSyntax(format!("{} is not a valid integer", num))),
Ok(num) => Err(InvalidSyntax(format!("{} is not a valid integer", num))),
Err(e) => Err(e),
}
}
Expand All @@ -105,46 +103,40 @@ fn pop_usize(vm: &mut Vm) -> Result<usize, Error> {
Ok(num) if num.is_integer() && num >= Number::from(0) && num.to_usize().is_some() => {
Ok(num.to_usize().unwrap())
}
Ok(num) => return Err(InvalidSyntax(format!("{} is not a valid size", num))),
Ok(num) => Err(InvalidSyntax(format!("{} is not a valid size", num))),
Err(e) => Err(e),
}
}

fn pop_char(vm: &mut Vm) -> Result<char, Error> {
match vm.heap.get(vm.stack.pop()?) {
VCell::Char(c) => Ok(c),
vcell => {
return Err(InvalidSyntax(format!(
"{:#} is not a valid character",
vm.heap.get_as_cell(&vcell)
)))
}
vcell => Err(InvalidSyntax(format!(
"{:#} is not a valid character",
vm.heap.get_as_cell(&vcell)
))),
}
}

fn pop_string(vm: &mut Vm, proc: &str) -> Result<Rc<RefCell<String>>, Error> {
match vm.heap.get(vm.stack.pop()?) {
VCell::String(s) => Ok(s),
vcell => {
return Err(InvalidSyntax(format!(
"bad argument to {}: {:#} is not a string",
proc,
vm.heap.get_as_cell(&vcell)
)))
}
vcell => Err(InvalidSyntax(format!(
"bad argument to {}: {:#} is not a string",
proc,
vm.heap.get_as_cell(&vcell)
))),
}
}

fn pop_symbol(vm: &mut Vm, proc: &str) -> Result<Rc<String>, Error> {
match vm.heap.get(vm.stack.pop()?) {
VCell::Symbol(s) => Ok(s),
vcell => {
return Err(InvalidSyntax(format!(
"bad argument to {}: {:#} is not a symbol",
proc,
vm.heap.get_as_cell(&vcell)
)))
}
vcell => Err(InvalidSyntax(format!(
"bad argument to {}: {:#} is not a symbol",
proc,
vm.heap.get_as_cell(&vcell)
))),
}
}

Expand All @@ -156,23 +148,19 @@ fn pop_index(vm: &mut Vm, proc: &str) -> Result<usize, Error> {
proc, num
))
}),
vcell => {
return Err(InvalidSyntax(format!(
"{:#} is not a valid index",
vm.heap.get_as_cell(&vcell)
)))
}
vcell => Err(InvalidSyntax(format!(
"{:#} is not a valid index",
vm.heap.get_as_cell(&vcell)
))),
}
}

fn pop_vector(vm: &mut Vm) -> Result<Rc<Vector>, Error> {
match vm.heap.get(vm.stack.pop()?) {
VCell::Vector(vector) => Ok(vector),
vcell => {
return Err(InvalidSyntax(format!(
"{:#} is not a vector",
vm.heap.get_as_cell(&vcell)
)))
}
vcell => Err(InvalidSyntax(format!(
"{:#} is not a vector",
vm.heap.get_as_cell(&vcell)
))),
}
}
4 changes: 2 additions & 2 deletions marwood/src/vm/builtin/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub fn atan(vm: &mut Vm) -> Result<VCell, Error> {
if let Some(result) = y.atan2(x.clone()) {
Ok(result.into())
} else {
return Err(InvalidSyntax(format!("atan is undefined for {} {}", y, x)));
Err(InvalidSyntax(format!("atan is undefined for {} {}", y, x)))
}
}
_ => {
Expand All @@ -346,7 +346,7 @@ pub fn atan(vm: &mut Vm) -> Result<VCell, Error> {
if let Some(result) = y.atan() {
Ok(result.into())
} else {
return Err(InvalidSyntax(format!("atan is undefined for {}", y)));
Err(InvalidSyntax(format!("atan is undefined for {}", y)))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions marwood/src/vm/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Heap {
Heap {
chunk_size,
heap: vec![VCell::undefined(); chunk_size],
free_list: (0..chunk_size).rev().into_iter().collect(),
free_list: (0..chunk_size).rev().collect(),
heap_map: gc::Map::new(chunk_size),
symbol_table: HashMap::new(),
}
Expand Down Expand Up @@ -359,10 +359,10 @@ impl Heap {
ptr = cdr;
}
VCell::Continuation(cont) => {
self.mark_continuation(&*cont);
self.mark_continuation(&cont);
}
VCell::Lambda(ptr) => {
self.mark_lambda(&*ptr);
self.mark_lambda(&ptr);
}
VCell::Closure(lambda, env) => {
self.mark(lambda);
Expand Down Expand Up @@ -410,7 +410,7 @@ impl Heap {
self.mark(*lambda);
}
VCell::Continuation(cont) => {
self.mark_continuation(&*cont);
self.mark_continuation(cont);
}
VCell::Lambda(lambda) => self.mark_lambda(lambda.as_ref()),
VCell::Closure(lambda, env) => {
Expand Down
4 changes: 2 additions & 2 deletions marwood/src/vm/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Display for DecompiledInstruction {
f,
"{0: <8} {1: <12} {2: <12} //{3: <10}",
op,
operands.get(0).unwrap_or(&"".to_string()),
operands.first().unwrap_or(&"".to_string()),
operands.get(1).unwrap_or(&"".to_string()),
values.join(" ")
)
Expand All @@ -151,7 +151,7 @@ impl Display for DecompiledInstruction {
f,
"{0: <8} {1: <12} {2: <12}",
op,
operands.get(0).unwrap_or(&"".to_string()),
operands.first().unwrap_or(&"".to_string()),
operands.get(1).unwrap_or(&"".to_string()),
)
} else {
Expand Down
8 changes: 4 additions & 4 deletions marwood/src/vm/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ impl Transform {
}
}

return Err(InvalidSyntax(format!(
Err(InvalidSyntax(format!(
"no matching syntax for {}",
self.keyword
)));
)))
}

/// Pattern Match
Expand All @@ -320,11 +320,11 @@ impl Transform {
/// `pattern` - The pattern to attempt to apply
/// `expr` - The expression to match
/// `bindings` - The set of matched variable bindings
fn pattern_match<'a, 'b>(
fn pattern_match<'a>(
&self,
pattern: &'a Cell,
expr: &'a Cell,
env: &'b mut PatternEnvironment<'a>,
env: &mut PatternEnvironment<'a>,
) -> bool {
// expr and pattern must either both be lists or improper lists
if (pattern.is_pair() || pattern.is_nil()) && !(expr.is_pair() || expr.is_nil()) {
Expand Down
12 changes: 6 additions & 6 deletions marwood/src/vm/vcell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,14 @@ impl VCell {

pub fn as_symbol(&self) -> Result<&str, Error> {
match self {
VCell::Symbol(s) => Ok(&*s),
VCell::Symbol(s) => Ok(s),
_ => Err(ExpectedType(SYMBOL_TYPE_TEXT, self.type_text())),
}
}

pub fn as_string(&self) -> Result<&RefCell<String>, Error> {
match self {
VCell::String(s) => Ok(&*s),
VCell::String(s) => Ok(s),
_ => Err(ExpectedType(SYMBOL_TYPE_TEXT, self.type_text())),
}
}
Expand All @@ -337,14 +337,14 @@ impl VCell {

pub fn as_lambda(&self) -> Result<&Lambda, Error> {
match self {
VCell::Lambda(lambda) => Ok(&*lambda),
VCell::Lambda(lambda) => Ok(lambda),
_ => Err(ExpectedType(LAMBDA_TYPE_TEXT, self.type_text())),
}
}

pub fn as_macro(&self) -> Result<&Transform, Error> {
match self {
VCell::Macro(transform) => Ok(&*transform),
VCell::Macro(transform) => Ok(transform),
_ => Err(ExpectedType(MACRO_TYPE_TEXT, self.type_text())),
}
}
Expand All @@ -365,14 +365,14 @@ impl VCell {

pub fn as_lexical_env(&self) -> Result<&LexicalEnvironment, Error> {
match self {
VCell::LexicalEnv(env) => Ok(&*env),
VCell::LexicalEnv(env) => Ok(env),
_ => Err(ExpectedType(LEXICAL_ENV_TYPE_TEXT, self.type_text())),
}
}

pub fn as_vector(&self) -> Result<&Vector, Error> {
match self {
VCell::Vector(vector) => Ok(&*vector),
VCell::Vector(vector) => Ok(vector),
_ => Err(ExpectedType(VECTOR_TYPE_TEXT, self.type_text())),
}
}
Expand Down

0 comments on commit a194f15

Please sign in to comment.