Update ErgConfig and Runnable

This commit is contained in:
Shunsuke Shibayama 2022-09-16 02:21:04 +09:00
parent d7ffb211a8
commit d2ad7caaab
8 changed files with 42 additions and 26 deletions

View file

@ -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: "<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::<u8>().unwrap();
}

View file

@ -324,23 +324,26 @@ pub trait Runnable: Sized {
type Errs: MultiErrorDisplay<Self::Err>;
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<String, Self::Errs>;
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<String, Self::Errs>;
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]

View file

@ -116,8 +116,8 @@ impl Runnable for Compiler {
}
#[inline]
fn input(&self) -> &Input {
&self.cfg.input
fn cfg(&self) -> &ErgConfig {
&self.cfg
}
#[inline]

View file

@ -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) {}

View file

@ -27,8 +27,8 @@ impl Runnable for LexerRunner {
}
#[inline]
fn input(&self) -> &Input {
&self.cfg.input
fn cfg(&self) -> &ErgConfig {
&self.cfg
}
#[inline]

View file

@ -178,8 +178,8 @@ impl Runnable for ParserRunner {
}
#[inline]
fn input(&self) -> &Input {
&self.cfg.input
fn cfg(&self) -> &ErgConfig {
&self.cfg
}
#[inline]

View file

@ -53,6 +53,8 @@ fn parse_test_from_code(file_path: &'static str) -> Result<(), ParserRunnerError
input: input.clone(),
module: "<module>",
verbose: 2,
ps1: ">>> ",
ps2: "... ",
};
let lexer = Lexer::new(input.clone());
let mut parser = ParserRunner::new(cfg);

View file

@ -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();