use crate::evaluator::RuntimeError;
use crate::value::Value;
use std::io::{self, Write};
pub fn print(args: &[Value]) -> Result<Value, RuntimeError> {
if args.is_empty() {
return Ok(Value::Null);
}
let output = args
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" ");
print!("{}", output);
io::stdout().flush().unwrap();
Ok(Value::Null)
}
pub fn println(args: &[Value]) -> Result<Value, RuntimeError> {
if args.is_empty() {
println!();
return Ok(Value::Null);
}
let output = args
.iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" ");
println!("{}", output);
Ok(Value::Null)
}
pub fn input(args: &[Value]) -> Result<Value, RuntimeError> {
if args.is_empty() {
return Err(RuntimeError::WrongArity {
expected: 1,
got: 0,
});
}
print!("{}", args[0].to_string());
io::stdout().flush().unwrap();
let mut buffer = String::new();
io::stdin()
.read_line(&mut buffer)
.map_err(|e| RuntimeError::InvalidOperation(format!("Failed to read input: {}", e)))?;
if buffer.ends_with('\n') {
buffer.pop();
if buffer.ends_with('\r') {
buffer.pop();
}
}
Ok(Value::String(buffer))
}