use unilang::pipeline::Pipeline;
use unilang::interpreter::ExecutionContext;
use std::io::{ self, Write };
pub fn run_repl(
pipeline : &Pipeline,
) -> Result< (), Box< dyn core::error::Error > >
{
println!( "genfile REPL v0.1.0" );
println!( "Type '.help' for help, 'exit' to quit" );
println!();
let mut session_count = 0u32;
let mut had_errors = false;
loop
{
print!( "genfile[{session_count}]> " );
io::stdout().flush()?;
let mut input = String::new();
match io::stdin().read_line( &mut input )
{
Ok( 0 ) => break, Ok( _ ) =>
{
let input = input.trim();
match input
{
"" => continue,
"quit" | "exit" => break,
_ => {}
}
session_count += 1;
let ctx = ExecutionContext::default();
let result = pipeline.process_command( input, ctx );
if result.success
{
for output in &result.outputs
{
if !output.content.is_empty()
{
println!( "{}", output.content );
}
}
}
else
{
had_errors = true;
eprintln!( "{}", result.error.unwrap_or_default() );
}
},
Err( e ) =>
{
eprintln!( "Input error: {e}" );
break;
}
}
}
println!( "\nGoodbye!" );
if had_errors
{
Err( "One or more commands failed during REPL session".into() )
}
else
{
Ok( () )
}
}