Minor fixes to the code

This commit is contained in:
Anthony Foxclaw 2020-02-08 15:36:04 -05:00
parent 853188b010
commit 317feb9188
2 changed files with 20 additions and 24 deletions

View file

@ -9,9 +9,9 @@ use nom::types::CompleteStr;
#[derive(Debug, PartialEq)]
pub struct AssemblerInstruction {
opcode: Token,
op1: Option<Token>,
op2: Option<Token>,
op3: Option<Token>,
operand1: Option<Token>,
operand2: Option<Token>,
operand3: Option<Token>,
}
impl AssemblerInstruction {
@ -31,10 +31,9 @@ impl AssemblerInstruction {
};
for operand in vec![&self.op1, &self.op2, &self.op3] {
match operand {
Some(t) => AssemblerInstruction::extract_operand(t, &mut results),
None => {}
for operand in &[&self.operand1, &self.operand2, &self.operand3] {
if let Some(token) = operand {
AssemblerInstruction::extract_operand(token, &mut results)
}
}
@ -71,9 +70,9 @@ named!(pub instruction_one<CompleteStr, AssemblerInstruction>,
(
AssemblerInstruction{
opcode: o,
op1: Some(r),
op2: Some(i),
op3: None
operand1: Some(r),
operand2: Some(i),
operand3: None
}
)
)
@ -93,9 +92,9 @@ mod instruction_parser_test {
CompleteStr(""),
AssemblerInstruction {
opcode: Token::Opcode { code: Opcode::LOAD },
op1: Some(Token::Register { reg_num: 0 }),
op2: Some(Token::Number { value: 100 }),
op3: None
operand1: Some(Token::Register { reg_num: 0 }),
operand2: Some(Token::Number { value: 100 }),
operand3: None
}
))
);

View file

@ -77,18 +77,15 @@ impl REPL {
println!("{:#?}", self.vm.registers);
}
_ => {
let prog = program(CompleteStr(buffer));
if !prog.is_ok() {
println!("Unable to parse input");
continue;
}
let (_, result) = prog.unwrap();
let bytecode = result.to_bytes();
for byte in bytecode {
self.vm.add_byte(byte);
}
let prog = match program(buffer.into()) {
Ok((_, prog)) => prog,
Err(_) => {
println!("Unable to parse input.");
continue;
}
};
self.vm.run_once();
self.vm.program.append(&mut prog.to_bytes());
}
}
}