Skip to main content

compile_and_run

Function compile_and_run 

pub fn compile_and_run(
    source: &str,
    output_dir: &Path,
) -> Result<String, CompileError>
Expand description

Compile and run a LOGOS program end-to-end.

This function performs the full compilation workflow:

  1. Compile LOGOS to Rust via compile_to_dir
  2. Run cargo build with JSON diagnostics
  3. Translate any rustc errors to LOGOS-friendly messages
  4. Run the compiled program via cargo run

§Arguments

  • source - LOGOS source code as a string
  • output_dir - Directory to create the temporary Cargo project in

§Returns

The stdout output of the executed program.

§Errors

Returns CompileError if:

  • Compilation fails (see compile_to_dir)
  • Rust compilation fails (CompileError::Build or CompileError::Ownership)
  • The program crashes at runtime (CompileError::Runtime)

§Diagnostic Translation

When rustc reports errors (e.g., E0382 for use-after-move), this function uses the diagnostic module to translate them into LOGOS-friendly Socratic error messages.

§Example

let source = "## Main\nShow \"Hello, World!\".";
let output = compile_and_run(source, Path::new("/tmp/run"))?;
assert_eq!(output.trim(), "Hello, World!");