From 317feb91888ca9d66dac4211eed314aaf3444217 Mon Sep 17 00:00:00 2001 From: Anthony Foxclaw <35226681+tonytins@users.noreply.github.com> Date: Sat, 8 Feb 2020 15:36:04 -0500 Subject: [PATCH] Minor fixes to the code --- src/assembler/instruction_parser.rs | 25 ++++++++++++------------- src/repl.rs | 19 ++++++++----------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/assembler/instruction_parser.rs b/src/assembler/instruction_parser.rs index 4d78796..ddcdf11 100644 --- a/src/assembler/instruction_parser.rs +++ b/src/assembler/instruction_parser.rs @@ -9,9 +9,9 @@ use nom::types::CompleteStr; #[derive(Debug, PartialEq)] pub struct AssemblerInstruction { opcode: Token, - op1: Option, - op2: Option, - op3: Option, + operand1: Option, + operand2: Option, + operand3: Option, } 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, ( 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 } )) ); diff --git a/src/repl.rs b/src/repl.rs index 5859e44..2154bcc 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -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()); } } }