diff --git a/compiler/erg_common/config.rs b/compiler/erg_common/config.rs index ebe08279..104bb1a5 100644 --- a/compiler/erg_common/config.rs +++ b/compiler/erg_common/config.rs @@ -132,6 +132,9 @@ pub struct ErgConfig { /// * 1: display errors and warns /// * 2 (default): display errors, warnings and hints pub verbose: u8, + /// needed for `jupyter-erg` + pub ps1: &'static str, + pub ps2: &'static str, } impl Default for ErgConfig { @@ -155,6 +158,8 @@ impl Default for ErgConfig { input, module: "", verbose: 2, + ps1: ">>> ", + ps2: "... ", } } } @@ -203,6 +208,12 @@ impl ErgConfig { "--mode" => { cfg.mode = Box::leak(args.next().unwrap().into_boxed_str()); } + "--ps1" => { + cfg.ps1 = Box::leak(args.next().unwrap().into_boxed_str()); + } + "--ps2" => { + cfg.ps2 = Box::leak(args.next().unwrap().into_boxed_str()); + } "-o" | "--opt-level" | "--optimization-level" => { cfg.opt_level = args.next().unwrap().parse::().unwrap(); } diff --git a/compiler/erg_common/traits.rs b/compiler/erg_common/traits.rs index 5ff5d6b1..b3c40f05 100644 --- a/compiler/erg_common/traits.rs +++ b/compiler/erg_common/traits.rs @@ -324,23 +324,26 @@ pub trait Runnable: Sized { type Errs: MultiErrorDisplay; const NAME: &'static str; fn new(cfg: ErgConfig) -> Self; - fn input(&self) -> &Input; + fn cfg(&self) -> &ErgConfig; + fn finish(&mut self); // called when the :exit command is received. + fn clear(&mut self); + fn eval(&mut self, src: String) -> Result; + fn exec(&mut self) -> Result<(), Self::Errs>; + + fn input(&self) -> &Input { + &self.cfg().input + } fn start_message(&self) -> String { format!( "{} {SEMVER} (tags/?:{GIT_HASH_SHORT}, {BUILD_DATE}) on {ARCH}/{OS}\n", Self::NAME ) } - fn finish(&mut self); // called when the :exit command is received. - fn clear(&mut self); - fn eval(&mut self, src: String) -> Result; - fn exec(&mut self) -> Result<(), Self::Errs>; - fn ps1(&self) -> String { - ">>> ".to_string() - } // TODO: &str (VMのせいで参照をとれない) + self.cfg().ps1.to_string() + } fn ps2(&self) -> String { - "... ".to_string() + self.cfg().ps2.to_string() } #[inline] diff --git a/compiler/erg_compiler/compile.rs b/compiler/erg_compiler/compile.rs index 86d22b1f..b92a04e5 100644 --- a/compiler/erg_compiler/compile.rs +++ b/compiler/erg_compiler/compile.rs @@ -116,8 +116,8 @@ impl Runnable for Compiler { } #[inline] - fn input(&self) -> &Input { - &self.cfg.input + fn cfg(&self) -> &ErgConfig { + &self.cfg } #[inline] diff --git a/compiler/erg_compiler/lower.rs b/compiler/erg_compiler/lower.rs index 391c3124..164c20c1 100644 --- a/compiler/erg_compiler/lower.rs +++ b/compiler/erg_compiler/lower.rs @@ -41,6 +41,11 @@ impl Runnable for ASTLowererRunner { type Errs = CompileErrors; const NAME: &'static str = "Erg lowerer"; + #[inline] + fn cfg(&self) -> &ErgConfig { + &self.cfg + } + fn new(cfg: ErgConfig) -> Self { Self { cfg, @@ -48,11 +53,6 @@ impl Runnable for ASTLowererRunner { } } - #[inline] - fn input(&self) -> &Input { - &self.cfg.input - } - #[inline] fn finish(&mut self) {} diff --git a/compiler/erg_parser/lex.rs b/compiler/erg_parser/lex.rs index 440bb2f9..dfce853e 100644 --- a/compiler/erg_parser/lex.rs +++ b/compiler/erg_parser/lex.rs @@ -27,8 +27,8 @@ impl Runnable for LexerRunner { } #[inline] - fn input(&self) -> &Input { - &self.cfg.input + fn cfg(&self) -> &ErgConfig { + &self.cfg } #[inline] diff --git a/compiler/erg_parser/parse.rs b/compiler/erg_parser/parse.rs index 7adcb024..08cea8f8 100644 --- a/compiler/erg_parser/parse.rs +++ b/compiler/erg_parser/parse.rs @@ -178,8 +178,8 @@ impl Runnable for ParserRunner { } #[inline] - fn input(&self) -> &Input { - &self.cfg.input + fn cfg(&self) -> &ErgConfig { + &self.cfg } #[inline] diff --git a/compiler/erg_parser/tests/parse_test.rs b/compiler/erg_parser/tests/parse_test.rs index c22ebc47..49b6c5a0 100644 --- a/compiler/erg_parser/tests/parse_test.rs +++ b/compiler/erg_parser/tests/parse_test.rs @@ -53,6 +53,8 @@ fn parse_test_from_code(file_path: &'static str) -> Result<(), ParserRunnerError input: input.clone(), module: "", verbose: 2, + ps1: ">>> ", + ps2: "... ", }; let lexer = Lexer::new(input.clone()); let mut parser = ParserRunner::new(cfg); diff --git a/src/dummy.rs b/src/dummy.rs index bac2cb1f..e5c272ff 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -4,7 +4,7 @@ use std::net::{Ipv4Addr, SocketAddrV4, TcpListener, TcpStream}; use std::thread::sleep; use std::time::Duration; -use erg_common::config::{ErgConfig, Input}; +use erg_common::config::ErgConfig; use erg_common::python_util::{exec_py, exec_pyc}; use erg_common::traits::Runnable; @@ -29,6 +29,11 @@ impl Runnable for DummyVM { type Errs = EvalErrors; const NAME: &'static str = "Erg interpreter"; + #[inline] + fn cfg(&self) -> &ErgConfig { + &self.cfg + } + fn new(cfg: ErgConfig) -> Self { let stream = if cfg.input.is_repl() { if !cfg.quiet_startup { @@ -69,11 +74,6 @@ impl Runnable for DummyVM { } } - #[inline] - fn input(&self) -> &Input { - &self.cfg.input - } - fn finish(&mut self) { if let Some(stream) = &mut self.stream { stream.write_all("exit".as_bytes()).unwrap();