Powered by AppSignal & Oban Pro

Spiral in Livebook

apps/kino/notebooks/spiral.livemd

Spiral in Livebook

Mix.install([
  {:spiral_kino, path: Path.join(__DIR__, "..")}
])

Running Spiral

Insert a cell with + Smart → Spiral. The form sets the timeout and whether to show the generated Rust. The cell compiles with the single-flight Spiral compiler's Rust backend, rustc builds that file, and the program runs. Output, the i32 value, and compile errors show up in the cell.

Spiral.Kino.eval!(
  ~S"""
  inl square x = x * x

  console.write_line "Hello from Spiral!"
  square 7i32
  """,
  timeout: 300000
)

With Show generated Rust the patched Rust is printed before the program output:

Spiral.Kino.eval!(
  ~S"""
  inl square x = x * x

  square 7i32
  """,
  timeout: 300000,
  print_code: true
)

A body that returns () is compiled again with 0i32 so the print still runs. That cell has output and no value:

Spiral.Kino.eval!(
  ~S"""
  console.write_line "Hello from Spiral!"
  """,
  timeout: 300000
)

///- --print-code and ///- --timeout <ms> on the first ///- line are honored. ///> rust is accepted. Any other ///> backend raises. A //// line is dropped.

Programmatic use

Spiral.Kino.run/2 returns a Spiral.Kino.Result:

{:ok, result} = Spiral.Kino.run("1i32 + 2i32", timeout: 120_000)
result.value

Errors

Compile errors are raised as Spiral.Kino.SpiralError. Positions refer to the wrapped main.spi:

Spiral.Kino.eval!(
  ~S"""
  1i32 + "a"
  """,
  timeout: 300000
)