Skip to content

Commit

Permalink
Make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
danthedaniel committed Jan 28, 2020
1 parent 43300d3 commit f7c77d6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use docopt::Docopt;

use parser::AST;

const USAGE: &'static str = "
const USAGE: &str = "
Fucker
Usage:
Expand All @@ -42,7 +42,7 @@ fn main() {
.unwrap_or_else(|e| e.exit());

let program = read_file(&args.arg_program)
.and_then(|chars| AST::from_char_vec(chars))
.and_then(AST::from_char_vec)
.map(|ast| ast.to_program())
.unwrap_or_else(|e| {
eprintln!("Error occurred while loading program: {}", e);
Expand Down
19 changes: 9 additions & 10 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ pub struct AST {

impl AST {
pub fn to_program(&self) -> Program {
let instrs = Self::to_instrs(self.data.clone(), 0);
let instrs = Self::nodes_to_instrs(self.data.clone(), 0);
Program::new(instrs)
}

fn to_instrs(nodes: Vec<ASTNode>, start: usize) -> Vec<Instr> {
fn nodes_to_instrs(nodes: Vec<ASTNode>, start: usize) -> Vec<Instr> {
let mut instrs = Vec::new();

for node in nodes {
Expand All @@ -48,7 +48,7 @@ impl AST {
ASTNode::Read => instrs.push(Instr::Read),
ASTNode::Loop(vec) => {
// Add 1 to the index to start counting after the BeginLoop
let inner_loop = Self::to_instrs(vec.clone(), program_index + 1);
let inner_loop = Self::nodes_to_instrs(vec.clone(), program_index + 1);

let offset = inner_loop.len() + 1;
instrs.push(Instr::BeginLoop(offset));
Expand All @@ -62,7 +62,7 @@ impl AST {
}

pub fn from_char_vec(input: Vec<char>) -> Result<Self, String> {
if input.len() == 0 {
if input.is_empty() {
// Return an empty program
return Ok(Self { data: Vec::new() });
}
Expand Down Expand Up @@ -100,7 +100,7 @@ impl AST {
',' => push_node!(ASTNode::Read),
'[' => loops.push(Vec::new()),
']' => {
let current_loop = loops.pop().ok_or(format!("More ] than ["))?;
let current_loop = loops.pop().ok_or("More ] than [")?;

// Do not add loop if it will be the first element in the
// ASTNode vector. This is because:
Expand All @@ -110,7 +110,7 @@ impl AST {
//
// So if no non-loops have executed there is no use in
// emitting a Loop ASTNode.
if output.len() > 0 {
if !output.is_empty() {
let optimized_loop = Self::shallow_run_length_optimize(current_loop);
push_node!(ASTNode::Loop(optimized_loop));
}
Expand All @@ -120,15 +120,14 @@ impl AST {
}
}

if loops.len() > 0 {
if !loops.is_empty() {
// Anywhere deeper than the top level should always return from the '['
// match branch above.
//
// Example program that will cause this error:
//
// [[]
println!("");
return Err(format!("More [ than ]"));
return Err("More [ than ]".to_string());
}

Ok(Self::shallow_run_length_optimize(output))
Expand All @@ -140,7 +139,7 @@ impl AST {

for node in input {
let len = output.len();
let last = output.get(len.checked_sub(1).unwrap_or(0));
let last = output.get(len.saturating_sub(1));

// For each operator +, -, < and >, if the last instruction in the
// output Vec is the same, then increment that instruction instead
Expand Down
8 changes: 4 additions & 4 deletions src/parser/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Program {

impl Program {
pub fn new(data: Vec<Instr>) -> Self {
Program { data: data }
Program { data }
}

pub fn len(&self) -> usize {
Expand Down Expand Up @@ -77,12 +77,12 @@ impl Index<usize> for Program {

impl fmt::Debug for Program {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Addr\tInstr\tOperands\n")?;
writeln!(f, "Addr\tInstr\tOperands")?;

for (pos, instr) in self.data.iter().enumerate() {
write!(f, "0x{:04X}\t{:?}\n", pos, instr)?;
writeln!(f, "0x{:04X}\t{:?}", pos, instr)?;
}

write!(f, "\n")
writeln!(f)
}
}
4 changes: 2 additions & 2 deletions src/runnable/fucker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Fucker {
impl Fucker {
pub fn new(program: Program) -> Self {
Fucker {
program: program,
program,
memory: vec![0u8; 0x4000],
pc: 0,
dp: 0,
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Fucker {

self.pc += 1;

return true;
true
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/runnable/jit_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::mem;
use std::ops::{Index, IndexMut};

use libc::{_SC_PAGESIZE, sysconf};
use libc::{sysconf, _SC_PAGESIZE};

use runnable::Runnable;

Expand All @@ -15,14 +15,12 @@ extern "C" {
/// `numerator` - The upper component of a division
/// `denominator` - The lower component of a division
fn int_ceil(numerator: usize, denominator: usize) -> usize {
return (numerator / denominator + 1) * denominator;
(numerator / denominator + 1) * denominator
}

// Dynamically read the page size. Unix only.
fn get_page_size() -> usize {
unsafe {
sysconf(_SC_PAGESIZE) as usize
}
unsafe { sysconf(_SC_PAGESIZE) as usize }
}

/// Container for executable bytes.
Expand All @@ -48,12 +46,12 @@ impl JITMemory {

memset(_ptr, 0xc3, size); // for now, prepopulate with 'RET'

data_ptr = mem::transmute(_ptr);
data_ptr = _ptr as *mut u8;
}

let contents = unsafe { Vec::from_raw_parts(data_ptr, source.len(), size) };

let mut jit = JITMemory { contents: contents };
let mut jit = JITMemory { contents };

// Copy source into JIT memory.
for (index, &byte) in source.iter().enumerate() {
Expand All @@ -65,7 +63,7 @@ impl JITMemory {
}

impl Runnable for JITMemory {
fn run(&mut self) -> () {
fn run(&mut self) {
let mut bf_mem = vec![0u8; 30_000]; // Memory space used by BrainFuck
let mem_ptr = bf_mem.as_mut_ptr();
let func: fn(*mut u8) -> () = unsafe { mem::transmute(self.contents.as_mut_ptr()) };
Expand Down Expand Up @@ -95,6 +93,6 @@ impl fmt::Debug for JITMemory {
write!(f, "{:02X}", byte)?;
}

write!(f, "\n")
writeln!(f)
}
}

0 comments on commit f7c77d6

Please sign in to comment.