This commit is contained in:
Nilton Constantino
2026-02-02 23:06:09 +00:00
parent 6cf1e71b99
commit 45696b99e7
5 changed files with 308 additions and 21 deletions
+42 -15
View File
@@ -17,7 +17,6 @@ use crate::lowering::core_to_vm;
use prometeu_bytecode::{ConstantPoolEntry, DebugInfo, FunctionMeta};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ExportKey {
@@ -61,6 +60,11 @@ pub struct CompiledModule {
#[derive(Debug)]
pub enum CompileError {
Frontend(crate::common::diagnostics::DiagnosticBundle),
DuplicateExport {
symbol: String,
first_dep: String,
second_dep: String,
},
Io(std::io::Error),
Internal(String),
}
@@ -69,6 +73,9 @@ impl std::fmt::Display for CompileError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CompileError::Frontend(d) => write!(f, "Frontend error: {:?}", d),
CompileError::DuplicateExport { symbol, first_dep, second_dep } => {
write!(f, "duplicate export: symbol `{}`\n first defined in dependency `{}`\n again defined in dependency `{}`", symbol, first_dep, second_dep)
}
CompileError::Io(e) => write!(f, "IO error: {}", e),
CompileError::Internal(s) => write!(f, "Internal error: {}", s),
}
@@ -107,7 +114,7 @@ pub fn compile_project(
// 1. Parse all files and group symbols by module
let mut module_symbols_map: HashMap<String, ModuleSymbols> = HashMap::new();
let mut parsed_files: Vec<(String, FileNode, String)> = Vec::new(); // (module_path, ast, file_stem)
let mut parsed_files: Vec<(String, FileNode)> = Vec::new(); // (module_path, ast)
for source_rel in &step.sources {
let source_abs = step.project_dir.join(source_rel);
@@ -120,10 +127,19 @@ pub fn compile_project(
let mut collector = SymbolCollector::new();
let (ts, vs) = collector.collect(&ast)?;
let module_path = source_rel.parent()
.unwrap_or(Path::new(""))
.to_string_lossy()
.replace('\\', "/");
let full_path = source_rel.to_string_lossy().replace('\\', "/");
let logical_module_path = if let Some(stripped) = full_path.strip_prefix("src/main/modules/") {
stripped
} else if let Some(stripped) = full_path.strip_prefix("src/test/modules/") {
stripped
} else {
&full_path
};
let module_path = std::path::Path::new(logical_module_path)
.parent()
.map(|p| p.to_string_lossy().replace('\\', "/"))
.unwrap_or_else(|| "".to_string());
let ms = module_symbols_map.entry(module_path.clone()).or_insert_with(ModuleSymbols::new);
@@ -145,8 +161,7 @@ pub fn compile_project(
}
}
let file_stem = source_abs.file_stem().unwrap().to_string_lossy().to_string();
parsed_files.push((module_path, ast, file_stem));
parsed_files.push((module_path, ast));
}
// 2. Synthesize ModuleSymbols for dependencies
@@ -155,7 +170,7 @@ pub fn compile_project(
if let Some(compiled) = dep_modules.get(project_id) {
for (key, meta) in &compiled.exports {
// Support syntax: "alias/module" and "@alias:module"
let key_module_path = key.module_path.replace("src/main/modules/", "");
let key_module_path = &key.module_path;
let synthetic_paths = [
format!("{}/{}", alias, key_module_path),
format!("@{}:{}", alias, key_module_path),
@@ -185,9 +200,21 @@ pub fn compile_project(
};
if sym.namespace == Namespace::Type {
ms.type_symbols.insert(sym).ok();
if let Err(existing) = ms.type_symbols.insert(sym.clone()) {
return Err(CompileError::DuplicateExport {
symbol: sym.name,
first_dep: existing.origin.unwrap_or_else(|| "unknown".to_string()),
second_dep: sym.origin.unwrap_or_else(|| "unknown".to_string()),
});
}
} else {
ms.value_symbols.insert(sym).ok();
if let Err(existing) = ms.value_symbols.insert(sym.clone()) {
return Err(CompileError::DuplicateExport {
symbol: sym.name,
first_dep: existing.origin.unwrap_or_else(|| "unknown".to_string()),
second_dep: sym.origin.unwrap_or_else(|| "unknown".to_string()),
});
}
}
}
}
@@ -202,7 +229,7 @@ pub fn compile_project(
// We need to collect imported symbols for Lowerer
let mut file_imported_symbols: HashMap<String, ModuleSymbols> = HashMap::new(); // keyed by module_path
for (module_path, ast, _) in &parsed_files {
for (module_path, ast) in &parsed_files {
let ms = module_symbols_map.get(module_path).unwrap();
let mut resolver = Resolver::new(ms, &module_provider);
resolver.resolve(ast)?;
@@ -225,11 +252,11 @@ pub fn compile_project(
field_types: HashMap::new(),
};
for (module_path, ast, file_stem) in &parsed_files {
for (module_path, ast) in &parsed_files {
let ms = module_symbols_map.get(module_path).unwrap();
let imported = file_imported_symbols.get(module_path).unwrap();
let lowerer = Lowerer::new(ms, imported);
let program = lowerer.lower_file(ast, &file_stem)?;
let program = lowerer.lower_file(ast, module_path)?;
// Combine program into combined_program
if combined_program.modules.is_empty() {
@@ -367,7 +394,7 @@ mod tests {
// Vec2 should be exported
let vec2_key = ExportKey {
module_path: "src/main/modules".to_string(),
module_path: "".to_string(),
symbol_name: "Vec2".to_string(),
kind: ExportSurfaceKind::DeclareType,
};