[2016][rust][23.1] refactorings

sorbet
Alpha Chen 8 years ago
parent c493689f5d
commit 94267f9c3b

@ -54,8 +54,7 @@ impl Iterator for Assembunny {
self.registers[Register::PC] += 1; self.registers[Register::PC] += 1;
match instruction { match instruction {
Instruction::Cpy(v, Variable::Register(r)) => { Instruction::Cpy(v, Variable::Register(r)) => {
let value = self.value(v); self.registers[r] = self.value(v);
self.registers[r] = value;
} }
Instruction::Inc(Variable::Register(r)) => { Instruction::Inc(Variable::Register(r)) => {
self.registers[r] += 1; self.registers[r] += 1;
@ -64,9 +63,8 @@ impl Iterator for Assembunny {
self.registers[r] -= 1; self.registers[r] -= 1;
} }
Instruction::Jnz(v, delta) if self.value(v) != 0 => { Instruction::Jnz(v, delta) if self.value(v) != 0 => {
let delta = self.value(delta); self.registers[Register::PC] -= 1;
let pc = self.value(Register::PC) + delta - 1; self.registers[Register::PC] += self.value(delta);
self.registers[Register::PC] = pc;
} }
Instruction::Tgl(v) => { Instruction::Tgl(v) => {
let index = (pc as isize) + self.value(v); let index = (pc as isize) + self.value(v);
@ -92,14 +90,12 @@ impl ops::Index<Register> for Registers {
type Output = isize; type Output = isize;
fn index(&self, _index: Register) -> &isize { fn index(&self, _index: Register) -> &isize {
let index: u8 = _index.into(); self.0.index(usize::from(_index))
self.0.index(index as usize)
} }
} }
impl ops::IndexMut<Register> for Registers { impl ops::IndexMut<Register> for Registers {
fn index_mut(&mut self, _index: Register) -> &mut isize { fn index_mut(&mut self, _index: Register) -> &mut isize {
let index: u8 = _index.into(); self.0.index_mut(usize::from(_index))
self.0.index_mut(index as usize)
} }
} }
@ -111,18 +107,6 @@ impl Instructions {
} }
} }
impl fmt::Display for Instructions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"{}",
self.0
.iter()
.map(Instruction::to_string)
.collect::<Vec<_>>()
.join("\n"))
}
}
impl ops::Index<usize> for Instructions { impl ops::Index<usize> for Instructions {
type Output = Instruction; type Output = Instruction;
@ -146,8 +130,8 @@ pub enum Register {
D, D,
} }
impl From<Register> for u8 { impl From<Register> for usize {
fn from(r: Register) -> u8 { fn from(r: Register) -> usize {
match r { match r {
Register::PC => 0, Register::PC => 0,
Register::A => 1, Register::A => 1,
@ -167,6 +151,38 @@ pub enum Instruction {
Tgl(Variable), Tgl(Variable),
} }
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Variable {
Register(Register),
Value(isize),
}
impl From<Register> for Variable {
fn from(r: Register) -> Self {
Variable::Register(r)
}
}
impl From<isize> for Variable {
fn from(i: isize) -> Self {
Variable::Value(i)
}
}
// Display
impl fmt::Display for Instructions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"{}",
self.0
.iter()
.map(Instruction::to_string)
.collect::<Vec<_>>()
.join("\n"))
}
}
impl fmt::Display for Instruction { impl fmt::Display for Instruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
@ -179,12 +195,6 @@ impl fmt::Display for Instruction {
} }
} }
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Variable {
Register(Register),
Value(isize),
}
impl fmt::Display for Variable { impl fmt::Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
@ -196,12 +206,6 @@ impl fmt::Display for Variable {
} }
} }
impl From<Register> for Variable {
fn from(r: Register) -> Self {
Variable::Register(r)
}
}
// Parsing // Parsing
impl str::FromStr for Instructions { impl str::FromStr for Instructions {

Loading…
Cancel
Save