// Charter Compiler - NAC原生智能合约语言编译器 // 基于NAC UDM v1.0.0统一定义模块 use clap::{Parser, Subcommand}; use std::path::PathBuf; use tracing::{info, error}; use tracing_subscriber; mod lexer; mod parser; mod semantic; mod codegen; mod optimizer; #[derive(Parser)] #[command(name = "charter")] #[command(about = "Charter Language Compiler for NAC Blockchain", long_about = None)] struct Cli { #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { /// 编译Charter源代码到NVM字节码 Compile { /// 输入文件路径 #[arg(short, long)] input: PathBuf, /// 输出文件路径 #[arg(short, long)] output: Option, /// 优化级别 (0-3) #[arg(short = 'O', long, default_value = "2")] optimization: u8, /// 生成调试信息 #[arg(short, long)] _debug: bool, }, /// 检查Charter源代码语法 Check { /// 输入文件路径 #[arg(short, long)] input: PathBuf, }, /// 显示AST(抽象语法树) Ast { /// 输入文件路径 #[arg(short, long)] input: PathBuf, }, /// 显示编译器版本和NAC UDM版本 Version, } fn main() { // 初始化日志系统 tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .init(); let cli = Cli::parse(); match cli.command { Commands::Compile { input, output, optimization, _debug } => { info!("编译文件: {:?}", input); info!("优化级别: {}", optimization); match compile_file(&input, output.as_ref(), optimization, _debug) { Ok(_) => { info!("编译成功!"); } Err(e) => { error!("编译失败: {}", e); std::process::exit(1); } } } Commands::Check { input } => { info!("检查文件: {:?}", input); match check_file(&input) { Ok(_) => { info!("语法检查通过!"); } Err(e) => { error!("语法错误: {}", e); std::process::exit(1); } } } Commands::Ast { input } => { info!("显示AST: {:?}", input); match show_ast(&input) { Ok(_) => {} Err(e) => { error!("解析失败: {}", e); std::process::exit(1); } } } Commands::Version => { println!("Charter Compiler v{}", env!("CARGO_PKG_VERSION")); println!("NAC UDM v1.0.0"); println!("NVM Target: 2.0"); } } } fn compile_file( input: &PathBuf, output: Option<&PathBuf>, optimization: u8, _debug: bool, ) -> anyhow::Result<()> { // 1. 读取源代码 let source_code = std::fs::read_to_string(input)?; // 2. 词法分析 info!("词法分析..."); let tokens = lexer::tokenize(&source_code)?; // 3. 语法分析 info!("语法分析..."); let ast = parser::parse(&tokens)?; // 4. 语义分析 info!("语义分析..."); semantic::analyze(&ast)?; // 5. 代码生成 info!("生成NVM字节码..."); let mut bytecode = codegen::generate(&ast)?; // 6. 优化 if optimization > 0 { info!("优化字节码 (级别: {})...", optimization); bytecode = optimizer::optimize(bytecode, optimization)?; } // 7. 写入输出文件 let output_path = output.cloned().unwrap_or_else(|| { let mut path = input.clone(); path.set_extension("nvm"); path }); std::fs::write(&output_path, bytecode)?; info!("输出文件: {:?}", output_path); Ok(()) } fn check_file(input: &PathBuf) -> anyhow::Result<()> { // 1. 读取源代码 let source_code = std::fs::read_to_string(input)?; // 2. 词法分析 let tokens = lexer::tokenize(&source_code)?; // 3. 语法分析 let ast = parser::parse(&tokens)?; // 4. 语义分析 semantic::analyze(&ast)?; Ok(()) } fn show_ast(input: &PathBuf) -> anyhow::Result<()> { // 1. 读取源代码 let source_code = std::fs::read_to_string(input)?; // 2. 词法分析 let tokens = lexer::tokenize(&source_code)?; // 3. 语法分析 let ast = parser::parse(&tokens)?; // 4. 打印AST println!("{:#?}", ast); Ok(()) }