Skip to content

Commit

Permalink
Fixed most Clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mschae23 committed Aug 6, 2022
1 parent 46963dc commit fde1679
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "density_function_lang"
version = "3.2.0"
version = "3.2.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Debug for Decl {
match self {
Decl::Template { name, this, args, expr } =>
write!(f, "template {}({}{}) = {:?};", name.source(),
this.as_ref().map(|this| format!("{}, ", this.source())).unwrap_or_else(|| String::new()),
this.as_ref().map(|this| format!("{}, ", this.source())).unwrap_or_else(String::new),
args.iter()
.map(|arg| arg.source().to_owned())
.collect::<Vec<String>>().join(", "), expr),
Expand Down
113 changes: 48 additions & 65 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct TemplateCall {
pub tailrec_index: u32,
}

type TemplateCompileData = (TemplateExpr, Vec<Rc<RefCell<Module>>>, Rc<RefCell<PathBuf>>);

pub struct Compiler {
current_module: Vec<Rc<RefCell<Module>>>,
top_level_module: Rc<RefCell<Module>>,
Expand Down Expand Up @@ -118,7 +120,7 @@ impl Compiler {
}

let template_current_modules = self.current_module.iter()
.map(|module| Rc::downgrade(module)).collect();
.map(Rc::downgrade).collect();

current_module.templates.push(Rc::new(RefCell::new(Template {
name: name.source().to_owned(),
Expand Down Expand Up @@ -210,7 +212,7 @@ impl Compiler {

let mut compiler = Compiler::new(path, self.target_dir.to_owned(), Rc::clone(&self.config));
compiler.outer_top_level_module = Some(Rc::clone(&self.top_level_module));
compiler.outer_current_module = Some(self.current_module.iter().map(|module| Rc::clone(module)).collect());
compiler.outer_current_module = Some(self.current_module.iter().map(Rc::clone).collect());
compiler.compile(declarations);

if compiler.had_error() {
Expand Down Expand Up @@ -289,7 +291,7 @@ impl Compiler {
return;
}

current_module_borrow.imported_templates.push(Rc::clone(&template));
current_module_borrow.imported_templates.push(Rc::clone(template));
}
}

Expand All @@ -314,7 +316,7 @@ impl Compiler {
return;
}

current_module_borrow.imported_sub_modules.push(Rc::clone(&sub_module));
current_module_borrow.imported_sub_modules.push(Rc::clone(sub_module));
}
}
}
Expand All @@ -340,7 +342,7 @@ impl Compiler {
Expr::ConstantBoolean(value) => JsonElement::ConstantBoolean(value),
Expr::ConstantString(value) => JsonElement::ConstantString(value),
Expr::Identifier(name) => {
if let Some((_, element)) = self.template_scopes.last().map(|scope| scope.args.iter().rfind(|(arg, _)| *arg == *name.source())).flatten() {
if let Some((_, element)) = self.template_scopes.last().and_then(|scope| scope.args.iter().rfind(|(arg, _)| *arg == *name.source())) {
element.clone()
} else {
self.evaluate_identifier(name)
Expand Down Expand Up @@ -401,7 +403,7 @@ impl Compiler {
};

if static_expr {
if let Some(result) = self.evaluate_static_operator(&name, &args) {
if let Some(result) = self.evaluate_static_operator(&name, args) {
return Err(result);
}
}
Expand All @@ -417,7 +419,7 @@ impl Compiler {
Ok((receiver, template))
}

fn begin_compile_template(&mut self, template: Rc<RefCell<Template>>, receiver: Option<JsonElement>, token_pos: TokenPos, args: &Vec<JsonElement>) -> (TemplateExpr, Vec<Rc<RefCell<Module>>>, Rc<RefCell<PathBuf>>) {
fn begin_compile_template(&mut self, template: Rc<RefCell<Template>>, receiver: Option<JsonElement>, token_pos: TokenPos, args: &[JsonElement]) -> TemplateCompileData {
let template_borrow = template.borrow();

let mut scope_args = vec![];
Expand All @@ -428,11 +430,8 @@ impl Compiler {
scope_args.push((String::from("this"), receiver));
}

for i in 0..template_borrow.args.len() {
let name = template_borrow.args[i].clone();
let element = args[i].clone();

scope_args.push((name, element));
for (name, element) in template_borrow.args.iter().zip(args.iter()) {
scope_args.push((name.to_owned(), element.to_owned()));
}

self.template_scopes.push(TemplateScope { args: scope_args });
Expand Down Expand Up @@ -462,7 +461,7 @@ impl Compiler {
(template_expr, template_current_modules, template_file_path)
}

fn begin_compile_tailrec_template(&mut self, template: Rc<RefCell<Template>>, receiver: Option<JsonElement>, args: &Vec<JsonElement>) -> TemplateExpr {
fn begin_compile_tailrec_template(&mut self, template: Rc<RefCell<Template>>, receiver: Option<JsonElement>, args: &[JsonElement]) -> TemplateExpr {
let template_borrow = template.borrow();

let mut scope_args = vec![];
Expand All @@ -473,11 +472,8 @@ impl Compiler {
scope_args.push((String::from("this"), receiver));
}

for i in 0..template_borrow.args.len() {
let name = template_borrow.args[i].clone();
let element = args[i].clone();

scope_args.push((name, element));
for (name, element) in template_borrow.args.iter().zip(args.iter()) {
scope_args.push((name.to_owned(), element.to_owned()));
}

*self.template_scopes.last_mut().expect("Internal compiler error: No template scope for tailrec template") =
Expand Down Expand Up @@ -507,7 +503,7 @@ impl Compiler {
};
let (template_expr, template_current_modules, template_file_path) = self.begin_compile_template(Rc::clone(&template), receiver, token_pos, &args);

let expr = self.compile_template_expr(template_expr.clone(), Some(template), static_expr);
let expr = self.compile_template_expr(template_expr, Some(template), static_expr);

self.end_compile_template(template_current_modules, template_file_path);
expr
Expand Down Expand Up @@ -607,21 +603,17 @@ impl Compiler {
"&&" => binary_logic_op!(args, &&),
"||" => binary_logic_op!(args, ||),

"[" => match &args[0] {
JsonElement::Array(array) => match &args[1] {
JsonElement::ConstantInt(index) => {
if *index < 0 || *index as usize > array.len() {
self.error_at_with_context(*name.start(), "Array index out of bounds", vec![
("Length", JsonElement::ConstantInt(array.len() as i32)),
("Index", JsonElement::ConstantInt(*index))
], false)
}

return Some(array[*index as usize].clone())
},
_ => {},
},
_ => {},
"[" => if let JsonElement::Array(array) = &args[0] {
if let JsonElement::ConstantInt(index) = &args[1] {
if *index < 0 || *index as usize > array.len() {
self.error_at_with_context(*name.start(), "Array index out of bounds", vec![
("Length", JsonElement::ConstantInt(array.len() as i32)),
("Index", JsonElement::ConstantInt(*index))
], false)
}

return Some(array[*index as usize].clone())
}
},
_ => {},
}
Expand Down Expand Up @@ -679,7 +671,7 @@ impl Compiler {
continue;
} else {
let (template_expr, template_current_modules, template_file_path) = self.begin_compile_template(Rc::clone(&template), receiver, *token.start(), &compiled_args);
let expr = self.compile_template_expr(template_expr.clone(), Some(template), static_expr);
let expr = self.compile_template_expr(template_expr, Some(template), static_expr);

self.end_compile_template(template_current_modules, template_file_path);
return expr
Expand All @@ -696,19 +688,14 @@ impl Compiler {
}

fn find_template(&mut self, name: &Token, receiver: Option<&JsonElement>, arg_count: usize) -> Option<Rc<RefCell<Template>>> {
if let Some(receiver) = receiver {
match receiver {
JsonElement::Module(module) => return Self::find_template_on(module, name, false, arg_count),
_ => {},
}
if let Some(JsonElement::Module(module)) = receiver {
return Self::find_template_on(module, name, false, arg_count)
}

if let Some((_, arg)) = self.template_scopes.last().and_then(|scope| scope.args.iter()
.find(|(arg_name, _)| arg_name == name.source())) {
match arg {
JsonElement::Template(template) => return Some(Rc::clone(template)),
_ => {},
}
if let Some((_, JsonElement::Template(template))) = self.template_scopes.last()
.and_then(|scope| scope.args.iter()
.find(|(arg_name, _)| arg_name == name.source())) {
return Some(Rc::clone(template));
}

let mut module_index: isize = self.current_module.len() as isize - 1;
Expand All @@ -718,7 +705,7 @@ impl Compiler {
&self.current_module[module_index as usize]
} else { &self.top_level_module });

if let Some(template) = Self::find_template_on(&module, &name, receiver.is_some(), arg_count) {
if let Some(template) = Self::find_template_on(&module, name, receiver.is_some(), arg_count) {
return Some(template);
}

Expand Down Expand Up @@ -775,12 +762,12 @@ impl Compiler {
}

fn evaluate_builtin_error_call(&mut self, name: Token, args: Vec<JsonElement>) -> JsonElement {
if args.len() == 0 {
if args.is_empty() {
self.error_at(*name.start(), "builtin.error() called", false);
} else if args.len() > 0 {
} else {
match &args[0] {
JsonElement::ConstantString(msg) =>
self.error_at_with_context(*name.start(), &msg.to_owned(), args.into_iter()
self.error_at_with_context(*name.start(), &msg.clone(), args.into_iter()
.skip(1).map(|arg| ("Context", arg)).collect(), false),
_ => self.error_at_with_context(*name.start(), "builtin.error() called", args.into_iter()
.map(|arg| ("Context", arg)).collect(), false),
Expand Down Expand Up @@ -851,20 +838,16 @@ impl Compiler {
if static_expr && name.source() == "type" {
return JsonElement::Type(JsonElementType::from(receiver));
} else if static_expr && name.source() == "length" {
match receiver {
JsonElement::Array(array) => return JsonElement::ConstantInt(array.len() as i32),
_ => {},
if let JsonElement::Array(array) = receiver {
return JsonElement::ConstantInt(array.len() as i32);
}
}

if static_expr {
match receiver {
JsonElement::Object(fields) => {
if let Some((_, field)) = fields.iter().find(|(key, _)| name.source() == key) {
return field.clone();
}
},
_ => {},
if let JsonElement::Object(fields) = receiver {
if let Some((_, field)) = fields.iter().find(|(key, _)| name.source() == key) {
return field.clone();
}
}
}

Expand All @@ -875,11 +858,11 @@ impl Compiler {
}

fn current_module(&self) -> Rc<RefCell<Module>> {
self.current_module.last().map(|module| Rc::clone(&module)).unwrap_or_else(|| Rc::clone(&self.top_level_module))
self.current_module.last().map(Rc::clone).unwrap_or_else(|| Rc::clone(&self.top_level_module))
}

fn current_or_outer_module(&self) -> Rc<RefCell<Module>> {
self.current_module.last().map(|module| Rc::clone(&module))
self.current_module.last().map(Rc::clone)
.or_else(|| self.outer_current_module.as_ref()
.and_then(|modules| modules.last().map(Rc::clone))
.or_else(|| self.outer_top_level_module.as_ref().map(Rc::clone)))
Expand Down Expand Up @@ -935,13 +918,13 @@ impl Compiler {
acc.push('.');
Some(acc)
} else { None }
} else { None }).unwrap_or_else(|| String::new());
} else { None }).unwrap_or_default();

if template_call.tailrec_index > 0 {
if template_call.tailrec_index == 1 {
eprint!(" ... 1 tail recursion call\n");
eprintln!(" ... 1 tail recursion call");
} else {
eprint!(" ... {} tail recursion calls\n", template_call.tailrec_index);
eprintln!(" ... {} tail recursion calls", template_call.tailrec_index);
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/compiler/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct Token {
start: TokenPos, end: TokenPos,
}

impl<'a> Token {
impl Token {
pub fn new(token_type: TokenType, source: String, start: TokenPos, end: TokenPos) -> Token {
Token {
token_type, source,
Expand Down Expand Up @@ -234,7 +234,7 @@ impl<'source> Lexer<'source> {
'-' => {
let next = self.peek()?;

if next >= '0' && next <= '9' {
if ('0'..='9').contains(&next) {
self.scan_number()
} else {
Ok(if self.expect('=')? { self.make_token(TokenType::MinusAssign) } else {
Expand Down Expand Up @@ -345,10 +345,8 @@ impl<'source> Lexer<'source> {
'i' => {
if let Some(c) = chars.next() {
match c {
'f' => if let Some(c) = chars.next() {
match c {
_ => TokenType::Identifier,
}
'f' => if chars.next().is_some() {
TokenType::Identifier
} else { TokenType::If },
'n' => {
if let Some(c) = chars.next() {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod ast;
pub mod lexer;
pub mod parser;
#[allow(clippy::module_inception)]
pub mod compiler;
pub mod writer;
21 changes: 10 additions & 11 deletions src/compiler/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,22 +249,19 @@ impl<'source> Parser<'source> {
let condition = self.parse_expression();
self.expect(TokenType::ParenthesisRight, "Expected ')' after 'if' condition");

let then;

if self.matches(TokenType::BracketLeft) {
then = self.parse_block_template_expression();
let then = if self.matches(TokenType::BracketLeft) {
self.parse_block_template_expression()
} else {
then = self.parse_template_expression();
}
self.parse_template_expression()
};

self.expect(TokenType::Else, "Expected 'else' after then clause of 'if' expression");
let otherwise;

if self.matches(TokenType::BracketLeft) {
otherwise = self.parse_block_template_expression();
let otherwise = if self.matches(TokenType::BracketLeft) {
self.parse_block_template_expression()
} else {
otherwise = self.parse_template_expression();
}
self.parse_template_expression()
};

TemplateExpr::If { token, condition, then: Box::new(then), otherwise: Box::new(otherwise) }
}
Expand Down Expand Up @@ -645,6 +642,8 @@ impl<'source> Parser<'source> {

match self.current.token_type() {
TokenType::Export => return,
TokenType::Template => return,
TokenType::Module => return,
_ => {},
};

Expand Down
Loading

0 comments on commit fde1679

Please sign in to comment.