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
Summary
Functions
Export an entire memory namespace as PTC-Lisp source.
Convert a Core AST node to a PTC-Lisp source string.
Serialize a closure tuple to PTC-Lisp source string.
Serialize all closures in a memory namespace to source strings.
Functions
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
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 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 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)"}