Skip to content

Commit

Permalink
make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 18, 2023
1 parent cdaf2fe commit 3003429
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 61 deletions.
4 changes: 2 additions & 2 deletions components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn compile_expr(db: &dyn crate::Db, expr: &syntax::Expr, chunk: &mut Chunk) {
syntax::Expr::BooleanLiteral(_) => todo!(),
syntax::Expr::NilLiteral => todo!(),
syntax::Expr::BinaryOp(left, op, right) => {
compile_expr(db, &left, chunk);
compile_expr(db, &right, chunk);
compile_expr(db, left, chunk);
compile_expr(db, right, chunk);
match op {
syntax::Op::Plus => chunk.emit_byte(Code::Add),
syntax::Op::Minus => chunk.emit_byte(Code::Subtract),
Expand Down
4 changes: 4 additions & 0 deletions components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ impl Chunk {
pub fn len(&self) -> usize {
self.code.len()
}

pub fn is_empty(&self) -> bool {
self.code.is_empty()
}
}
4 changes: 4 additions & 0 deletions components/lox-ir/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ impl Span {
self.end.0 - self.start.0
}

pub fn is_empty(&self) -> bool {
self.start == self.end
}

pub fn to(self, other: Span) -> Span {
assert!(self.start <= other.start && other.end >= self.end);
Span {
Expand Down
2 changes: 1 addition & 1 deletion components/lox-ir/src/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ impl Word {
}

pub fn as_str(self, db: &dyn Db) -> &str {
&self.string(db)
self.string(db)
}
}
2 changes: 1 addition & 1 deletion components/lox-lex/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ where
push_token(Token::Alphabetic(text));
}
'0'..='9' => {
let text = self.accumulate(ch, |c| matches!(c, '0'..='9'));
let text = self.accumulate(ch, |c| c.is_ascii_digit());
push_token(Token::Number(text));
}
'+' | '-' | '*' | '/' | '!' | '<' | '>' | '=' => {
Expand Down
15 changes: 5 additions & 10 deletions components/lox-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<'me> Parser<'me> {
parse_rhs: impl Fn(&mut Self) -> Option<Expr>,
) -> Option<Expr> {
for op in ops {
if let Some(_) = self.eat_op(*op) {
if self.eat_op(*op).is_some() {
let right = parse_rhs(self)?;
let left = Expr::BinaryOp(Box::new(left), *op, Box::new(right));
return Some(left);
Expand All @@ -132,7 +132,7 @@ impl<'me> Parser<'me> {

fn unary(&mut self) -> Option<Expr> {
for op in &[Op::Minus, Op::Bang] {
if let Some(_) = self.eat_op(*op) {
if self.eat_op(*op).is_some() {
let expr = self.unary()?;
return Some(Expr::UnaryOp(*op, Box::new(expr)));
}
Expand All @@ -141,11 +141,11 @@ impl<'me> Parser<'me> {
}

fn primary(&mut self) -> Option<Expr> {
if let Some(_) = self.eat(Keyword::True) {
if self.eat(Keyword::True).is_some() {
Some(Expr::BooleanLiteral(true))
} else if let Some(_) = self.eat(Keyword::False) {
} else if self.eat(Keyword::False).is_some() {
Some(Expr::BooleanLiteral(false))
} else if let Some(_) = self.eat(Keyword::Nil) {
} else if self.eat(Keyword::Nil).is_some() {
Some(Expr::NilLiteral)
} else if let Some((_, word)) = self.eat(Number) {
Some(Expr::NumberLiteral(word))
Expand All @@ -165,11 +165,6 @@ impl<'me> Parser<'me> {
test.test(self.db, self.tokens.peek()?, span)
}

/// Span of the next pending token, or the span of EOF if there is no next token.
fn peek_span(&mut self) -> Span {
self.tokens.peek_span()
}

/// If the next pending token matches `test`, consumes it and
/// returns the span + narrowed view. Otherwise returns None
/// and has no effect. Returns None if there is no pending token.
Expand Down
79 changes: 38 additions & 41 deletions components/lox-parse/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
use std::{
fs,
path::{Path, PathBuf},
};

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
struct TestCase {
lox: PathBuf,
syntax: PathBuf,
text: String,
}

impl TestCase {
fn list(path: &'static str) -> Vec<TestCase> {
let crate_root_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let test_data_dir = crate_root_dir.join("test_data");
let dir = test_data_dir.join(path);

let mut res = Vec::new();
let read_dir = fs::read_dir(&dir)
.unwrap_or_else(|err| panic!("can't `read_dir` {}: {err}", dir.display()));
for file in read_dir {
let file = file.unwrap();
let path = file.path();
if path.extension().unwrap_or_default() == "lox" {
let rs = path;
let rast = rs.with_extension("syntax");
let text = fs::read_to_string(&rs).unwrap();
res.push(TestCase {
lox: rs,
syntax: rast,
text,
});
}
}
res.sort();
res
}
}

#[cfg(test)]
mod tests {
use std::{path::{PathBuf, Path}, fs};

use expect_test::expect_file;
use lox_ir::{diagnostic::Diagnostics, input_file::InputFile, word::Word};
use salsa::DebugWithDb;

use crate::file_parser::parse_file;

use super::TestCase;
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
struct TestCase {
lox: PathBuf,
syntax: PathBuf,
text: String,
}

impl TestCase {
fn list(path: &'static str) -> Vec<TestCase> {
let crate_root_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let test_data_dir = crate_root_dir.join("test_data");
let dir = test_data_dir.join(path);

let mut res = Vec::new();
let read_dir = fs::read_dir(&dir)
.unwrap_or_else(|err| panic!("can't `read_dir` {}: {err}", dir.display()));
for file in read_dir {
let file = file.unwrap();
let path = file.path();
if path.extension().unwrap_or_default() == "lox" {
let rs = path;
let rast = rs.with_extension("syntax");
let text = fs::read_to_string(&rs).unwrap();
res.push(TestCase {
lox: rs,
syntax: rast,
text,
});
}
}
res.sort();
res
}
}



#[salsa::db(crate::Jar, lox_ir::Jar, lox_lex::Jar)]
#[derive(Default)]
Expand Down
6 changes: 0 additions & 6 deletions components/lox-parse/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ impl<'me> Tokens<'me> {
}
}

/// True if we skipped a newline after consuming
/// the last token.
pub(crate) fn skipped_newline(&self) -> bool {
self.skipped >= Skipped::Newline
}

/// True if we skipped whitespace after consuming
/// the last token.
pub(crate) fn skipped_any(&self) -> bool {
Expand Down

0 comments on commit 3003429

Please sign in to comment.