The following, is the class inheritance for the nodes on the parse-tree, that are in the AST. Now the interpreter or a conversion tool like Octave-2-Matlab will walk this tree. The AST Walker implements the visitor pattern supported by the tree object.

We can now look into the organization of the AST Walker, which is neatly documented/illustrated in this working piece of code at /cvs/octave/src/pt-pr-code.{cc|h}. This AST walker inherits from the base class as follows,

Now a debugger, code conversion tool, lint-like checker or whatever has to conveniently invoke the parser for the AST, and then walk it for whatever operations it needs to perform on the tree.
virtual void visit_index_expression (tree_index_expression &)=0
virtual void visit_matrix (tree_matrix &)=0
virtual void visit_cell (tree_cell &)=0
virtual void visit_multi_assignment (tree_multi_assignment &)=0
virtual void visit_no_op_command (tree_no_op_command &)=0
virtual void visit_constant (tree_constant &)=0
virtual void visit_fcn_handle (tree_fcn_handle &)=0
virtual void visit_parameter_list (tree_parameter_list &)=0
and many more functions that correspond to each language-object that derives from tree, and relevant functions that 'execute' that command or expression.
So the organization of all this code would start at the front end like:
1 main
2 begin
3 AST=call-parser()
4 cwalk=Code_Converting_Walker(OCT2MAT)
5 cwalk.set_tree(AST)
6 cwalk.visit()
7 cleanup abracadabra
8 end
So that settles it all, if you know what to write within the pure virtual functions, visit_index_expression (tree_index_expression &)=0 and such. This conversion code is put simply in the previous blogpost by walking this tree in a post-order fashion.Next time we could see how to write the Oct2Mat converter based on the parse tree, or atleast drive the tree-printer class code.
Cheers,
Muthu
1 comment:
[ I am talking from C++ perspective ]
visitor pattern is nice to seperate algorithms frm data structures.. But the names which you have selected doesnt utilize the feature called "function overloading" [ again in C++ ]..
Though double dispatching will be achieved with your idiom, it will be more "ELEGANT" if the structure which request its visiting call the functions like [visitor_impl].visit( this ) rather than [visitor_impl].visit_x_y_z( this ).. Thereby lettin the program to decide which visit method to pick..
virtual void visit(tree_index_expression &)=0
virtual void visit(tree_matrix &)=0
virtual void visit(tree_cell )=0
virtual void visit(tree_multi_assignment &)=0
virtual void visit (tree_no_op_command &)=0
virtual void visit(tree_constant &)=0
virtual void visit(tree_fcn_handle &)=0
virtual void visit(tree_parameter_list &)=0
Post a Comment