Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A31 Feature: Add linter Clippy #22

Merged
merged 3 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/target

# Test files
test.amber
test.ab
test.sh

# MacOS
Expand Down
4 changes: 2 additions & 2 deletions 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
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
heraclitus-compiler = "1.3.0"
heraclitus-compiler = "1.3.2"
similar-string = "1.4.2"
colored = "2.0.0"
7 changes: 6 additions & 1 deletion build.ab
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ let linux_x64 = 'x86_64-unknown-linux-musl'
$test -f "Cargo.toml" > /dev/null$

if $echo \$?$ == '0' {
$cargo build --release --target {macos_arm} --target {linux_arm} --target {macos_x64} --target {linux_x64}$
$cargo build --release
--target {macos_arm}
--target {linux_arm}
--target {macos_x64}
--target {linux_x64}
$
# Move ambers to the release directory
$mv target/aarch64-apple-darwin/release/amber target/release/amber_macos_aarch64$
$mv target/aarch64-unknown-linux-musl/release/amber target/release/amber_linux_aarch64$
Expand Down
28 changes: 17 additions & 11 deletions src/cli/cli.rs → src/cli/cli_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ pub struct CLI {
ext: String
}

impl Default for CLI {
fn default() -> Self {
Self::new()
}
}

impl CLI {
pub fn new() -> Self {
CLI {
args: vec![],
flags: FlagRegistry::new(),
name: format!("Amber"),
exe_name: format!("amber"),
version: format!("{}", env!("CARGO_PKG_VERSION")),
ext: format!(".amber")
name: "Amber".to_string(),
exe_name: "amber".to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
ext: ".amber".to_string()
}
}

Expand All @@ -40,7 +46,7 @@ impl CLI {
if self.flags.flag_triggered("-e") {
match self.flags.get_flag("-e").unwrap().value.clone() {
Some(code) => {
let translation = self.compile(code.clone(), None);
let translation = self.compile(code, None);
self.execute(translation);
},
None => {
Expand All @@ -55,14 +61,14 @@ impl CLI {
let input = self.args[1].clone();
match self.read_file(input.clone()) {
Ok(code) => {
let code = self.compile(code, Some(input.clone()));
let code = self.compile(code, Some(input));
// Save to the output file
if self.args.len() >= 3 {
let output = self.args[2].clone();
match fs::File::create(output.clone()) {
Ok(mut file) => {
write!(file, "{}", code).unwrap();
self.set_file_permission(&file, output.clone());
self.set_file_permission(&file, output);

},
Err(err) => {
Expand Down Expand Up @@ -120,14 +126,14 @@ impl CLI {
let mut block = block::Block::new();
cc.load(code.clone());
if let Ok(tokens) = cc.tokenize() {
let mut meta = ParserMetadata::new(tokens, path.clone(), Some(code.clone()));
let mut meta = ParserMetadata::new(tokens, path, Some(code));
if let Ok(()) = block.parse(&mut meta) {
let mut meta = TranslateMetadata::new();
return format!("{}", block.translate(&mut meta));
return block.translate(&mut meta);
}
return format!("[parsing err]")
return "[parsing err]".to_string()
}
format!("[lexing err]")
"[lexing err]".to_string()
}

fn execute(&self, code: String) {
Expand Down
6 changes: 6 additions & 0 deletions src/cli/flag_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub struct FlagRegistry {
flags: HashMap<String, Flag>
}

impl Default for FlagRegistry {
fn default() -> Self {
Self::new()
}
}

impl FlagRegistry {
#[inline]
pub fn new() -> Self {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod flag_registry;
pub mod cli;
pub mod cli_interface;

#[cfg(test)]
mod tests {
use super::cli::CLI;
use super::cli_interface::CLI;

#[test]
fn hello_world() {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod rules;
mod utils;
mod translate;
pub mod cli;
use cli::cli::CLI;
use cli::cli_interface::CLI;

fn main() {
let mut cli = CLI::new();
Expand Down
35 changes: 15 additions & 20 deletions src/modules/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use heraclitus_compiler::prelude::*;
use crate::{utils::{metadata::ParserMetadata, error::get_error_logger, TranslateMetadata}};
use crate::translate::module::TranslateModule;
use super::statement::statement::Statement;
use super::statement::st::Statement;

#[derive(Debug)]
pub struct Block {
Expand All @@ -28,25 +28,20 @@ impl SyntaxModule<ParserMetadata> for Block {

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
meta.var_mem.push_scope();
loop {
match meta.get_current_token() {
Some(token) => {
// Handle the end of line or command
if ["\n", ";"].contains(&token.word.as_str()) {
meta.increment_index();
continue;
}
// Handle comments
if token.word.starts_with("#") {
meta.increment_index();
continue
}
// Handle block end
else if token.word == "}" {
break;
}
}
None => break
while let Some(token) = meta.get_current_token() {
// Handle the end of line or command
if ["\n", ";"].contains(&token.word.as_str()) {
meta.increment_index();
continue;
}
// Handle comments
if token.word.starts_with('#') {
meta.increment_index();
continue
}
// Handle block end
else if token.word == "}" {
break;
}
let mut statemant = Statement::new();
if let Err(details) = statemant.parse(meta) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/command/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ impl TranslateModule for CommandExpr {
let interps = self.interps.iter()
.map(|item| item.translate(meta))
.collect::<Vec<String>>();
format!("$({})", translate_interpolated_region(self.strings.clone(), interps.clone(), false))
format!("$({})", translate_interpolated_region(self.strings.clone(), interps, false))
}
}
2 changes: 1 addition & 1 deletion src/modules/command/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ impl TranslateModule for CommandStatement {
let interps = self.interps.iter()
.map(|item| item.translate(meta))
.collect::<Vec<String>>();
format!("{}", translate_interpolated_region(self.strings.clone(), interps.clone(), false))
translate_interpolated_region(self.strings.clone(), interps, false)
}
}
10 changes: 5 additions & 5 deletions src/modules/conditions/ifchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ impl SyntaxModule<ParserMetadata> for IfChain {
let mut block = Block::new();
cond.cannot_fail();
// Handle comments and empty lines
if let Ok(_) = token_by(meta, |token| token.starts_with("#") || token.starts_with("\n")) {
if token_by(meta, |token| token.starts_with('#') || token.starts_with('\n')).is_ok() {
continue
}
// Handle else keyword
if let Ok(_) = token(meta, "else") {
if token(meta, "else").is_ok() {
is_else = true;
break
}
// Handle end of the if chain
if let Err(_) = syntax(meta, &mut cond) {
if syntax(meta, &mut cond).is_err() {
break
}
token(meta, "{")?;
Expand Down Expand Up @@ -75,10 +75,10 @@ impl TranslateModule for IfChain {
}
}
if let Some(false_block) = &self.false_block {
result.push(format!("else"));
result.push("else".to_string());
result.push(false_block.translate(meta));
}
result.push(format!("fi"));
result.push("fi".to_string());
result.join("\n")
}
}
6 changes: 3 additions & 3 deletions src/modules/conditions/ifcond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl SyntaxModule<ParserMetadata> for IfCondition {
syntax(meta, &mut *self.true_block)?;
token(meta, "}")?;
// Parse false block
if let Ok(_) = token(meta, "else") {
if token(meta, "else").is_ok() {
token(meta, "{")?;
let mut false_block = Box::new(Block::new());
syntax(meta, &mut *false_block)?;
Expand All @@ -48,10 +48,10 @@ impl TranslateModule for IfCondition {
result.push(format!("if [ {} != 0 ]; then", self.expr.translate(meta)));
result.push(self.true_block.translate(meta));
if let Some(false_block) = &self.false_block {
result.push(format!("else"));
result.push("else".to_string());
result.push(false_block.translate(meta));
}
result.push(format!("fi"));
result.push("fi".to_string());
result.join("\n")
}
}
2 changes: 1 addition & 1 deletion src/modules/expression/binop/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SyntaxModule<ParserMetadata> for Add {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "+")?;
parse_left_expr(meta, &mut self.left, "+")?;
let tok = meta.get_current_token();
token(meta, "+")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SyntaxModule<ParserMetadata> for And {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "and")?;
parse_left_expr(meta, &mut self.left, "and")?;
let tok = meta.get_current_token();
token(meta, "and")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl SyntaxModule<ParserMetadata> for Div {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "/")?;
parse_left_expr(meta, &mut self.left, "/")?;
let tok = meta.get_current_token();
token(meta, "/")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SyntaxModule<ParserMetadata> for Eq {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "==")?;
parse_left_expr(meta, &mut self.left, "==")?;
let tok = meta.get_current_token();
token(meta, "==")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/ge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SyntaxModule<ParserMetadata> for Ge {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, ">=")?;
parse_left_expr(meta, &mut self.left, ">=")?;
let tok = meta.get_current_token();
token(meta, ">=")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/gt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SyntaxModule<ParserMetadata> for Gt {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, ">")?;
parse_left_expr(meta, &mut self.left, ">")?;
let tok = meta.get_current_token();
token(meta, ">")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/le.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SyntaxModule<ParserMetadata> for Le {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "<=")?;
parse_left_expr(meta, &mut self.left, "<=")?;
let tok = meta.get_current_token();
token(meta, "<=")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/lt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SyntaxModule<ParserMetadata> for Lt {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "<")?;
parse_left_expr(meta, &mut self.left, "<")?;
let tok = meta.get_current_token();
token(meta, "<")?;
syntax(meta, &mut *self.right)?;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/expression/binop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod eq;
pub mod neq;


pub fn expression_arms_of_type(meta: &mut ParserMetadata, left: &Box<Expr>, right: &Box<Expr>, kind: Type, tok_pos: Option<Token>, message: &str) {
pub fn expression_arms_of_type(meta: &mut ParserMetadata, left: &Expr, right: &Expr, kind: Type, tok_pos: Option<Token>, message: &str) {
if ![left.get_type(), right.get_type()].iter().all(|item| *item == kind) {
get_error_logger(meta, ErrorDetails::from_token_option(tok_pos))
.attach_message(message)
Expand All @@ -25,7 +25,7 @@ pub fn expression_arms_of_type(meta: &mut ParserMetadata, left: &Box<Expr>, righ
}
}

pub fn expression_arms_of_same_type(meta: &mut ParserMetadata, left: &Box<Expr>, right: &Box<Expr>, tok_pos: Option<Token>, message: &str) {
pub fn expression_arms_of_same_type(meta: &mut ParserMetadata, left: &Expr, right: &Expr, tok_pos: Option<Token>, message: &str) {
if left.get_type() != right.get_type() {
get_error_logger(meta, ErrorDetails::from_token_option(tok_pos))
.attach_message(message)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SyntaxModule<ParserMetadata> for Mul {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "*")?;
parse_left_expr(meta, &mut self.left, "*")?;
let tok = meta.get_current_token();
token(meta, "*")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/neq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SyntaxModule<ParserMetadata> for Neq {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "!=")?;
parse_left_expr(meta, &mut self.left, "!=")?;
let tok = meta.get_current_token();
token(meta, "!=")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SyntaxModule<ParserMetadata> for Or {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "or")?;
parse_left_expr(meta, &mut self.left, "or")?;
let tok = meta.get_current_token();
token(meta, "or")?;
syntax(meta, &mut *self.right)?;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/expression/binop/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SyntaxModule<ParserMetadata> for Sub {
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
parse_left_expr(meta, &mut *self.left, "-")?;
parse_left_expr(meta, &mut self.left, "-")?;
let tok = meta.get_current_token();
token(meta, "-")?;
syntax(meta, &mut *self.right)?;
Expand Down
Loading