typescript 0.0.4

TypeScript compiler and runtime
Documentation
//! 主编译器模块
//!
//! 整合所有编译阶段,实现完整的编译流程,支持增量编译和并行编译。

use crate::compiler::{
    CompilationResult, CompilationStage, CompilationTask, CompilationUnit, Compiler, cache::CompilationCache,
    codegen::CodeGenerator, context::CompilationContext, executor::Executor, ir_generator::IRGenerator, optimizer::Optimizer,
    parallel::ParallelCompiler, semantic::SemanticAnalyzer,
};
use std::{collections::HashSet, path::PathBuf, sync::Arc};
use typescript_types::{TsError, TsValue};

/// 主编译器
#[derive(Debug, Clone)]
pub struct MainCompiler {
    /// 语义分析器
    semantic_analyzer: SemanticAnalyzer,
    /// 中间表示生成器
    ir_generator: IRGenerator,
    /// 优化器
    optimizer: Optimizer,
    /// 代码生成器
    code_generator: CodeGenerator,
    /// 执行器
    executor: Executor,
    /// 编译上下文
    context: CompilationContext,
    /// 编译缓存
    cache: CompilationCache,
    /// 当前编译阶段
    current_stage: CompilationStage,
}

impl MainCompiler {
    /// 创建新的主编译器
    pub fn new(context: CompilationContext, cache: CompilationCache) -> Self {
        Self {
            semantic_analyzer: SemanticAnalyzer::new(context.clone()),
            ir_generator: IRGenerator::new(),
            optimizer: Optimizer::new(),
            code_generator: CodeGenerator::new(),
            executor: Executor::new(),
            context,
            cache,
            current_stage: CompilationStage::LexicalAnalysis,
        }
    }

    /// 编译单个文件
    pub fn compile_file(&mut self, path: &PathBuf) -> CompilationResult<TsValue> {
        // 读取文件内容
        let source_content = match std::fs::read_to_string(path) {
            Ok(content) => content,
            Err(error) => return CompilationResult::Error(TsError::Other(error.to_string())),
        };

        // 创建编译单元
        let unit = CompilationUnit::with_path(path.to_string_lossy().to_string(), source_content);

        // 执行编译
        self.compile_unit(&unit)
    }

    /// 编译编译单元
    pub fn compile_unit(&mut self, unit: &CompilationUnit) -> CompilationResult<TsValue> {
        // 计算源内容的哈希值
        let hash = CompilationCache::compute_hash(&unit.source_content);

        // 检查缓存是否有效
        if self.context.options.incremental {
            if self.cache.is_cache_valid(&hash, &unit.dependencies.iter().cloned().collect()) {
                // 从缓存中获取结果
                if let Some((_, item)) = self.cache.get_cache(&hash) {
                    if let crate::compiler::cache::CacheItem::ExecutionResult(value) = item {
                        return CompilationResult::Success(*value.clone());
                    }
                }
            }
        }

        // 执行编译过程
        self.current_stage = CompilationStage::LexicalAnalysis;

        // 1. 语义分析
        self.current_stage = CompilationStage::SemanticAnalysis;
        match self.semantic_analyzer.analyze(&unit.source_content) {
            CompilationResult::Success(_) => {}
            CompilationResult::Error(error) => return CompilationResult::Error(error),
            _ => return CompilationResult::Error(TsError::Other("Semantic analysis failed".to_string())),
        }

        // 2. IR 生成
        self.current_stage = CompilationStage::IRGeneration;
        let ir = self.ir_generator.generate(&unit.source_content);

        match ir {
            CompilationResult::Success(program) => {
                // 3. 优化
                self.current_stage = CompilationStage::Optimization;
                let optimized_ir = self.optimizer.optimize(&program);

                match optimized_ir {
                    CompilationResult::Success(optimized_program) => {
                        // 4. 代码生成
                        self.current_stage = CompilationStage::CodeGeneration;
                        let typed_program = typescript_ir::TypedProgram::from_program(&optimized_program);
                        let instructions = self.code_generator.generate(&typed_program);

                        // 5. 执行
                        self.current_stage = CompilationStage::Execution;
                        let result = self.executor.execute(&optimized_program);

                        // 存储缓存
                        if self.context.options.incremental {
                            if let CompilationResult::Success(ref result) = result {
                                self.cache.store_cache(
                                    &hash,
                                    crate::compiler::cache::CacheItem::ExecutionResult(Box::new(result.clone())),
                                    unit.dependencies.iter().cloned().collect(),
                                    CompilationStage::Execution,
                                );
                            }
                        }

                        result
                    }
                    CompilationResult::Error(error) => CompilationResult::Error(error),
                    _ => CompilationResult::Error(TsError::Other("Optimization failed".to_string())),
                }
            }
            CompilationResult::Error(error) => CompilationResult::Error(error),
            _ => CompilationResult::Error(TsError::Other("IR generation failed".to_string())),
        }
    }

    /// 并行编译多个文件
    pub fn compile_files_parallel(&mut self, paths: &[PathBuf]) -> Vec<(String, CompilationResult<TsValue>)> {
        // 暂时使用串行编译,避免 Send + Sync 错误
        paths
            .iter()
            .map(|path| {
                let result = self.compile_file(path);
                (path.to_string_lossy().to_string(), result)
            })
            .collect()
    }

    /// 获取编译上下文
    pub fn context(&self) -> &CompilationContext {
        &self.context
    }

    /// 获取可变的编译上下文
    pub fn context_mut(&mut self) -> &mut CompilationContext {
        &mut self.context
    }

    /// 获取编译缓存
    pub fn cache(&self) -> &CompilationCache {
        &self.cache
    }

    /// 获取可变的编译缓存
    pub fn cache_mut(&mut self) -> &mut CompilationCache {
        &mut self.cache
    }
}

impl Compiler for MainCompiler {
    /// 编译方法
    fn compile(&mut self, input: &str) -> CompilationResult<TsValue> {
        let unit = CompilationUnit::new(input.to_string());
        self.compile_unit(&unit)
    }

    /// 重置编译器状态
    fn reset(&mut self) {
        self.semantic_analyzer.reset();
        self.ir_generator.reset();
        self.optimizer.reset();
        self.code_generator.reset();
        self.executor.reset();
        self.context = CompilationContext::default();
        self.current_stage = CompilationStage::LexicalAnalysis;
    }

    /// 获取当前编译阶段
    fn current_stage(&self) -> CompilationStage {
        self.current_stage
    }
}

impl Default for MainCompiler {
    fn default() -> Self {
        Self::new(CompilationContext::default(), CompilationCache::default())
    }
}