Metastatic.Adapters.Elixir.ToMeta (Metastatic v0.10.4)

View Source

Transform Elixir AST (M1) to MetaAST (M2).

This module implements the abstraction function α_Elixir that lifts Elixir-specific AST structures to the meta-level representation.

New 3-Tuple Format

All MetaAST nodes are uniform 3-element tuples:

{type_atom, keyword_meta, children_or_value}

Where:

  • type_atom - Node type (e.g., :literal, :binary_op, :function_def)
  • keyword_meta - Keyword list with metadata (line, subtype, operator, etc.)
  • children_or_value - Value for leaf nodes, list of children for composites

Metadata Preservation

The transformation preserves M1-specific information:

  • :original_meta - Original Elixir AST metadata keyword list
  • :original_code - Source code snippet (when available)
  • :line, :col - Source location from Elixir metadata

Transformation Strategy

Uses Macro.traverse/4 for bottom-up AST transformation:

  1. Pre-pass: Track context (module, function, arity)
  2. Post-pass: Transform Elixir nodes to MetaAST nodes

Examples

iex> {:ok, ast} = Code.string_to_quoted("x + 5")
iex> {:ok, meta_ast, _metadata} = ToMeta.transform(ast)
iex> meta_ast
{:binary_op, [category: :arithmetic, operator: :+, original_meta: [line: 1]],
 [{:variable, [original_meta: [line: 1]], "x"},
  {:literal, [subtype: :integer], 5}]}

Summary

Functions

Transform Elixir AST to MetaAST.

Functions

transform(elixir_ast)

@spec transform(term()) :: {:ok, term(), map()} | {:error, String.t()}

Transform Elixir AST to MetaAST.

Returns {:ok, meta_ast, metadata} on success or {:error, reason} on failure.

Examples

iex> ToMeta.transform(42)
{:ok, {:literal, [subtype: :integer], 42}, %{}}

iex> ToMeta.transform({:x, [line: 1], nil})
{:ok, {:variable, [line: 1, original_meta: [line: 1]], "x"}, %{}}