Skip to main content

runec/
lib.rs

1pub mod bytecode;
2pub mod compiler;
3pub mod diagnostic_messages;
4pub mod diagnostics;
5pub mod error;
6pub mod lexer;
7pub mod lints;
8pub mod parser;
9pub mod pointer;
10pub mod pointer_checker;
11pub mod symbol;
12pub mod symloader;
13pub mod token;
14pub mod trigger_table;
15pub mod typechecker;
16pub mod types;
17pub mod writer;
18
19use crate::bytecode::CompiledScript;
20use crate::compiler::Compiler;
21use crate::diagnostics::DiagnosticsCollector;
22use crate::lexer::Lexer;
23use crate::parser::{Parser, ScriptFile};
24use crate::symbol::SymbolRegistry;
25use crate::typechecker::TypeChecker;
26use crate::writer::ScriptWriter;
27use std::collections::HashMap;
28use std::error::Error;
29use std::fs;
30use std::path::{Path, PathBuf};
31use std::rc::Rc;
32use tracing::info;
33
34struct ParsedFile {
35    path: PathBuf,
36    file: ScriptFile,
37    testscript_line: Option<usize>,
38}
39
40/// Compile scripts and write output. When `lint` is true, also run lint passes
41/// (unused locals, unreachable code) after code generation.
42pub fn compile_memory(
43    scripts_dir: &Path,
44    pack_dir: Option<&Path>,
45    lint: bool,
46) -> Result<(Vec<u8>, Vec<u8>), Box<dyn Error>> {
47    compile_memory_with_tests(scripts_dir, pack_dir, lint, false)
48}
49
50/// Like `compile_memory` but includes `#testscript` sections when
51/// `include_tests` is true.
52pub fn compile_memory_with_tests(
53    scripts_dir: &Path,
54    pack_dir: Option<&Path>,
55    lint: bool,
56    include_tests: bool,
57) -> Result<(Vec<u8>, Vec<u8>), Box<dyn Error>> {
58    let (compiled_scripts, diagnostics) = run_pipeline(scripts_dir, pack_dir, lint, include_tests)?;
59
60    info!("Phase 5: Writing output to memory...");
61    let writer = ScriptWriter::new("".into());
62
63    diagnostics.print_all();
64    info!("Compilation complete!");
65
66    Ok(writer.build_all(&compiled_scripts)?)
67}
68
69/// Compile scripts and write output. When `lint` is true, also run lint passes
70/// (unused locals, unreachable code) after code generation.
71pub fn compile(
72    scripts_dir: &Path,
73    pack_dir: Option<&Path>,
74    output_dir: &Path,
75    lint: bool,
76) -> Result<(), Box<dyn Error>> {
77    let (compiled_scripts, diagnostics) = run_pipeline(scripts_dir, pack_dir, lint, false)?;
78
79    let output = output_dir.display().to_string();
80    info!("Phase 5: Writing output to {}...", output);
81    let writer = ScriptWriter::new(output);
82    writer.write_all(&compiled_scripts)?;
83
84    diagnostics.print_all();
85    info!("Compilation complete!");
86
87    Ok(())
88}
89
90/// Run all analysis passes (parse, type-check, codegen, pointer-check, lints)
91/// without writing output. Useful for editor tooling and CI lint gates.
92pub fn lint(scripts_dir: &Path, pack_dir: Option<&Path>) -> Result<(), Box<dyn Error>> {
93    let (_, diagnostics) = run_pipeline(scripts_dir, pack_dir, true, false)?;
94
95    diagnostics.print_all();
96    info!("Compilation complete!");
97
98    Ok(())
99}
100
101/// Runs the full compilation pipeline (parse, register, type-check, codegen,
102/// pointer-check, and optional lint passes), returning the compiled scripts
103/// and accumulated diagnostics for the caller to handle output.
104fn run_pipeline(
105    scripts_dir: &Path,
106    pack_dir: Option<&Path>,
107    lint: bool,
108    include_tests: bool,
109) -> Result<(Vec<CompiledScript>, DiagnosticsCollector), Box<dyn Error>> {
110    if !scripts_dir.exists() || !scripts_dir.is_dir() {
111        return Err(Box::new(error::CompilerError::FileNotFound(
112            scripts_dir.display().to_string(),
113        )));
114    }
115
116    let mut rs2_files: Vec<PathBuf> = Vec::new();
117    collect_files(scripts_dir, "rs2", &mut rs2_files)?;
118    rs2_files.sort();
119    info!("Found {} script files", rs2_files.len());
120
121    let mut diagnostics = DiagnosticsCollector::new();
122
123    // Phase 1: Parse all files
124    info!("Phase 1: Parsing...");
125    let mut all_files: Vec<ParsedFile> = Vec::new();
126    let mut source_cache: HashMap<String, Rc<String>> = HashMap::new();
127    for path in &rs2_files {
128        let raw_source = fs::read_to_string(path)?;
129        let testscript_line = find_testscript_line(&raw_source);
130
131        let source_code = if !include_tests {
132            if let Some(line_no) = testscript_line {
133                truncate_at_line(&raw_source, line_no)
134            } else {
135                raw_source.clone()
136            }
137        } else {
138            strip_testscript_annotation(&raw_source)
139        };
140
141        let canonical = fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
142        let mut key = canonical.to_string_lossy().into_owned();
143        if key.starts_with("\\\\?\\") {
144            key = key[4..].to_string();
145        }
146        key = key.replace("\\Content\\", "\\content\\");
147        source_cache.insert(key, Rc::new(raw_source));
148
149        let tokens = match Lexer::new(&source_code, path).tokenize() {
150            Ok(t) => t,
151            Err(e) => {
152                diagnostics.error(
153                    path.clone(),
154                    e.line,
155                    e.position,
156                    e.message.clone(),
157                    crate::diagnostics::Phase::Lexing,
158                );
159                continue;
160            }
161        };
162        let mut parser = Parser::new(tokens, path);
163        match parser.parse() {
164            Ok(file) => all_files.push(ParsedFile {
165                path: path.clone(),
166                file,
167                testscript_line: if include_tests { testscript_line } else { None },
168            }),
169            Err(e) => {
170                diagnostics.error(
171                    path.clone(),
172                    e.line,
173                    e.position,
174                    e.message.clone(),
175                    crate::diagnostics::Phase::Parsing,
176                );
177            }
178        }
179    }
180
181    if diagnostics.has_errors() {
182        diagnostics.print_all();
183        return Err(Box::new(error::CompilerError::TypeError(
184            "Parsing failed".to_string(),
185        )));
186    }
187
188    // Phase 2: Register all scripts
189    info!("Phase 2: Registering scripts...");
190    let registry = register_scripts(scripts_dir, pack_dir, &mut diagnostics, &mut all_files);
191    info!("  Registered {} scripts", registry.scripts.len());
192
193    for pf in &all_files {
194        for script in &pf.file.scripts {
195            if let Some(msg) =
196                Compiler::validate_trigger_subject(&script.trigger, &script.name, &registry)
197            {
198                diagnostics.warning(
199                    pf.path.clone(),
200                    script.line,
201                    0,
202                    msg,
203                    crate::diagnostics::Phase::SymbolRegistration,
204                );
205            }
206        }
207    }
208
209    // Phase 3: Type checking
210    info!("Phase 3: Type checking {} files...", all_files.len());
211    run_type_checker(&mut diagnostics, &mut all_files, &registry);
212
213    if diagnostics.has_errors() {
214        diagnostics.print_all();
215        return Err(Box::new(error::CompilerError::TypeError(
216            "Type checking failed".to_string(),
217        )));
218    }
219
220    // Phase 4: Code generation
221    info!("Phase 4: Code generation...");
222    let (codegen, mut compiled_scripts) = codegen(&mut all_files, registry);
223    info!("  Generated {} compiled scripts", compiled_scripts.len());
224    compiled_scripts.sort_by_key(|s| s.id);
225
226    // Phase 4.5: Pointer checking & lints
227    info!("Phase 4.5: Pointer checking...");
228    check_pointers(
229        lint,
230        &mut diagnostics,
231        &mut source_cache,
232        &codegen,
233        &compiled_scripts,
234    );
235
236    Ok((compiled_scripts, diagnostics))
237}
238
239fn check_pointers(
240    lint: bool,
241    diagnostics: &mut DiagnosticsCollector,
242    source_cache: &mut HashMap<String, Rc<String>>,
243    codegen: &Compiler,
244    compiled_scripts: &[CompiledScript],
245) {
246    {
247        use crate::pointer_checker::PointerChecker;
248        let mut checker = PointerChecker::new(compiled_scripts, &codegen.registry);
249        checker.set_source_cache(source_cache);
250        let pointer_diags = checker.run();
251        let ptr_warnings = pointer_diags.warning_count();
252        if ptr_warnings > 0 {
253            info!("  Pointer check: {} warning(s)", ptr_warnings);
254        }
255        diagnostics.merge(pointer_diags);
256    }
257
258    // Phase 4.6: Lint passes (optional)
259    if lint {
260        info!("Phase 4.6: Lint checks...");
261        let lint_diags = lints::run_lints(compiled_scripts, Some(source_cache));
262        let lint_warnings = lint_diags.warning_count();
263        if lint_warnings > 0 {
264            info!("  Lints: {} warning(s)", lint_warnings);
265        }
266        diagnostics.merge(lint_diags);
267    }
268}
269
270fn codegen(
271    all_files: &mut [ParsedFile],
272    registry: SymbolRegistry,
273) -> (Compiler, Vec<CompiledScript>) {
274    let mut codegen = Compiler::new(registry);
275    let mut compiled_scripts = Vec::new();
276    for pf in all_files.iter() {
277        for script in &pf.file.scripts {
278            if script.trigger == "command" {
279                continue;
280            }
281            let mut compiled = codegen.compile_script(script);
282            let canonical =
283                fs::canonicalize(pf.path.clone()).unwrap_or_else(|_| pf.path.to_path_buf());
284            let mut source_path = canonical.to_string_lossy().into_owned();
285            if source_path.starts_with("\\\\?\\") {
286                source_path = source_path[4..].to_string();
287            }
288            source_path = source_path.replace("\\Content\\", "\\content\\");
289            compiled.source_path = source_path;
290            compiled_scripts.push(compiled);
291        }
292    }
293    (codegen, compiled_scripts)
294}
295
296fn run_type_checker(
297    diagnostics: &mut DiagnosticsCollector,
298    all_files: &mut [ParsedFile],
299    registry: &SymbolRegistry,
300) {
301    let mut type_checker = TypeChecker::new(registry);
302    for pf in all_files.iter() {
303        type_checker.check_file_with_test_boundary(&pf.file, &pf.path, pf.testscript_line);
304    }
305    diagnostics.merge(type_checker.diagnostics);
306}
307
308fn register_scripts(
309    scripts_dir: &Path,
310    pack_dir: Option<&Path>,
311    diagnostics: &mut DiagnosticsCollector,
312    all_files: &mut [ParsedFile],
313) -> SymbolRegistry {
314    let mut registry = SymbolRegistry::new();
315
316    let resolved_pack_dir = pack_dir
317        .map(PathBuf::from)
318        .filter(|p| p.exists())
319        .or_else(|| {
320            scripts_dir
321                .parent()
322                .map(|p| p.join("pack"))
323                .filter(|p| p.exists())
324        })
325        .or_else(|| {
326            scripts_dir
327                .ancestors()
328                .nth(3)
329                .map(|p| p.join("content/pack"))
330                .filter(|p| p.exists())
331        });
332
333    if let Some(ref pd) = resolved_pack_dir {
334        symloader::generate_script_pack(scripts_dir, pd);
335        info!("  Loading packs from {}", pd.display());
336        symloader::load_packs(&mut registry, pd);
337        info!(
338            "  Loaded {} commands, {} game vars",
339            registry.commands.len(),
340            registry.game_vars.len(),
341        );
342    } else {
343        info!("  No pack directory found - symbol resolution will be limited");
344    }
345
346    symloader::load_constant_files(&mut registry, scripts_dir);
347
348    info!(
349        "  Loaded {} constants from .constant files",
350        registry.constants.len()
351    );
352
353    symloader::load_game_var_types(&mut registry, scripts_dir);
354
355    if let Some(ref pd) = resolved_pack_dir {
356        let dbtable_ids = symloader::load_dbtable_pack(&pd.join("dbtable.pack"));
357        if !dbtable_ids.is_empty() {
358            symloader::load_dbtable_configs(&mut registry, scripts_dir, &dbtable_ids);
359            info!(
360                "  Registered {} dbcolumn compound IDs",
361                registry.dbcolumn_types.len()
362            );
363        }
364    }
365
366    let engine_rs2 = scripts_dir.join("engine.rs2");
367
368    if engine_rs2.exists() {
369        crate::symloader::load_engine_command_params(&mut registry, &engine_rs2);
370    } else {
371        if let Ok(entries) = std::fs::read_dir(scripts_dir) {
372            for entry in entries.flatten() {
373                let candidate = entry.path().join("engine.rs2");
374                if candidate.exists() {
375                    crate::symloader::load_engine_command_params(&mut registry, &candidate);
376                    break;
377                }
378            }
379        }
380    }
381
382    crate::symloader::patch_command_return_types(&mut registry);
383
384    {
385        use crate::diagnostic_messages as msg;
386
387        let mut registered_scripts: std::collections::HashSet<String> =
388            std::collections::HashSet::new();
389
390        for pf in all_files.iter() {
391            for script in &pf.file.scripts {
392                if script.trigger == "command" {
393                    continue;
394                }
395
396                if !trigger_table::is_valid_trigger(&script.trigger) {
397                    diagnostics.warning(
398                        pf.path.clone(),
399                        script.line,
400                        0,
401                        msg::fmt(msg::SCRIPT_TRIGGER_INVALID, &[&script.trigger]),
402                        crate::diagnostics::Phase::SymbolRegistration,
403                    );
404                }
405
406                let key = format!("{}:{}", script.trigger, script.name);
407                if !registered_scripts.insert(key.clone()) {
408                    diagnostics.warning(
409                        pf.path.clone(),
410                        script.line,
411                        0,
412                        msg::fmt(msg::SCRIPT_REDECLARATION, &[&script.trigger, &script.name]),
413                        crate::diagnostics::Phase::SymbolRegistration,
414                    );
415                }
416
417                if !script.return_types.is_empty()
418                    && !trigger_table::allows_returns(&script.trigger)
419                {
420                    diagnostics.warning(
421                        pf.path.clone(),
422                        script.line,
423                        0,
424                        msg::fmt(msg::SCRIPT_TRIGGER_NO_RETURNS, &[&script.trigger]),
425                        crate::diagnostics::Phase::SymbolRegistration,
426                    );
427                }
428
429                let param_types: Vec<crate::types::Type> =
430                    script.params.iter().map(|p| p.param_type).collect();
431                registry.register_script(
432                    script.name.clone(),
433                    script.trigger.clone(),
434                    param_types,
435                    script.return_types.clone(),
436                );
437
438                if let Some(ts_line) = pf.testscript_line
439                    && script.line >= ts_line
440                {
441                    registry.mark_test_script(&script.trigger, &script.name);
442                }
443            }
444        }
445    }
446    registry
447}
448
449fn find_testscript_line(source: &str) -> Option<usize> {
450    for (i, line) in source.lines().enumerate() {
451        if line.trim() == "#testscript" {
452            return Some(i + 1);
453        }
454    }
455    None
456}
457
458fn truncate_at_line(source: &str, line_no: usize) -> String {
459    source
460        .lines()
461        .take(line_no - 1)
462        .collect::<Vec<_>>()
463        .join("\n")
464}
465
466fn strip_testscript_annotation(source: &str) -> String {
467    source
468        .lines()
469        .map(|line| {
470            if line.trim() == "#testscript" {
471                ""
472            } else {
473                line
474            }
475        })
476        .collect::<Vec<_>>()
477        .join("\n")
478}
479
480fn collect_files(dir: &Path, ext: &str, out: &mut Vec<PathBuf>) -> Result<(), Box<dyn Error>> {
481    for entry in std::fs::read_dir(dir)? {
482        let entry = entry?;
483        let path = entry.path();
484        if path.is_dir() {
485            collect_files(&path, ext, out)?;
486        } else if path.extension().and_then(|e| e.to_str()) == Some(ext) {
487            out.push(path);
488        }
489    }
490    Ok(())
491}