mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 12:24:45 +00:00
Add: finish() to Runnable
This commit is contained in:
parent
2c24f4377a
commit
f701fc5194
5 changed files with 27 additions and 0 deletions
16
src/dummy.rs
16
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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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) {}
|
||||
|
||||
|
|
|
@ -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) {}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue