# `PtcRunner.Lisp.CoreToSource`
[🔗](https://github.com/andreasronge/ptc_runner/blob/main/lib/ptc_runner/lisp/core_to_source.ex#L1)

Convert Core AST (the analyzed/desugared representation) back to PTC-Lisp source strings.

This is distinct from `PtcRunner.Lisp.Formatter` which handles raw parser AST.
CoreToSource works with the intermediate representation produced by the analyzer
and stored inside closures.

## Use Cases

- Serializing closures for archive persistence
- Novelty comparison between memory designs
- Debugging Core AST output

# `export_namespace`

```elixir
@spec export_namespace(map()) :: String.t()
```

Export an entire memory namespace as PTC-Lisp source.

Serializes all entries — closures become `(def name (fn [...] ...))`,
non-closure values become `(def name value)`. Multiple top-level forms
are emitted without a `do` wrapper (the parser handles them natively).

## Examples

    iex> memory = %{x: 5, f: {:closure, [{:var, :n}], {:call, {:var, :+}, [{:var, :n}, 1]}, %{}, [], %{}}}
    iex> source = PtcRunner.Lisp.CoreToSource.export_namespace(memory)
    iex> source =~ "def x"
    true
    iex> source =~ "def f"
    true

# `format`

```elixir
@spec format(term()) :: String.t()
```

Convert a Core AST node to a PTC-Lisp source string.

## Examples

    iex> PtcRunner.Lisp.CoreToSource.format({:var, :x})
    "x"

    iex> PtcRunner.Lisp.CoreToSource.format({:string, "hello"})
    ~S("hello")

    iex> PtcRunner.Lisp.CoreToSource.format({:call, {:var, :+}, [1, 2]})
    "(+ 1 2)"

# `serialize_closure`

```elixir
@spec serialize_closure(tuple()) :: String.t()
```

Serialize a closure tuple to PTC-Lisp source string.

Drops the captured environment intentionally — forces pure functions
or use of `def`/memory namespace for state.

## Examples

    iex> closure = {:closure, [{:var, :x}], {:call, {:var, :+}, [{:var, :x}, 1]}, %{}, [], %{}}
    iex> PtcRunner.Lisp.CoreToSource.serialize_closure(closure)
    "(fn [x] (+ x 1))"

# `serialize_namespace`

```elixir
@spec serialize_namespace(map()) :: map()
```

Serialize all closures in a memory namespace to source strings.

Returns a map of `{name => source_string}` for each closure value in memory.
Non-closure values are skipped.

## Examples

    iex> memory = %{f: {:closure, [{:var, :x}], {:var, :x}, %{}, [], %{}}, count: 5}
    iex> PtcRunner.Lisp.CoreToSource.serialize_namespace(memory)
    %{f: "(fn [x] x)"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
