-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63f8653
commit 3da4546
Showing
6 changed files
with
268 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod optimized_instructions; | ||
mod optimizer; | ||
|
||
pub use optimizer::Optimizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use std::fmt; | ||
|
||
use crate::ast::instructions::{Instruction, InstructionType}; | ||
|
||
#[derive(Clone)] | ||
pub struct OptimizedInstruction { | ||
pub instruction: Instruction, | ||
content: Option<Vec<OptimizedInstruction>>, | ||
pub amount: u32, | ||
} | ||
|
||
impl OptimizedInstruction { | ||
pub fn from( | ||
instruction: Instruction, | ||
content: Option<Vec<OptimizedInstruction>>, | ||
) -> OptimizedInstruction { | ||
OptimizedInstruction { | ||
instruction, | ||
content, | ||
amount: 1, | ||
} | ||
} | ||
|
||
pub fn get_content(&self) -> Vec<OptimizedInstruction> { | ||
match &self.content { | ||
Some(content) => content.clone(), | ||
None => vec![], | ||
} | ||
} | ||
pub fn get_content_ref(&self) -> &Vec<OptimizedInstruction> { | ||
match &self.content { | ||
Some(content) => content, | ||
None => panic!("Instruction has no content"), | ||
} | ||
} | ||
|
||
pub fn add(&mut self, amount: u32) { | ||
self.amount += amount | ||
} | ||
|
||
pub fn sub(&mut self, amount: u32) { | ||
self.amount -= amount | ||
} | ||
|
||
pub fn get_amount(&self) -> u32 { | ||
self.amount | ||
} | ||
|
||
pub fn is_opposed(&self, other: &OptimizedInstruction) -> bool { | ||
match self.instruction.instruction_type { | ||
InstructionType::Increment => { | ||
match other.instruction.instruction_type { | ||
InstructionType::Decrement => true, | ||
_ => false, | ||
} | ||
} | ||
InstructionType::Decrement => { | ||
match other.instruction.instruction_type { | ||
InstructionType::Increment => true, | ||
_ => false, | ||
} | ||
} | ||
InstructionType::MoveLeft => { | ||
match other.instruction.instruction_type { | ||
InstructionType::MoveRight => true, | ||
_ => false, | ||
} | ||
} | ||
InstructionType::MoveRight => { | ||
match other.instruction.instruction_type { | ||
InstructionType::MoveLeft => true, | ||
_ => false, | ||
} | ||
} | ||
InstructionType::MoveLeftScope => { | ||
match other.instruction.instruction_type { | ||
InstructionType::MoveRightScope => true, | ||
_ => false, | ||
} | ||
} | ||
InstructionType::MoveRightScope => { | ||
match other.instruction.instruction_type { | ||
InstructionType::MoveLeftScope => true, | ||
_ => false, | ||
} | ||
} | ||
|
||
_ => false | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Debug for OptimizedInstruction { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.instruction.instruction_type { | ||
InstructionType::Increment => write!(f, "Increment({})", self.amount), | ||
InstructionType::Decrement => write!(f, "Decrement({})", self.amount), | ||
|
||
InstructionType::MoveLeft => write!(f, "MoveLeft({})", self.amount), | ||
InstructionType::MoveRight => write!(f, "MoveRight({})", self.amount), | ||
|
||
InstructionType::Input => write!(f, "Input"), | ||
InstructionType::Output => write!(f, "Output"), | ||
|
||
InstructionType::Loop => write!(f, "Loop{:?}", self.get_content_ref()), | ||
|
||
InstructionType::Function => write!(f, "Function{:?}", self.get_content_ref()), | ||
InstructionType::CallFunction => write!(f, "CallFunction"), | ||
|
||
InstructionType::MoveLeftScope => write!(f, "MoveLeftScope"), | ||
InstructionType::MoveRightScope => write!(f, "MoveRightScope"), | ||
|
||
InstructionType::Random => write!(f, "Random"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use crate::ast::instructions::{Instruction, InstructionType}; | ||
|
||
use crate::optimization::optimized_instructions::OptimizedInstruction; | ||
|
||
pub struct Optimizer { | ||
instructions: Vec<Instruction>, | ||
} | ||
|
||
impl Optimizer { | ||
pub fn new(instructions: Vec<Instruction>) -> Optimizer { | ||
Optimizer { instructions } | ||
} | ||
|
||
pub fn optimize(&mut self) -> Vec<OptimizedInstruction> { | ||
let mut optimized_instructions = vec![]; | ||
self.merge_instructions(&mut optimized_instructions); | ||
self.cancel_opposed_instructions(&mut optimized_instructions); | ||
optimized_instructions | ||
} | ||
|
||
fn optimize_container(&self, container: Instruction) -> OptimizedInstruction { | ||
let optimized_instructions = Optimizer::new(container.get_content()).optimize(); | ||
let mut optimized_container = OptimizedInstruction::from(container, Some(optimized_instructions)); | ||
|
||
optimized_container | ||
} | ||
|
||
fn merge_instructions(&self, optimized_instructions: &mut Vec<OptimizedInstruction>) -> () { | ||
for instruction in self.instructions.iter() { | ||
match optimized_instructions.last_mut() { | ||
Some(last_optimized_instruction) => { | ||
if last_optimized_instruction.instruction.instruction_type | ||
== instruction.instruction_type | ||
{ | ||
last_optimized_instruction.add(1); | ||
} else { | ||
match instruction.instruction_type { | ||
InstructionType::Function | InstructionType::Loop => { | ||
optimized_instructions.push(self.optimize_container(instruction.clone())); | ||
} | ||
_ => { | ||
optimized_instructions.push(OptimizedInstruction::from( | ||
instruction.clone(), | ||
None, | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
None => match instruction.instruction_type { | ||
InstructionType::Function | InstructionType::Loop => { | ||
optimized_instructions.push(self.optimize_container(instruction.clone())); | ||
} | ||
_ => optimized_instructions.push(OptimizedInstruction::from(instruction.clone(), None)), | ||
|
||
}, | ||
} | ||
} | ||
} | ||
|
||
fn cancel_opposed_instructions(&self, optimized_instructions: &mut Vec<OptimizedInstruction>) -> () { | ||
let mut new_optimized_instructions: Vec<OptimizedInstruction> = vec![]; | ||
|
||
for optimized_instruction in optimized_instructions.iter() { | ||
let last_optimized_instruction = new_optimized_instructions.last_mut(); | ||
|
||
match last_optimized_instruction { | ||
Some(last_optimized_instruction) => { | ||
if last_optimized_instruction.is_opposed(optimized_instruction) { | ||
last_optimized_instruction.sub(optimized_instruction.get_amount()); | ||
} else if last_optimized_instruction.instruction.instruction_type == optimized_instruction.instruction.instruction_type { | ||
last_optimized_instruction.add(optimized_instruction.get_amount()) | ||
} else { | ||
new_optimized_instructions.push(optimized_instruction.clone()); | ||
} | ||
}, | ||
|
||
None => new_optimized_instructions.push(optimized_instruction.clone()) | ||
} | ||
} | ||
|
||
*optimized_instructions = new_optimized_instructions; | ||
} | ||
} |