Compiler that converts AST to various representations using visitors.
The compiler orchestrates different visitors to transform Abstract Syntax Trees into executable instructions, string representations, or other formats.
Examples
iex> ast = {:comparison, :gt, {:identifier, "score"}, {:literal, 85}}
iex> Predicator.Compiler.to_instructions(ast)
[["load", "score"], ["lit", 85], ["compare", "GT"]]
# Future visitors will enable:
# iex> Predicator.Compiler.to_string(ast)
# "score > 85"
# iex> Predicator.Compiler.to_dot(ast)
# "digraph {...}"
Summary
Functions
Converts an AST to stack machine instructions.
Converts an AST to stack machine instructions plus a source-position side table.
Converts an AST to a string representation.
Functions
@spec to_instructions( Predicator.Parser.ast() | Predicator.Parser.bare_ast(), keyword() ) :: [[binary() | term()]]
Converts an AST to stack machine instructions.
Uses the InstructionsVisitor to generate a list of instructions that can be executed by the stack-based evaluator.
Parameters
ast- The Abstract Syntax Tree to compileopts- Optional compiler options
Returns
List of instructions in the format [["operation", ...args]]
Examples
iex> ast = {:literal, 42}
iex> Predicator.Compiler.to_instructions(ast)
[["lit", 42]]
iex> ast = {:comparison, :eq, {:identifier, "name"}, {:literal, "John"}}
iex> Predicator.Compiler.to_instructions(ast)
[["load", "name"], ["lit", "John"], ["compare", "EQ"]]
@spec to_instructions_with_positions( Predicator.Parser.ast() | Predicator.Parser.bare_ast(), keyword() ) :: {[[binary() | term()]], Predicator.Types.position_table()}
Converts an AST to stack machine instructions plus a source-position side table.
The instruction list is identical to to_instructions/2's - the side table is
a separate Elixir-side value, so the interchange format and any stored
compiled artifacts are unaffected (ADR-0001). The table maps each
instruction's 0-based index to the {line, column} of the AST node that
emitted it; nodes with no position contribute no entry, so a caller-supplied
position-free AST yields an empty table.
Examples
iex> ast = {:comparison, :gt, {:identifier, "score", {1, 1}}, {:literal, 85, {1, 9}}, {1, 7}}
iex> Predicator.Compiler.to_instructions_with_positions(ast)
{[["load", "score"], ["lit", 85], ["compare", "GT"]],
%{0 => {1, 1}, 1 => {1, 9}, 2 => {1, 7}}}
iex> Predicator.Compiler.to_instructions_with_positions({:literal, 42})
{[["lit", 42]], %{}}
@spec to_string( Predicator.Parser.ast() | Predicator.Parser.bare_ast(), keyword() ) :: binary()
Converts an AST to a string representation.
Uses the StringVisitor to generate a readable string representation of the Abstract Syntax Tree. This is useful for debugging, documentation, and displaying expressions to users.
Parameters
ast- The Abstract Syntax Tree to convertopts- Optional formatting options::parentheses-:minimal(default) |:explicit|:none:spacing-:normal(default) |:compact|:verbose
Returns
String representation of the AST
Examples
iex> ast = {:literal, 42}
iex> Predicator.Compiler.to_string(ast)
"42"
iex> ast = {:comparison, :gt, {:identifier, "score"}, {:literal, 85}}
iex> Predicator.Compiler.to_string(ast)
"score > 85"
iex> ast = {:comparison, :gt, {:identifier, "age"}, {:literal, 21}}
iex> Predicator.Compiler.to_string(ast, parentheses: :explicit)
"(age > 21)"