use std::path::PathBuf;
use std::process::ExitCode;
use rustyline::error::ReadlineError;
use rustyline::DefaultEditor;
use miruscriptx::value::Value;
use miruscriptx::Session;
pub fn run() -> ExitCode {
println!(
"MiruScriptX {} REPL. Press Ctrl-D to exit.",
miruscriptx::VERSION
);
let mut editor = match DefaultEditor::new() {
Ok(editor) => editor,
Err(err) => {
eprintln!("miru: could not start the REPL: {err}");
return ExitCode::FAILURE;
}
};
let history = history_path();
if let Some(path) = &history {
let _ = editor.load_history(path);
}
let mut session = Session::new();
let mut buffer = String::new();
let exit_code = loop {
let prompt = if buffer.is_empty() {
"miru> "
} else {
"... "
};
match editor.readline(prompt) {
Ok(line) => {
buffer.push_str(&line);
buffer.push('\n');
if is_incomplete(&buffer) {
continue; }
let source = std::mem::take(&mut buffer);
if source.trim().is_empty() {
continue;
}
let _ = editor.add_history_entry(source.trim_end());
evaluate(&mut session, &source);
}
Err(ReadlineError::Interrupted) => buffer.clear(),
Err(ReadlineError::Eof) => break ExitCode::SUCCESS,
Err(err) => {
eprintln!("miru: input error: {err}");
break ExitCode::FAILURE;
}
}
};
if let Some(path) = &history {
let _ = editor.save_history(path);
}
exit_code
}
fn history_path() -> Option<PathBuf> {
std::env::var_os("HOME")
.or_else(|| std::env::var_os("USERPROFILE"))
.map(|home| PathBuf::from(home).join(".miru_history"))
}
fn evaluate(session: &mut Session, source: &str) {
match session.eval(source) {
Ok(value) => {
if !matches!(value, Value::Nil) {
println!("{}", value.repr());
}
}
Err(err) => eprintln!("{}", err.render(source)),
}
}
fn is_incomplete(source: &str) -> bool {
let mut depth: i32 = 0;
let mut chars = source.chars().peekable();
while let Some(c) = chars.next() {
match c {
'/' if chars.peek() == Some(&'/') => {
for skipped in chars.by_ref() {
if skipped == '\n' {
break;
}
}
}
'"' => {
while let Some(string_char) = chars.next() {
match string_char {
'\\' => {
chars.next();
}
'"' => break,
_ => {}
}
}
}
'(' | '[' | '{' => depth += 1,
')' | ']' | '}' => depth -= 1,
_ => {}
}
}
depth > 0
}
#[cfg(test)]
mod tests {
use super::is_incomplete;
#[test]
fn complete_input_is_not_incomplete() {
assert!(!is_incomplete("let x = 1\n"));
assert!(!is_incomplete("print(1)\n"));
}
#[test]
fn unclosed_brackets_are_incomplete() {
assert!(is_incomplete("fn f() {\n"));
assert!(is_incomplete("[1, 2,\n"));
}
#[test]
fn brackets_in_strings_and_comments_do_not_count() {
assert!(!is_incomplete("let s = \"{[(\"\n"));
assert!(!is_incomplete("print(1) // a ( comment\n"));
}
}