Skip to main content

logicaffeine_compile/
compile.rs

1//! LOGOS Compilation Pipeline
2//!
3//! This module provides the end-to-end compilation pipeline that transforms
4//! LOGOS source code into executable Rust programs.
5//!
6//! # Pipeline Overview
7//!
8//! ```text
9//! LOGOS Source (.md)
10//!       │
11//!       ▼
12//! ┌───────────────────┐
13//! │  1. Lexer         │ Tokenize source
14//! └─────────┬─────────┘
15//!           ▼
16//! ┌───────────────────┐
17//! │  2. Discovery     │ Type & policy definitions
18//! └─────────┬─────────┘
19//!           ▼
20//! ┌───────────────────┐
21//! │  3. Parser        │ Build AST
22//! └─────────┬─────────┘
23//!           ▼
24//! ┌───────────────────┐
25//! │  4. Analysis      │ Escape, ownership, verification
26//! └─────────┬─────────┘
27//!           ▼
28//! ┌───────────────────┐
29//! │  5. CodeGen       │ Emit Rust source
30//! └─────────┬─────────┘
31//!           ▼
32//!     Rust Source
33//! ```
34//!
35//! # Compilation Functions
36//!
37//! | Function | Analysis | Use Case |
38//! |----------|----------|----------|
39//! | [`compile_to_rust`] | Escape only | Basic compilation |
40//! | [`compile_to_rust_checked`] | Escape + Ownership | Use with `--check` flag |
41//! | `compile_to_rust_verified` | All + Z3 | Formal verification (requires `verification` feature) |
42//! | [`compile_project`] | Multi-file | Projects with imports |
43//! | [`compile_and_run`] | Full + Execute | Development workflow |
44//!
45//! # Examples
46//!
47//! ## Basic Compilation
48//!
49//! ```
50//! # use logicaffeine_compile::compile::compile_to_rust;
51//! # use logicaffeine_compile::ParseError;
52//! # fn main() -> Result<(), ParseError> {
53//! let source = "## Main\nLet x be 5.\nShow x.";
54//! let rust_code = compile_to_rust(source)?;
55//! // rust_code contains:
56//! // fn main() {
57//! //     let x = 5;
58//! //     println!("{}", x);
59//! // }
60//! # Ok(())
61//! # }
62//! ```
63//!
64//! ## With Ownership Checking
65//!
66//! ```
67//! # use logicaffeine_compile::compile::compile_to_rust_checked;
68//! let source = "## Main\nLet x be 5.\nGive x to y.\nShow x.";
69//! let result = compile_to_rust_checked(source);
70//! // Returns Err: "x has already been given away"
71//! ```
72
73use std::fs;
74use std::io::Write;
75use std::path::Path;
76use std::process::Command;
77
78// Runtime crates paths (relative to workspace root)
79const CRATES_DATA_PATH: &str = "crates/logicaffeine_data";
80const CRATES_SYSTEM_PATH: &str = "crates/logicaffeine_system";
81
82use std::fmt::Write as FmtWrite;
83
84use crate::analysis::{DiscoveryPass, EscapeChecker, OwnershipChecker, PolicyRegistry};
85use crate::arena::Arena;
86use crate::arena_ctx::AstContext;
87use crate::ast::{Expr, Stmt, TypeExpr};
88use crate::codegen::{codegen_program, generate_c_header, generate_python_bindings, generate_typescript_bindings};
89use crate::diagnostic::{parse_rustc_json, translate_diagnostics, LogosError};
90use crate::drs::WorldState;
91use crate::error::ParseError;
92use crate::intern::Interner;
93use crate::lexer::Lexer;
94use crate::parser::Parser;
95use crate::sourcemap::SourceMap;
96
97/// A declared external crate dependency from a `## Requires` block.
98#[derive(Debug, Clone)]
99pub struct CrateDependency {
100    pub name: String,
101    pub version: String,
102    pub features: Vec<String>,
103}
104
105/// Full compilation output including generated Rust code and extracted dependencies.
106#[derive(Debug)]
107pub struct CompileOutput {
108    pub rust_code: String,
109    pub dependencies: Vec<CrateDependency>,
110    /// Generated C header content (populated when C exports exist).
111    pub c_header: Option<String>,
112    /// Generated Python ctypes bindings (populated when C exports exist).
113    pub python_bindings: Option<String>,
114    /// Generated TypeScript type declarations (.d.ts content, populated when C exports exist).
115    pub typescript_types: Option<String>,
116    /// Generated TypeScript FFI bindings (.js content, populated when C exports exist).
117    pub typescript_bindings: Option<String>,
118}
119
120/// Interpret LOGOS source and return output as a string.
121///
122/// Runs the full pipeline (lex → discovery → parse → interpret) without
123/// generating Rust code. Useful for sub-second feedback during development.
124///
125/// # Arguments
126///
127/// * `source` - LOGOS source code as a string
128///
129/// # Returns
130///
131/// The collected output from `Show` statements, joined by newlines.
132///
133/// # Errors
134///
135/// Returns [`ParseError`] if parsing fails or the interpreter encounters
136/// a runtime error.
137pub fn interpret_program(source: &str) -> Result<String, ParseError> {
138    let result = crate::ui_bridge::interpret_for_ui_sync(source);
139    if let Some(err) = result.error {
140        Err(ParseError {
141            kind: crate::error::ParseErrorKind::Custom(err),
142            span: crate::token::Span::default(),
143        })
144    } else {
145        Ok(result.lines.join("\n"))
146    }
147}
148
149/// Compile LOGOS source to Rust source code.
150///
151/// This is the basic compilation function that runs lexing, parsing, and
152/// escape analysis before generating Rust code.
153///
154/// # Arguments
155///
156/// * `source` - LOGOS source code as a string
157///
158/// # Returns
159///
160/// Generated Rust source code on success.
161///
162/// # Errors
163///
164/// Returns [`ParseError`] if:
165/// - Lexical analysis fails (invalid tokens)
166/// - Parsing fails (syntax errors)
167/// - Escape analysis fails (zone-local values escaping)
168///
169/// # Example
170///
171/// ```
172/// # use logicaffeine_compile::compile::compile_to_rust;
173/// # use logicaffeine_compile::ParseError;
174/// # fn main() -> Result<(), ParseError> {
175/// let source = "## Main\nLet x be 5.\nShow x.";
176/// let rust_code = compile_to_rust(source)?;
177/// assert!(rust_code.contains("let x = 5;"));
178/// # Ok(())
179/// # }
180/// ```
181pub fn compile_to_rust(source: &str) -> Result<String, ParseError> {
182    compile_program_full(source).map(|o| o.rust_code)
183}
184
185/// Compile LOGOS source and return full output including dependency metadata.
186///
187/// This is the primary compilation entry point that returns both the generated
188/// Rust code and any crate dependencies declared in `## Requires` blocks.
189pub fn compile_program_full(source: &str) -> Result<CompileOutput, ParseError> {
190    let mut interner = Interner::new();
191    let mut lexer = Lexer::new(source, &mut interner);
192    let tokens = lexer.tokenize();
193
194    // Pass 1: Discovery - scan for type definitions and policies
195    let (type_registry, policy_registry) = {
196        let mut discovery = DiscoveryPass::new(&tokens, &mut interner);
197        let result = discovery.run_full();
198        (result.types, result.policies)
199    };
200    // Clone for codegen (parser takes ownership)
201    let codegen_registry = type_registry.clone();
202    let codegen_policies = policy_registry.clone();
203
204    let mut world_state = WorldState::new();
205    let expr_arena = Arena::new();
206    let term_arena = Arena::new();
207    let np_arena = Arena::new();
208    let sym_arena = Arena::new();
209    let role_arena = Arena::new();
210    let pp_arena = Arena::new();
211    let stmt_arena: Arena<Stmt> = Arena::new();
212    let imperative_expr_arena: Arena<Expr> = Arena::new();
213    let type_expr_arena: Arena<TypeExpr> = Arena::new();
214
215    let ast_ctx = AstContext::with_types(
216        &expr_arena,
217        &term_arena,
218        &np_arena,
219        &sym_arena,
220        &role_arena,
221        &pp_arena,
222        &stmt_arena,
223        &imperative_expr_arena,
224        &type_expr_arena,
225    );
226
227    // Pass 2: Parse with type context
228    let mut parser = Parser::new(tokens, &mut world_state, &mut interner, ast_ctx, type_registry);
229    // Note: Don't call process_block_headers() - parse_program handles blocks itself
230
231    let stmts = parser.parse_program()?;
232
233    // Pass 2.5: Optimization - constant folding and dead code elimination
234    let stmts = crate::optimize::optimize_program(stmts, &imperative_expr_arena, &stmt_arena, &mut interner);
235
236    // Extract dependencies before escape analysis
237    let mut dependencies = extract_dependencies(&stmts, &interner)?;
238
239    // FFI: Auto-inject wasm-bindgen dependency if any function is exported for WASM
240    let needs_wasm_bindgen = stmts.iter().any(|stmt| {
241        if let Stmt::FunctionDef { is_exported: true, export_target: Some(target), .. } = stmt {
242            interner.resolve(*target).eq_ignore_ascii_case("wasm")
243        } else {
244            false
245        }
246    });
247    if needs_wasm_bindgen && !dependencies.iter().any(|d| d.name == "wasm-bindgen") {
248        dependencies.push(CrateDependency {
249            name: "wasm-bindgen".to_string(),
250            version: "0.2".to_string(),
251            features: vec![],
252        });
253    }
254
255    // Pass 3: Escape analysis - check for zone escape violations
256    // This catches obvious cases like returning zone-local variables
257    let mut escape_checker = EscapeChecker::new(&interner);
258    escape_checker.check_program(&stmts).map_err(|e| {
259        // Convert EscapeError to ParseError for now
260        // The error message is already Socratic from EscapeChecker
261        ParseError {
262            kind: crate::error::ParseErrorKind::Custom(e.to_string()),
263            span: e.span,
264        }
265    })?;
266
267    // Note: Static verification is available when the `verification` feature is enabled,
268    // but must be explicitly invoked via compile_to_rust_verified().
269
270    let rust_code = codegen_program(&stmts, &codegen_registry, &codegen_policies, &interner);
271
272    // Universal ABI: Generate C header + bindings if any C exports exist
273    let has_c = stmts.iter().any(|stmt| {
274        if let Stmt::FunctionDef { is_exported: true, export_target, .. } = stmt {
275            match export_target {
276                None => true,
277                Some(t) => interner.resolve(*t).eq_ignore_ascii_case("c"),
278            }
279        } else {
280            false
281        }
282    });
283
284    let c_header = if has_c {
285        Some(generate_c_header(&stmts, "module", &interner, &codegen_registry))
286    } else {
287        None
288    };
289
290    // Auto-inject serde_json dependency when C exports exist (needed for collection to_json and portable struct JSON accessors)
291    if has_c && !dependencies.iter().any(|d| d.name == "serde_json") {
292        dependencies.push(CrateDependency {
293            name: "serde_json".to_string(),
294            version: "1".to_string(),
295            features: vec![],
296        });
297    }
298
299    let python_bindings = if has_c {
300        Some(generate_python_bindings(&stmts, "module", &interner, &codegen_registry))
301    } else {
302        None
303    };
304
305    let (typescript_bindings, typescript_types) = if has_c {
306        let (js, dts) = generate_typescript_bindings(&stmts, "module", &interner, &codegen_registry);
307        (Some(js), Some(dts))
308    } else {
309        (None, None)
310    };
311
312    Ok(CompileOutput { rust_code, dependencies, c_header, python_bindings, typescript_types, typescript_bindings })
313}
314
315/// Extract crate dependencies from `Stmt::Require` nodes.
316///
317/// Deduplicates by crate name: same name + same version keeps one copy.
318/// Same name + different version returns a `ParseError`.
319/// Preserves declaration order (first occurrence wins).
320fn extract_dependencies(stmts: &[Stmt], interner: &Interner) -> Result<Vec<CrateDependency>, ParseError> {
321    use std::collections::HashMap;
322
323    let mut seen: HashMap<String, String> = HashMap::new(); // name → version
324    let mut deps: Vec<CrateDependency> = Vec::new();
325
326    for stmt in stmts {
327        if let Stmt::Require { crate_name, version, features, span } = stmt {
328            let name = interner.resolve(*crate_name).to_string();
329            let ver = interner.resolve(*version).to_string();
330
331            if let Some(existing_ver) = seen.get(&name) {
332                if *existing_ver != ver {
333                    return Err(ParseError {
334                        kind: crate::error::ParseErrorKind::Custom(format!(
335                            "Conflicting versions for crate \"{}\": \"{}\" and \"{}\".",
336                            name, existing_ver, ver
337                        )),
338                        span: *span,
339                    });
340                }
341                // Same name + same version: skip duplicate
342            } else {
343                seen.insert(name.clone(), ver.clone());
344                deps.push(CrateDependency {
345                    name,
346                    version: ver,
347                    features: features.iter().map(|f| interner.resolve(*f).to_string()).collect(),
348                });
349            }
350        }
351    }
352
353    Ok(deps)
354}
355
356/// Compile LOGOS source to Rust with ownership checking enabled.
357///
358/// This runs the lightweight ownership analysis pass that catches use-after-move
359/// errors with control flow awareness. The analysis is fast enough to run on
360/// every keystroke in an IDE.
361///
362/// # Arguments
363///
364/// * `source` - LOGOS source code as a string
365///
366/// # Returns
367///
368/// Generated Rust source code on success.
369///
370/// # Errors
371///
372/// Returns [`ParseError`] if:
373/// - Any error from [`compile_to_rust`] occurs
374/// - Ownership analysis detects use-after-move
375/// - Ownership analysis detects use-after-borrow violations
376///
377/// # Example
378///
379/// ```
380/// # use logicaffeine_compile::compile::compile_to_rust_checked;
381/// // This will fail ownership checking
382/// let source = "## Main\nLet x be 5.\nGive x to y.\nShow x.";
383/// let result = compile_to_rust_checked(source);
384/// assert!(result.is_err()); // "x has already been given away"
385/// ```
386///
387/// # Use Case
388///
389/// Use this function with the `--check` CLI flag for instant feedback on
390/// ownership errors before running the full Rust compilation.
391pub fn compile_to_rust_checked(source: &str) -> Result<String, ParseError> {
392    let mut interner = Interner::new();
393    let mut lexer = Lexer::new(source, &mut interner);
394    let tokens = lexer.tokenize();
395
396    // Pass 1: Discovery - scan for type definitions and policies
397    let (type_registry, policy_registry) = {
398        let mut discovery = DiscoveryPass::new(&tokens, &mut interner);
399        let result = discovery.run_full();
400        (result.types, result.policies)
401    };
402    // Clone for codegen (parser takes ownership)
403    let codegen_registry = type_registry.clone();
404    let codegen_policies = policy_registry.clone();
405
406    let mut world_state = WorldState::new();
407    let expr_arena = Arena::new();
408    let term_arena = Arena::new();
409    let np_arena = Arena::new();
410    let sym_arena = Arena::new();
411    let role_arena = Arena::new();
412    let pp_arena = Arena::new();
413    let stmt_arena: Arena<Stmt> = Arena::new();
414    let imperative_expr_arena: Arena<Expr> = Arena::new();
415    let type_expr_arena: Arena<TypeExpr> = Arena::new();
416
417    let ast_ctx = AstContext::with_types(
418        &expr_arena,
419        &term_arena,
420        &np_arena,
421        &sym_arena,
422        &role_arena,
423        &pp_arena,
424        &stmt_arena,
425        &imperative_expr_arena,
426        &type_expr_arena,
427    );
428
429    // Pass 2: Parse with type context
430    let mut parser = Parser::new(tokens, &mut world_state, &mut interner, ast_ctx, type_registry);
431    let stmts = parser.parse_program()?;
432
433    // Pass 3: Escape analysis
434    let mut escape_checker = EscapeChecker::new(&interner);
435    escape_checker.check_program(&stmts).map_err(|e| {
436        ParseError {
437            kind: crate::error::ParseErrorKind::Custom(e.to_string()),
438            span: e.span,
439        }
440    })?;
441
442    // Pass 4: Ownership analysis
443    // Catches use-after-move errors with control flow awareness
444    let mut ownership_checker = OwnershipChecker::new(&interner);
445    ownership_checker.check_program(&stmts).map_err(|e| {
446        ParseError {
447            kind: crate::error::ParseErrorKind::Custom(e.to_string()),
448            span: e.span,
449        }
450    })?;
451
452    let rust_code = codegen_program(&stmts, &codegen_registry, &codegen_policies, &interner);
453
454    Ok(rust_code)
455}
456
457/// Compile LOGOS source to Rust with full Z3 static verification.
458///
459/// This runs the Z3-based verifier on Assert statements before code generation,
460/// proving that assertions hold for all possible inputs. This is the most
461/// thorough compilation mode, suitable for high-assurance code.
462///
463/// # Arguments
464///
465/// * `source` - LOGOS source code as a string
466///
467/// # Returns
468///
469/// Generated Rust source code on success.
470///
471/// # Errors
472///
473/// Returns [`ParseError`] if:
474/// - Any error from [`compile_to_rust`] occurs
475/// - Z3 cannot prove an Assert statement
476/// - Refinement type constraints cannot be satisfied
477/// - Termination cannot be proven for loops with `decreasing`
478///
479/// # Example
480///
481/// ```no_run
482/// # use logicaffeine_compile::compile::compile_to_rust_verified;
483/// # use logicaffeine_compile::ParseError;
484/// # fn main() -> Result<(), ParseError> {
485/// let source = r#"
486/// ## Main
487/// Let x: { it: Int | it > 0 } be 5.
488/// Assert that x > 0.
489/// "#;
490/// let rust_code = compile_to_rust_verified(source)?;
491/// # Ok(())
492/// # }
493/// ```
494///
495/// # Feature Flag
496///
497/// This function requires the `verification` feature to be enabled:
498///
499/// ```toml
500/// [dependencies]
501/// logicaffeine_compile = { version = "...", features = ["verification"] }
502/// ```
503#[cfg(feature = "verification")]
504pub fn compile_to_rust_verified(source: &str) -> Result<String, ParseError> {
505    use crate::verification::VerificationPass;
506
507    let mut interner = Interner::new();
508    let mut lexer = Lexer::new(source, &mut interner);
509    let tokens = lexer.tokenize();
510
511    // Pass 1: Discovery - scan for type definitions and policies
512    let (type_registry, policy_registry) = {
513        let mut discovery = DiscoveryPass::new(&tokens, &mut interner);
514        let result = discovery.run_full();
515        (result.types, result.policies)
516    };
517    // Clone for codegen (parser takes ownership)
518    let codegen_registry = type_registry.clone();
519    let codegen_policies = policy_registry.clone();
520
521    let mut world_state = WorldState::new();
522    let expr_arena = Arena::new();
523    let term_arena = Arena::new();
524    let np_arena = Arena::new();
525    let sym_arena = Arena::new();
526    let role_arena = Arena::new();
527    let pp_arena = Arena::new();
528    let stmt_arena: Arena<Stmt> = Arena::new();
529    let imperative_expr_arena: Arena<Expr> = Arena::new();
530    let type_expr_arena: Arena<TypeExpr> = Arena::new();
531
532    let ast_ctx = AstContext::with_types(
533        &expr_arena,
534        &term_arena,
535        &np_arena,
536        &sym_arena,
537        &role_arena,
538        &pp_arena,
539        &stmt_arena,
540        &imperative_expr_arena,
541        &type_expr_arena,
542    );
543
544    // Pass 2: Parse with type context
545    let mut parser = Parser::new(tokens, &mut world_state, &mut interner, ast_ctx, type_registry);
546    let stmts = parser.parse_program()?;
547
548    // Pass 3: Escape analysis
549    let mut escape_checker = EscapeChecker::new(&interner);
550    escape_checker.check_program(&stmts).map_err(|e| {
551        ParseError {
552            kind: crate::error::ParseErrorKind::Custom(e.to_string()),
553            span: e.span,
554        }
555    })?;
556
557    // Pass 4: Static verification
558    let mut verifier = VerificationPass::new(&interner);
559    verifier.verify_program(&stmts).map_err(|e| {
560        ParseError {
561            kind: crate::error::ParseErrorKind::Custom(format!(
562                "Verification Failed:\n\n{}",
563                e
564            )),
565            span: crate::token::Span::default(),
566        }
567    })?;
568
569    let rust_code = codegen_program(&stmts, &codegen_registry, &codegen_policies, &interner);
570
571    Ok(rust_code)
572}
573
574/// Compile LOGOS source and write output to a directory as a Cargo project.
575///
576/// Creates a complete Cargo project structure with:
577/// - `src/main.rs` containing the generated Rust code
578/// - `Cargo.toml` with runtime dependencies
579/// - `crates/` directory with runtime crate copies
580///
581/// # Arguments
582///
583/// * `source` - LOGOS source code as a string
584/// * `output_dir` - Directory to create the Cargo project in
585///
586/// # Errors
587///
588/// Returns [`CompileError`] if:
589/// - Compilation fails (wrapped as `CompileError::Parse`)
590/// - File system operations fail (wrapped as `CompileError::Io`)
591///
592/// # Example
593///
594/// ```no_run
595/// # use logicaffeine_compile::compile::{compile_to_dir, CompileError};
596/// # use std::path::Path;
597/// # fn main() -> Result<(), CompileError> {
598/// let source = "## Main\nShow \"Hello\".";
599/// compile_to_dir(source, Path::new("/tmp/my_project"))?;
600/// // Now /tmp/my_project is a buildable Cargo project
601/// # Ok(())
602/// # }
603/// ```
604pub fn compile_to_dir(source: &str, output_dir: &Path) -> Result<(), CompileError> {
605    let output = compile_program_full(source).map_err(CompileError::Parse)?;
606
607    // Create output directory structure
608    let src_dir = output_dir.join("src");
609    fs::create_dir_all(&src_dir).map_err(|e| CompileError::Io(e.to_string()))?;
610
611    // Write main.rs (codegen already includes the use statements)
612    let main_path = src_dir.join("main.rs");
613    let mut file = fs::File::create(&main_path).map_err(|e| CompileError::Io(e.to_string()))?;
614    file.write_all(output.rust_code.as_bytes()).map_err(|e| CompileError::Io(e.to_string()))?;
615
616    // Write Cargo.toml with runtime crate dependencies
617    let mut cargo_toml = String::from(r#"[package]
618name = "logos_output"
619version = "0.1.0"
620edition = "2021"
621
622[dependencies]
623logicaffeine-data = { path = "./crates/logicaffeine_data" }
624logicaffeine-system = { path = "./crates/logicaffeine_system", features = ["full"] }
625tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
626"#);
627
628    // Append user-declared dependencies from ## Requires blocks
629    for dep in &output.dependencies {
630        if dep.features.is_empty() {
631            let _ = writeln!(cargo_toml, "{} = \"{}\"", dep.name, dep.version);
632        } else {
633            let feats = dep.features.iter()
634                .map(|f| format!("\"{}\"", f))
635                .collect::<Vec<_>>()
636                .join(", ");
637            let _ = writeln!(
638                cargo_toml,
639                "{} = {{ version = \"{}\", features = [{}] }}",
640                dep.name, dep.version, feats
641            );
642        }
643    }
644
645    let cargo_path = output_dir.join("Cargo.toml");
646    let mut file = fs::File::create(&cargo_path).map_err(|e| CompileError::Io(e.to_string()))?;
647    file.write_all(cargo_toml.as_bytes()).map_err(|e| CompileError::Io(e.to_string()))?;
648
649    // Copy runtime crates to output directory
650    copy_runtime_crates(output_dir)?;
651
652    Ok(())
653}
654
655/// Copy the runtime crates to the output directory.
656/// Copies logicaffeine_data and logicaffeine_system.
657pub fn copy_runtime_crates(output_dir: &Path) -> Result<(), CompileError> {
658    let crates_dir = output_dir.join("crates");
659    fs::create_dir_all(&crates_dir).map_err(|e| CompileError::Io(e.to_string()))?;
660
661    // Find workspace root
662    let workspace_root = find_workspace_root()?;
663
664    // Copy logicaffeine_data
665    let data_src = workspace_root.join(CRATES_DATA_PATH);
666    let data_dest = crates_dir.join("logicaffeine_data");
667    copy_dir_recursive(&data_src, &data_dest)?;
668    deworkspace_cargo_toml(&data_dest.join("Cargo.toml"))?;
669
670    // Copy logicaffeine_system
671    let system_src = workspace_root.join(CRATES_SYSTEM_PATH);
672    let system_dest = crates_dir.join("logicaffeine_system");
673    copy_dir_recursive(&system_src, &system_dest)?;
674    deworkspace_cargo_toml(&system_dest.join("Cargo.toml"))?;
675
676    // Also need to copy logicaffeine_base since both crates depend on it
677    let base_src = workspace_root.join("crates/logicaffeine_base");
678    let base_dest = crates_dir.join("logicaffeine_base");
679    copy_dir_recursive(&base_src, &base_dest)?;
680    deworkspace_cargo_toml(&base_dest.join("Cargo.toml"))?;
681
682    Ok(())
683}
684
685/// Resolve workspace-inherited fields in a copied crate's Cargo.toml.
686///
687/// When runtime crates are copied to a standalone project, any fields using
688/// `*.workspace = true` won't resolve because there's no parent workspace.
689/// This rewrites them with concrete values (matching the workspace's settings).
690fn deworkspace_cargo_toml(cargo_toml_path: &Path) -> Result<(), CompileError> {
691    let content = fs::read_to_string(cargo_toml_path)
692        .map_err(|e| CompileError::Io(e.to_string()))?;
693
694    let mut result = String::with_capacity(content.len());
695    for line in content.lines() {
696        let trimmed = line.trim();
697        if trimmed == "edition.workspace = true" {
698            result.push_str("edition = \"2021\"");
699        } else if trimmed == "rust-version.workspace = true" {
700            result.push_str("rust-version = \"1.75\"");
701        } else if trimmed == "authors.workspace = true"
702            || trimmed == "repository.workspace = true"
703            || trimmed == "homepage.workspace = true"
704            || trimmed == "documentation.workspace = true"
705            || trimmed == "keywords.workspace = true"
706            || trimmed == "categories.workspace = true"
707            || trimmed == "license.workspace = true"
708        {
709            // Drop these lines — they're metadata not needed for compilation
710            continue;
711        } else if trimmed.contains(".workspace = true") {
712            // Catch-all: drop any other workspace-inherited fields
713            continue;
714        } else {
715            result.push_str(line);
716        }
717        result.push('\n');
718    }
719
720    fs::write(cargo_toml_path, result)
721        .map_err(|e| CompileError::Io(e.to_string()))?;
722
723    Ok(())
724}
725
726/// Find the workspace root directory.
727fn find_workspace_root() -> Result<std::path::PathBuf, CompileError> {
728    // 1. Explicit override via LOGOS_WORKSPACE env var
729    if let Ok(workspace) = std::env::var("LOGOS_WORKSPACE") {
730        let path = Path::new(&workspace);
731        if path.join("Cargo.toml").exists() && path.join("crates").exists() {
732            return Ok(path.to_path_buf());
733        }
734    }
735
736    // 2. Try CARGO_MANIFEST_DIR (works during cargo build of largo itself)
737    if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
738        let path = Path::new(&manifest_dir);
739        if let Some(parent) = path.parent().and_then(|p| p.parent()) {
740            if parent.join("Cargo.toml").exists() {
741                return Ok(parent.to_path_buf());
742            }
743        }
744    }
745
746    // 3. Infer from the largo binary's own location
747    //    e.g. /workspace/target/release/largo → /workspace
748    if let Ok(exe) = std::env::current_exe() {
749        if let Some(dir) = exe.parent() {
750            // Walk up from the binary's directory
751            let mut candidate = dir.to_path_buf();
752            for _ in 0..5 {
753                if candidate.join("Cargo.toml").exists() && candidate.join("crates").exists() {
754                    return Ok(candidate);
755                }
756                if !candidate.pop() {
757                    break;
758                }
759            }
760        }
761    }
762
763    // 4. Fallback to current directory traversal
764    let mut current = std::env::current_dir()
765        .map_err(|e| CompileError::Io(e.to_string()))?;
766
767    loop {
768        if current.join("Cargo.toml").exists() && current.join("crates").exists() {
769            return Ok(current);
770        }
771        if !current.pop() {
772            return Err(CompileError::Io(
773                "Could not find workspace root. Set LOGOS_WORKSPACE env var or run from within the workspace.".to_string()
774            ));
775        }
776    }
777}
778
779/// Recursively copy a directory.
780/// Skips files that disappear during copy (race condition with parallel builds).
781fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<(), CompileError> {
782    fs::create_dir_all(dst).map_err(|e| CompileError::Io(e.to_string()))?;
783
784    for entry in fs::read_dir(src).map_err(|e| CompileError::Io(e.to_string()))? {
785        let entry = entry.map_err(|e| CompileError::Io(e.to_string()))?;
786        let src_path = entry.path();
787        let file_name = entry.file_name();
788        let dst_path = dst.join(&file_name);
789
790        // Skip target directory, build artifacts, and lock files
791        if file_name == "target"
792            || file_name == ".git"
793            || file_name == "Cargo.lock"
794            || file_name == ".DS_Store"
795        {
796            continue;
797        }
798
799        // Skip files that start with a dot (hidden files)
800        if file_name.to_string_lossy().starts_with('.') {
801            continue;
802        }
803
804        // Check if path still exists (race condition protection)
805        if !src_path.exists() {
806            continue;
807        }
808
809        if src_path.is_dir() {
810            copy_dir_recursive(&src_path, &dst_path)?;
811        } else if file_name == "Cargo.toml" {
812            // Special handling for Cargo.toml: remove [workspace] line
813            // which can interfere with nested crate dependencies
814            match fs::read_to_string(&src_path) {
815                Ok(content) => {
816                    let filtered: String = content
817                        .lines()
818                        .filter(|line| !line.trim().starts_with("[workspace]"))
819                        .collect::<Vec<_>>()
820                        .join("\n");
821                    fs::write(&dst_path, filtered)
822                        .map_err(|e| CompileError::Io(e.to_string()))?;
823                }
824                Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
825                Err(e) => return Err(CompileError::Io(e.to_string())),
826            }
827        } else {
828            match fs::copy(&src_path, &dst_path) {
829                Ok(_) => {}
830                Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
831                Err(e) => return Err(CompileError::Io(e.to_string())),
832            }
833        }
834    }
835
836    Ok(())
837}
838
839/// Compile and run a LOGOS program end-to-end.
840///
841/// This function performs the full compilation workflow:
842/// 1. Compile LOGOS to Rust via [`compile_to_dir`]
843/// 2. Run `cargo build` with JSON diagnostics
844/// 3. Translate any rustc errors to LOGOS-friendly messages
845/// 4. Run the compiled program via `cargo run`
846///
847/// # Arguments
848///
849/// * `source` - LOGOS source code as a string
850/// * `output_dir` - Directory to create the temporary Cargo project in
851///
852/// # Returns
853///
854/// The stdout output of the executed program.
855///
856/// # Errors
857///
858/// Returns [`CompileError`] if:
859/// - Compilation fails (see [`compile_to_dir`])
860/// - Rust compilation fails (`CompileError::Build` or `CompileError::Ownership`)
861/// - The program crashes at runtime (`CompileError::Runtime`)
862///
863/// # Diagnostic Translation
864///
865/// When rustc reports errors (e.g., E0382 for use-after-move), this function
866/// uses the [`diagnostic`](crate::diagnostic) module to translate them into
867/// LOGOS-friendly Socratic error messages.
868///
869/// # Example
870///
871/// ```no_run
872/// # use logicaffeine_compile::compile::{compile_and_run, CompileError};
873/// # use std::path::Path;
874/// # fn main() -> Result<(), CompileError> {
875/// let source = "## Main\nShow \"Hello, World!\".";
876/// let output = compile_and_run(source, Path::new("/tmp/run"))?;
877/// assert_eq!(output.trim(), "Hello, World!");
878/// # Ok(())
879/// # }
880/// ```
881pub fn compile_and_run(source: &str, output_dir: &Path) -> Result<String, CompileError> {
882    compile_to_dir(source, output_dir)?;
883
884    // Run cargo build with JSON message format for structured error parsing
885    let build_output = Command::new("cargo")
886        .arg("build")
887        .arg("--message-format=json")
888        .current_dir(output_dir)
889        .output()
890        .map_err(|e| CompileError::Io(e.to_string()))?;
891
892    if !build_output.status.success() {
893        let stderr = String::from_utf8_lossy(&build_output.stderr);
894        let stdout = String::from_utf8_lossy(&build_output.stdout);
895
896        // Try to parse JSON diagnostics and translate them
897        let diagnostics = parse_rustc_json(&stdout);
898
899        if !diagnostics.is_empty() {
900            // Create a basic source map with the LOGOS source
901            let source_map = SourceMap::new(source.to_string());
902            let interner = Interner::new();
903
904            if let Some(logos_error) = translate_diagnostics(&diagnostics, &source_map, &interner) {
905                return Err(CompileError::Ownership(logos_error));
906            }
907        }
908
909        // Fallback to raw error if translation fails
910        return Err(CompileError::Build(stderr.to_string()));
911    }
912
913    // Run the compiled program
914    let run_output = Command::new("cargo")
915        .arg("run")
916        .arg("--quiet")
917        .current_dir(output_dir)
918        .output()
919        .map_err(|e| CompileError::Io(e.to_string()))?;
920
921    if !run_output.status.success() {
922        let stderr = String::from_utf8_lossy(&run_output.stderr);
923        return Err(CompileError::Runtime(stderr.to_string()));
924    }
925
926    let stdout = String::from_utf8_lossy(&run_output.stdout);
927    Ok(stdout.to_string())
928}
929
930/// Compile a LOGOS source file.
931/// For single-file compilation without dependencies.
932pub fn compile_file(path: &Path) -> Result<String, CompileError> {
933    let source = fs::read_to_string(path).map_err(|e| CompileError::Io(e.to_string()))?;
934    compile_to_rust(&source).map_err(CompileError::Parse)
935}
936
937/// Compile a multi-file LOGOS project with dependency resolution.
938///
939/// This function:
940/// 1. Reads the entry file
941/// 2. Scans for dependencies in the abstract (Markdown links)
942/// 3. Recursively loads and discovers types from dependencies
943/// 4. Compiles with the combined type registry
944///
945/// # Arguments
946/// * `entry_file` - The main entry file to compile (root is derived from parent directory)
947///
948/// # Example
949/// ```no_run
950/// # use logicaffeine_compile::compile::compile_project;
951/// # use std::path::Path;
952/// let result = compile_project(Path::new("/project/main.md"));
953/// ```
954pub fn compile_project(entry_file: &Path) -> Result<CompileOutput, CompileError> {
955    use crate::loader::Loader;
956    use crate::analysis::discover_with_imports;
957
958    let root_path = entry_file.parent().unwrap_or(Path::new(".")).to_path_buf();
959    let mut loader = Loader::new(root_path);
960    let mut interner = Interner::new();
961
962    // Read the entry file
963    let source = fs::read_to_string(entry_file)
964        .map_err(|e| CompileError::Io(format!("Failed to read entry file: {}", e)))?;
965
966    // Discover types from entry file and all imports
967    let type_registry = discover_with_imports(entry_file, &source, &mut loader, &mut interner)
968        .map_err(|e| CompileError::Io(e))?;
969
970    // Now compile with the discovered types
971    compile_to_rust_with_registry_full(&source, type_registry, &mut interner)
972        .map_err(CompileError::Parse)
973}
974
975/// Compile LOGOS source with a pre-populated type registry, returning full output.
976/// Returns both generated Rust code and extracted dependencies.
977fn compile_to_rust_with_registry_full(
978    source: &str,
979    type_registry: crate::analysis::TypeRegistry,
980    interner: &mut Interner,
981) -> Result<CompileOutput, ParseError> {
982    let mut lexer = Lexer::new(source, interner);
983    let tokens = lexer.tokenize();
984
985    // Discovery pass for policies (types already discovered)
986    let policy_registry = {
987        let mut discovery = DiscoveryPass::new(&tokens, interner);
988        discovery.run_full().policies
989    };
990
991    let codegen_registry = type_registry.clone();
992    let codegen_policies = policy_registry.clone();
993
994    let mut world_state = WorldState::new();
995    let expr_arena = Arena::new();
996    let term_arena = Arena::new();
997    let np_arena = Arena::new();
998    let sym_arena = Arena::new();
999    let role_arena = Arena::new();
1000    let pp_arena = Arena::new();
1001    let stmt_arena: Arena<Stmt> = Arena::new();
1002    let imperative_expr_arena: Arena<Expr> = Arena::new();
1003    let type_expr_arena: Arena<TypeExpr> = Arena::new();
1004
1005    let ast_ctx = AstContext::with_types(
1006        &expr_arena,
1007        &term_arena,
1008        &np_arena,
1009        &sym_arena,
1010        &role_arena,
1011        &pp_arena,
1012        &stmt_arena,
1013        &imperative_expr_arena,
1014        &type_expr_arena,
1015    );
1016
1017    let mut parser = Parser::new(tokens, &mut world_state, interner, ast_ctx, type_registry);
1018    let stmts = parser.parse_program()?;
1019
1020    // Extract dependencies before escape analysis
1021    let mut dependencies = extract_dependencies(&stmts, interner)?;
1022
1023    // FFI: Auto-inject wasm-bindgen dependency if any function is exported for WASM
1024    let needs_wasm_bindgen = stmts.iter().any(|stmt| {
1025        if let Stmt::FunctionDef { is_exported: true, export_target: Some(target), .. } = stmt {
1026            interner.resolve(*target).eq_ignore_ascii_case("wasm")
1027        } else {
1028            false
1029        }
1030    });
1031    if needs_wasm_bindgen && !dependencies.iter().any(|d| d.name == "wasm-bindgen") {
1032        dependencies.push(CrateDependency {
1033            name: "wasm-bindgen".to_string(),
1034            version: "0.2".to_string(),
1035            features: vec![],
1036        });
1037    }
1038
1039    let mut escape_checker = EscapeChecker::new(interner);
1040    escape_checker.check_program(&stmts).map_err(|e| {
1041        ParseError {
1042            kind: crate::error::ParseErrorKind::Custom(e.to_string()),
1043            span: e.span,
1044        }
1045    })?;
1046
1047    let rust_code = codegen_program(&stmts, &codegen_registry, &codegen_policies, interner);
1048
1049    // Universal ABI: Generate C header + bindings if any C exports exist
1050    let has_c = stmts.iter().any(|stmt| {
1051        if let Stmt::FunctionDef { is_exported: true, export_target, .. } = stmt {
1052            match export_target {
1053                None => true,
1054                Some(t) => interner.resolve(*t).eq_ignore_ascii_case("c"),
1055            }
1056        } else {
1057            false
1058        }
1059    });
1060
1061    let c_header = if has_c {
1062        Some(generate_c_header(&stmts, "module", interner, &codegen_registry))
1063    } else {
1064        None
1065    };
1066
1067    if has_c && !dependencies.iter().any(|d| d.name == "serde_json") {
1068        dependencies.push(CrateDependency {
1069            name: "serde_json".to_string(),
1070            version: "1".to_string(),
1071            features: vec![],
1072        });
1073    }
1074
1075    let python_bindings = if has_c {
1076        Some(generate_python_bindings(&stmts, "module", interner, &codegen_registry))
1077    } else {
1078        None
1079    };
1080
1081    let (typescript_bindings, typescript_types) = if has_c {
1082        let (js, dts) = generate_typescript_bindings(&stmts, "module", interner, &codegen_registry);
1083        (Some(js), Some(dts))
1084    } else {
1085        (None, None)
1086    };
1087
1088    Ok(CompileOutput { rust_code, dependencies, c_header, python_bindings, typescript_types, typescript_bindings })
1089}
1090
1091/// Errors that can occur during the LOGOS compilation pipeline.
1092///
1093/// This enum represents the different stages where compilation can fail,
1094/// from parsing through to runtime execution.
1095///
1096/// # Error Hierarchy
1097///
1098/// ```text
1099/// CompileError
1100/// ├── Parse      ← Lexing, parsing, or static analysis
1101/// ├── Io         ← File system operations
1102/// ├── Build      ← Rust compilation (cargo build)
1103/// ├── Ownership  ← Translated borrow checker errors
1104/// └── Runtime    ← Program execution failure
1105/// ```
1106///
1107/// # Error Translation
1108///
1109/// The `Ownership` variant contains LOGOS-friendly error messages translated
1110/// from rustc's borrow checker errors (E0382, E0505, E0597) using the
1111/// [`diagnostic`](crate::diagnostic) module.
1112#[derive(Debug)]
1113pub enum CompileError {
1114    /// Parsing or static analysis failed.
1115    ///
1116    /// This includes lexer errors, syntax errors, escape analysis failures,
1117    /// ownership analysis failures, and Z3 verification failures.
1118    Parse(ParseError),
1119
1120    /// File system operation failed.
1121    ///
1122    /// Typically occurs when reading source files or writing output.
1123    Io(String),
1124
1125    /// Rust compilation failed (`cargo build`).
1126    ///
1127    /// Contains the raw stderr output from rustc when diagnostic translation
1128    /// was not possible.
1129    Build(String),
1130
1131    /// Runtime execution failed.
1132    ///
1133    /// Contains stderr output from the executed program.
1134    Runtime(String),
1135
1136    /// Translated ownership/borrow checker error with LOGOS-friendly message.
1137    ///
1138    /// This variant is used when rustc reports errors like E0382 (use after move)
1139    /// and we can translate them into natural language error messages that
1140    /// reference the original LOGOS source.
1141    Ownership(LogosError),
1142}
1143
1144impl std::fmt::Display for CompileError {
1145    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1146        match self {
1147            CompileError::Parse(e) => write!(f, "Parse error: {:?}", e),
1148            CompileError::Io(e) => write!(f, "IO error: {}", e),
1149            CompileError::Build(e) => write!(f, "Build error: {}", e),
1150            CompileError::Runtime(e) => write!(f, "Runtime error: {}", e),
1151            CompileError::Ownership(e) => write!(f, "{}", e),
1152        }
1153    }
1154}
1155
1156impl std::error::Error for CompileError {}
1157
1158#[cfg(test)]
1159mod tests {
1160    use super::*;
1161
1162    #[test]
1163    fn test_compile_let_statement() {
1164        let source = "## Main\nLet x be 5.";
1165        let result = compile_to_rust(source);
1166        assert!(result.is_ok(), "Should compile: {:?}", result);
1167        let rust = result.unwrap();
1168        assert!(rust.contains("fn main()"));
1169        assert!(rust.contains("let x = 5;"));
1170    }
1171
1172    #[test]
1173    fn test_compile_return_statement() {
1174        let source = "## Main\nReturn 42.";
1175        let result = compile_to_rust(source);
1176        assert!(result.is_ok(), "Should compile: {:?}", result);
1177        let rust = result.unwrap();
1178        assert!(rust.contains("return 42;"));
1179    }
1180}