Cleanup main.rs by fixing warnings
This commit is contained in:
+15
-5
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(clippy::from_str_radix_10)]
|
||||||
|
use std::cmp::Ordering;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@@ -54,7 +56,7 @@ fn extract_operand(statement: Pair<Rule>) -> &str {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_constant(text: &str) -> Result<u32, std::num::ParseIntError> {
|
fn parse_constant(text: &str) -> Result<u32, std::num::ParseIntError> {
|
||||||
if text.starts_with("\"") {
|
if text.starts_with('\"') {
|
||||||
let c = text.chars().nth(1).unwrap();
|
let c = text.chars().nth(1).unwrap();
|
||||||
Ok(c as u32)
|
Ok(c as u32)
|
||||||
} else if text.starts_with("0x") {
|
} else if text.starts_with("0x") {
|
||||||
@@ -79,7 +81,11 @@ fn to_bf(rule: Rule, operand: &str, state: &mut State, out: &mut Builder) {
|
|||||||
}
|
}
|
||||||
Rule::delvar => {
|
Rule::delvar => {
|
||||||
let variable_name = operand;
|
let variable_name = operand;
|
||||||
if let None = state.variables.remove(&String::from(variable_name)) {
|
if state
|
||||||
|
.variables
|
||||||
|
.remove(&String::from(variable_name))
|
||||||
|
.is_none()
|
||||||
|
{
|
||||||
panic!("Variable '{}' did not exists", variable_name);
|
panic!("Variable '{}' did not exists", variable_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,11 +102,15 @@ fn to_bf(rule: Rule, operand: &str, state: &mut State, out: &mut Builder) {
|
|||||||
Rule::pointa => {
|
Rule::pointa => {
|
||||||
let address = operand;
|
let address = operand;
|
||||||
let address_parsed = parse_constant(address).unwrap();
|
let address_parsed = parse_constant(address).unwrap();
|
||||||
if address_parsed < state.mem_pointer {
|
match address_parsed.cmp(&state.mem_pointer) {
|
||||||
|
Ordering::Less => {
|
||||||
out.append("<".repeat((state.mem_pointer - address_parsed) as usize))
|
out.append("<".repeat((state.mem_pointer - address_parsed) as usize))
|
||||||
} else if address_parsed > state.mem_pointer {
|
}
|
||||||
|
Ordering::Greater => {
|
||||||
out.append(">".repeat((address_parsed - state.mem_pointer) as usize))
|
out.append(">".repeat((address_parsed - state.mem_pointer) as usize))
|
||||||
}
|
}
|
||||||
|
Ordering::Equal => (),
|
||||||
|
}
|
||||||
state.mem_pointer = address_parsed;
|
state.mem_pointer = address_parsed;
|
||||||
}
|
}
|
||||||
Rule::pointm => {
|
Rule::pointm => {
|
||||||
@@ -234,7 +244,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let bf = builder.string().unwrap();
|
let bf = builder.string().unwrap();
|
||||||
|
|
||||||
let mut out = File::create(args.output_file)?;
|
let mut out = File::create(args.output_file)?;
|
||||||
out.write(bf.as_bytes())?;
|
out.write_all(bf.as_bytes())?;
|
||||||
out.sync_all()?;
|
out.sync_all()?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user