Merge pull request #57 from mist-go/module-system

Module system
This commit is contained in:
2026-06-01 13:49:21 +02:00
committed by GitHub
3 changed files with 146 additions and 155 deletions
+70 -33
View File
@@ -3,6 +3,7 @@ use <std::path::PathBuf>;
use <std::fs>; use <std::fs>;
use <std::process>; use <std::process>;
#[derive(Debug)]
pub class Module { pub class Module {
pub String name; pub String name;
pub PathBuf path; pub PathBuf path;
@@ -14,55 +15,91 @@ pub class Module {
self.path = mist_path; self.path = mist_path;
if (self.path.is_dir()) { if (self.path.is_dir()) {
var entries = match (fs::read_dir(&self.path)) { PathBuf dir = self.path.clone();
Ok[entries] => entries;
Err[e] => {
eprintln!(
"error: failed to read directory {}\n {}",
self.path.display(),
e
);
// Behavior 2: It's a directory node. Check for gateway package.mist first.
PathBuf pkg_gate = self.path.join("package.mist");
if (pkg_gate.exists()) {
self.path = pkg_gate;
}
self.scan_directory(&dir, &rust_path);
}
}
void scan_directory(self mut*, Path* search_dir, Path* rust_path) {
var entries = match (fs::read_dir(search_dir)) {
Ok[entries] => entries;
Err[e] => {
eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e);
process::exit(1);
}
};
for (entry : entries) {
var entry = match (entry) {
Ok[entry] => entry;
Err[e] => {
eprintln!("error: failed to read directory entry\n {}", e);
process::exit(1); process::exit(1);
} }
}; };
for (entry : entries) { PathBuf child_path = entry.path();
var entry = match (entry) {
Ok[entry] => entry;
Err[e] => {
eprintln!("error: failed to read directory entry\n {}", e);
process::exit(1); if (child_path == self.path) {
} continue;
};
PathBuf child_path = entry.path();
String child_name = child_path.file_stem()
.and_then(|var s| s.to_str())
.expect("Failed to get module name")
.to_string();
PathBuf child_output = rust_path.join(&child_name).with_extension("rs");
if (should_skip(&child_path, &child_output)) {
continue;
}
self.children.push(new Module(child_name, child_path, child_output));
} }
String child_name = child_path.file_stem()
.and_then(|var s| s.to_str())
.expect("Failed to get module name")
.to_string();
PathBuf child_output = rust_path.join(&child_name).with_extension("rs");
if (!child_path.is_dir() && should_skip(&child_path, &child_output)) {
continue;
}
self.children.push(new Module(child_name, child_path, child_output));
}
}
pub PathBuf output_dir(self*, PathBuf* parent_dir) {
if (self.path.is_dir() || Some("package.mist") == self.path.file_name().unwrap().to_str()) {
parent_dir.join(&self.name)
} else {
parent_dir.clone()
}
}
pub PathBuf output_path(self*, PathBuf* parent_dir) {
if (self.path.is_dir() || Some("package.mist") == self.path.file_name().unwrap().to_str()) {
parent_dir.join(&self.name).join("mod.rs")
} else {
parent_dir.join(&self.name).with_extension("rs")
} }
} }
} }
pub Module new_package(PathBuf mist_path, PathBuf rust_path) { pub Module master_package(PathBuf mist_path, PathBuf rust_path) {
String name = mist_path.file_stem() String name = mist_path.file_stem()
.and_then(|var s| s.to_str()) .and_then(|var s| s.to_str())
.expect("Failed to get module name") .expect("Failed to get module name")
.to_string(); .to_string();
new Module(name, mist_path, rust_path) var mut root_mod = new Module(name, mist_path.clone(), rust_path.clone());
if (!mist_path.is_dir()) {
PathBuf parent_dir = mist_path.parent()
.expect("Failed to get parent directory of root file")
.to_path_buf();
root_mod.scan_directory(&parent_dir, &rust_path);
}
root_mod
} }
bool should_skip(Path* source, Path* output) { bool should_skip(Path* source, Path* output) {
+76 -116
View File
@@ -6,6 +6,7 @@ use <std::time>;
use <std::env>; use <std::env>;
use <mist_parser::error>; use <mist_parser::error>;
use <crate::modules>;
pub PathBuf build(bool force) { pub PathBuf build(bool force) {
var start = time::Instant::now(); var start = time::Instant::now();
@@ -15,7 +16,9 @@ pub PathBuf build(bool force) {
PathBuf src = root.join("src"); PathBuf src = root.join("src");
PathBuf out = root.join(".mist/src"); PathBuf out = root.join(".mist/src");
build_dir(&root, &src, &src, &out, force); var master = modules::master_package(src.join("main.mist"), out.join("main.rs"));
master.transpile(&out);
var elapsed = start.elapsed(); var elapsed = start.elapsed();
@@ -27,136 +30,93 @@ pub PathBuf build(bool force) {
root root
} }
void build_dir(Path* root, Path* base_src, Path* current, Path* out, bool force) { impl modules::Module {
var entries = match (fs::read_dir(current)) { String transpile(self*, PathBuf* parent_dir) {
Ok[entries] => entries; PathBuf dir = self.output_dir(&parent_dir);
PathBuf output_file = self.output_path(&parent_dir);
var _ = fs::create_dir_all(&dir);
String mut output = String::new();
for (child : &self.children) {
output.push_str(&child.transpile(&dir));
}
if (self.path.is_dir()) {
var res = fs::write(&output_file, output);
if (res.is_err()) {
eprintln!(
"error: failed to write output {}\n {}",
output_file.display(),
res.unwrap_err(),
);
process::exit(1);
}
} else {
transpile_file(&self.path, &output_file, &output);
}
format!("pub mod {};\n", self.name)
}
}
void transpile_file(PathBuf* path, PathBuf* output_file, str* mod_decl) {
String source = match (fs::read_to_string(path)) {
Ok[s] => s;
Err[e] => { Err[e] => {
eprintln!( eprintln!("error: failed to read file {}\n {}", path.display(), e);
"error: failed to read directory {}\n {}",
current.display(),
e
);
process::exit(1); process::exit(1);
} }
}; };
for (entry : entries) { var mut gc = mist_codegen::RustCodegen::new();
var entry = match (entry) {
Ok[entry] => entry;
Err[e] => {
eprintln!("error: failed to read directory entry\n {}", e);
process::exit(1); String output = gc.generate(match (mist_parser::parse(&source)) {
} Ok[ast] => ast;
}; Err[e] => {
match (e) {
error::ParseError::Ast[e] => {
var start_pos = e.span.start_pos().line_col();
PathBuf path = entry.path(); var span = e.span.as_str();
if (path.is_dir()) { eprintln!(
build_dir(root, base_src, &path, out, force); "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}",
continue; path.as_os_str().display(),
} start_pos.0,
start_pos.1,
e.error_message,
span,
if span.ends_with("\n") { "" } else { "\n" },
"^".repeat(span.trim().len()),
);
Path* relative = path.strip_prefix(base_src).expect("Failed to strip base src from path"); process::exit(1);
}
if (path.extension().and_then(|var e| e.to_str()) != Some("mist")) { error::ParseError::PreAst[e] => {
PathBuf dest = out.join(relative); eprintln!("error: parse failed in {}\n{}", path.display(), e);
Option<Path*> parent = dest.parent(); process::exit(1);
if (parent.is_some()) {
fs::create_dir_all(parent.unwrap()).expect("failed to create output directory");
}
if (!force && should_skip(&path, &dest)) {
continue
}
fs::copy(&path, dest).expect("Failed to copy non-mist file");
continue;
}
PathBuf output_file = out.join(relative).with_extension("rs");
if (!force && should_skip(&path, &output_file)) {
continue;
}
Option<Path*> parent = output_file.parent();
if (parent.is_some()) {
fs::create_dir_all(parent.unwrap()).expect("failed to create output directory");
}
String source = match (fs::read_to_string(&path)) {
Ok[s] => s;
Err[e] => {
eprintln!("error: failed to read file {}\n {}", path.display(), e);
process::exit(1);
}
};
var mut gc = mist_codegen::RustCodegen::new();
String output = gc.generate(match (mist_parser::parse(&source)) {
Ok[ast] => ast;
Err[e] => {
match (e) {
error::ParseError::Ast[e] => {
var start_pos = e.span.start_pos().line_col();
var span = e.span.as_str();
eprintln!(
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}",
path.as_os_str().display(),
start_pos.0,
start_pos.1,
e.error_message,
span,
if span.ends_with("\n") { "" } else { "\n" },
"^".repeat(span.trim().len()),
);
process::exit(1);
}
error::ParseError::PreAst[e] => {
eprintln!("error: parse failed in {}\n{}", path.display(), e);
process::exit(1);
}
} }
} }
});
var res = fs::write(&output_file, output);
if (res.is_err()) {
eprintln!(
"error: failed to write output {}\n {}",
output_file.display(),
res.unwrap_err(),
);
process::exit(1);
} }
});
var 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);
} }
} }
bool should_skip(Path* source, Path* output) {
var src_time = fs::metadata(source)
.ok()
.and_then(|var v| v.modified().ok());
var out_time = fs::metadata(output)
.ok()
.and_then(|var v| v.modified().ok());
out_time
.map(|var out_time| out_time >= src_time.unwrap())
.unwrap_or_default()
}
-6
View File
@@ -1,9 +1,3 @@
pub mod app;
pub mod task;
pub mod plugin;
pub mod logger;
pub mod event;
void main() { void main() {
var mut app_instance = new app::Application("mist-app"); var mut app_instance = new app::Application("mist-app");