roblox_rs_core/
compiler.rs1use crate::error::{Error, Result};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum OptimizationLevel {
8 Minimal,
10 Default,
12 Aggressive,
14}
15
16#[derive(Debug, Clone)]
18pub struct CompileOptions {
19 pub include_runtime: bool,
21 pub debug_mode: bool,
23 pub enable_parallelization: bool,
25 pub optimization_level: OptimizationLevel,
27 pub target_dir: Option<String>,
29 pub flags: Vec<String>,
31}
32
33impl Default for CompileOptions {
34 fn default() -> Self {
35 Self {
36 include_runtime: true,
37 debug_mode: false,
38 enable_parallelization: false,
39 optimization_level: OptimizationLevel::Default,
40 target_dir: None,
41 flags: Vec::new(),
42 }
43 }
44}
45
46#[cfg(feature = "ast-parser")]
48pub fn compile(input: &str, options: CompileOptions) -> Result<String> {
49 use crate::ast::parser::parse_rust;
50
51 let ast = parse_rust(input).map_err(|e| Error::Parse(e.to_string()))?;
53
54 #[cfg(feature = "ast-parser")]
56 {
57 let luau_ast = crate::ast::transformer::transform_ast(&ast)?;
58
59 let mut luau_code = crate::luau::generator::generate_code(&luau_ast)
61 .map_err(|e| Error::CodeGen(e.to_string()))?;
62
63 if options.optimization_level != OptimizationLevel::Minimal {
65 luau_code = crate::luau::optimizer::optimize_code(&luau_code, &options)
66 .map_err(|e| Error::Optimization(e.to_string()))?;
67 }
68
69 if options.debug_mode {
71 luau_code = format!("--!optimize 2\n--!native\n\n{}", luau_code);
72 }
73
74 Ok(luau_code)
75 }
76
77 #[cfg(not(feature = "ast-parser"))]
78 {
79 Err(Error::Other("AST parsing feature is not enabled".to_string()))
80 }
81}
82
83#[cfg(not(feature = "ast-parser"))]
84pub fn compile(_input: &str, _options: CompileOptions) -> Result<String> {
85 Err(Error::Other("No compiler implementation enabled. Enable either 'ast-parser' or 'llvm-ir' feature.".to_string()))
86}
87
88pub fn compile_default(input: &str) -> Result<String> {
90 compile(input, CompileOptions::default())
91}