From eea72f5bb363cd3d0614849a1c2e263be4d6825f Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Mon, 15 Jun 2026 17:10:15 +0200 Subject: [PATCH] Modularized the transpiler --- crates/mist-api/src/transpiler.mist | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/mist-api/src/transpiler.mist b/crates/mist-api/src/transpiler.mist index 42a8302..f2975fe 100644 --- a/crates/mist-api/src/transpiler.mist +++ b/crates/mist-api/src/transpiler.mist @@ -184,7 +184,9 @@ fn is_source_newer(source *Path, output *Path) std::io::Result { Ok(source_time > output_time) } -fn transpile_file(path *PathBuf, source *str, mut output_file PathBuf, mod_decl *str, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) { +pub fn transpile_code<'a>(path *PathBuf, source *'a str, mut output_file PathBuf, mod_decl *str, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) + Result<[PathBuf, mist_codegen::RustCodegen, String], error::ParseError<'a, Vec>> { + if let Some[[_, name]] = &curr_mod_decl { if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") { if let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned()) { @@ -202,8 +204,18 @@ fn transpile_file(path *PathBuf, source *str, mut output_file PathBuf, mod_decl let mut gc = mist_codegen::RustCodegen::new(path.clone()); - let parsed = match mist_parser::parse(source) { - Ok[ast] => ast; + let parsed = mist_parser::parse(source)?; + + let output = gc.generate(parsed); + + gc.mapping.shift_rust(mod_decl.lines().count() as isize, 0); + + Ok((output_file, gc, output)) +} + +pub fn transpile_file(path *PathBuf, source *str, output_file PathBuf, mod_decl *str, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) { + let [output_file, gc, output] = match transpile_code(path, source, output_file, mod_decl, curr_mod_decl) { + Ok[output] => output; Err[e] => { match e { error::ParseError::Ast[e] => { @@ -234,12 +246,8 @@ fn transpile_file(path *PathBuf, source *str, mut output_file PathBuf, mod_decl } }; - let output = gc.generate(parsed); - let res = fs::write(&output_file, format!("{mod_decl}{output}")); - gc.mapping.shift_rust(mod_decl.lines().count() as isize, 0); - let output_file_map = output_file.with_extension("map.json"); let res_map = fs::write(&output_file_map, serde_json::to_string(&gc.mapping).unwrap()); @@ -264,4 +272,3 @@ fn transpile_file(path *PathBuf, source *str, mut output_file PathBuf, mod_decl process::exit(1); } } -