Skip to main content

mortar_language/
lib.rs

1//! # lib.rs
2//!
3//! # lib.rs 文件
4//!
5//! ## Module Overview
6//!
7//! ## 模块概述
8//!
9//! This crate is the umbrella entry point for the Mortar toolchain. It re-exports compiler and
10//! LSP capabilities and adds a small programmatic CLI facade so external callers can compile Mortar
11//! files without spawning the binary directly.
12//!
13//! 这个 crate 是 Mortar 工具链的总入口。它重新导出编译器和 LSP 能力,并额外提供一个很小的
14//! 编程式 CLI 门面,让外部调用方无需直接启动二进制程序也能编译 Mortar 文件。
15
16// Re-export all functionality from sub-crates
17pub use mortar_compiler::*;
18
19// Re-export CLI functionality for programmatic use
20pub mod cli {
21    // CLI main function is not re-exported as it's meant to be used as binary
22    // But we can provide a programmatic interface
23    use crate::{FileHandler, ParseHandler, Serializer};
24
25    /// Compile a mortar file programmatically
26    pub fn compile_file(input_path: &str, output_path: Option<&str>) -> Result<(), String> {
27        compile_file_with_format(input_path, output_path, false)
28    }
29
30    /// Compile a mortar file programmatically with format option
31    pub fn compile_file_with_format(
32        input_path: &str,
33        output_path: Option<&str>,
34        pretty: bool,
35    ) -> Result<(), String> {
36        // Read source file
37        let content = FileHandler::read_source_file(input_path)
38            .map_err(|err| format!("Failed to read input file: {}", err))?;
39
40        let program = ParseHandler::parse_source_code(&content, false)
41            .map_err(|err| format!("Parse error: {}", err))?;
42
43        // Generate .mortared file
44        let output_path = output_path.unwrap_or(input_path);
45
46        Serializer::save_to_file(&program, output_path, pretty)
47            .map_err(|err| format!("Failed to generate .mortared file: {}", err))?;
48
49        Ok(())
50    }
51}
52
53// Re-export LSP functionality when available
54pub mod lsp {
55    pub use mortar_lsp::*;
56}
57
58// Convenience prelude module
59pub mod prelude {
60    pub use crate::*;
61}