Class: Loxxy::Ast::ASTVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/loxxy/ast/ast_visitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aTop) ⇒ ASTVisitor

Build a visitor for the given top.

Parameters:

  • aTop (AST::LoxNode)

    the parse tree to visit.

Raises:

  • (StandardError)


16
17
18
19
20
21
# File 'lib/loxxy/ast/ast_visitor.rb', line 16

def initialize(aTop)
  raise StandardError if aTop.nil?

  @top = aTop
  @subscribers = []
end

Instance Attribute Details

#subscribersObject (readonly)

List of objects that subscribed to the visit event notification.



10
11
12
# File 'lib/loxxy/ast/ast_visitor.rb', line 10

def subscribers
  @subscribers
end

#topObject (readonly)

Link to the top node to visit



7
8
9
# File 'lib/loxxy/ast/ast_visitor.rb', line 7

def top
  @top
end

Instance Method Details

#end_visit_ptree(aParseTree) ⇒ Object

Visit event. The visitor has completed the visit of the ptree.

Parameters:

  • aParseTree (Rley::PTre::ParseTree)

    the visited ptree.



50
51
52
# File 'lib/loxxy/ast/ast_visitor.rb', line 50

def end_visit_ptree(aParseTree)
  broadcast(:after_ptree, aParseTree)
end

#startObject

The signal to begin the visit of the top.



37
38
39
40
# File 'lib/loxxy/ast/ast_visitor.rb', line 37

def start # (aRuntime)
  # @runtime = aRuntime
  top.accept(self)
end

#start_visit_ptree(aParseTree) ⇒ Object

Visit event. The visitor is about to visit the ptree.

Parameters:

  • aParseTree (Rley::PTre::ParseTree)

    the ptree to visit.



44
45
46
# File 'lib/loxxy/ast/ast_visitor.rb', line 44

def start_visit_ptree(aParseTree)
  broadcast(:before_ptree, aParseTree)
end

#subscribe(aSubscriber) ⇒ Object

Add a subscriber for the visit event notifications.

Parameters:

  • aSubscriber (Object)


25
26
27
# File 'lib/loxxy/ast/ast_visitor.rb', line 25

def subscribe(aSubscriber)
  subscribers << aSubscriber
end

#unsubscribe(aSubscriber) ⇒ Object

Remove the given object from the subscription list. The object won't be notified of visit events.

Parameters:

  • aSubscriber (Object)


32
33
34
# File 'lib/loxxy/ast/ast_visitor.rb', line 32

def unsubscribe(aSubscriber)
  subscribers.delete_if { |entry| entry == aSubscriber }
end

#visit_assign_expr(anAssignExpr) ⇒ Object

Visit event. The visitor is visiting an assignment node

Parameters:

  • anAssignExpr (AST::LoxAssignExpr)

    the variable assignment node to visit.



119
120
121
122
123
# File 'lib/loxxy/ast/ast_visitor.rb', line 119

def visit_assign_expr(anAssignExpr)
  broadcast(:before_assign_expr, anAssignExpr)
  traverse_subnodes(anAssignExpr)
  broadcast(:after_assign_expr, anAssignExpr, self)
end

#visit_binary_expr(aBinaryExpr) ⇒ Object

Visit event. The visitor is about to visit a binary expression.

Parameters:

  • aBinaryExpr (AST::LOXBinaryExpr)

    the binary expression node to visit



147
148
149
150
151
# File 'lib/loxxy/ast/ast_visitor.rb', line 147

def visit_binary_expr(aBinaryExpr)
  broadcast(:before_binary_expr, aBinaryExpr)
  traverse_subnodes(aBinaryExpr)
  broadcast(:after_binary_expr, aBinaryExpr)
end

#visit_block_stmt(aBlockStmt) ⇒ Object

Visit event. The visitor is about to visit a block statement.

Parameters:

  • aBlockStmt (AST::LOXBlockStmt)

    the print statement node to visit



111
112
113
114
115
# File 'lib/loxxy/ast/ast_visitor.rb', line 111

def visit_block_stmt(aBlockStmt)
  broadcast(:before_block_stmt, aBlockStmt)
  traverse_subnodes(aBlockStmt) unless aBlockStmt.empty?
  broadcast(:after_block_stmt, aBlockStmt)
end

#visit_builtin(aValue) ⇒ Object

Visit event. The visitor is about to visit the given terminal datatype value.

Parameters:

  • aValue (Ast::BuiltinDattype)

    the built-in datatype value



214
215
216
217
# File 'lib/loxxy/ast/ast_visitor.rb', line 214

def visit_builtin(aValue)
  broadcast(:before_visit_builtin, aValue)
  broadcast(:after_visit_builtin, aValue)
end

#visit_call_expr(aCallExpr) ⇒ Object

Visit event. The visitor is about to visit a call expression.

Parameters:

  • aCallExpr (AST::LoxCallExpr)

    call expression to visit



163
164
165
166
167
# File 'lib/loxxy/ast/ast_visitor.rb', line 163

def visit_call_expr(aCallExpr)
  broadcast(:before_call_expr, aCallExpr)
  traverse_subnodes(aCallExpr)
  broadcast(:after_call_expr, aCallExpr, self)
end

#visit_class_stmt(aClassStmt) ⇒ Object

Visit event. The visitor is about to visit a class declaration.

Parameters:

  • aClassStmt (AST::LOXClassStmt)

    the for statement node to visit



72
73
74
75
# File 'lib/loxxy/ast/ast_visitor.rb', line 72

def visit_class_stmt(aClassStmt)
  broadcast(:before_class_stmt, aClassStmt)
  broadcast(:after_class_stmt, aClassStmt, self)
end

#visit_fun_stmt(aFunStmt) ⇒ Object

Visit event. The visitor is about to visit a function statement node.

Parameters:

  • aFunStmt (AST::LoxFunStmt)

    function declaration to visit



221
222
223
224
# File 'lib/loxxy/ast/ast_visitor.rb', line 221

def visit_fun_stmt(aFunStmt)
  broadcast(:before_fun_stmt, aFunStmt, self)
  broadcast(:after_fun_stmt, aFunStmt, self)
end

#visit_get_expr(aGetExpr) ⇒ Object

Parameters:

  • aGetExpr (AST::LOXGetExpr)

    the get expression node to visit



170
171
172
173
# File 'lib/loxxy/ast/ast_visitor.rb', line 170

def visit_get_expr(aGetExpr)
  broadcast(:before_get_expr, aGetExpr)
  broadcast(:after_get_expr, aGetExpr, self)
end

#visit_grouping_expr(aGroupingExpr) ⇒ Object

Visit event. The visitor is about to visit a grouping expression.

Parameters:

  • aGroupingExpr (AST::LoxGroupingExpr)

    grouping expression to visit



177
178
179
180
181
# File 'lib/loxxy/ast/ast_visitor.rb', line 177

def visit_grouping_expr(aGroupingExpr)
  broadcast(:before_grouping_expr, aGroupingExpr)
  traverse_subnodes(aGroupingExpr)
  broadcast(:after_grouping_expr, aGroupingExpr)
end

#visit_if_stmt(anIfStmt) ⇒ Object

Visit event. The visitor is about to visit a if statement.

Parameters:

  • anIfStmt (AST::LOXIfStmt)

    the if statement node to visit



79
80
81
82
83
# File 'lib/loxxy/ast/ast_visitor.rb', line 79

def visit_if_stmt(anIfStmt)
  broadcast(:before_if_stmt, anIfStmt)
  traverse_subnodes(anIfStmt) # The condition is visited/evaluated here...
  broadcast(:after_if_stmt, anIfStmt, self)
end

#visit_literal_expr(aLiteralExpr) ⇒ Object

Visit event. The visitor is visiting the given terminal node containing a datatype object.

Parameters:

  • aLiteralExpr (AST::LoxLiteralExpr)

    the leaf node to visit.



186
187
188
189
# File 'lib/loxxy/ast/ast_visitor.rb', line 186

def visit_literal_expr(aLiteralExpr)
  broadcast(:before_literal_expr, aLiteralExpr)
  broadcast(:after_literal_expr, aLiteralExpr)
end

#visit_logical_expr(aLogicalExpr) ⇒ Object

Visit event. The visitor is about to visit a logical expression. Since logical expressions may take shorcuts by not evaluating all their sub-expressiosns, they are responsible for visiting or not their children.

Parameters:

  • aLogicalExpr (AST::LOXLogicalExpr)

    the logical expression node to visit



135
136
137
138
139
140
141
142
143
# File 'lib/loxxy/ast/ast_visitor.rb', line 135

def visit_logical_expr(aLogicalExpr)
  broadcast(:before_logical_expr, aLogicalExpr)

  # As logical connectors may take a shortcut only the first argument is visited
  traverse_given_subnode(aLogicalExpr, 0)

  # The second child could be visited: this action is deferred in handler
  broadcast(:after_logical_expr, aLogicalExpr, self)
end

#visit_nonterminal(aNonTerminalNode) ⇒ Object

Visit event. The visitor is about to visit the given non terminal node.

Parameters:

  • aNonTerminalNode (Rley::PTree::NonTerminalNode)

    the node to visit.

Raises:

  • (NotImplementedError)


228
229
230
231
232
233
234
# File 'lib/loxxy/ast/ast_visitor.rb', line 228

def visit_nonterminal(aNonTerminalNode)
  # Loxxy interpreter encountered a CST node (Concrete Syntax Tree)
  # that it cannot handle.
  symb = aNonTerminalNode.symbol.name
  msg = "Loxxy cannot execute this code yet for non-terminal symbol '#{symb}'."
  raise NotImplementedError, msg
end

#visit_print_stmt(aPrintStmt) ⇒ Object

Visit event. The visitor is about to visit a print statement.

Parameters:

  • aPrintStmt (AST::LOXPrintStmt)

    the print statement node to visit



87
88
89
90
91
# File 'lib/loxxy/ast/ast_visitor.rb', line 87

def visit_print_stmt(aPrintStmt)
  broadcast(:before_print_stmt, aPrintStmt)
  traverse_subnodes(aPrintStmt)
  broadcast(:after_print_stmt, aPrintStmt)
end

#visit_return_stmt(aReturnStmt) ⇒ Object

Visit event. The visitor is about to visit a return statement.

Parameters:

  • aReturnStmt (AST::LOXReturnStmt)

    the return statement node to visit



95
96
97
98
99
# File 'lib/loxxy/ast/ast_visitor.rb', line 95

def visit_return_stmt(aReturnStmt)
  broadcast(:before_return_stmt, aReturnStmt)
  traverse_subnodes(aReturnStmt)
  broadcast(:after_return_stmt, aReturnStmt, self)
end

#visit_seq_decl(aSeqDecls) ⇒ Object

Visit event. The visitor is about to visit a variable declaration statement.

Parameters:

  • aSeqDecls (AST::LOXSeqDecl)

    the variable declaration node to visit



56
57
58
59
60
# File 'lib/loxxy/ast/ast_visitor.rb', line 56

def visit_seq_decl(aSeqDecls)
  broadcast(:before_seq_decl, aSeqDecls)
  traverse_subnodes(aSeqDecls)
  broadcast(:after_seq_decl, aSeqDecls)
end

#visit_set_expr(aSetExpr) ⇒ Object

Parameters:

  • aSetExpr (AST::LOXGetExpr)

    the get expression node to visit



126
127
128
129
# File 'lib/loxxy/ast/ast_visitor.rb', line 126

def visit_set_expr(aSetExpr)
  broadcast(:before_set_expr, aSetExpr, self)
  broadcast(:after_set_expr, aSetExpr, self)
end

#visit_super_expr(aSuperExpr) ⇒ Object

Visit event. The visitor is about to visit the super keyword.

Parameters:



207
208
209
210
# File 'lib/loxxy/ast/ast_visitor.rb', line 207

def visit_super_expr(aSuperExpr)
  broadcast(:before_super_expr, aSuperExpr)
  broadcast(:after_super_expr, aSuperExpr, self)
end

#visit_this_expr(aThisExpr) ⇒ Object

Visit event. The visitor is about to visit the this keyword.

Parameters:



200
201
202
203
# File 'lib/loxxy/ast/ast_visitor.rb', line 200

def visit_this_expr(aThisExpr)
  broadcast(:before_this_expr, aThisExpr)
  broadcast(:after_this_expr, aThisExpr, self)
end

#visit_unary_expr(anUnaryExpr) ⇒ Object

Visit event. The visitor is about to visit an unary expression.

Parameters:

  • anUnaryExpr (AST::anUnaryExpr)

    unary expression node to visit



155
156
157
158
159
# File 'lib/loxxy/ast/ast_visitor.rb', line 155

def visit_unary_expr(anUnaryExpr)
  broadcast(:before_unary_expr, anUnaryExpr)
  traverse_subnodes(anUnaryExpr)
  broadcast(:after_unary_expr, anUnaryExpr)
end

#visit_var_stmt(aVarStmt) ⇒ Object

Visit event. The visitor is about to visit a variable declaration statement.

Parameters:

  • aVarStmt (AST::LOXVarStmt)

    the variable declaration node to visit



64
65
66
67
68
# File 'lib/loxxy/ast/ast_visitor.rb', line 64

def visit_var_stmt(aVarStmt)
  broadcast(:before_var_stmt, aVarStmt)
  traverse_subnodes(aVarStmt)
  broadcast(:after_var_stmt, aVarStmt)
end

#visit_variable_expr(aVariableExpr) ⇒ Object

Visit event. The visitor is visiting a variable usage node

Parameters:

  • aVariableExpr (AST::LoxVariableExpr)

    the variable reference node to visit.



193
194
195
196
# File 'lib/loxxy/ast/ast_visitor.rb', line 193

def visit_variable_expr(aVariableExpr)
  broadcast(:before_variable_expr, aVariableExpr)
  broadcast(:after_variable_expr, aVariableExpr, self)
end

#visit_while_stmt(aWhileStmt) ⇒ Object

Visit event. The visitor is about to visit a while statement node.

Parameters:

  • aWhileStmt (AST::LOXWhileStmt)

    the while statement node to visit



103
104
105
106
107
# File 'lib/loxxy/ast/ast_visitor.rb', line 103

def visit_while_stmt(aWhileStmt)
  broadcast(:before_while_stmt, aWhileStmt)
  traverse_subnodes(aWhileStmt) if aWhileStmt.condition # The condition is visited/evaluated here...
  broadcast(:after_while_stmt, aWhileStmt, self)
end