Refactoring

This commit is contained in:
2022-03-12 15:39:29 -06:00
parent 90f2177e5b
commit 19eb4d57f3
14 changed files with 444 additions and 22 deletions
+54
View File
@@ -163,6 +163,60 @@ impl TryFrom<u8> for Opcode {
}
}
impl TryFrom<&str> for Opcode {
type Error = InvalidOpcode;
fn try_from(value: &str) -> Result<Self, Self::Error> {
use Opcode::*;
Ok(match value {
"nop" => Nop,
"add" => Add,
"sub" => Sub,
"mul" => Mul,
"div" => Div,
"mod" => Mod,
"rand" => Rand,
"and" => And,
"or" => Or,
"xor" => Xor,
"not" => Not,
"gt" => Gt,
"lt" => Lt,
"agt" => Agt,
"alt" => Alt,
"lshift" => Lshift,
"rshift" => Rshift,
"arshift" => Arshift,
"pop" => Pop,
"dup" => Dup,
"swap" => Swap,
"pick" => Pick,
"rot" => Rot,
"jmp" => Jmp,
"jmpr" => Jmpr,
"call" => Call,
"ret" => Ret,
"brz" => Brz,
"brnz" => Brnz,
"hlt" => Hlt,
"load" => Load,
"loadw" => Loadw,
"store" => Store,
"storew" => Storew,
"inton" => Inton,
"intoff" => Intoff,
"setiv" => Setiv,
"sdp" => Sdp,
"setsdp" => Setsdp,
"pushr" => Pushr,
"popr" => Popr,
"peekr" => Peekr,
"debug" => Debug,
_ => return Err(InvalidOpcode(255))
})
}
}
#[test]
fn test_decode() {
assert_eq!(Opcode::try_from(18), Ok(Opcode::Pop));