From c37392046ace645026536178fc761dbc0bab6ae6 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Thu, 11 Jun 2026 16:05:45 +0200 Subject: [PATCH] Working explicit module system --- crates/analyzer/src/transpiler.rs | 13 ++++-- crates/mist-api/src/transpiler.mist | 66 +++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/crates/analyzer/src/transpiler.rs b/crates/analyzer/src/transpiler.rs index 3c4a431..347267a 100644 --- a/crates/analyzer/src/transpiler.rs +++ b/crates/analyzer/src/transpiler.rs @@ -3,7 +3,10 @@ use std::{ path::{Path, PathBuf}, }; -use mist_parser::{ast::TopLevel, error::ParseError}; +use mist_parser::{ + ast::{Identifier, TopLevel, Visibility}, + error::ParseError, +}; pub fn build(root: &PathBuf) { let src_dir = root.join("src"); @@ -112,7 +115,7 @@ pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> { let source = fs::read_to_string(path) .map_err(|e| format!("failed to read file {}: {}", path.display(), e))?; - let parser_result = mist_parser::parse(&source, true).map_err(|e| match e { + let parser_result = mist_parser::parse(&source).map_err(|e| match e { ParseError::Ast(e) => { // Using components carefully to prevent out-of-bounds or zero layout crashes let start_pos = e.span.start_pos().line_col(); @@ -146,10 +149,12 @@ pub fn transpile_file(path: &Path, output_path: &Path) -> Result<(), String> { Ok(()) } -pub fn transpile_text<'a>(source: &'a str) -> Result>> { +pub fn transpile_text<'a>( + source: &'a str, +) -> Result, Vec)>> { let mut gc = mist_codegen::RustCodegen::new(); - Ok(gc.generate(mist_parser::parse(&source, true)?.1)) + Ok(gc.generate(mist_parser::parse(&source)?.1)) } fn should_skip(source: &Path, output: &Path) -> bool { diff --git a/crates/mist-api/src/transpiler.mist b/crates/mist-api/src/transpiler.mist index 90d57c8..ffa8355 100644 --- a/crates/mist-api/src/transpiler.mist +++ b/crates/mist-api/src/transpiler.mist @@ -6,6 +6,7 @@ use std::env; use mist_parser::error; use crate::modules; +use mist_codegen::GetRust; pub fn transpile() PathBuf { let start = time::Instant::now(); @@ -68,16 +69,16 @@ impl modules::Module { process::exit(1); } - } else { - transpile_file(&self.path, &output_file, &output); - } - format!("pub mod {};\n", self.name) + format!("pub mod {};\n", self.name) + } else { + transpile_file(&self.path, output_file, &output) + } } } -fn transpile_file(path *PathBuf, output_file *PathBuf, mod_decl *str) { - let source = match (fs::read_to_string(path)) { +fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String { + let source = match (fs::read_to_string(&path)) { Ok[s] => s; Err[e] => { eprintln!("error: failed to read file {}\n {}", path.display(), e); @@ -88,9 +89,7 @@ fn transpile_file(path *PathBuf, output_file *PathBuf, mod_decl *str) { let mut gc = mist_codegen::RustCodegen::new(); - let is_master = path.file_name().unwrap().to_str().unwrap() == "main.mist"; - - let [[vis, name], ast] = match (mist_parser::parse(&source, is_master)) { + let parsed = match (mist_parser::parse(&source)) { Ok[ast] => ast; Err[e] => { match (e) { @@ -122,18 +121,49 @@ fn transpile_file(path *PathBuf, output_file *PathBuf, mod_decl *str) { } }; - let output = gc.generate(ast); + if (let [Some[[vis, name]], ast] = parsed) { + let output = gc.generate(ast); - let res = fs::write(&output_file, format!("{mod_decl}{output}")); + if (let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned())) { + let mut new_name = std::ffi::OsString::from(&name.0); - if (res.is_err()) { - eprintln!( - "error: failed to write output {}\n {}", - output_file.display(), - res.unwrap_err(), - ); + new_name.push("."); + new_name.push(ext); - process::exit(1); + output_file.set_file_name(new_name); + } else { + output_file.set_file_name(&name.0); + } + + let res = fs::write(&output_file, format!("{mod_decl}{output}")); + + if (res.is_err()) { + eprintln!( + "error: failed to write output {}\n {}", + output_file.display(), + res.unwrap_err(), + ); + + process::exit(1); + } + + format!("{}mod {};", vis.get_rust(), name.get_rust()) + } else { + let output = gc.generate(parsed.1); + + let res = fs::write(&output_file, format!("{mod_decl}{output}")); + + if (res.is_err()) { + eprintln!( + "error: failed to write output {}\n {}", + output_file.display(), + res.unwrap_err(), + ); + + process::exit(1); + } + + String::new() } }