use std::process::ExitCode;
fn main() -> ExitCode {
let cli = phplang::cli::parse();
if let Some(code) = cli.run {
let src = format!("<?php {code}");
return run_source(&src);
}
if let Some(file) = cli.file {
if cli.dump_bytecode {
return match dump(&file) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => fail(&e),
};
}
return match phplang::eval_file(&file) {
Ok(_) => ExitCode::SUCCESS,
Err(e) => fail(&e),
};
}
if cli.interactive || atty_stdin() {
phplang::repl::run();
return ExitCode::SUCCESS;
}
let src = std::io::read_to_string(std::io::stdin()).unwrap_or_default();
run_source(&src)
}
fn run_source(src: &str) -> ExitCode {
match phplang::eval_str(src) {
Ok(_) => ExitCode::SUCCESS,
Err(e) => fail(&e),
}
}
fn dump(file: &str) -> Result<(), String> {
let src = std::fs::read_to_string(file).map_err(|e| format!("cannot read {file}: {e}"))?;
let prog = phplang::compile(&src)?;
println!("== main ==\n{:#?}", prog.main.ops);
for (name, f) in &prog.functions {
println!(
"== function {name}({}) ==\n{:#?}",
f.params.join(", "),
f.chunk.ops
);
}
Ok(())
}
fn atty_stdin() -> bool {
unsafe { libc::isatty(libc::STDIN_FILENO) == 1 }
}
fn fail(msg: &str) -> ExitCode {
eprintln!("php: {msg}");
ExitCode::FAILURE
}