Skip to content

Commit

Permalink
all: clippy fixes, rename enum
Browse files Browse the repository at this point in the history
  • Loading branch information
birkenfeld committed Sep 5, 2015
1 parent 1a4cf9b commit 591df61
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
58 changes: 29 additions & 29 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub struct Eval<'a> {
}

/// Represents the control flow effect of an executed statement.
enum StmtRes {
enum Flow {
/// normal execution, next statement
Next,
/// jump around, from DO ... NEXT
Expand Down Expand Up @@ -180,20 +180,20 @@ impl<'a> Eval<'a> {
};
// handle control flow effects
match res {
StmtRes::Next => { }
StmtRes::Jump(n) => {
Flow::Next => { }
Flow::Jump(n) => {
self.jumps.push(pctr as u16); // push the line with the NEXT
pctr = n;
continue; // do not increment or check for COME FROMs
}
StmtRes::Back(n) => {
Flow::Back(n) => {
pctr = n; // will be incremented below after COME FROM check
}
StmtRes::FromTop => {
Flow::FromTop => {
pctr = 0; // start from the beginning, do not push any stack
continue;
}
StmtRes::End => break,
Flow::End => break,
}
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'a> Eval<'a> {
}

/// Interpret a single statement.
fn eval_stmt(&mut self, stmt: &Stmt) -> Res<StmtRes> {
fn eval_stmt(&mut self, stmt: &Stmt) -> Res<Flow> {
if self.debug {
println!("\nExecuting Stmt #{} (state before following)", self.stmt_ctr);
self.dump_state();
Expand All @@ -254,60 +254,60 @@ impl<'a> Eval<'a> {
StmtBody::Calc(ref var, ref expr) => {
let val = try!(self.eval_expr(expr));
try!(self.assign(var, val));
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::Dim(ref var, ref exprs) => {
try!(self.array_dim(var, exprs));
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::DoNext(n) => {
match self.program.labels.get(&n) {
// too many jumps on stack already?
Some(_) if self.jumps.len() >= 80 => IE123.err(),
Some(i) => Ok(StmtRes::Jump(*i as usize)),
Some(i) => Ok(Flow::Jump(*i as usize)),
None => IE129.err(),
}
}
StmtBody::ComeFrom(_) => {
// nothing to do here at runtime
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::Resume(ref expr) => {
let n = try!(self.eval_expr(expr)).as_u32();
// this expect() is safe: if the third arg is true, there will
// be no Ok(None) returns
let next = try!(pop_jumps(&mut self.jumps, n, true, 0))
.expect("https://xkcd.com/378/ ?!");
Ok(StmtRes::Back(next as usize))
Ok(Flow::Back(next as usize))
}
StmtBody::Forget(ref expr) => {
let n = try!(self.eval_expr(expr)).as_u32();
try!(pop_jumps(&mut self.jumps, n, false, 0));
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::Ignore(ref vars) => {
for var in vars {
self.set_rw(var, false);
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::Remember(ref vars) => {
for var in vars {
self.set_rw(var, true);
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::Stash(ref vars) => {
for var in vars {
self.stash(var);
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::Retrieve(ref vars) => {
for var in vars {
try!(self.retrieve(var));
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::Abstain(ref expr, ref whats) => {
let f: Box<Fn(u32) -> u32> = if let Some(ref e) = *expr {
Expand All @@ -319,13 +319,13 @@ impl<'a> Eval<'a> {
for what in whats {
self.abstain(what, &*f);
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::Reinstate(ref whats) => {
for what in whats {
self.abstain(what, &|v: u32| v.saturating_sub(1));
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::ReadOut(ref vars) => {
for var in vars {
Expand All @@ -345,7 +345,7 @@ impl<'a> Eval<'a> {
_ => return IE994.err(),
};
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::WriteIn(ref vars) => {
for var in vars {
Expand All @@ -358,17 +358,17 @@ impl<'a> Eval<'a> {
try!(self.assign(var, Val::from_u32(n)));
}
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
// this one is only generated by the constant-program optimizer
StmtBody::Print(ref s) => {
if let Err(_) = self.stdout.write(&s) {
return IE252.err();
}
Ok(StmtRes::Next)
Ok(Flow::Next)
}
StmtBody::TryAgain => Ok(StmtRes::FromTop),
StmtBody::GiveUp => Ok(StmtRes::End),
StmtBody::TryAgain => Ok(Flow::FromTop),
StmtBody::GiveUp => Ok(Flow::End),
StmtBody::Error(ref e) => Err((*e).clone()),
}
}
Expand Down Expand Up @@ -481,7 +481,7 @@ impl<'a> Eval<'a> {
match *var {
Var::A16(n, _) => self.tail[n].dimension(dims, 0),
Var::A32(n, _) => self.hybrid[n].dimension(dims, 0),
_ => return IE994.err(),
_ => IE994.err()
}
}

Expand Down Expand Up @@ -569,7 +569,7 @@ impl<'a> Eval<'a> {
match *var {
Var::A16(n, _) => self.tail[n].readout(self.stdout, state, 0),
Var::A32(n, _) => self.hybrid[n].readout(self.stdout, state, 0),
_ => return IE994.err(),
_ => IE994.err()
}
}

Expand All @@ -579,7 +579,7 @@ impl<'a> Eval<'a> {
match *var {
Var::A16(n, _) => self.tail[n].writein(state, 0),
Var::A32(n, _) => self.hybrid[n].writein(state, 0),
_ => return IE994.err(),
_ => IE994.err()
}
}

Expand All @@ -589,14 +589,14 @@ impl<'a> Eval<'a> {
self.dump_state_one(&self.twospot, ":");
self.dump_state_one(&self.tail, ",");
self.dump_state_one(&self.hybrid, ";");
if self.jumps.len() > 0 {
if !self.jumps.is_empty() {
println!("Next stack: {:?}", self.jumps);
}
//println!("Abstained: {:?}", self.abstain);
}

fn dump_state_one<T: Debug + Display>(&self, vec: &Vec<Bind<T>>, sigil: &str) {
if vec.len() > 0 {
if !vec.is_empty() {
for (i, v) in vec.iter().enumerate() {
print!("{}{} = {}, ", sigil, i, v);
}
Expand Down
10 changes: 5 additions & 5 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ impl<'p> Parser<'p> {
} else if self.take(TT::READOUT) {
Ok(StmtBody::ReadOut(try!(self.parse_readlist())))
} else if self.take(TT::TRYAGAIN) {
return Ok(StmtBody::TryAgain)
Ok(StmtBody::TryAgain)
} else if self.take(TT::GIVEUP) {
return Ok(StmtBody::GiveUp)
Ok(StmtBody::GiveUp)
} else {
Err(self.invalid())
}
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<'p> Parser<'p> {
};
return Ok(Some(Var::A32(val as usize, subs)));
}
return Ok(None);
Ok(None)
}

/// Require a variable reference.
Expand Down Expand Up @@ -358,7 +358,7 @@ impl<'p> Parser<'p> {
let subs = try!(self.parse_subs());
return Ok(Some(constr(Expr::Var(Var::A32(val as usize, subs)))));
}
return Ok(None);
Ok(None)
}

/// Parse an ABSTAIN statement.
Expand Down Expand Up @@ -442,7 +442,7 @@ impl<'p> Parser<'p> {
let right = try!(self.parse_expr2());
return Ok(Expr::Select(right.get_vtype(), box left, box right));
}
return Ok(left);
Ok(left)
}

fn parse_expr2(&mut self) -> ParseRes<Expr> {
Expand Down
6 changes: 3 additions & 3 deletions src/stdops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl<T: LikeU16 + Default> Bind<Array<T>> {
return IE241.err_with(None, line);
}
let mut res = Vec::with_capacity(self.val.elems.len());
for val in self.val.elems.iter() {
for val in &self.val.elems {
let byte = ((*state as i16 - val.to_u16() as i16) as u16 % 256) as u8;
let mut c = byte;
*state = byte as u8;
Expand All @@ -194,7 +194,7 @@ impl<T: LikeU16 + Default> Bind<Array<T>> {
// only dimension-1 arrays can be input
return IE241.err_with(None, line);
}
for place in self.val.elems.iter_mut() {
for place in &mut self.val.elems {
let byte = read_byte();
let c = if byte == 256 {
*state = 0;
Expand All @@ -218,7 +218,7 @@ impl<T: Debug + Display> Display for Bind<T> {
if !self.rw {
try!(write!(fmt, " RO"));
}
if self.stack.len() > 0 {
if !self.stack.is_empty() {
try!(write!(fmt, " STACK={:?}", self.stack));
}
Ok(())
Expand Down

0 comments on commit 591df61

Please sign in to comment.