Nasty.Interop.CodeGen.Elixir (Nasty v0.3.0)
View SourceGenerates Elixir code from natural language intents.
This module converts Nasty.AST.Intent structures into executable Elixir code
by generating Elixir AST using the quote macro and pattern matching on intent types.
Supported Patterns
List Operations
- "Sort X" →
Enum.sort(x) - "Filter X" →
Enum.filter(x, fn item -> condition end) - "Map X" →
Enum.map(x, fn item -> transformation end) - "Sum X" →
Enum.sum(x) - "Count X" →
Enum.count(x)
Arithmetic
- "Add X and Y" →
x + y - "X plus Y" →
x + y - "Multiply X by Y" →
x * y
Assignments
- "X is Y" →
x = y - "Set X to Y" →
x = y
Conditionals
- "If X then Y" →
if x, do: y
Examples
# Action intent → Function call
intent = %Intent{type: :action, action: "sort", target: "list"}
{:ok, ast} = Elixir.generate(intent)
Macro.to_string(ast) # => "Enum.sort(list)"
# Definition intent → Assignment
intent = %Intent{type: :definition, action: "assign", target: "x", arguments: [5]}
{:ok, ast} = Elixir.generate(intent)
Macro.to_string(ast) # => "x = 5"
Summary
Functions
Generates Elixir AST from an intent.
Generates Elixir code string from an intent.
Validates that generated code is syntactically correct.
Functions
@spec generate( Nasty.AST.Intent.t(), keyword() ) :: {:ok, Macro.t()} | {:error, term()}
Generates Elixir AST from an intent.
Parameters
intent- The intent to convertopts- Options (currently unused)
Returns
{:ok, Macro.t()}- Elixir AST{:error, reason}- Generation error
Examples
iex> intent = %Intent{type: :action, action: "sort", target: "numbers", ...}
iex> {:ok, ast} = Elixir.generate(intent)
iex> Macro.to_string(ast)
"Enum.sort(numbers)"
@spec generate_string( Nasty.AST.Intent.t(), keyword() ) :: {:ok, String.t()} | {:error, term()}
Generates Elixir code string from an intent.
This is a convenience function that generates AST and converts it to a string.
Examples
iex> intent = %Intent{type: :action, action: "sort", target: "list", ...}
iex> {:ok, code} = Elixir.generate_string(intent)
iex> code
"Enum.sort(list)"
Validates that generated code is syntactically correct.
Examples
iex> ast = quote do: Enum.sort(list)
iex> Elixir.validate(ast)
{:ok, ast}