-
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
3da4546
commit ac087d6
Showing
10 changed files
with
477 additions
and
219 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -10,4 +10,5 @@ name = "fbf" | |
path = "src/main.rs" | ||
|
||
[dependencies] | ||
rand = "0.8.5" | ||
rand = "0.8.5" | ||
num = "0.4.0" |
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 |
---|---|---|
@@ -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; |
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,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); | ||
} | ||
} |
Oops, something went wrong.