rust fmt ...

This commit is contained in:
tali 2023-12-21 18:30:41 -05:00
parent d66b336435
commit d8df7e1eac
3 changed files with 12 additions and 7 deletions

View File

@ -16,8 +16,8 @@ pub enum DecodeError {
/// (in bytes).
pub fn decode_one(bs: &[u8]) -> Result<(Ins, usize), DecodeError> {
let byte0 = byte(bs, 0)?;
let len = (byte0 & 1) as usize * 2 + 2;
let ins = match byte0 & 0x3f {
0x00 => {
let a = arg(byte(bs, 1)?, byte0);
@ -50,7 +50,7 @@ pub fn decode_one(bs: &[u8]) -> Result<(Ins, usize), DecodeError> {
let opr = opr(byte(bs, 1)?)?;
let a = Reg(byte(bs, 2)?);
let b = arg(byte(bs, 3)?, byte0);
Ins::MOV(InsMov::Op(opr, a, b))
Ins::Op(InsOp(opr, a, b))
}
0x17 => {
let cnd = cnd(byte(bs, 1)?)?;

View File

@ -16,9 +16,11 @@ fn entry() -> Result<(), Error> {
BufReader::new(std::io::stdin()).read_to_end(&mut bytes)?;
let mut decode = decode(&bytes);
while let Some(ins) = decode.next() {
let ins = ins.map_err(|err| Error::Decode(err, decode.position()))?;
println!("{ins}");
loop {
let pos = decode.position();
let Some(ins) = decode.next() else { break };
let ins = ins.map_err(|err| Error::Decode(err, pos))?;
println!("{pos:>3} {ins}");
}
Ok(())

View File

@ -56,6 +56,7 @@ pub enum Ins {
LOC(InsLoc),
CAL(InsCal),
JMP(InsJmp),
Op(InsOp),
Br(InsBr),
RET(Arg),
}
@ -67,7 +68,6 @@ impl fmt::Display for Ins {
use InsMov::*;
match *self {
MOV(Op(opr, a, b)) => write!(f, "{opr} {a}, {b}"),
MOV(RV(a, b)) => write!(f, "mov {a}, {b}"),
MOV(RL(a, b)) => write!(f, "mov {a}, {b}"),
MOV(LV(a, b)) => write!(f, "mov {a}, {b}"),
@ -75,6 +75,7 @@ impl fmt::Display for Ins {
CAL(InsCal(a, b, n)) => write!(f, "cal {a}, {b}({a},...{n})"),
JMP(InsJmp(d)) => write!(f, "jmp {d:+}"),
RET(v) => write!(f, "ret {v}"),
Op(InsOp(opr, a, b)) => write!(f, "{opr} {a}, {b}"),
Br(BTR(v)) => write!(f, "btr {v}"),
Br(BFL(v)) => write!(f, "btr {v}"),
Br(BCnd(cnd, a, b)) => write!(f, "b{cnd} {a}, {b}"),
@ -84,7 +85,6 @@ impl fmt::Display for Ins {
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum InsMov {
Op(Opr, Reg, Arg),
RV(Reg, Arg),
RL(Reg, Loc),
LV(Loc, Arg),
@ -97,6 +97,9 @@ pub enum InsBr {
BCnd(Cnd, Reg, Arg),
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct InsOp(pub Opr, pub Reg, pub Arg);
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct InsLoc(pub Reg, pub Reg, pub Cst);