Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

AST::Node Class Reference

Root node class, can save / load itself to its own file format, as well as be converted to 3ac subclasses declare the attributes they must have subclasses implement to3ac functionality. More...

#include <ast.h>

Inheritance diagram for AST::Node:

AST::Assignment AST::Constant AST::Def_Array AST::Def_Function AST::Def_Struct AST::Def_Var AST::FunctionCall AST::Identifier AST::If AST::Jump AST::Leaf AST::List AST::Loop AST::Op_Binary AST::Op_Bracket AST::Op_Dot AST::Op_Ternary AST::Op_Unary AST::Program AST::Return List of all members.

Public Member Functions

 Node (string new_name)
 Constructor -> subclassed to a parameterless construct that sets this name according to their type -> (If, Identifier).
virtual void to3ACFile (FILE *f)=0
 convert me to 3ac - subclassed
void toASTFile (FILE *f)
 recursively dump me and my child nodes to a file
void set_attribute (string attributeName, Node *child)
 create a new attribute pointing to another node
void set_attribute (string attributeName, string value)
 create a new attribute pointing to a new leaf node with str as its value
void set_attribute (string attributeName, vector< Node * > vec_nodes)
 create a new attribute pointing to a new list who has child nodes containing vec_nodes
AttributeProxy operator[] (string attributeName)
 allows node['hello'] = something
const char * get_name ()

Protected Types

enum  {
  NODE_BRANCH, NODE_LIST, NODE_LEAF, NODE_ID,
  NODE_CONSTANT
}
 Used for effecient type checking of subclassed nodes we only need to identify the special children, so this does not contain all the subclasses. More...

Protected Member Functions

AttributeProxy addAttribute (const char *attributeName)
 declare that this class requires a particular attributeproxy -> also determines the order in which they are saved

Protected Attributes

enum AST::Node:: { ... }  nodeType
 Used for effecient type checking of subclassed nodes we only need to identify the special children, so this does not contain all the subclasses.
map< string, Node * > attributes
 maps attribute's from name to objects
string name

Private Attributes

vector< string > orderedAttributes
 ordered list of attributes to save to file

Detailed Description

Root node class, can save / load itself to its own file format, as well as be converted to 3ac subclasses declare the attributes they must have subclasses implement to3ac functionality.

Definition at line 17 of file ast.h.


Member Enumeration Documentation

anonymous enum [protected]
 

Used for effecient type checking of subclassed nodes we only need to identify the special children, so this does not contain all the subclasses.

Enumeration values:
NODE_BRANCH 
NODE_LIST 
NODE_LEAF 
NODE_ID 
NODE_CONSTANT 

Definition at line 24 of file ast.h.

00024          {  
00025         NODE_BRANCH,
00026         NODE_LIST,
00027         NODE_LEAF,
00028         NODE_ID,
00029         NODE_CONSTANT,
00030     } nodeType; 


Constructor & Destructor Documentation

AST::Node::Node string  new_name  )  [inline]
 

Constructor -> subclassed to a parameterless construct that sets this name according to their type -> (If, Identifier).

Definition at line 33 of file ast.h.

00033                          :
00034         name(new_name),
00035         nodeType(NODE_BRANCH)
00036     {
00037     }
public:


Member Function Documentation

AttributeProxy Node::addAttribute const char *  attributeName  )  [protected]
 

declare that this class requires a particular attributeproxy -> also determines the order in which they are saved

Called by sublcasses to declare the attributes they need Also defines the order in which attributes are saved to file

Parameters:
attributeName name of the attribute I am defining
Returns:
a new AttributeProxy to which I can assign later -> for nice program->statements =

Definition at line 151 of file ast.cc.

References operator[](), and orderedAttributes.

00151                                                           {
00152     this->orderedAttributes.push_back(attributeName);
00153     return this->operator[](attributeName);
00154 }

const char* AST::Node::get_name  )  [inline]
 

Definition at line 45 of file ast.h.

References name.

00045 { return this->name.c_str(); }

AttributeProxy Node::operator[] string  attributeName  ) 
 

allows node['hello'] = something

Returns an attribute proxy for more convienent attribute setting

Parameters:
attributeName name of the attribute to be set

Definition at line 142 of file ast.cc.

Referenced by addAttribute().

00142                                                    {
00143      return AttributeProxy(this, attributeName);
00144 }

void Node::set_attribute string  attributeName,
vector< Node * >  vec_nodes
 

create a new attribute pointing to a new list who has child nodes containing vec_nodes

Instantiate a new AST::List node containing the list of nodes

Parameters:
vec_nodes STL::vector of nodes to be part of the list

Definition at line 134 of file ast.cc.

References attributes.

00134                                                                      {
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 }

void Node::set_attribute string  attributeName,
string  value
 

create a new attribute pointing to a new leaf node with str as its value

instantiate a Leaf node containing the string value and set it as the child

Parameters:
string -> value for this node to contain

Definition at line 126 of file ast.cc.

References attributes.

00126                                                           {
00127     printf("%s.%s = %s\n", this->name.c_str(), attributeName.c_str(), value.c_str());
00128     this->attributes[attributeName] = new Leaf(value);
00129 }

void Node::set_attribute string  attributeName,
Node child
 

create a new attribute pointing to another node

Set this attribute to point to some other child node

Definition at line 118 of file ast.cc.

References attributes, and name.

Referenced by AST::AttributeProxy::operator=().

00118                                                          {
00119     printf("%s.%s = %s\n", this->name.c_str(), attributeName.c_str(), child->name.c_str());
00120     this->attributes[attributeName] = child;
00121 }

virtual void AST::Node::to3ACFile FILE *  f  )  [pure virtual]
 

convert me to 3ac - subclassed

Implemented in AST::List, AST::Leaf, AST::Program, AST::Def_Function, AST::Def_Struct, AST::If, AST::Jump, AST::Loop, AST::Return, AST::Op_Unary, AST::Op_Binary, AST::Op_Ternary, AST::Assignment, AST::Constant, AST::FunctionCall, AST::Identifier, AST::Op_Bracket, AST::Op_Dot, AST::Def_Var, and AST::Def_Array.

void Node::toASTFile FILE *  f  ) 
 

recursively dump me and my child nodes to a file

Dump all the data representing this AST into a file This will be a recursive call, with some clever outputing to deal with the shortcuts

Parameters:
f stream to which I should write output -- must be writeable

Definition at line 160 of file ast.cc.

References attributes, ifprintf(), indent(), AST::List::list, NODE_BRANCH, NODE_CONSTANT, NODE_ID, NODE_LEAF, NODE_LIST, nodeType, orderedAttributes, outdent(), and AST::Leaf::value.

Referenced by main().

00160                            {
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 }


Member Data Documentation

map<string, Node*> AST::Node::attributes [protected]
 

maps attribute's from name to objects

Definition at line 51 of file ast.h.

Referenced by set_attribute(), and toASTFile().

string AST::Node::name [protected]
 

Definition at line 52 of file ast.h.

Referenced by get_name(), and set_attribute().

enum { ... } AST::Node::nodeType [protected]
 

Used for effecient type checking of subclassed nodes we only need to identify the special children, so this does not contain all the subclasses.

Referenced by AST::Constant::Constant(), AST::Identifier::Identifier(), AST::Leaf::Leaf(), AST::List::List(), and toASTFile().

vector<string> AST::Node::orderedAttributes [private]
 

ordered list of attributes to save to file

Definition at line 54 of file ast.h.

Referenced by addAttribute(), and toASTFile().


The documentation for this class was generated from the following files:
Generated on Thu Oct 20 12:00:04 2005 for ASTree by  doxygen 1.4.2-20050421