PtcRunner.Lisp.CoreToSource (PtcRunner v0.9.0)

Copy Markdown View Source

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_namespace(memory)

@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(n)

@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(arg)

@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(memory)

@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)"}