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::sync::Arc;
32use tracing::info;
33
34struct ParsedFile {
35 path: PathBuf,
36 file: ScriptFile,
37 testscript_line: Option<usize>,
38 source_path: String,
43}
44
45pub(crate) fn parallel_map<T, R, F>(items: &[T], f: F) -> Vec<R>
50where
51 T: Sync,
52 R: Send,
53 F: Fn(&T) -> R + Sync,
54{
55 let threads = std::thread::available_parallelism()
56 .map(|n| n.get())
57 .unwrap_or(1);
58 if threads <= 1 || items.len() <= 1 {
59 return items.iter().map(&f).collect();
60 }
61 let threads = threads.min(items.len());
62 let chunk_size = items.len().div_ceil(threads);
63 let mut out: Vec<R> = Vec::with_capacity(items.len());
64 std::thread::scope(|scope| {
65 let handles: Vec<_> = items
66 .chunks(chunk_size)
67 .map(|chunk| {
68 let f = &f;
69 scope.spawn(move || chunk.iter().map(f).collect::<Vec<R>>())
70 })
71 .collect();
72 for h in handles {
75 out.extend(h.join().expect("worker thread panicked"));
76 }
77 });
78 out
79}
80
81fn parallel_chunks<T, R, F>(items: &[T], f: F) -> Vec<R>
85where
86 T: Sync,
87 R: Send,
88 F: Fn(&[T]) -> R + Sync,
89{
90 let threads = std::thread::available_parallelism()
91 .map(|n| n.get())
92 .unwrap_or(1);
93 if threads <= 1 || items.len() <= 1 {
94 return vec![f(items)];
95 }
96 let threads = threads.min(items.len());
97 let chunk_size = items.len().div_ceil(threads);
98 let mut out: Vec<R> = Vec::new();
99 std::thread::scope(|scope| {
100 let handles: Vec<_> = items
101 .chunks(chunk_size)
102 .map(|chunk| {
103 let f = &f;
104 scope.spawn(move || f(chunk))
105 })
106 .collect();
107 for h in handles {
108 out.push(h.join().expect("worker thread panicked"));
109 }
110 });
111 out
112}
113
114struct FileParseResult {
118 path: PathBuf,
119 read: Result<ParsedSource, std::io::Error>,
120}
121
122struct ParsedSource {
123 cache_key: String,
124 raw_source: String,
125 outcome: ParseOutcome,
126}
127
128enum ParseOutcome {
129 Parsed {
130 file: ScriptFile,
131 testscript_line: Option<usize>,
132 },
133 Error {
134 line: usize,
135 position: usize,
136 message: String,
137 phase: diagnostics::Phase,
138 },
139}
140
141fn parse_one_file(path: &Path, include_tests: bool) -> FileParseResult {
146 let raw_source = match fs::read_to_string(path) {
147 Ok(s) => s,
148 Err(e) => {
149 return FileParseResult {
150 path: path.to_path_buf(),
151 read: Err(e),
152 };
153 }
154 };
155 let testscript_line = find_testscript_line(&raw_source);
156
157 let source_code = if !include_tests {
158 if let Some(line_no) = testscript_line {
159 truncate_at_line(&raw_source, line_no)
160 } else {
161 raw_source.clone()
162 }
163 } else {
164 strip_testscript_annotation(&raw_source)
165 };
166
167 let canonical = fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
168 let mut cache_key = canonical.to_string_lossy().into_owned();
169 if cache_key.starts_with("\\\\?\\") {
170 cache_key = cache_key[4..].to_string();
171 }
172 cache_key = cache_key.replace("\\Content\\", "\\content\\");
173
174 let path_buf = path.to_path_buf();
176 let outcome = match Lexer::new(&source_code, &path_buf).tokenize() {
177 Err(e) => ParseOutcome::Error {
178 line: e.line,
179 position: e.position,
180 message: e.message,
181 phase: diagnostics::Phase::Lexing,
182 },
183 Ok(tokens) => match Parser::new(tokens, &path_buf).parse() {
184 Ok(file) => ParseOutcome::Parsed {
185 file,
186 testscript_line: if include_tests { testscript_line } else { None },
187 },
188 Err(e) => ParseOutcome::Error {
189 line: e.line,
190 position: e.position,
191 message: e.message,
192 phase: diagnostics::Phase::Parsing,
193 },
194 },
195 };
196
197 FileParseResult {
198 path: path_buf,
199 read: Ok(ParsedSource {
200 cache_key,
201 raw_source,
202 outcome,
203 }),
204 }
205}
206
207pub fn compile_memory(
210 scripts_dir: &Path,
211 pack_dir: Option<&Path>,
212 lint: bool,
213) -> Result<(Vec<u8>, Vec<u8>), Box<dyn Error>> {
214 compile_memory_with_tests(scripts_dir, pack_dir, lint, false)
215}
216
217pub fn compile_memory_with_tests(
220 scripts_dir: &Path,
221 pack_dir: Option<&Path>,
222 lint: bool,
223 include_tests: bool,
224) -> Result<(Vec<u8>, Vec<u8>), Box<dyn Error>> {
225 let (compiled_scripts, diagnostics) = run_pipeline(scripts_dir, pack_dir, lint, include_tests)?;
226
227 info!("Writing compiled output to memory...");
228 let writer = ScriptWriter::new("".into());
229
230 diagnostics.print_all();
231 info!("Compilation complete!");
232
233 Ok(writer.build_all(&compiled_scripts)?)
234}
235
236pub fn compile(
239 scripts_dir: &Path,
240 pack_dir: Option<&Path>,
241 output_dir: &Path,
242 lint: bool,
243) -> Result<(), Box<dyn Error>> {
244 let (compiled_scripts, diagnostics) = run_pipeline(scripts_dir, pack_dir, lint, false)?;
245
246 let output = output_dir.display().to_string();
247 info!("Writing output to {}...", output.replace('\\', "/"));
250 let writer = ScriptWriter::new(output);
251 writer.write_all(&compiled_scripts)?;
252
253 diagnostics.print_all();
254 info!("Compilation complete!");
255
256 Ok(())
257}
258
259pub fn lint(scripts_dir: &Path, pack_dir: Option<&Path>) -> Result<(), Box<dyn Error>> {
262 let (_, diagnostics) = run_pipeline(scripts_dir, pack_dir, true, false)?;
263
264 diagnostics.print_all();
265 info!("Compilation complete!");
266
267 Ok(())
268}
269
270fn run_pipeline(
274 scripts_dir: &Path,
275 pack_dir: Option<&Path>,
276 lint: bool,
277 include_tests: bool,
278) -> Result<(Vec<CompiledScript>, DiagnosticsCollector), Box<dyn Error>> {
279 if !scripts_dir.exists() || !scripts_dir.is_dir() {
280 return Err(Box::new(error::CompilerError::FileNotFound(
281 scripts_dir.display().to_string(),
282 )));
283 }
284
285 let mut rs2_files: Vec<PathBuf> = get_rs2_files(scripts_dir, "rs2")?;
286
287 rs2_files.sort();
288 info!("Found {} script files", rs2_files.len());
289
290 let mut diagnostics = DiagnosticsCollector::new();
291
292 info!("Parsing script files into syntax trees...");
296 let parse_results = parallel_map(&rs2_files, |path| parse_one_file(path, include_tests));
297
298 let mut all_files: Vec<ParsedFile> = Vec::new();
299 let mut source_cache: HashMap<String, Arc<String>> = HashMap::new();
300 let mut raw_by_path: HashMap<PathBuf, Arc<String>> = HashMap::new();
303 for result in parse_results {
304 let parsed = match result.read {
305 Ok(p) => p,
306 Err(e) => return Err(Box::new(e)),
309 };
310 let rc = Arc::new(parsed.raw_source);
311 let source_path = parsed.cache_key.clone();
314 source_cache.insert(parsed.cache_key, rc.clone());
315 raw_by_path.insert(result.path.clone(), rc);
316 match parsed.outcome {
317 ParseOutcome::Parsed {
318 file,
319 testscript_line,
320 } => all_files.push(ParsedFile {
321 path: result.path,
322 file,
323 testscript_line,
324 source_path,
325 }),
326 ParseOutcome::Error {
327 line,
328 position,
329 message,
330 phase,
331 } => diagnostics.error(result.path, line, position, message, phase),
332 }
333 }
334
335 if diagnostics.has_errors() {
336 diagnostics.print_all();
337 return Err(Box::new(error::CompilerError::TypeError(
338 "Parsing failed".to_string(),
339 )));
340 }
341
342 info!("Registering scripts and building the symbol registry...");
345 let resolved_pack_dir = resolve_pack_dir(scripts_dir, pack_dir);
346 let mut registry = build_symbol_registry(scripts_dir, resolved_pack_dir.as_deref());
347 register_scripts(
348 &mut registry,
349 scripts_dir,
350 resolved_pack_dir.as_deref(),
351 &mut diagnostics,
352 &all_files,
353 &raw_by_path,
354 );
355 info!(" Registered {} scripts", registry.scripts.len());
356
357 for pf in &all_files {
358 for script in &pf.file.scripts {
359 if let Some(msg) =
360 Compiler::validate_trigger_subject(&script.trigger, &script.name, ®istry)
361 {
362 diagnostics.warning(
363 pf.path.clone(),
364 script.line,
365 0,
366 msg,
367 diagnostics::Phase::SymbolRegistration,
368 );
369 }
370 }
371 }
372
373 info!("Type checking {} files...", all_files.len());
375 run_type_checker(&mut diagnostics, &all_files, ®istry);
376
377 if diagnostics.has_errors() {
378 diagnostics.print_all();
379 return Err(Box::new(error::CompilerError::TypeError(
380 "Type checking failed".to_string(),
381 )));
382 }
383
384 info!("Generating bytecode from type-checked scripts...");
386 let mut compiled_scripts = codegen(&all_files, ®istry);
387 info!(" Generated {} compiled scripts", compiled_scripts.len());
388 compiled_scripts.sort_by_key(|s| s.id);
389
390 info!("Checking pointers in generated code...");
392 check_pointers(
393 lint,
394 &mut diagnostics,
395 &mut source_cache,
396 ®istry,
397 &compiled_scripts,
398 );
399
400 Ok((compiled_scripts, diagnostics))
401}
402
403fn check_pointers(
404 lint: bool,
405 diagnostics: &mut DiagnosticsCollector,
406 source_cache: &mut HashMap<String, Arc<String>>,
407 registry: &SymbolRegistry,
408 compiled_scripts: &[CompiledScript],
409) {
410 {
411 use crate::pointer_checker::PointerChecker;
412 let names = PointerChecker::new(compiled_scripts, registry).script_names();
417 let cache: &HashMap<String, Arc<String>> = source_cache;
418 let chunk_diags = parallel_chunks(&names, |chunk| {
419 let mut checker = PointerChecker::new(compiled_scripts, registry);
420 checker.set_source_cache(cache);
421 checker.validate_names(chunk)
422 });
423 let mut pointer_diags = DiagnosticsCollector::new();
424 for d in chunk_diags {
425 pointer_diags.merge(d);
426 }
427 let ptr_warnings = pointer_diags.warning_count();
428 if ptr_warnings > 0 {
429 info!(" Pointer check: {} warning(s)", ptr_warnings);
430 }
431 diagnostics.merge(pointer_diags);
432 }
433
434 if lint {
436 info!("Running lint passes (unused locals, unreachable code)...");
437 let lint_diags = lints::run_lints(compiled_scripts, Some(source_cache));
438 let lint_warnings = lint_diags.warning_count();
439 if lint_warnings > 0 {
440 info!(" Lints: {} warning(s)", lint_warnings);
441 }
442 diagnostics.merge(lint_diags);
443 }
444}
445
446fn codegen(all_files: &[ParsedFile], registry: &SymbolRegistry) -> Vec<CompiledScript> {
447 parallel_map(all_files, |pf| compile_file(pf, registry))
452 .into_iter()
453 .flatten()
454 .collect()
455}
456
457fn compile_file(pf: &ParsedFile, registry: &SymbolRegistry) -> Vec<CompiledScript> {
461 let mut compiler = Compiler::new(registry);
462
463 let mut compiled_scripts = Vec::new();
464 for script in &pf.file.scripts {
465 if script.trigger == "command" {
466 continue;
467 }
468 let mut compiled = compiler.compile_script(script);
469 compiled.source_path = pf.source_path.clone();
471 compiled_scripts.push(compiled);
472 }
473 compiled_scripts
474}
475
476fn run_type_checker(
477 diagnostics: &mut DiagnosticsCollector,
478 all_files: &[ParsedFile],
479 registry: &SymbolRegistry,
480) {
481 let chunk_diags = parallel_chunks(all_files, |chunk| {
485 let mut type_checker = TypeChecker::new(registry);
486 for pf in chunk {
487 type_checker.check_file_with_test_boundary(&pf.file, &pf.path, pf.testscript_line);
488 }
489 type_checker.diagnostics
490 });
491 for d in chunk_diags {
492 diagnostics.merge(d);
493 }
494}
495
496fn resolve_pack_dir(scripts_dir: &Path, pack_dir: Option<&Path>) -> Option<PathBuf> {
499 pack_dir
500 .map(PathBuf::from)
501 .filter(|p| p.exists())
502 .or_else(|| {
503 scripts_dir
504 .parent()
505 .map(|p| p.join("pack"))
506 .filter(|p| p.exists())
507 })
508 .or_else(|| {
509 scripts_dir
510 .ancestors()
511 .nth(3)
512 .map(|p| p.join("content/pack"))
513 .filter(|p| p.exists())
514 })
515}
516
517fn build_symbol_registry(scripts_dir: &Path, resolved_pack_dir: Option<&Path>) -> SymbolRegistry {
523 let mut registry = SymbolRegistry::new();
524
525 if let Some(pd) = resolved_pack_dir {
526 info!(" Loading packs from {}", pd.display());
527 symloader::load_packs(&mut registry, pd);
528 info!(
529 " Loaded {} commands, {} game vars",
530 registry.commands.len(),
531 registry.game_vars.len(),
532 );
533 } else {
534 info!(" No pack directory found - symbol resolution will be limited");
535 }
536
537 let mut config_files: Vec<PathBuf> = Vec::new();
541 symloader::collect_all_files(scripts_dir, &mut config_files);
542
543 symloader::load_constant_files(&mut registry, &config_files);
544
545 info!(
546 " Loaded {} constants from .constant files",
547 registry.constants.len()
548 );
549
550 symloader::load_game_var_types(&mut registry, &config_files);
551
552 if let Some(pd) = resolved_pack_dir {
553 let dbtable_ids = symloader::load_dbtable_pack(&pd.join("dbtable.pack"));
554 if !dbtable_ids.is_empty() {
555 symloader::load_dbtable_configs(&mut registry, &config_files, &dbtable_ids);
556 info!(
557 " Registered {} dbcolumn compound IDs",
558 registry.dbcolumn_types.len()
559 );
560 }
561 }
562
563 let engine_rs2 = scripts_dir.join("engine.rs2");
564
565 if engine_rs2.exists() {
566 symloader::load_engine_command_params(&mut registry, &engine_rs2);
567 } else {
568 if let Ok(entries) = std::fs::read_dir(scripts_dir) {
569 for entry in entries.flatten() {
570 let candidate = entry.path().join("engine.rs2");
571 if candidate.exists() {
572 crate::symloader::load_engine_command_params(&mut registry, &candidate);
573 break;
574 }
575 }
576 }
577 }
578
579 symloader::patch_command_return_types(&mut registry);
580
581 registry
582}
583
584fn register_scripts(
589 registry: &mut SymbolRegistry,
590 scripts_dir: &Path,
591 resolved_pack_dir: Option<&Path>,
592 diagnostics: &mut DiagnosticsCollector,
593 all_files: &[ParsedFile],
594 raw_by_path: &HashMap<PathBuf, Arc<String>>,
595) {
596 if let Some(pd) = resolved_pack_dir {
597 symloader::generate_script_pack(scripts_dir, pd, raw_by_path);
598 symloader::load_script_ids(registry, pd);
599 }
600
601 {
602 use crate::diagnostic_messages as msg;
603
604 let mut registered_scripts: std::collections::HashSet<String> =
605 std::collections::HashSet::new();
606
607 for pf in all_files.iter() {
608 for script in &pf.file.scripts {
609 if script.trigger == "command" {
610 continue;
611 }
612
613 if !trigger_table::is_valid_trigger(&script.trigger) {
614 diagnostics.warning(
615 pf.path.clone(),
616 script.line,
617 0,
618 msg::fmt(msg::SCRIPT_TRIGGER_INVALID, &[&script.trigger]),
619 crate::diagnostics::Phase::SymbolRegistration,
620 );
621 }
622
623 let key = format!("{}:{}", script.trigger, script.name);
624 if !registered_scripts.insert(key.clone()) {
625 diagnostics.warning(
626 pf.path.clone(),
627 script.line,
628 0,
629 msg::fmt(msg::SCRIPT_REDECLARATION, &[&script.trigger, &script.name]),
630 crate::diagnostics::Phase::SymbolRegistration,
631 );
632 }
633
634 if !script.return_types.is_empty()
635 && !trigger_table::allows_returns(&script.trigger)
636 {
637 diagnostics.warning(
638 pf.path.clone(),
639 script.line,
640 0,
641 msg::fmt(msg::SCRIPT_TRIGGER_NO_RETURNS, &[&script.trigger]),
642 diagnostics::Phase::SymbolRegistration,
643 );
644 }
645
646 let param_types: Vec<types::Type> =
647 script.params.iter().map(|p| p.param_type).collect();
648 registry.register_script(
649 script.name.clone(),
650 script.trigger.clone(),
651 param_types,
652 script.return_types.clone(),
653 );
654
655 if let Some(ts_line) = pf.testscript_line
656 && script.line >= ts_line
657 {
658 registry.mark_test_script(&script.trigger, &script.name);
659 }
660 }
661 }
662 }
663}
664
665fn find_testscript_line(source: &str) -> Option<usize> {
666 for (i, line) in source.lines().enumerate() {
667 if line.trim() == "#testscript" {
668 return Some(i + 1);
669 }
670 }
671 None
672}
673
674fn truncate_at_line(source: &str, line_no: usize) -> String {
675 source
676 .lines()
677 .take(line_no - 1)
678 .collect::<Vec<_>>()
679 .join("\n")
680}
681
682fn strip_testscript_annotation(source: &str) -> String {
683 source
684 .lines()
685 .map(|line| {
686 if line.trim() == "#testscript" {
687 ""
688 } else {
689 line
690 }
691 })
692 .collect::<Vec<_>>()
693 .join("\n")
694}
695
696fn get_rs2_files(dir: &Path, ext: &str) -> Result<Vec<PathBuf>, Box<dyn Error>> {
697 let mut rs2_files: Vec<PathBuf> = Vec::new();
698 search_dir_recursively(dir, ext, &mut rs2_files)?;
699 Ok(rs2_files)
700}
701
702fn search_dir_recursively(
703 dir: &Path,
704 ext: &str,
705 out: &mut Vec<PathBuf>,
706) -> Result<(), Box<dyn Error>> {
707 for entry in fs::read_dir(dir)? {
708 let entry = entry?;
709 let path = entry.path();
710 if symloader::entry_is_dir(&entry, &path) {
711 search_dir_recursively(&path, ext, out)?;
712 } else if path.extension().and_then(|e| e.to_str()) == Some(ext) {
713 out.push(path);
714 }
715 }
716 Ok(())
717}