From 9071a057a20ec53ad547226b16a154182a24fa84 Mon Sep 17 00:00:00 2001 From: aaron Date: Tue, 21 Sep 2021 14:07:03 +0200 Subject: [PATCH] improve machine and make arguments work --- src/machine.rs | 89 ++++++++++++++++++++++++++------------------------ src/parser.rs | 23 +++++++------ 2 files changed, 59 insertions(+), 53 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index d77e517..4afbd8a 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -33,28 +33,12 @@ impl<'a> Machine<'a> { println!("{:?}", instruction); // match type and execute match instruction.itype { - InstructionType::Increment => { - self.memory[self.mp] += instruction.argument as u32 - } - InstructionType::Decrement => { - self.memory[self.mp] -= instruction.argument as u32 - } - InstructionType::Leftshift => { - self.mp += instruction.argument - } - InstructionType::Rightshift => { - self.mp -= instruction.argument - } - InstructionType::PutChar => { - for _ in 0..instruction.argument { - self.put_char() - } - } - InstructionType::GetChar => { - for _ in 0..instruction.argument { - self.get_char() - } - } + InstructionType::Increment => self.do_increment(instruction.argument), + InstructionType::Decrement => self.do_decrement(instruction.argument), + InstructionType::Leftshift => self.do_leftshift(instruction.argument), + InstructionType::Rightshift => self.do_rightshift(instruction.argument), + InstructionType::PutChar => self.do_put_char(instruction.argument), + InstructionType::GetChar => self.do_get_char(instruction.argument), InstructionType::JumpIfZero => { if self.memory[self.mp] == 0 { self.ip = instruction.argument; @@ -68,36 +52,55 @@ impl<'a> Machine<'a> { } } } - self.ip += 1; } } - pub fn get_char(&mut self){ - let byte_input = match self.input.read(&mut self.buf) { - Ok(byte_input) => byte_input, - Err(e) => panic!(e), - }; - - if byte_input != 1 { - panic!("input read error") - } - - self.memory[self.mp] = self.buf[0] as u32; + pub fn do_increment(&mut self, argument: usize){ + self.memory[self.mp] += argument as u32 } - pub fn put_char(&mut self){ - self.buf[0] = self.memory[self.mp] as u8; + pub fn do_decrement(&mut self, argument: usize){ + self.memory[self.mp] -= argument as u32 + } - let byte_write = match self.output.write(&self.buf) { - Ok(byte_write) => byte_write, - Err(e) => panic!(e), - }; + pub fn do_leftshift(&mut self, argument: usize){ + self.mp += argument + } - if byte_write != 1 { - panic!("output write error") + pub fn do_rightshift(&mut self, argument: usize){ + self.mp -= argument + } + + pub fn do_get_char(&mut self, argument: usize){ + for _ in 0..argument { + let byte_input = match self.input.read(&mut self.buf) { + Ok(byte_input) => byte_input, + Err(e) => panic!(e), + }; + + if byte_input != 1 { + panic!("input read error") + } + + self.memory[self.mp] = self.buf[0] as u32; } + } - self.output.flush(); + pub fn do_put_char(&mut self, argument: usize){ + for _ in 0..argument { + self.buf[0] = self.memory[self.mp] as u8; + + let byte_write = match self.output.write(&self.buf) { + Ok(byte_write) => byte_write, + Err(e) => panic!(e), + }; + + if byte_write != 1 { + panic!("output write error") + } + + self.output.flush(); + } } } diff --git a/src/parser.rs b/src/parser.rs index 4645a95..a003fff 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -25,14 +25,14 @@ impl Parser { while self.position < self.code_length { let current = self.code[self.position]; match current { - '+' => self.load_instruction(Increment), - '-' => self.load_instruction(Decrement), - '<' => self.load_instruction(Leftshift), - '>' => self.load_instruction(Rightshift), - '.' => self.load_instruction(PutChar), - ',' => self.load_instruction(GetChar), - '[' => self.load_instruction(JumpIfZero), - ']' => self.load_instruction(JumpIfNotZero), + '+' => self.load_instruction(Increment, 1), + '-' => self.load_instruction(Decrement, 1), + '<' => self.load_instruction(Leftshift, 1), + '>' => self.load_instruction(Rightshift, 1), + '.' => self.load_instruction(PutChar, 1), + ',' => self.load_instruction(GetChar, 1), + '[' => self.load_instruction(JumpIfZero, 1), + ']' => self.load_instruction(JumpIfNotZero, 1), _ => (), } @@ -40,10 +40,13 @@ impl Parser { } } - fn load_instruction(&mut self, itype: InstructionType) { + // ToDo: Write a wrapper function for this to optimize the code by + // counting the number of similar consecutive instructions and setting + // the argument accordingly. + fn load_instruction(&mut self, itype: InstructionType, arg: usize) { let instruction = Instruction { itype, - argument: 1, + argument: arg, }; self.instructions.push(instruction);