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

View file

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