use std::collections::HashSet;
use crate::scope_kernel::ScopeKernel;
use polydat::iteration::comprehension::pre_evaluate_clause;
use polydat::kernel::ManifestEntry;
use super::cascade::{CascadeInputs, CascadeOutputs, cascade_parent_into_source};
use super::helpers::{collect_leaf_placeholders, scan_one};
#[allow(clippy::too_many_arguments)]
pub fn build_for_each_scope_kernel(
bindings: &[(String, String)],
parent_manifest: &[ManifestEntry],
parent_kernel: &ScopeKernel,
workload_params: &std::collections::HashMap<String, String>,
polydat_lib_paths: Vec<std::path::PathBuf>,
workload_dir: Option<&std::path::Path>,
strict: bool,
context: &str,
phase_bindings: Option<&str>,
) -> Result<ScopeKernel, String> {
let iter_vars: Vec<String> = bindings.iter().map(|(v, _)| v.clone()).collect();
let spec_exprs: Vec<String> = bindings.iter().map(|(_, e)| e.clone()).collect();
let mut source = String::new();
let mut emitted: HashSet<String> = HashSet::new();
let mut inherited_names: Vec<String> = Vec::new();
let mut probes: std::collections::HashMap<String, String> = std::collections::HashMap::new();
let mut all_referenced: HashSet<String> = collect_leaf_placeholders(&spec_exprs);
for (idx, var) in iter_vars.iter().enumerate() {
if emitted.contains(var) {
continue;
}
let spec_text = spec_exprs.get(idx).map(String::as_str).unwrap_or("");
let values = match pre_evaluate_clause(spec_text, parent_kernel, workload_params, &probes) {
Ok(values) => values,
Err(_)
if probe_waits_on_outer_iteration(
spec_text,
parent_kernel,
workload_params,
&probes,
) =>
{
Vec::new()
}
Err(e) => return Err(format!("{context}: for_each `{var} in {spec_text}`: {e}")),
};
let detected_type = values
.first()
.map(polydat::ast::Value::port_type)
.unwrap_or(polydat::ast::PortType::Str);
source.push_str(&format!("extern {var}: {detected_type}\n"));
emitted.insert(var.clone());
for v in &values {
let v_str = v.to_display_string();
for next_spec in &spec_exprs[idx + 1..] {
let mut substituted = next_spec.clone();
substituted = substituted.replace(&format!("{{{var}}}"), &v_str);
let mut emergent = HashSet::new();
scan_one(&substituted, &mut emergent);
all_referenced.extend(emergent);
}
}
if let Some(first) = values.into_iter().next() {
probes.insert(var.clone(), first.to_display_string());
}
}
let pre_emitted: HashSet<String> = iter_vars.iter().cloned().collect();
cascade_parent_into_source(
CascadeInputs {
parent_kernel,
workload_params,
parent_manifest,
referenced: &all_referenced,
pre_emitted: &pre_emitted,
shadow_names: &pre_emitted,
include_referenced_cascade: true,
},
CascadeOutputs {
source: &mut source,
emitted: &mut emitted,
inherited_names: &mut inherited_names,
},
);
if let Some(body) = phase_bindings {
let trimmed = body.trim();
if !trimmed.is_empty() {
if !source.ends_with('\n') && !source.is_empty() {
source.push('\n');
}
source.push_str(body);
if !source.ends_with('\n') {
source.push('\n');
}
}
}
if source.is_empty() {
source.push_str("const __empty := 0\n");
}
let compile_options = polydat::kernel::subcontext::CompileOptions {
workload_dir: workload_dir.map(|p| p.to_path_buf()),
polydat_lib_paths,
strict,
required_outputs: Vec::new(),
context_label: Some(context.to_string()),
cursor_limit: None,
..Default::default()
};
crate::scope_kernel::ScopeKernel::synthesize_under(
parent_kernel,
crate::scope_kernel::SourceMatter::source(context, source, compile_options)
.inherited(inherited_names),
)
.map_err(|e| format!("{context}: for_each scope synthesis: {e}"))
}
fn probe_waits_on_outer_iteration(
spec_text: &str,
parent_kernel: &ScopeKernel,
workload_params: &std::collections::HashMap<String, String>,
probes: &std::collections::HashMap<String, String>,
) -> bool {
let mut names = collect_leaf_placeholders(&[spec_text.to_string()]);
let bare = spec_text.trim();
if !bare.is_empty() && bare.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
names.insert(bare.to_string());
}
let kernel = parent_kernel.kernel();
names.iter().any(|name| {
!probes.contains_key(name)
&& !workload_params.contains_key(name)
&& parent_kernel.lookup(name).is_none()
&& (kernel.input_port_type(name).is_some() || kernel.output_type(name).is_some())
})
}
#[cfg(test)]
mod tests {
use super::build_for_each_scope_kernel;
use crate::scope_kernel::ScopeKernel;
use polydat::ast::PortType;
use std::collections::HashMap;
fn synth(parent: &ScopeKernel, var: &str, spec: &str) -> Result<ScopeKernel, String> {
build_for_each_scope_kernel(
&[(var.to_string(), spec.to_string())],
&[],
parent,
&HashMap::new(),
Vec::new(),
None,
false,
"test_for_each",
None,
)
}
#[test]
fn unresolvable_clause_fails_with_its_own_diagnostic() {
let parent = ScopeKernel::compile("\n").unwrap();
for (spec, why) in [
("nope_values", "did not resolve"),
("pre_{nobody}", "unresolved placeholder"),
] {
let err = synth(&parent, "k", spec).expect_err("must fail at synthesis");
assert!(
err.contains(&format!("for_each `k in {spec}`")) && err.contains(why),
"{spec}: {err}"
);
}
}
#[test]
fn clause_on_an_outer_iter_var_waits_for_the_iteration() {
let parent = ScopeKernel::compile("extern outer: str\n").unwrap();
let kernel = synth(&parent, "inner", "pre_{outer}").expect("deferred, not an error");
assert_eq!(
kernel.kernel().input_port_type("inner"),
Some(PortType::Str)
);
}
}