Search documentation of Metastatic

# Metastatic.Adapters.Erlang (Metastatic v0.13.2)

View Source

Erlang language adapter for MetaAST transformations.

Bridges between Erlang AST (M1) and MetaAST (M2), enabling cross-language code analysis and transformation for Erlang source code.

## Erlang AST Structure (M1)

Erlang represents AST as tuples with specific structures. The format varies by construct but generally follows patterns like:

    {Type, Line, Value}
    {Type, Line, Left, Right}

### Examples

    # Integer literal
    {:integer, 1, 42}

    # Variable
    {:var, 1, :X}

    # Binary operation (addition)
    {:op, 1, :+, {:var, 1, :X}, {:integer, 1, 5}}

    # Function call
    {:call, 1, {:atom, 1, :foo}, [{:integer, 1, 1}, {:integer, 1, 2}]}

    # Case expression
    {:case, 1, {:var, 1, :X},
     [{:clause, 1, [{:integer, 1, 1}], [], [{:atom, 1, :ok}]}]}

## M1 ↔ M2 Transformations

This adapter performs bidirectional transformations between Erlang AST (M1) and MetaAST (M2):

### Literals

    # M1 → M2
    {:integer, _, 42}           → {:literal, :integer, 42}
    {:float, _, 3.14}           → {:literal, :float, 3.14}
    {:string, _, 'hello'}       → {:literal, :string, "hello"}
    {:atom, _, true}            → {:literal, :boolean, true}
    {:atom, _, undefined}       → {:literal, :null, nil}

### Variables

    {:var, _, :X}               → {:variable, "X"}

### Binary Operations

    {:op, _, :+, left, right}   → {:binary_op, :arithmetic, :+, left, right}
    {:op, _, :==, left, right}  → {:binary_op, :comparison, :==, left, right}

### Function Calls

    {:call, _, func, args}      → {:function_call, "func", args}

## Round-Trip Fidelity

The adapter achieves >90% round-trip fidelity for M2.1 (Core) constructs. Metadata preserves:

- Line numbers
- Variable names (case-sensitive)
- Erlang-specific type information

## Usage

    # Parse Erlang source
    {:ok, ast} = Metastatic.Adapters.Erlang.parse("X + 5.")

    # Transform to MetaAST
    {:ok, meta_ast, metadata} = Metastatic.Adapters.Erlang.to_meta(ast)

    # Transform back to Erlang AST
    {:ok, ast2} = Metastatic.Adapters.Erlang.from_meta(meta_ast, metadata)

    # Unparse to source
    {:ok, source} = Metastatic.Adapters.Erlang.unparse(ast2)

## Theory

This adapter implements the Galois connection:

    α_Erlang: AS_Erlang → MetaAST × Metadata
    ρ_Erlang: MetaAST × Metadata → AS_Erlang

Where:

- `α_Erlang` is `to_meta/1` (abstraction)
- `ρ_Erlang` is `from_meta/2` (reification)