pr 02
This commit is contained in:
@@ -15,6 +15,7 @@ use crate::frontends::pbs::types::PbsType;
|
||||
use crate::lowering::core_to_vm;
|
||||
use crate::semantics::export_surface::ExportSurfaceKind;
|
||||
use prometeu_bytecode::{ConstantPoolEntry, DebugInfo, FunctionMeta};
|
||||
use prometeu_analysis::NameInterner;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
@@ -112,6 +113,7 @@ pub fn compile_project(
|
||||
dep_modules: &HashMap<ProjectId, CompiledModule>,
|
||||
file_manager: &mut FileManager,
|
||||
) -> Result<CompiledModule, CompileError> {
|
||||
let mut interner = NameInterner::new();
|
||||
// 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)> = Vec::new(); // (module_path, ast)
|
||||
@@ -121,10 +123,10 @@ pub fn compile_project(
|
||||
let source_code = std::fs::read_to_string(&source_abs)?;
|
||||
let file_id = file_manager.add(source_abs.clone(), source_code.clone());
|
||||
|
||||
let mut parser = Parser::new(&source_code, file_id);
|
||||
let mut parser = Parser::new(&source_code, file_id, &mut interner);
|
||||
let ast = parser.parse_file()?;
|
||||
|
||||
let mut collector = SymbolCollector::new();
|
||||
let mut collector = SymbolCollector::new(&interner);
|
||||
let (ts, vs) = collector.collect(&ast)?;
|
||||
|
||||
let full_path = source_rel.to_string_lossy().replace('\\', "/");
|
||||
@@ -145,20 +147,30 @@ pub fn compile_project(
|
||||
|
||||
// Merge symbols
|
||||
for sym in ts.symbols.into_values() {
|
||||
if let Err(existing) = ms.type_symbols.insert(sym) {
|
||||
if let Some(existing) = ms.type_symbols.get(sym.name) {
|
||||
return Err(DiagnosticBundle::error(
|
||||
format!("Duplicate type symbol '{}' in module '{}'", existing.name, module_path),
|
||||
format!(
|
||||
"Duplicate type symbol '{}' in module '{}'",
|
||||
interner.resolve(existing.name),
|
||||
module_path
|
||||
),
|
||||
Some(existing.span)
|
||||
).into());
|
||||
}
|
||||
let _ = ms.type_symbols.insert(sym);
|
||||
}
|
||||
for sym in vs.symbols.into_values() {
|
||||
if let Err(existing) = ms.value_symbols.insert(sym) {
|
||||
if let Some(existing) = ms.value_symbols.get(sym.name) {
|
||||
return Err(DiagnosticBundle::error(
|
||||
format!("Duplicate value symbol '{}' in module '{}'", existing.name, module_path),
|
||||
format!(
|
||||
"Duplicate value symbol '{}' in module '{}'",
|
||||
interner.resolve(existing.name),
|
||||
module_path
|
||||
),
|
||||
Some(existing.span)
|
||||
).into());
|
||||
}
|
||||
let _ = ms.value_symbols.insert(sym);
|
||||
}
|
||||
|
||||
parsed_files.push((module_path, ast));
|
||||
@@ -180,7 +192,7 @@ pub fn compile_project(
|
||||
let ms = all_visible_modules.entry(synthetic_module_path.clone()).or_insert_with(ModuleSymbols::new);
|
||||
|
||||
let sym = Symbol {
|
||||
name: key.symbol_name.clone(),
|
||||
name: interner.intern(&key.symbol_name),
|
||||
kind: match key.kind {
|
||||
ExportSurfaceKind::Service => SymbolKind::Service,
|
||||
ExportSurfaceKind::DeclareType => {
|
||||
@@ -200,21 +212,23 @@ pub fn compile_project(
|
||||
};
|
||||
|
||||
if sym.namespace == Namespace::Type {
|
||||
if let Err(existing) = ms.type_symbols.insert(sym.clone()) {
|
||||
if let Some(existing) = ms.type_symbols.get(sym.name) {
|
||||
return Err(CompileError::DuplicateExport {
|
||||
symbol: sym.name,
|
||||
first_dep: existing.origin.unwrap_or_else(|| "unknown".to_string()),
|
||||
symbol: interner.resolve(sym.name).to_string(),
|
||||
first_dep: existing.origin.clone().unwrap_or_else(|| "unknown".to_string()),
|
||||
second_dep: sym.origin.unwrap_or_else(|| "unknown".to_string()),
|
||||
});
|
||||
}
|
||||
let _ = ms.type_symbols.insert(sym.clone());
|
||||
} else {
|
||||
if let Err(existing) = ms.value_symbols.insert(sym.clone()) {
|
||||
if let Some(existing) = ms.value_symbols.get(sym.name) {
|
||||
return Err(CompileError::DuplicateExport {
|
||||
symbol: sym.name,
|
||||
first_dep: existing.origin.unwrap_or_else(|| "unknown".to_string()),
|
||||
symbol: interner.resolve(sym.name).to_string(),
|
||||
first_dep: existing.origin.clone().unwrap_or_else(|| "unknown".to_string()),
|
||||
second_dep: sym.origin.unwrap_or_else(|| "unknown".to_string()),
|
||||
});
|
||||
}
|
||||
let _ = ms.value_symbols.insert(sym.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -231,7 +245,7 @@ pub fn compile_project(
|
||||
|
||||
for (module_path, ast) in &parsed_files {
|
||||
let ms = module_symbols_map.get(module_path).unwrap();
|
||||
let mut resolver = Resolver::new(ms, &module_provider);
|
||||
let mut resolver = Resolver::new(ms, &module_provider, &interner);
|
||||
resolver.resolve(ast)?;
|
||||
|
||||
// Capture imported symbols
|
||||
@@ -240,7 +254,7 @@ pub fn compile_project(
|
||||
// TypeChecker also needs &mut ModuleSymbols
|
||||
let mut ms_mut = module_symbols_map.get_mut(module_path).unwrap();
|
||||
let imported = file_imported_symbols.get(module_path).unwrap();
|
||||
let mut typechecker = TypeChecker::new(&mut ms_mut, imported, &module_provider);
|
||||
let mut typechecker = TypeChecker::new(&mut ms_mut, imported, &module_provider, &interner);
|
||||
typechecker.check(ast)?;
|
||||
}
|
||||
|
||||
@@ -255,7 +269,7 @@ pub fn compile_project(
|
||||
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 lowerer = Lowerer::new(ms, imported, &interner);
|
||||
let program = lowerer.lower_file(ast, module_path)?;
|
||||
|
||||
// Combine program into combined_program
|
||||
@@ -281,7 +295,7 @@ pub fn compile_project(
|
||||
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
|
||||
exports.insert(ExportKey {
|
||||
module_path: module_path.clone(),
|
||||
symbol_name: sym.name.clone(),
|
||||
symbol_name: interner.resolve(sym.name).to_string(),
|
||||
kind: surface_kind,
|
||||
}, ExportMetadata {
|
||||
func_idx: None,
|
||||
@@ -295,11 +309,15 @@ pub fn compile_project(
|
||||
if sym.visibility == Visibility::Pub {
|
||||
if let Some(surface_kind) = ExportSurfaceKind::from_symbol_kind(sym.kind) {
|
||||
// Find func_idx if it's a function or service
|
||||
let func_idx = vm_module.functions.iter().position(|f| f.name == sym.name).map(|i| i as u32);
|
||||
let func_idx = vm_module
|
||||
.functions
|
||||
.iter()
|
||||
.position(|f| f.name == interner.resolve(sym.name))
|
||||
.map(|i| i as u32);
|
||||
|
||||
exports.insert(ExportKey {
|
||||
module_path: module_path.clone(),
|
||||
symbol_name: sym.name.clone(),
|
||||
symbol_name: interner.resolve(sym.name).to_string(),
|
||||
kind: surface_kind,
|
||||
}, ExportMetadata {
|
||||
func_idx,
|
||||
@@ -312,7 +330,12 @@ pub fn compile_project(
|
||||
}
|
||||
|
||||
// 6. Collect symbols
|
||||
let project_symbols = crate::common::symbols::collect_symbols(&step.project_id.name, &module_symbols_map, file_manager);
|
||||
let project_symbols = crate::common::symbols::collect_symbols(
|
||||
&step.project_id.name,
|
||||
&module_symbols_map,
|
||||
file_manager,
|
||||
&interner,
|
||||
);
|
||||
|
||||
// 7. Collect imports from unresolved labels
|
||||
let mut imports = Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user