Skip to content

Commit

Permalink
style: cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavale committed Feb 17, 2024
1 parent e3a0926 commit 93317f6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn run(files: &[&str]) {
for (filename_idx, file) in files.iter().enumerate() {
let mut number_of_lines: u32 = 1;
let mut reader = BufReader::new(File::open(file).expect("Failed file open"));
tokens.add_file(&file);
tokens.add_file(file);

let mut buf = String::new();
while 0 < reader.read_line(&mut buf).unwrap() {
Expand Down
32 changes: 16 additions & 16 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use super::token::register::RegisterKind;
/// Recieve 1 line
/// nol: number_of_lines
/// fi: filename_idx
pub fn tokenize(nol: u32, fi: usize, line: &str, mut tokens: &mut Tokens)
pub fn tokenize(nol: u32, fi: usize, line: &str, tokens: &mut Tokens)
-> Result<(), String>
{
let words: Vec<String> = split_words(&line);
let words: Vec<String> = split_words(line);
let words: Vec<&str> = words.iter().map(|s| &**s).collect();

//println!("{:?}", words);
Expand All @@ -24,13 +24,13 @@ pub fn tokenize(nol: u32, fi: usize, line: &str, mut tokens: &mut Tokens)
tokens.push(TokenKind::INTEGER(num), nol, fi);
} else if let Ok(num) = word.parse::<f32>() {
tokens.push(TokenKind::FLOATING(num), nol, fi);
} else if let Some(num) = is_hexadecimal(&word) {
} else if let Some(num) = is_hexadecimal(word) {
tokens.push(TokenKind::INTEGER(num), nol, fi);
} else if let Ok((k, i)) = is_register(&word) {
} else if let Ok((k, i)) = is_register(word) {
tokens.push(TokenKind::REGISTER(k, i), nol, fi);
} else if let Ok((k, i, a)) = is_memory(&word) {
} else if let Ok((k, i, a)) = is_memory(word) {
tokens.push(TokenKind::MEMORY(k, i, a), nol, fi);
} else if let Ok((k, i, s)) = is_data_address(&word) {
} else if let Ok((k, i, s)) = is_data_address(word) {
tokens.push(TokenKind::DATA(k, i, s), nol, fi);
} else {
let token_kind = match &*word.to_ascii_uppercase() {
Expand Down Expand Up @@ -182,12 +182,12 @@ pub fn tokenize(nol: u32, fi: usize, line: &str, mut tokens: &mut Tokens)
"CVT.W.S" => TokenKind::INSTRUCTION(InstructionKind::CVT_W_S),

_ =>
if is_label(&word) {
if is_label(word) {
let mut identifier = (*word).to_string();
identifier.pop(); // Delete ':'
tokens.add_address(identifier.clone(), tokens.len());
TokenKind::LABEL(identifier, tokens.len(), None)
} else if is_indicate(&word) {
} else if is_indicate(word) {
match *word {
".text" => {
// ignore <Addr>
Expand All @@ -204,24 +204,24 @@ pub fn tokenize(nol: u32, fi: usize, line: &str, mut tokens: &mut Tokens)
TokenKind::INDICATE(IndicateKind::globl(label))
},
".word" => {
indicate_word(&mut tokens, nol, fi, words);
indicate_word(tokens, nol, fi, words);
break;
},
".half" => {
indicate_half(&mut tokens, nol, fi, words);
indicate_half(tokens, nol, fi, words);
break;
},
".byte" => {
indicate_byte(&mut tokens, nol, fi, words);
indicate_byte(tokens, nol, fi, words);
break;
},
".float" => {
indicate_float(&mut tokens, nol, fi, words);
indicate_float(tokens, nol, fi, words);
break;
},
".space" => {
let word = words.next().unwrap();
let length = indicate_space(&word);
let length = indicate_space(word);
TokenKind::INDICATE(IndicateKind::space(length))
},
".ascii" => {
Expand Down Expand Up @@ -480,7 +480,7 @@ fn indicate_word(tokens: &mut Tokens, nol: u32, fi: usize, mut words: std::slice

} else {
int = {
if let Some(num) = is_hexadecimal(&word) {
if let Some(num) = is_hexadecimal(word) {
num as u32
} else if let Ok(num) = word.parse::<i32>() {
num as u32
Expand Down Expand Up @@ -520,7 +520,7 @@ fn indicate_half(tokens: &mut Tokens, nol: u32, fi: usize, mut words: std::slice

} else {
half = {
if let Some(num) = is_hexadecimal(&word) {
if let Some(num) = is_hexadecimal(word) {
num as u16
} else if let Ok(num) = word.parse::<i16>() {
num as u16
Expand Down Expand Up @@ -559,7 +559,7 @@ fn indicate_byte(tokens: &mut Tokens, nol: u32, fi: usize, mut words: std::slice
}
} else {
byte = {
if let Some(num) = is_hexadecimal(&word) {
if let Some(num) = is_hexadecimal(word) {
num as u8
} else if let Ok(num) = word.parse::<i8>() {
num as u8
Expand Down
32 changes: 16 additions & 16 deletions src/parser/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub fn eval_load(memory: &mut Memory, tokens: &mut Tokens, byte: usize, se: Sign
} else {
tokens.expect_address()? as u32
};
memory.registers[register_idx] = get_int(&memory, idx, byte, se)?;
memory.registers[register_idx] = get_int(memory, idx, byte, se)?;

Ok(())
}
Expand Down Expand Up @@ -317,13 +317,13 @@ pub fn eval_myown(memory: &Memory, tokens: &mut Tokens, kind: InstructionKind) -
print!("{}", num);
} else if let Ok((r_idx, s_idx)) = tokens.expect_memory() { // data or stack
let idx = memory.registers[r_idx] as u32 + s_idx;
print!("{}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("{}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
} else if let Ok((r_idx, d_idx)) = tokens.expect_data() {
let idx = (memory.registers[r_idx] as usize + d_idx) as u32;
print!("{}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("{}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
} else {
let idx = tokens.expect_address()? as u32;
print!("{}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("{}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
}
let _ = std::io::stdout().flush();
},
Expand All @@ -335,13 +335,13 @@ pub fn eval_myown(memory: &Memory, tokens: &mut Tokens, kind: InstructionKind) -
print!("{:x}", num);
} else if let Ok((r_idx, s_idx)) = tokens.expect_memory() { // data or stack
let idx = memory.registers[r_idx] as u32 + s_idx;
print!("{:x}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("{:x}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
} else if let Ok((r_idx, d_idx)) = tokens.expect_data() {
let idx = memory.registers[r_idx] as u32 + d_idx as u32;
print!("{:x}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("{:x}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
} else {
let idx = tokens.expect_address()? as u32;
print!("{:x}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("{:x}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
}
let _ = std::io::stdout().flush();
},
Expand All @@ -353,13 +353,13 @@ pub fn eval_myown(memory: &Memory, tokens: &mut Tokens, kind: InstructionKind) -
print!("0x{:x}", num);
} else if let Ok((r_idx, s_idx)) = tokens.expect_memory() { // data or stack
let idx = memory.registers[r_idx] as u32 + s_idx;
print!("0x{:x}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("0x{:x}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
} else if let Ok((r_idx, d_idx)) = tokens.expect_data() {
let idx = memory.registers[r_idx] as u32 + d_idx as u32;
print!("0x{:x}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("0x{:x}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
} else {
let idx = tokens.expect_address()? as u32;
print!("0x{:x}", get_int(&memory, idx, 4, SignExtension::Unsigned)?);
print!("0x{:x}", get_int(memory, idx, 4, SignExtension::Unsigned)?);
}
let _ = std::io::stdout().flush();
},
Expand All @@ -371,9 +371,9 @@ pub fn eval_myown(memory: &Memory, tokens: &mut Tokens, kind: InstructionKind) -
print!("{}", memory.static_data[d_idx-1] as char);
} else if let Ok((r_idx, s_idx)) = tokens.expect_memory() { // data or stack
let idx = memory.registers[r_idx] as u32 + s_idx;
print!("{}", &get_string(&memory, idx)?[..1]);
print!("{}", &get_string(memory, idx)?[..1]);
} else if let Ok((r_idx, d_idx)) = tokens.expect_data() {
let idx = (memory.registers[r_idx] as usize + d_idx) as usize;
let idx = memory.registers[r_idx] as usize + d_idx;
print!("{}", memory.static_data[idx-1] as char);
} else {
let ch = tokens.expect_integer()? as u8 as char;
Expand All @@ -384,15 +384,15 @@ pub fn eval_myown(memory: &Memory, tokens: &mut Tokens, kind: InstructionKind) -
InstructionKind::PRTS => {
tokens.consume().ok_or(CONSUME_ERR)?;
if let Ok(r_idx) = tokens.expect_register() {
print!("{}", get_string(&memory, memory.registers[r_idx] as u32)?);
print!("{}", get_string(memory, memory.registers[r_idx] as u32)?);
} else if let Ok(d_idx) = tokens.expect_address() {
print!("{}", get_string(&memory, d_idx as u32)?);
print!("{}", get_string(memory, d_idx as u32)?);
} else if let Ok((r_idx, s_idx)) = tokens.expect_memory() { // data or stack
let idx = memory.registers[r_idx] as u32 + s_idx;
print!("{}", get_string(&memory, idx)?);
print!("{}", get_string(memory, idx)?);
} else if let Ok((r_idx, d_idx)) = tokens.expect_data() {
let idx = memory.registers[r_idx] as u32 + d_idx as u32;
print!("{}", get_string(&memory, idx)?);
print!("{}", get_string(memory, idx)?);
} else {
let s = tokens.expect_literal()?;
print!("{}", s);
Expand Down
Loading

0 comments on commit 93317f6

Please sign in to comment.