Predicator.Visitors.InstructionsVisitor (predicator v2.2.0)
View SourceVisitor that converts AST nodes to stack machine instructions.
This visitor implements post-order traversal to generate instruction lists that can be executed by the stack-based evaluator. Instructions are generated in the correct order for stack-based evaluation.
Examples
iex> ast = {:literal, 42}
iex> Predicator.Visitors.InstructionsVisitor.visit(ast, [])
[["lit", 42]]
iex> ast = {:identifier, "score"}
iex> Predicator.Visitors.InstructionsVisitor.visit(ast, [])
[["load", "score"]]
iex> ast = {:comparison, :gt, {:identifier, "score"}, {:literal, 85}}
iex> Predicator.Visitors.InstructionsVisitor.visit(ast, [])
[["load", "score"], ["lit", 85], ["compare", "GT"]]
iex> ast = {:logical_and, {:literal, true}, {:literal, false}}
iex> Predicator.Visitors.InstructionsVisitor.visit(ast, [])
[["lit", true], ["lit", false], ["and"]]
iex> ast = {:function_call, "len", [{:identifier, "name"}]}
iex> Predicator.Visitors.InstructionsVisitor.visit(ast, [])
[["load", "name"], ["call", "len", 1]]
Summary
Functions
Visits an AST node and returns stack machine instructions.
Functions
@spec visit( Predicator.Parser.ast(), keyword() ) :: [[binary() | term()]]
Visits an AST node and returns stack machine instructions.
Uses post-order traversal to ensure operands are pushed onto the stack before operators are applied.
Parameters
ast_node
- The AST node to convert to instructionsopts
- Optional visitor options (currently unused)
Returns
List of instructions in the format [["operation", ...args]]