00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00048
00049
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
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
00067 vector<boost::shared_ptr<component> > &v = stack.pop();
00068 assert(v.size() == 3);
00069 node.set_else(v[0]);
00070
00071 node.set_then(v[1]);
00072
00073 node.set_condition(v[2]);
00074
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
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
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
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
00156
00157
00158
00159
00160
00161 }
00162 }
00164
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
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
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
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
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
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
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
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
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
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
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
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
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 }