Metastatic.CLI.Translator (Metastatic v0.10.4)

View Source

Cross-language translation orchestration for Metastatic CLI.

Handles translation of individual files or entire directories from one programming language to another using MetaAST as the intermediate representation.

Translation Pipeline

  1. Parse source code to native AST (M1)
  2. Transform to MetaAST (M2)
  3. Transform MetaAST to target language AST (M1')
  4. Unparse to target language source code

Example

iex> translate("example.py", :python, :elixir, "example.ex")
{:ok, "example.ex"}

Summary

Functions

Translate a single file from one language to another.

Translate a directory of files from one language to another.

Translate a single file, auto-detecting output path.

Types

file_path()

@type file_path() :: String.t()

language()

@type language() :: atom()

translate_result()

@type translate_result() :: {:ok, file_path()} | {:error, String.t()}

Functions

translate(source_path, from_lang, to_lang, output_path)

@spec translate(file_path(), language(), language(), file_path()) ::
  translate_result()

Translate a single file from one language to another.

Parameters

  • source_path - Path to source file
  • from_lang - Source language (:python, :elixir, :erlang)
  • to_lang - Target language (:python, :elixir, :erlang)
  • output_path - Path for output file

Examples

iex> translate("hello.py", :python, :elixir, "hello.ex")
{:ok, "hello.ex"}

iex> translate("invalid.py", :python, :elixir, "out.ex")
{:error, "Parse error: ..."}

translate_directory(source_dir, from_lang, to_lang, output_dir)

@spec translate_directory(file_path(), language(), language(), file_path()) ::
  {:ok, [file_path()]} | {:error, String.t()}

Translate a directory of files from one language to another.

Recursively processes all files with matching extensions in the source directory and writes translated files to the output directory, preserving the directory structure.

Parameters

  • source_dir - Source directory path
  • from_lang - Source language
  • to_lang - Target language
  • output_dir - Output directory path

Examples

iex> translate_directory("src/python", :python, :elixir, "src/elixir")
{:ok, ["src/elixir/foo.ex", "src/elixir/bar.ex"]}

translate_with_auto_output(source_path, from_lang, to_lang)

@spec translate_with_auto_output(file_path(), language(), language()) ::
  translate_result()

Translate a single file, auto-detecting output path.

Output path is generated by changing the file extension to match the target language.

Examples

iex> translate_with_auto_output("hello.py", :python, :elixir)
{:ok, "hello.ex"}

iex> translate_with_auto_output("src/foo.ex", :elixir, :python)
{:ok, "src/foo.py"}