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_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
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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
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.
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 |