Working explicit module system

This commit is contained in:
2026-06-11 16:05:45 +02:00
parent 2256ef2a9f
commit c37392046a
2 changed files with 57 additions and 22 deletions
+9 -4
View File
@@ -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<String, ParseError<'a, Vec<TopLevel>>> {
pub fn transpile_text<'a>(
source: &'a str,
) -> Result<String, ParseError<'a, (Option<(Visibility, Identifier)>, Vec<TopLevel>)>> {
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 {
+38 -8
View File
@@ -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)
} 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,8 +121,20 @@ fn transpile_file(path *PathBuf, output_file *PathBuf, mod_decl *str) {
}
};
if (let [Some[[vis, name]], ast] = parsed) {
let output = gc.generate(ast);
if (let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned())) {
let mut new_name = std::ffi::OsString::from(&name.0);
new_name.push(".");
new_name.push(ext);
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()) {
@@ -135,5 +146,24 @@ fn transpile_file(path *PathBuf, output_file *PathBuf, mod_decl *str) {
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()
}
}