ExPanda.CompilerExpand (ExPanda v0.2.1)

View Source

Wrapper around :elixir_expand.expand/3 for expression-level macro expansion.

This module uses the Elixir compiler's internal expansion engine to produce fully expanded AST for expressions. It provides a version-guarded interface with a fallback to Macro.expand/2 when the internal API is unavailable.

The internal :elixir_expand.expand/3 is more thorough than Macro.expand/2 because it recursively expands all nested macros, resolves aliases, and handles special forms. However, it requires variables to be pre-registered in the environment and raises on undefined references.

Summary

Functions

Check whether :elixir_expand.expand/3 is available in the current runtime.

Expand an expression AST using the compiler's internal expansion engine.

Functions

compiler_available?()

@spec compiler_available?() :: boolean()

Check whether :elixir_expand.expand/3 is available in the current runtime.

expand(ast, env)

@spec expand(Macro.t(), Macro.Env.t()) ::
  {:ok, Macro.t(), Macro.Env.t()} | {:error, String.t()}

Expand an expression AST using the compiler's internal expansion engine.

Returns {:ok, expanded_ast, updated_env} on success, or {:error, reason} on failure.

Parameters

  • ast - The Elixir AST to expand
  • env - A Macro.Env struct with the current compilation context

Examples

iex> env = ExPanda.EnvManager.new_env()
iex> {:ok, expanded, _env} = ExPanda.CompilerExpand.expand({:unless, [line: 1], [true, [do: :never]]}, env)
iex> match?({:case, _, _}, expanded)
true