Class: Loxxy::Ast::ASTVisitor
- Inherits:
-
Object
- Object
- Loxxy::Ast::ASTVisitor
- Defined in:
- lib/loxxy/ast/ast_visitor.rb
Instance Attribute Summary collapse
-
#subscribers ⇒ Object
readonly
List of objects that subscribed to the visit event notification.
-
#top ⇒ Object
readonly
Link to the top node to visit.
Instance Method Summary collapse
-
#end_visit_ptree(aParseTree) ⇒ Object
Visit event.
-
#initialize(aTop) ⇒ ASTVisitor
constructor
Build a visitor for the given top.
-
#start ⇒ Object
The signal to begin the visit of the top.
-
#start_visit_ptree(aParseTree) ⇒ Object
Visit event.
-
#subscribe(aSubscriber) ⇒ Object
Add a subscriber for the visit event notifications.
-
#unsubscribe(aSubscriber) ⇒ Object
Remove the given object from the subscription list.
-
#visit_assign_expr(anAssignExpr) ⇒ Object
Visit event.
-
#visit_binary_expr(aBinaryExpr) ⇒ Object
Visit event.
-
#visit_block_stmt(aBlockStmt) ⇒ Object
Visit event.
-
#visit_builtin(aValue) ⇒ Object
Visit event.
-
#visit_call_expr(aCallExpr) ⇒ Object
Visit event.
-
#visit_class_stmt(aClassStmt) ⇒ Object
Visit event.
-
#visit_for_stmt(aForStmt) ⇒ Object
Visit event.
-
#visit_fun_stmt(aFunStmt) ⇒ Object
Visit event.
- #visit_get_expr(aGetExpr) ⇒ Object
-
#visit_grouping_expr(aGroupingExpr) ⇒ Object
Visit event.
-
#visit_if_stmt(anIfStmt) ⇒ Object
Visit event.
-
#visit_literal_expr(aLiteralExpr) ⇒ Object
Visit event.
-
#visit_logical_expr(aLogicalExpr) ⇒ Object
Visit event.
-
#visit_nonterminal(aNonTerminalNode) ⇒ Object
Visit event.
-
#visit_print_stmt(aPrintStmt) ⇒ Object
Visit event.
-
#visit_return_stmt(aReturnStmt) ⇒ Object
Visit event.
-
#visit_seq_decl(aSeqDecls) ⇒ Object
Visit event.
- #visit_set_expr(aSetExpr) ⇒ Object
-
#visit_super_expr(aSuperExpr) ⇒ Object
Visit event.
-
#visit_this_expr(aThisExpr) ⇒ Object
Visit event.
-
#visit_unary_expr(anUnaryExpr) ⇒ Object
Visit event.
-
#visit_var_stmt(aVarStmt) ⇒ Object
Visit event.
-
#visit_variable_expr(aVariableExpr) ⇒ Object
Visit event.
-
#visit_while_stmt(aWhileStmt) ⇒ Object
Visit event.
Constructor Details
#initialize(aTop) ⇒ ASTVisitor
Build a visitor for the given top.
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
#subscribers ⇒ Object (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 |
#top ⇒ Object (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.
50 51 52 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 50 def end_visit_ptree(aParseTree) broadcast(:after_ptree, aParseTree) end |
#start ⇒ Object
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.
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.
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.
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
128 129 130 131 132 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 128 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.
157 158 159 160 161 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 157 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.
120 121 122 123 124 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 120 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.
224 225 226 227 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 224 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.
173 174 175 176 177 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 173 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.
72 73 74 75 76 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 72 def visit_class_stmt(aClassStmt) broadcast(:before_class_stmt, aClassStmt) traverse_subnodes(aClassStmt) # The methods are visited here... broadcast(:after_class_stmt, aClassStmt, self) end |
#visit_for_stmt(aForStmt) ⇒ Object
Visit event. The visitor is about to visit a for statement.
80 81 82 83 84 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 80 def visit_for_stmt(aForStmt) broadcast(:before_for_stmt, aForStmt) traverse_subnodes(aForStmt) # The condition is visited/evaluated here... broadcast(:after_for_stmt, aForStmt, self) end |
#visit_fun_stmt(aFunStmt) ⇒ Object
Visit event. The visitor is about to visit a function statement node.
231 232 233 234 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 231 def visit_fun_stmt(aFunStmt) broadcast(:before_fun_stmt, aFunStmt, self) broadcast(:after_fun_stmt, aFunStmt, self) end |
#visit_get_expr(aGetExpr) ⇒ Object
180 181 182 183 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 180 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.
187 188 189 190 191 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 187 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.
88 89 90 91 92 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 88 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.
196 197 198 199 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 196 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.
145 146 147 148 149 150 151 152 153 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 145 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.
238 239 240 241 242 243 244 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 238 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.
96 97 98 99 100 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 96 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.
104 105 106 107 108 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 104 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.
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
135 136 137 138 139 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 135 def visit_set_expr(aSetExpr) broadcast(:before_set_expr, aSetExpr) traverse_subnodes(aSetExpr) broadcast(:after_set_expr, aSetExpr, self) end |
#visit_super_expr(aSuperExpr) ⇒ Object
Visit event. The visitor is about to visit the super keyword.
217 218 219 220 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 217 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.
210 211 212 213 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 210 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.
165 166 167 168 169 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 165 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.
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
203 204 205 206 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 203 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.
112 113 114 115 116 |
# File 'lib/loxxy/ast/ast_visitor.rb', line 112 def visit_while_stmt(aWhileStmt) broadcast(:before_while_stmt, aWhileStmt) traverse_subnodes(aWhileStmt) # The condition is visited/evaluated here... broadcast(:after_while_stmt, aWhileStmt, self) end |