Partially fi x #296

This commit is contained in:
Shunsuke Shibayama 2022-12-17 16:44:51 +09:00
parent 261927e0f9
commit 1cc17bdd5a
3 changed files with 31 additions and 15 deletions

View file

@ -38,7 +38,7 @@ impl Input {
pub fn enclosed_name(&self) -> &str {
match self {
Self::File(filename) => filename.to_str().unwrap_or("???"),
Self::File(filename) => filename.to_str().unwrap_or("_"),
Self::REPL | Self::Pipe(_) => "<stdin>",
Self::Str(_) => "<string>",
Self::Dummy => "<dummy>",
@ -47,7 +47,7 @@ impl Input {
pub fn full_path(&self) -> &str {
match self {
Self::File(filename) => filename.to_str().unwrap_or("???"),
Self::File(filename) => filename.to_str().unwrap_or("_"),
Self::REPL | Self::Pipe(_) => "stdin",
Self::Str(_) => "string",
Self::Dummy => "dummy",
@ -56,10 +56,7 @@ impl Input {
pub fn filename(&self) -> &str {
match self {
Self::File(filename) => filename
.file_name()
.and_then(|f| f.to_str())
.unwrap_or("???"),
Self::File(filename) => filename.file_name().and_then(|f| f.to_str()).unwrap_or("_"),
Self::REPL | Self::Pipe(_) => "stdin",
Self::Str(_) => "string",
Self::Dummy => "dummy",
@ -266,6 +263,32 @@ impl ErgConfig {
}
}
pub fn dump_filename(&self) -> String {
if let Some(output) = &self.output_dir {
format!("{output}/{}", self.input.filename())
} else {
self.input.filename().to_string()
}
}
pub fn dump_pyc_path(&self) -> String {
let dump_path = self.dump_path();
if dump_path.ends_with(".er") {
dump_path.replace(".er", ".pyc")
} else {
dump_path + ".pyc"
}
}
pub fn dump_pyc_filename(&self) -> String {
let dump_filename = self.dump_filename();
if dump_filename.ends_with(".er") {
dump_filename.replace(".er", ".pyc")
} else {
dump_filename + ".pyc"
}
}
pub fn inherit(&self, path: PathBuf) -> Self {
Self {
module: Box::leak(path.to_str().unwrap().to_string().into_boxed_str()),

View file

@ -147,7 +147,7 @@ impl Runnable for Compiler {
}
fn exec(&mut self) -> Result<i32, Self::Errs> {
let path = self.cfg.dump_path().replace(".er", ".pyc");
let path = self.cfg.dump_pyc_path();
let warns = self
.compile_and_dump_as_pyc(path, self.input().read(), "exec")
.map_err(|eart| {

View file

@ -121,14 +121,7 @@ impl Runnable for DummyVM {
fn exec(&mut self) -> Result<i32, Self::Errs> {
// Parallel execution is not possible without dumping with a unique file name.
let filename = self
.cfg()
.input
.full_path()
.split('/')
.last()
.unwrap()
.replace(".er", ".pyc");
let filename = self.cfg().dump_pyc_filename();
let warns = self
.compiler
.compile_and_dump_as_pyc(&filename, self.input().read(), "exec")