Modularized the transpiler

This commit is contained in:
2026-06-15 17:10:15 +02:00
parent 3b6fc03546
commit eea72f5bb3
+15 -8
View File
@@ -184,7 +184,9 @@ fn is_source_newer(source *Path, output *Path) std::io::Result<bool> {
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<mist_parser::ast::TopLevel>>> {
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);
}
}