use std::process::ExitCode;
use kaish_kernel::{expand_fragment, plan_program, Expansion, FragmentAddr};
use kaish_types::Value;
const SCRIPT: &str = r#"python3 <<'PY'
import os
print(os.getcwd())
PY
sqlite3 db <<SQL
select * from t where user = '${USER}';
SQL
python3 <<PY
stamp = "$(date +%s)"
PY"#;
fn main() -> ExitCode {
let scope = vec![("USER".to_string(), Value::String("amy".to_string()))];
let statements = match plan_program(SCRIPT) {
Ok(statements) => statements,
Err(errors) => {
eprintln!("does not parse: {} error(s)", errors.len());
return ExitCode::FAILURE;
}
};
for planned in statements {
for command in &planned.plan.commands {
for heredoc in &command.heredocs {
println!(
"── {} <<{} (literal={}, reads {:?})",
command.name, heredoc.delimiter, heredoc.literal, heredoc.free_variables,
);
let addr = FragmentAddr::new(planned.index, heredoc.index);
match expand_fragment(SCRIPT, addr, &scope) {
Ok(Expansion::Complete(text)) => println!("reads on stdin:\n{text}"),
Ok(Expansion::Blocked { holes }) => {
for hole in holes {
println!(
"blocked on {} — it would run: {}",
hole.source, hole.plans[0].rendered,
);
}
}
Err(error) => println!("cannot expand: {error}"),
}
}
}
}
ExitCode::SUCCESS
}