Skip to content

Commit

Permalink
Add optimization evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sellig6792 committed Dec 27, 2022
1 parent 3da4546 commit ac087d6
Show file tree
Hide file tree
Showing 10 changed files with 477 additions and 219 deletions.
83 changes: 83 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ name = "fbf"
path = "src/main.rs"

[dependencies]
rand = "0.8.5"
rand = "0.8.5"
num = "0.4.0"
84 changes: 30 additions & 54 deletions src/ast/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
pub enum InstructionType {
Expand All @@ -22,30 +21,16 @@ pub enum InstructionType {
Random,
}

impl fmt::Display for InstructionType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InstructionType::Increment => write!(f, "Increment"),
InstructionType::Decrement => write!(f, "Decrement"),

InstructionType::MoveLeft => write!(f, "MoveLeft"),
InstructionType::MoveRight => write!(f, "MoveRight"),

InstructionType::Input => write!(f, "Input"),
InstructionType::Output => write!(f, "Output"),

InstructionType::Loop => write!(f, "Loop"),

InstructionType::Function => write!(f, "Function"),
InstructionType::CallFunction => write!(f, "CallFunction"),

InstructionType::MoveLeftScope => write!(f, "MoveLeftScope"),
InstructionType::MoveRightScope => write!(f, "MoveRightScope"),

InstructionType::Random => write!(f, "Random"),
}
}
pub trait InstructionTrait<T> {
fn new(instruction_type: InstructionType, content: Option<Vec<T>>) -> Self;
fn get_instruction_type(&self) -> InstructionType;
fn get_content(&self) -> Vec<T>;
fn get_content_ref(&self) -> &Vec<T>;
fn get_content_mut(&mut self) -> &mut Vec<T>;
fn get_amount(&self) -> u32;
}

#[derive(Clone)]
pub struct Instruction {
pub instruction_type: InstructionType,
Expand All @@ -60,47 +45,38 @@ impl Instruction {
content,
}
}
pub fn get_content(&self) -> Vec<Instruction> {
}

impl InstructionTrait<Instruction> for Instruction {
fn new(instruction_type: InstructionType, content: Option<Vec<Instruction>>) -> Instruction {
Instruction {
instruction_type,
content,
}
}
fn get_instruction_type(&self) -> InstructionType {
self.instruction_type.clone()
}

fn get_content(&self) -> Vec<Instruction> {
match &self.content {
Some(content) => content.clone(),
None => panic!("Instruction has no content"),
}
}
pub fn get_content_ref(&self) -> &Vec<Instruction> {
fn get_content_ref(&self) -> &Vec<Instruction> {
match &self.content {
Some(content) => content,
None => panic!("Instruction has no content"),
}
}
}

impl fmt::Display for Instruction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.instruction_type {
InstructionType::Increment => write!(f, "Increment"),
InstructionType::Decrement => write!(f, "Decrement"),

InstructionType::MoveLeft => write!(f, "MoveLeft"),
InstructionType::MoveRight => write!(f, "MoveRight"),

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"),
fn get_content_mut(&mut self) -> &mut Vec<Instruction> {
match &mut self.content {
Some(content) => content,
None => panic!("Instruction has no content"),
}
}
}

impl fmt::Debug for Instruction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
fn get_amount(&self) -> u32 {
1
}
}
}
9 changes: 6 additions & 3 deletions src/evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub mod evaluator;
pub mod scope;
mod evaluator;
mod scope;
mod cell;

pub use evaluator::Evaluator;
pub use scope::Scope;

use cell::Cell;
use scope::Scopes;
76 changes: 76 additions & 0 deletions src/evaluation/cell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// A cyclic integer is a number that is always in the range. If it is greater than the maximum value, it will be set to the minimum value. If it is less than the minimum value + the overflow, it will be set to the maximum value.

use num::PrimInt;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
pub struct Cell {
value: u8,
}

impl Cell {
pub fn new(value: u8) -> Self {
Cell { value }
}

pub fn get_value(&self) -> u8 {
self.value
}

pub fn set_value(&mut self, value: u8) {
self.value = value;
}

pub fn add<T: PrimInt>(&mut self, value: T) {
let sum: usize = self.value as usize + value.to_usize().unwrap();
self.value = (sum % (u8::MAX as usize + 1)) as u8;
}

pub fn sub<T: PrimInt>(&mut self, value: T) {
let sub: isize = self.value as isize - value.to_isize().unwrap();
self.value = if sub < 0 {
u8::MAX - (sub.abs() as u8 - 1)
} else {
sub as u8
};
}
}

impl PartialEq<u8> for Cell {
fn eq(&self, other: &u8) -> bool {
self.value == *other
}
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_add() {
let mut cell = Cell::new(5);
cell.add(5);
assert_eq!(cell.get_value(), 10);
}

#[test]
fn test_add_overflow() {
let mut cell = Cell::new(255);
cell.add(1);
assert_eq!(cell.get_value(), 0);
}

#[test]
fn test_sub() {
let mut cell = Cell::new(5);
cell.sub(5);
assert_eq!(cell.get_value(), 0);
}

#[test]
fn test_sub_overflow() {
let mut cell = Cell::new(5);
cell.sub(10);
assert_eq!(cell.get_value(), 251);
}
}
Loading

0 comments on commit ac087d6

Please sign in to comment.