diff --git a/src/dummy.rs b/src/dummy.rs index d9d59386..cadb8b0b 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -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(); } diff --git a/src/erg_common/traits.rs b/src/erg_common/traits.rs index 0df0ba25..408cc42b 100644 --- a/src/erg_common/traits.rs +++ b/src/erg_common/traits.rs @@ -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; @@ -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); } diff --git a/src/erg_compiler/compile.rs b/src/erg_compiler/compile.rs index ea5f34ea..5a455cb6 100644 --- a/src/erg_compiler/compile.rs +++ b/src/erg_compiler/compile.rs @@ -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(); } diff --git a/src/erg_compiler/erg_parser/lex.rs b/src/erg_compiler/erg_parser/lex.rs index 60937711..e37f8fcb 100644 --- a/src/erg_compiler/erg_parser/lex.rs +++ b/src/erg_compiler/erg_parser/lex.rs @@ -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) {} diff --git a/src/erg_compiler/erg_parser/parse.rs b/src/erg_compiler/erg_parser/parse.rs index 95a34bc2..4bb2077a 100644 --- a/src/erg_compiler/erg_parser/parse.rs +++ b/src/erg_compiler/erg_parser/parse.rs @@ -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) {}