parent
0945010f0f
commit
21ae3bee01
@ -0,0 +1,27 @@
|
|||||||
|
#[allow(unused_imports)]
|
||||||
|
use stackline::prelude::*;
|
||||||
|
use stackline::signal;
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_if() {
|
||||||
|
let mut world = load_test!("tests/logic/if.json");
|
||||||
|
world.init();
|
||||||
|
|
||||||
|
world.set_signal((0, 0), signal!([0]));
|
||||||
|
|
||||||
|
run!(world, 8);
|
||||||
|
|
||||||
|
assert_stored!(world, 1, 2);
|
||||||
|
assert_no_stored!(world, 2, 2);
|
||||||
|
|
||||||
|
let mut world = load_test!("tests/logic/if.json");
|
||||||
|
world.init();
|
||||||
|
|
||||||
|
world.set_signal((0, 0), signal!([1]));
|
||||||
|
|
||||||
|
run!(world, 8);
|
||||||
|
|
||||||
|
assert_no_stored!(world, 1, 2);
|
||||||
|
assert_stored!(world, 2, 2);
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
{"panes":{"main":{"tiles":[{"cell":{"Diode":"Right"},"signal":null,"state":"Idle","updated":false},{"cell":{"If":{"invert":false}},"signal":null,"state":"Idle","updated":false},{"cell":{"Diode":"Down"},"signal":null,"state":"Idle","updated":false},{"cell":null,"signal":null,"state":"Idle","updated":false},{"cell":{"Wire":"Vertical"},"signal":null,"state":"Idle","updated":false},{"cell":{"Wire":"Vertical"},"signal":null,"state":"Idle","updated":false},{"cell":null,"signal":null,"state":"Idle","updated":false},{"cell":{"Store":{"signal":null}},"signal":null,"state":"Idle","updated":false},{"cell":{"Store":{"signal":null}},"signal":null,"state":"Idle","updated":false}],"width":3,"height":3,"position":[0,0]}},"blink_speed":{"secs":0,"nanos":250000000}}
|
@ -0,0 +1,34 @@
|
|||||||
|
#[allow(unused_imports)]
|
||||||
|
use stackline::prelude::*;
|
||||||
|
use stackline::signal;
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_prime() {
|
||||||
|
let mut world = load_test!("tests/other/prime.json");
|
||||||
|
world.set_signal((4, 0), signal!());
|
||||||
|
world.init();
|
||||||
|
|
||||||
|
run!(world, 500);
|
||||||
|
|
||||||
|
assert_stored!(world, 1, 3, signal!([2, 3, 5, 7, 11, 13, 17, 19]));
|
||||||
|
|
||||||
|
run!(world, 1500);
|
||||||
|
|
||||||
|
let mut signal = signal!([2]);
|
||||||
|
|
||||||
|
for n in 3..=53 {
|
||||||
|
let mut prime = true;
|
||||||
|
for f in 2..n {
|
||||||
|
if n % f == 0 {
|
||||||
|
prime = false;
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if prime {
|
||||||
|
signal.push(Value::Number(n.into()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_stored!(world, 1, 3, signal);
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
@ -0,0 +1 @@
|
|||||||
|
{"panes":{"main":{"tiles":[{"cell":{"Diode":"Down"},"signal":null,"state":"Idle","updated":false},{"cell":{"Add":null},"signal":null,"state":"Idle","updated":false},{"cell":{"Diode":"Left"},"signal":null,"state":"Idle","updated":false},{"cell":null,"signal":null,"state":"Idle","updated":false},{"cell":null,"signal":null,"state":"Idle","updated":false},{"cell":{"Wire":"Vertical"},"signal":null,"state":"Idle","updated":false},{"cell":null,"signal":null,"state":"Idle","updated":false},{"cell":{"Push":{"value":{"Number":1.0}}},"signal":null,"state":"Idle","updated":false},{"cell":{"Diode":"Right"},"signal":null,"state":"Idle","updated":false},{"cell":{"Diode":"Down"},"signal":null,"state":"Idle","updated":false},{"cell":{"Store":{"signal":{"direction":"Right","stack":[{"Number":0.0}]}}},"signal":null,"state":"Idle","updated":false},{"cell":{"Reader":"Right"},"signal":null,"state":"Idle","updated":false},{"cell":{"Wire":"Any"},"signal":null,"state":"Idle","updated":false},{"cell":{"Stacker":{"signal":null,"orientation":"Vertical"}},"signal":null,"state":"Idle","updated":false},{"cell":{"Wire":"Vertical"},"signal":null,"state":"Idle","updated":false},{"cell":{"Diode":"Right"},"signal":null,"state":"Idle","updated":false},{"cell":{"Diode":"Up"},"signal":null,"state":"Idle","updated":false},{"cell":{"Resistor":{"direction":"Right","signal":null}},"signal":null,"state":"Idle","updated":false},{"cell":{"Reader":"Up"},"signal":null,"state":"Idle","updated":false},{"cell":{"Wire":"Vertical"},"signal":null,"state":"Idle","updated":false},{"cell":{"Wire":"Any"},"signal":null,"state":"Idle","updated":false},{"cell":{"Resistor":{"direction":"Right","signal":null}},"signal":null,"state":"Idle","updated":false},{"cell":{"Resistor":{"direction":"Down","signal":null}},"signal":null,"state":"Idle","updated":false},{"cell":{"Store":{"signal":{"direction":"Right","stack":[]}}},"signal":null,"state":"Idle","updated":false},{"cell":{"Diode":"Left"},"signal":null,"state":"Idle","updated":false},{"cell":{"Resistor":{"direction":"Up","signal":null}},"signal":null,"state":"Idle","updated":false},{"cell":{"Resistor":{"direction":"Left","signal":null}},"signal":null,"state":"Idle","updated":false},{"cell":{"Resistor":{"direction":"Left","signal":null}},"signal":null,"state":"Idle","updated":false},{"cell":null,"signal":null,"state":"Idle","updated":false},{"cell":null,"signal":null,"state":"Idle","updated":false}],"width":5,"height":6,"position":[0,0]}},"blink_speed":{"secs":0,"nanos":250000000}}
|
@ -0,0 +1,69 @@
|
|||||||
|
//! Logical operations: if, etc.
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
|
use crate::tile::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||||
|
pub struct If {
|
||||||
|
invert: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl If {
|
||||||
|
pub fn new(invert: bool) -> Self {
|
||||||
|
Self {
|
||||||
|
invert
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tile for If {
|
||||||
|
fn update<'b>(&'b mut self, mut context: UpdateContext<'b>) {
|
||||||
|
if let Some(mut signal) = context.take_signal() {
|
||||||
|
let truthy = match signal.pop() {
|
||||||
|
Some(Value::Number(x)) => x != 0.0,
|
||||||
|
Some(Value::String(s)) => !s.is_empty(),
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
|
let reorient = if self.invert {
|
||||||
|
truthy
|
||||||
|
} else {
|
||||||
|
!truthy
|
||||||
|
};
|
||||||
|
|
||||||
|
if reorient {
|
||||||
|
if matches!(signal.direction(), Direction::Left | Direction::Right) {
|
||||||
|
if let Some(up) = context.offset((0, -1)) {
|
||||||
|
let _ = context.send(up, Direction::Up, signal.clone());
|
||||||
|
}
|
||||||
|
if let Some(down) = context.offset((0, 1)) {
|
||||||
|
let _ = context.send(down, Direction::Down, signal);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if let Some(left) = context.offset((-1, 0)) {
|
||||||
|
let _ = context.send(left, Direction::Left, signal.clone());
|
||||||
|
}
|
||||||
|
if let Some(right) = context.offset((1, 0)) {
|
||||||
|
let _ = context.send(right, Direction::Right, signal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if let Some(target_position) = context.offset(signal.direction().into_offset()) {
|
||||||
|
let _ = context.send(target_position, signal.direction(), signal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if context.state() != State::Idle {
|
||||||
|
context.next_state();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_simple(&self, ctx: DrawContext) -> TextChar {
|
||||||
|
if self.invert {
|
||||||
|
TextChar::from_state('¿', ctx.state)
|
||||||
|
} else {
|
||||||
|
TextChar::from_state('?', ctx.state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue