This commit is contained in:
Richard 2025-08-23 12:24:37 +02:00
parent 69d9753fff
commit 3181d2c80d
2 changed files with 42 additions and 19 deletions

View File

@ -1,10 +1,32 @@
extern crate um;
use um::instruction::{Instruction, ParsingError};
use std::io::Read;
fn main() {
let i = Instruction::parse(0x81234567);
match i {
Ok(x) => println!("Ins {:?}!", x.fmt()),
Err(ParsingError) => println!("OOPS")
let mut stdin = std::io::stdin().lock();
let mut cont = true;
while cont {
let mut buffer = [0u8; 4];
cont = match stdin.read(&mut buffer) {
Ok(4) => hdl(buffer),
Ok(_) => false,
Err(_) => false,
};
}
}
fn hdl(buffer: [u8; 4]) -> bool{
let num = u32::from_be_bytes(
buffer.try_into().unwrap());
let i = Instruction::parse(num);
match i {
Ok(x) => {
println!("{}", x.fmt());
true
},
Err(ParsingError) => {
println!("parsingerror");
false
}
}
}

View File

@ -39,22 +39,23 @@ const fn u3at(d: u32, pos: u8) -> u3 {
}
impl Instruction {
pub fn fmt(&self) -> &'static str {
pub fn fmt(&self) -> String {
match self {
Instruction::ConditionalMove(i) => "CMV",
Instruction::ArrayIndex(i) => "IDX",
Instruction::ArrayAmend(i) => "AMD",
Instruction::Add(i) => "ADD",
Instruction::Multiply(i) => "MUL",
Instruction::Divide(i) => "DIV",
Instruction::NotAnd(i) => "NAD",
Instruction::Halt(i) => "HLT",
Instruction::Allocate(i) => "ALC",
Instruction::Abandon(i) => "ABN",
Instruction::Output(i) => "OUT",
Instruction::Input(i) => "INP",
Instruction::Load(i) => "LOD",
Instruction::Ortho(i) => "ORT"
Instruction::ConditionalMove(i) => format!("CMV r{}=r{} if r{}", i.
a, i.b, i.c),
Instruction::ArrayIndex(i) => format!("IDX r{}=r{}[r{}]", i.a, i.b, i.c) ,
Instruction::ArrayAmend(i) => format!("AMD r{}[r{}]=r{}", i.a, i.b, i.c),
Instruction::Add(i) => format!("ADD r{}=r{}+r{}", i.a, i.b, i.c),
Instruction::Multiply(i) => format!("MUL r{}=r{}*r{}", i.a, i.b, i.c),
Instruction::Divide(i) => format!("DIV r{}=r{}/r{}", i.a, i.b, i.c),
Instruction::NotAnd(i) => format!("NAD r{}=r{} nand r{}", i.a, i.b, i.c),
Instruction::Halt(i) => format!("HLT"),
Instruction::Allocate(i) => format!("ALC r{} r{}", i.b, i.c),
Instruction::Abandon(i) => format!("ABN r{}", i.c),
Instruction::Output(i) => format!("OUT r{}", i.c),
Instruction::Input(i) => format!("INP r{}", i.c),
Instruction::Load(i) => format!("LOD r{}@r{}", i.b, i.c),
Instruction::Ortho(i) => format!("ORT r{}={}", i.a, i.v)
}
}