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

View Source

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

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

Transformation Strategy

The transformation follows a pattern-matching approach, handling each Ruby AST construct (from parser gem) and mapping it to the appropriate MetaAST node type.

M2.1 (Core Layer)

  • Literals: integers, floats, strings, booleans, nil, symbols
  • Variables: local, instance, class, global
  • Binary operators: arithmetic, comparison, boolean
  • Unary operators: negation, logical not
  • Method calls
  • Conditionals: if/elsif/else, unless, ternary
  • Blocks: sequential expressions
  • Assignment: local variable assignment

M2.2 (Extended Layer)

  • Loops: while, until, for
  • Iterators: each, map, select, reduce
  • Blocks & Procs: blocks, lambdas, procs
  • Pattern matching: case/when (classic), case/in (Ruby 3+)
  • Exception handling: begin/rescue/ensure

M2.3 (Native Layer)

  • Class definitions
  • Module definitions
  • Method definitions
  • String interpolation
  • Regular expressions
  • Metaprogramming constructs

Summary

Functions

Transform Ruby AST to MetaAST.

Functions

transform(ast)

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

Transform Ruby AST to MetaAST.

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

New 3-Tuple Format

All MetaAST nodes are uniform 3-element tuples:

{type_atom, keyword_meta, children_or_value}

Examples

iex> transform(%{"type" => "int", "children" => [42]})
{:ok, {:literal, [subtype: :integer], 42}, %{}}

iex> transform(%{"type" => "lvar", "children" => ["x"]})
{:ok, {:variable, [], "x"}, %{}}