00001 #include <boost/spirit/core.hpp>
00002 #include <boost/spirit/actor/push_back_actor.hpp>
00003
00004 #include "ast.h"
00005 #include "ifprintf.h"
00006
00007 using namespace std;
00008 using namespace boost::spirit;
00009 using namespace AST;
00118 void Node::set_attribute(string attributeName, Node* child){
00119 printf("%s.%s = %s\n", this->name.c_str(), attributeName.c_str(), child->name.c_str());
00120 this->attributes[attributeName] = child;
00121 }
00126 void Node::set_attribute(string attributeName, string value){
00127 printf("%s.%s = %s\n", this->name.c_str(), attributeName.c_str(), value.c_str());
00128 this->attributes[attributeName] = new Leaf(value);
00129 }
00134 void Node::set_attribute(string attributeName, vector<Node*> vec_nodes){
00135 printf("%s.%s = %i\n", this->name.c_str(), attributeName.c_str(), vec_nodes.size());
00136 this->attributes[attributeName] = new List(vec_nodes);
00137 }
00142 AttributeProxy Node::operator[](string attributeName){
00143 return AttributeProxy(this, attributeName);
00144 }
00151 AttributeProxy Node::addAttribute(const char* attributeName){
00152 this->orderedAttributes.push_back(attributeName);
00153 return this->operator[](attributeName);
00154 }
00160 void Node::toASTFile(FILE* f){
00161 ifprintf(f, "%s\n", this->name.c_str());
00162 for(int i = 0; i < this->orderedAttributes.size(); i++){
00163 string attributeName = orderedAttributes[i];
00164 Node* node = this->attributes[attributeName];
00165 if( node == NULL)
00166 ifprintf(f, "%s NULL\n", attributeName.c_str());
00167 else {
00168 switch(node->nodeType){
00169 case NODE_BRANCH: {
00170 ifprintf(f, "%s {\n", attributeName.c_str());
00171 indent();
00172 this->attributes[attributeName]->toASTFile(f);
00173 outdent();
00174 ifprintf(f, "}\n");
00175 }
00176 break;
00177 case NODE_LIST:{
00178 List* listNode = (List*) node;
00179 ifprintf(f, "%s [\n", attributeName.c_str());
00180 indent();
00181 for(int i = 0; i < listNode->list.size(); i ++){
00182 Node* childNode = listNode->list[i];
00183 ifprintf(f, "{\n");
00184 indent();
00185 listNode->list[i]->toASTFile(f);
00186 outdent();
00187 ifprintf(f, "}\n");
00188 }
00189 outdent();
00190 ifprintf(f, "]\n");
00191 }
00192 break;
00193 case NODE_LEAF:{
00194 Leaf* leafNode = (Leaf*) node;
00195 ifprintf(f, "%s %s\n", attributeName.c_str(), leafNode->value.c_str());
00196 }
00197 break;
00198 case NODE_ID:{
00199 Identifier* idNode = (Identifier*) node;
00200 Leaf* idNodeHandle = (Leaf*) idNode->attributes["handle"];
00201 ifprintf(f, "%s id:%s\n", attributeName.c_str(), idNodeHandle->value.c_str());
00202 }
00203 break;
00204 case NODE_CONSTANT:{
00205 Identifier* idNode = (Identifier*) node;
00206 Leaf* idNodeType = (Leaf*) idNode->attributes["type"];
00207 Leaf* idNodeValue = (Leaf*) idNode->attributes["value"];
00208 ifprintf(f, "%s const:%s:%s\n", attributeName.c_str(), idNodeType->value.c_str(), idNodeValue->value.c_str());
00209 }
00210 break;
00211 }
00212 }
00213 }
00214 }