Add: finish() to Runnable

This commit is contained in:
Shunsuke Shibayama 2022-08-12 12:58:41 +09:00
parent 2c24f4377a
commit f701fc5194
5 changed files with 27 additions and 0 deletions

View file

@ -54,6 +54,22 @@ impl Runnable for DummyVM {
#[inline]
fn start_message(&self) -> String { format!("Erg interpreter {} {}\n", SEMVER, &*BUILD_INFO) }
fn finish(&mut self) {
self.stream.write("exit".as_bytes()).unwrap();
let mut buf = [0; 1024];
match self.stream.read(&mut buf) {
Result::Ok(n) => {
let s = std::str::from_utf8(&buf[..n]).unwrap();
if s.contains("closed") {
println!("The REPL server is closed.");
}
}
Result::Err(e) => {
panic!("{}", format!("Read error: {e}"));
}
}
}
fn clear(&mut self) {
self.compiler.clear();
}

View file

@ -216,6 +216,7 @@ pub trait Runnable: Sized {
fn new(cfg: ErgConfig) -> Self;
fn input(&self) -> &Input;
fn start_message(&self) -> String;
fn finish(&mut self); // called when the :exit command is received.
fn clear(&mut self);
fn eval(&mut self, src: Str) -> Result<String, Self::Errs>;
@ -248,6 +249,7 @@ pub trait Runnable: Sized {
loop {
let line = chomp(&instance.input().read());
if &line[..] == ":quit" || &line[..] == ":exit" {
instance.finish();
log!(f output, "{GREEN}[DEBUG] The REPL has finished successfully.{RESET}\n");
process::exit(0);
}

View file

@ -91,6 +91,9 @@ impl Runnable for Compiler {
#[inline]
fn start_message(&self) -> String { format!("Erg compiler {} {}\n", SEMVER, &*BUILD_INFO) }
#[inline]
fn finish(&mut self) {}
fn clear(&mut self) {
self.code_generator.clear();
}

View file

@ -28,6 +28,9 @@ impl Runnable for LexerRunner {
#[inline]
fn start_message(&self) -> String { "Erg lexer\n".to_string() }
#[inline]
fn finish(&mut self) {}
#[inline]
fn clear(&mut self) {}

View file

@ -253,6 +253,9 @@ impl Runnable for ParserRunner {
#[inline]
fn start_message(&self) -> String { format!("Erg parser {} {}\n", SEMVER, &*BUILD_INFO) }
#[inline]
fn finish(&mut self) {}
#[inline]
fn clear(&mut self) {}