tree.cpp

Go to the documentation of this file.
00001 //  This file is part of jdl_parser.
00002 //
00003 //  jdl_parser is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  jdl_parser is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with jdl_parser.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 // Copyright Simone Pellegrini 2007-2008; e-mail: motonacciu@gmail.com
00017 
00018 #include "tree.hpp"
00019 #include "visitor.hpp"
00020 
00021 #include <cctype>
00022 #include <algorithm>
00023 
00024 #include <boost/ref.hpp>
00025 
00026 namespace jdl{
00027 
00028 void create_activation_record(parser_stack<boost::shared_ptr<component> >& node_stack){
00029 #ifdef DEBUG
00030         cout << "Pushing new Activation Record." << endl;
00031 #endif
00032         node_stack.new_ar();
00033 }
00034 
00035 void reduce_stack(parser_stack<boost::shared_ptr<component> >& node_stack){
00036 #ifdef DEBUG
00037         cout << "Reducing stack." << endl;
00038 #endif
00039     vector<boost::shared_ptr<component> > &v = node_stack.pop();
00040         assert(v.size() == 1);
00041         node_stack.push(v[0]);
00042         delete &v;
00043 }
00044 
00045 void discard_activation_record(parser_stack<boost::shared_ptr<component> >& node_stack){
00046         vector<boost::shared_ptr<component> > &v = node_stack.pop();
00047 //      vector<boost::shared_ptr<component> >::iterator it = v.begin();
00048 //      while(it != v.end())
00049 //              delete *(it++);
00050         delete &v;
00051 }
00052 
00053 void create(attribute_definition_node &node, parser_stack<boost::shared_ptr<component> >& stack){
00054         vector<boost::shared_ptr<component> > &v = stack.pop();
00055 //      cout << "Activation record size: " << v.size() << endl;
00056         assert(v.size() == 2);
00057         
00058         boost::shared_ptr<string_node> sn = boost::dynamic_pointer_cast<string_node>(v[1]);
00059         assert(sn != NULL);
00060         node.set_name(sn);
00061         node.set_value(v[0]);
00062         delete &v;
00063 }
00064 
00065 void create(conditional_expr &node, parser_stack<boost::shared_ptr<component> >& stack){
00066         // else
00067         vector<boost::shared_ptr<component> > &v = stack.pop();
00068         assert(v.size() == 3);
00069         node.set_else(v[0]);
00070         // then
00071         node.set_then(v[1]);
00072         // if
00073         node.set_condition(v[2]);
00074         //cout << node.to_string() << endl;
00075         stack.new_ar();
00076         delete &v;
00077 }
00078 
00079 void create(binary_expr_node &node, parser_stack<boost::shared_ptr<component> >& stack){
00080         vector<boost::shared_ptr<component> > &v = stack.pop();
00081 //      cout << "Activation record size: " << v.size() << endl;
00082         assert(v.size() == 3);
00083         
00084         node.left_operand(v[2]);
00085         node.set_operator(boost::dynamic_pointer_cast<abstract_leaf<string> >(v[1])->data());
00086         node.right_operand(v[0]);
00087         
00088         stack.new_ar();
00089         delete &v;
00090 }
00091 
00092 void create(bracket_node &node, parser_stack<boost::shared_ptr<component> >& stack){
00093         vector<boost::shared_ptr<component> > &v = stack.pop();
00094         assert(v.size() == 2);
00095         
00096         node.left(v[1]);
00097         node.right(v[0]);
00098         
00099         stack.new_ar();
00100         delete &v;
00101 }
00102 
00103 void create(dot_node &node, parser_stack<boost::shared_ptr<component> >& stack){
00104         vector<boost::shared_ptr<component> > &v = stack.pop();
00105         assert(v.size() == 2);
00106         
00107         node.left(v[1]);
00108         node.right(v[0]);
00109         
00110         stack.new_ar();
00111         delete &v;
00112 }
00113 
00114 void create(term_node &node, parser_stack<boost::shared_ptr<component> >& stack){
00115         vector<boost::shared_ptr<component> > &v = stack.pop();
00116 //      assert(v.size() == 1);
00117         vector<boost::shared_ptr<component> >::reverse_iterator it = v.rbegin();
00118         while(it != v.rend())
00119                 node.add_child(*(it++));
00120         stack.new_ar();
00121         delete &v;
00122 }
00123 
00124 void create(list_node &node, parser_stack<boost::shared_ptr<component> >& stack){
00125         vector<boost::shared_ptr<component> > &v = stack.pop();
00126 //      assert(v.size() == 1);
00127         vector<boost::shared_ptr<component> >::reverse_iterator it = v.rbegin();
00128         while(it != v.rend())
00129                 node.add_child(*(it++));
00130         stack.new_ar();
00131         delete &v;
00132 }
00133 
00134 void create(func_call_node &node, parser_stack<boost::shared_ptr<component> >& stack){
00135         vector<boost::shared_ptr<component> > &v = stack.pop();
00136         assert(v.size() == 2);
00137         
00138         node.func_name(v[1]);
00139         node.args(v[0]);
00140         stack.new_ar();
00141         delete &v;
00142 }
00143 
00144 void create(unary_expr_node &node, parser_stack<boost::shared_ptr<component> >& stack){
00145         vector<boost::shared_ptr<component> > &v = stack.pop();
00146         assert(v.size() == 2);
00147         
00148         node.operand(v[0]);
00149         node.set_operator((boost::dynamic_pointer_cast<string_node>(v[1]))->data());
00150         stack.new_ar();
00151         delete &v;
00152 }
00153 
00154 abstract_node::~abstract_node(){
00155         //#ifdef DEGUG
00156 //              cout << "Invoking COMPONENT DESTRUCTOR (node: " << endl;
00157         //#endif
00158 //      vector<component *>::const_iterator it = children.begin();
00159 //      while(it != children.end())
00160 //              delete *(it++);
00161 } 
00162 }
00164 // binary_expr_node
00166 string binary_expr_node::to_string() const{
00167         return "BIN_EXPR:{ " + 
00168                 children[0]->to_string() + 
00169                 " " + _op + " "  + children[1]->to_string() + " }";
00170 }
00171 void binary_expr_node::accept(const_visitor &v) const{
00172         v.visit(*this);
00173 }
00174 
00175 void binary_expr_node::accept(visitor &v){
00176         v.visit(*this);
00177 }
00179 // unary_expr_node
00181 string unary_expr_node::to_string() const{
00182         return "UNARY_EXPR: [ " + _op +
00183                 children[0]->to_string() +  " ]";
00184 }               
00185 void unary_expr_node::accept(const_visitor &v) const{
00186         v.visit(*this);
00187 }
00188 
00189 void unary_expr_node::accept(visitor &v){
00190         v.visit(*this);
00191 }
00193 // attribute_definition_node
00195 string attribute_definition_node::to_string() const{
00196         return "ATTR_DEF_NODE( NAME: " + 
00197                 children[0]->to_string() + 
00198                 " VAL: " + children[1]->to_string() + " )";
00199 }
00200 void attribute_definition_node::accept(const_visitor &v) const{
00201         v.visit(*this);
00202 }
00203 
00204 void attribute_definition_node::accept(visitor &v){
00205         v.visit(*this);
00206 }
00208 // conditional_expr
00210 string conditional_expr::to_string() const{
00211         return "CONDITIONAL_EXPR: " + 
00212                 children[0]->to_string() + " ? " + 
00213                 children[1]->to_string() + " : " + 
00214                 children[2]->to_string();
00215 }
00216 void conditional_expr::accept(const_visitor &v) const{
00217         v.visit(*this);
00218 }
00219 void conditional_expr::accept(visitor &v){
00220         v.visit(*this);
00221 }
00223 // dot_node
00225 string dot_node::to_string() const{ 
00226         return "DOT_NODE: " + children[0]->to_string() + "." + children[1]->to_string(); 
00227 }
00228 void dot_node::accept(const_visitor &v) const{
00229         v.visit(*this);
00230 }
00231 void dot_node::accept(visitor &v){
00232         v.visit(*this);
00233 }
00235 // bracket_node
00237 string bracket_node::to_string() const{ 
00238         return "BRAKET_NODE: " + children[0]->to_string() + "[" + children[1]->to_string() + "]"; 
00239 }
00240 void bracket_node::accept(const_visitor &v) const{
00241         v.visit(*this);
00242 }
00243 void bracket_node::accept(visitor &v){
00244         v.visit(*this);
00245 }
00247 // term_node
00249 string term_node::to_string() const{
00250         string s("TERM_NODE: {");
00251         vector<boost::shared_ptr<component> >::const_iterator it = children.begin();
00252         while(it != children.end()){
00253                 s += (*(it++))->to_string();
00254                 if(it != children.end())
00255                         s += ", ";
00256         }
00257         s+=" }";
00258         return s;
00259 }
00260 void term_node::accept(const_visitor &v) const{
00261         v.visit(*this);
00262 }
00263 void term_node::accept(visitor &v){
00264         v.visit(*this);
00265 }
00267 // list_node
00269 string list_node::to_string() const{
00270         string s("LIST_NODE: {");
00271         vector<boost::shared_ptr<component> >::const_iterator it = children.begin();
00272         while(it != children.end()){
00273                 s += (*(it++))->to_string();
00274                 if(it != children.end())
00275                         s += ", ";
00276         }
00277         s+=" }";
00278         return s;
00279 }
00280 void list_node::accept(const_visitor &v) const{
00281         v.visit(*this);
00282 }
00283 void list_node::accept(visitor &v){
00284         v.visit(*this);
00285 }
00287 // func_call_node
00289 string func_call_node::to_string() const {
00290         string s("FUNC_CALL_NODE: ");
00291         s += children[0]->to_string() + "(" + children[1]->to_string() + ")";
00292         return s;
00293 }
00294 void func_call_node::accept(const_visitor &v) const{
00295         v.visit(*this);
00296 }
00297 void func_call_node::accept(visitor &v){
00298         v.visit(*this);
00299 }
00301 // int_node
00303 void int_node::accept(const_visitor &v) const{
00304         v.visit(*this);
00305 }
00306 void int_node::accept(visitor &v){
00307         v.visit(*this);
00308 }
00310 // string_node
00312 void string_node::accept(const_visitor &v) const{
00313         v.visit(*this);
00314 }
00315 void string_node::accept(visitor &v) {
00316         v.visit(*this);
00317 }
00319 // string_literal_node
00321 void string_literal_node::accept(const_visitor &v) const{
00322         v.visit(*this);
00323 }
00324 void string_literal_node::accept(visitor &v){
00325         v.visit(*this);
00326 }
00328 // float_node
00330 void float_node::accept(const_visitor &v) const{
00331         v.visit(*this);
00332 }
00333 void float_node::accept(visitor &v){
00334         v.visit(*this);
00335 }

Generated on Sun Feb 17 16:55:14 2008 for JDLParser by  doxygen 1.5.3