use polydat::Kernel;
use polydat::ast::Value;
use polydat::dsl::compile::compile_polydat_to_assembler;
use polydat::kernel::bind_under;
fn kernel(source: &str) -> Box<dyn Kernel> {
compile_polydat_to_assembler(source)
.and_then(|asm| asm.compile_kernel())
.unwrap_or_else(|e| panic!("{e}\n{source}"))
}
fn child_of(parent: &dyn Kernel, source: &str) -> Box<dyn Kernel> {
bind_under(parent, kernel(source).into_program(), &[])
.unwrap_or_else(|e| panic!("{e}\n{source}"))
}
fn held(kernel: &dyn Kernel, input: &str) -> String {
match kernel.input_value(input) {
Some(Value::None) | None => "nothing yet".to_string(),
Some(value) => value.to_display_string(),
}
}
fn main() {
let mut parent = kernel(
"input cycle: u64\n\
const buckets := 16\n\
const table := \"users\"\n\
slot := hash(cycle) % buckets\n",
);
println!("== A constant reaches a child when the child is bound ==");
let mut reader = child_of(
parent.as_ref(),
"input cycle: u64\n\
extern buckets: u64\n\
extern table: str\n\
label := \"{table}/{buckets}\"\n",
);
println!(
"before any cycle, buckets holds: {}",
held(reader.as_ref(), "buckets")
);
println!(
"before any cycle, table holds: {}",
held(reader.as_ref(), "table")
);
reader.set_inputs(&[0]);
println!("label = {}", reader.pull("label").to_display_string());
println!();
println!("== A value computed per cycle reaches a child when the parent computes it ==");
let mut follower = child_of(
parent.as_ref(),
"input cycle: u64\n\
extern slot: u64\n\
next := slot + 1\n",
);
println!(
"before any cycle, slot holds: {}",
held(follower.as_ref(), "slot")
);
for cycle in [7u64, 8] {
parent.set_inputs(&[cycle]);
let slot = parent.pull("slot").to_display_string();
follower.set_inputs(&[cycle]);
println!(
"cycle {cycle}: parent slot = {slot}, child next = {}",
follower.pull("next").to_display_string()
);
}
println!();
println!("== What a child knows when it is constructed ==");
let knows = child_of(
parent.as_ref(),
"input cycle: u64\n\
extern buckets: u64\n\
extern slot: u64\n\
out := slot + buckets\n",
);
for (name, port_type) in knows.externs() {
println!("{name}: {port_type} holds: {}", held(knows.as_ref(), &name));
}
println!();
println!("== Needing at construction a fact about a per-cycle value ==");
let mut maker = child_of(
parent.as_ref(),
"input cycle: u64\n\
extern buckets: u64\n\
slot := hash(cycle) % buckets\n",
);
println!(
"at construction, buckets holds: {}",
held(maker.as_ref(), "buckets")
);
for cycle in [7u64, 8] {
parent.set_inputs(&[cycle]);
maker.set_inputs(&[cycle]);
println!(
"cycle {cycle}: parent slot = {}, child slot = {}",
parent.pull("slot").to_display_string(),
maker.pull("slot").to_display_string()
);
}
println!();
println!("== A child that declares a name itself owns it ==");
let mut owner = child_of(
parent.as_ref(),
"input cycle: u64\n\
const buckets := 4\n\
slot := hash(cycle) % buckets\n",
);
owner.set_inputs(&[7]);
println!(
"child buckets = {}",
owner.pull("buckets").to_display_string()
);
println!(
"parent buckets = {}",
parent.pull("buckets").to_display_string()
);
}