Skip to content

Commit

Permalink
fix: warnings and clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ali77gh committed Apr 14, 2024
1 parent ae4df46 commit f775c8a
Show file tree
Hide file tree
Showing 46 changed files with 120 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/parser/get_single_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn get_single_var(params: Vec<Param>, line_number: u32) -> Result<String> {
));
}

match params.get(0).unwrap() {
match params.first().unwrap() {
Param::Tag(_) => Err(ChapError::syntax_with_msg(
line_number,
"you can't set function result to a tag".to_string(),
Expand Down
20 changes: 10 additions & 10 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Parser {
match nc {
None => {
// nothing -> param -> nothing
match cc.get(0) {
match cc.first() {
None => {
return Err(ChapError::syntax_with_msg(
ln,
Expand Down Expand Up @@ -241,7 +241,7 @@ mod tests {
.unwrap();

assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(
1,
"new_tag".to_string(),
Expand All @@ -258,7 +258,7 @@ mod tests {
.on_new_line(LineOfCode::new(1, " $myVar ".to_string()))
.unwrap();
assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(
1,
"print".to_string(),
Expand All @@ -271,7 +271,7 @@ mod tests {
.on_new_line(LineOfCode::new(1, " \"hello\" ".to_string()))
.unwrap();
assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(
1,
"print".to_string(),
Expand All @@ -288,7 +288,7 @@ mod tests {
.on_new_line(LineOfCode::new(1, " exit ".to_string()))
.unwrap();
assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(1, "exit".to_string(), vec![], None)
);
}
Expand All @@ -301,7 +301,7 @@ mod tests {
.on_new_line(LineOfCode::new(1, " 1 -> $var ".to_string()))
.unwrap();
assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(
1,
"assign".to_string(),
Expand All @@ -318,7 +318,7 @@ mod tests {
.on_new_line(LineOfCode::new(1, " $var -> function ".to_string()))
.unwrap();
assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(
1,
"function".to_string(),
Expand All @@ -335,7 +335,7 @@ mod tests {
.on_new_line(LineOfCode::new(1, " input -> $var ".to_string()))
.unwrap();
assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(1, "input".to_string(), vec![], Some("var".to_string()))
);
}
Expand All @@ -348,7 +348,7 @@ mod tests {
.on_new_line(LineOfCode::new(1, " 2 -> sum -> $var ".to_string()))
.unwrap();
assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(
1,
"sum".to_string(),
Expand All @@ -370,7 +370,7 @@ mod tests {
.unwrap();
assert_eq!(result.len(), 2);
assert_eq!(
result.get(0).unwrap(),
result.first().unwrap(),
&ExecutableLine::new(
1,
"add".to_string(),
Expand Down
21 changes: 8 additions & 13 deletions src/preprocessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,7 @@ impl Preprocessor {
}

fn find_backward(s: &str, from: usize) -> Option<usize> {
for i in (0..from).rev() {
if s.chars().nth(i).unwrap() == '(' {
return Some(i);
}
}
None
(0..from).rev().find(|&i| s.chars().nth(i).unwrap() == '(')
}
}

Expand Down Expand Up @@ -179,7 +174,7 @@ mod tests {

let pped = pp.on_new_line("param -> func -> out".to_string()).unwrap();

assert_eq!(3, pped.get(0).unwrap().line_number)
assert_eq!(3, pped.first().unwrap().line_number)
}

#[test]
Expand All @@ -190,7 +185,7 @@ mod tests {
.on_new_line("; command1 ; ; command2 ;;//comment;".to_string())
.unwrap();

assert_eq!("command1", pped.get(0).unwrap().code);
assert_eq!("command1", pped.first().unwrap().code);
assert_eq!("command2", pped.get(1).unwrap().code);
}

Expand All @@ -199,7 +194,7 @@ mod tests {
let mut p = Preprocessor::default();
let result = p.on_new_line("\"hello // world\"".to_string()).unwrap();
assert_eq!(
result.get(0).unwrap().code,
result.first().unwrap().code,
"\"hello // world\"".to_string()
);
}
Expand All @@ -213,7 +208,7 @@ mod tests {

assert_eq!(result1.len(), 0);
assert_eq!(result2.len(), 0);
assert_eq!(result3.get(0).unwrap().code, "1,2->add".to_string());
assert_eq!(result3.first().unwrap().code, "1,2->add".to_string());
}

#[test]
Expand All @@ -222,7 +217,7 @@ mod tests {
let result = p.on_new_line("(1,2->add),2->add".to_string()).unwrap();

assert_eq!(result.len(), 2);
assert_eq!(result.get(0).unwrap().code, "1,2->add->$TMP_0".to_string());
assert_eq!(result.first().unwrap().code, "1,2->add->$TMP_0".to_string());
assert_eq!(result.get(1).unwrap().code, "$TMP_0,2->add".to_string());
}

Expand All @@ -233,7 +228,7 @@ mod tests {
.on_new_line("(1,2->add),(3,4->add)->add".to_string())
.unwrap();
assert_eq!(result.len(), 3);
assert_eq!(result.get(0).unwrap().code, "1,2->add->$TMP_0".to_string());
assert_eq!(result.first().unwrap().code, "1,2->add->$TMP_0".to_string());
assert_eq!(result.get(1).unwrap().code, "3,4->add->$TMP_1".to_string());
assert_eq!(
result.get(2).unwrap().code,
Expand All @@ -248,7 +243,7 @@ mod tests {
.on_new_line("((1,2->add),2->add),(3,4->add)->add".to_string())
.unwrap();
assert_eq!(result.len(), 4);
assert_eq!(result.get(0).unwrap().code, "1,2->add->$TMP_0".to_string());
assert_eq!(result.first().unwrap().code, "1,2->add->$TMP_0".to_string());
assert_eq!(
result.get(1).unwrap().code,
"$TMP_0,2->add->$TMP_1".to_string()
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
pub fn assign(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
assign_validator(executable)?;

let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;

returns(runtime, executable, p1.clone())
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/bools/equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn equal(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;

returns(runtime, executable, DataType::Bool(p1 == p2))
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/bools/greater_than.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn greater_than(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;

let result = greater_than_data_types(p1, p2)?;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/bools/less_than.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn less_than(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;

let result = less_than_data_types(p1, p2)?;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/bools/not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn not(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;

let result = match p1 {
DataType::Bool(x) => !x,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/bools/not_equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn not_equal(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;

returns(runtime, executable, DataType::Bool(p1 != p2))
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/builtin_function/control_flow/jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

// this function can't jump to a tag that is not added to runtime.executables
pub fn jump(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
if let Some(Param::Tag(tag)) = executable.params.get(0) {
if let Some(Param::Tag(tag)) = executable.params.first() {
if let Some(line_number) = runtime.tags.get(tag) {
// jumping back (loop)
runtime.current_line = *line_number;
Expand All @@ -16,7 +16,7 @@ pub fn jump(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
match runtime.executables.get(runtime.current_line) {
Some(el) => {
if el.function_name == "new_tag" {
if let Some(Param::Tag(eltag)) = el.params.get(0) {
if let Some(Param::Tag(eltag)) = el.params.first() {
if tag == eltag {
runtime.tags.insert(tag.clone(), runtime.current_line);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/control_flow/new_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};

pub fn new_tag(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
if let Some(Param::Tag(tag)) = executable.params.get(0) {
if let Some(Param::Tag(tag)) = executable.params.first() {
runtime.tags.insert(tag.clone(), runtime.current_line);
} else {
return Err(ChapError::runtime_with_msg(
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/debugger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn debugger(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()

// run actual function
let mut executable = executable.clone();
executable.function_name = executable.function_name.replace("?", "");
executable.function_name = executable.function_name.replace('?', "");
let real_function: BuiltinFunction = closure_gen(&executable)?;
real_function(runtime, &executable)?;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/delay/wait_hour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{common::executable::ExecutableLine, runtime::Runtime};
use std::{thread, time};

pub fn wait_hour(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;

if let DataType::Int(x) = p1 {
let hour = x * 60 * 24;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/delay/wait_millis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{common::executable::ExecutableLine, runtime::Runtime};
use std::{thread, time};

pub fn wait_millis(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;

if let DataType::Int(x) = p1 {
let ten_millis = time::Duration::from_millis((*x).try_into().unwrap());
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/delay/wait_minute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{common::executable::ExecutableLine, runtime::Runtime};
use std::{thread, time};

pub fn wait_minute(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;

if let DataType::Int(x) = p1 {
let min = x * 60;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/delay/wait_second.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{common::executable::ExecutableLine, runtime::Runtime};
use std::{thread, time};

pub fn wait_second(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;

if let DataType::Int(x) = p1 {
let ten_millis = time::Duration::from_secs((*x).try_into().unwrap());
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/list/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn get(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;

if let (DataType::List(list), DataType::Int(index)) = (p1, p2) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/list/has.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn has(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;

let mut result = DataType::Bool(false);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/list/index_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn index_of(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;

let result = if let DataType::List(list) = p1 {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/list/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn insert(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()>
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?.clone();
let p1 = param_to_datatype_mut(
&mut (*runtime),
executable.params.get(0),
executable.params.first(),
executable.line_number,
)?;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/list/last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{common::executable::ExecutableLine, runtime::Runtime};
pub fn last(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype_mut(
&mut (*runtime),
executable.params.get(0),
executable.params.first(),
executable.line_number,
)?;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/list/pop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{common::executable::ExecutableLine, runtime::Runtime};
pub fn pop(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype_mut(
&mut (*runtime),
executable.params.get(0),
executable.params.first(),
executable.line_number,
)?;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/list/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn remove(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()>
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?.clone();
let p1 = param_to_datatype_mut(
&mut (*runtime),
executable.params.get(0),
executable.params.first(),
executable.line_number,
)?;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/list/remove_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn remove_at(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<(
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?.clone();
let p1 = param_to_datatype_mut(
&mut (*runtime),
executable.params.get(0),
executable.params.first(),
executable.line_number,
)?;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/math/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime::builtin_function::utils::{param_to_datatype, returns};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn add(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let p1 = param_to_datatype(runtime, executable.params.get(0), executable.line_number)?;
let p1 = param_to_datatype(runtime, executable.params.first(), executable.line_number)?;
let p2 = param_to_datatype(runtime, executable.params.get(1), executable.line_number)?;

let sum = add_data_types(p1, p2)?;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/builtin_function/math/decrease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::common::param::Param;
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn decrease(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
if let Some(Param::Variable(name)) = executable.params.get(0) {
if let Some(Param::Variable(name)) = executable.params.first() {
match runtime.variables.get(name) {
Some(x) => match x {
DataType::Int(x) => {
Expand Down
Loading

0 comments on commit f775c8a

Please sign in to comment.