Changed the FNV include path
[fridhskrift.git] / interpreter / symbol.cpp
blobdf1225cddbaaa9bdda4bee0ac4d48b42d9f47dfc
1 #include <iostream>
3 #include <fridh/symbol.hpp>
5 namespace fridh
7 symbol_tree_node::symbol_tree_node():
8 type(symbol::uninitialised),
9 parent(0)
11 //std::cout << "new symbol_tree_node default " << (void *)this << std::endl;
14 symbol_tree_node::symbol_tree_node(symbol_tree_node const & other)
16 copy(other);
19 symbol_tree_node::symbol_tree_node(symbol::type type):
20 type(type),
21 parent(0)
23 //std::cout << "new symbol_tree_node " << (void *)this << std::endl;
25 switch(type)
27 case symbol::function:
28 function_pointer = new function;
29 break;
31 case symbol::class_symbol:
32 class_pointer = new class_type;
33 break;
37 symbol_tree_node::~symbol_tree_node()
39 destroy();
42 symbol_tree_node & symbol_tree_node::operator=(symbol_tree_node const & other)
44 destroy();
45 copy(other);
46 return *this;
49 void symbol_tree_node::copy(symbol_tree_node const & other)
51 //std::cout << "symbol_tree_node " << (void *)this << " from " << (void *)&other << std::endl;
53 type = other.type;
54 parent = other.parent;
56 for(node_children::const_iterator i = other.children.begin(), end = other.children.end(); i != end; i++)
58 symbol_tree_node * & node_pointer = children[i->first];
59 node_pointer = new symbol_tree_node(*i->second);
60 node_pointer->parent = this;
63 #define COPY_MEMBER(type, member_type, member) \
64 case symbol::type: \
65 member = new member_type(*other.member); \
66 break;
68 switch(type)
70 COPY_MEMBER(function, function, function_pointer);
71 COPY_MEMBER(class_symbol, class_type, class_pointer);
72 COPY_MEMBER(module, module, module_pointer);
75 #undef COPY_MEMBER
79 void symbol_tree_node::destroy()
81 //std::cout << "~symbol_tree_node " << (void *)this << std::endl;
83 for(node_children::iterator i = children.begin(), end = children.end(); i != end; i++)
84 delete i->second;
86 #define DELETE_MEMBER(type, member) \
87 case symbol::type: \
88 delete member; \
89 break;
91 switch(type)
93 DELETE_MEMBER(function, function_pointer);
94 DELETE_MEMBER(class_symbol, class_pointer);
95 DELETE_MEMBER(module, module_pointer);
98 type = symbol::uninitialised;
100 #undef DELETE_MEMBER
104 bool symbol_tree_node::exists(std::string const & name)
106 //std::cout << "exists " << (void *)this << std::endl;
108 node_children::iterator iterator = children.find(name);
109 return iterator != children.end();
112 bool symbol_tree_node::find_entity(std::string const & name, symbol_tree_node * & output)
114 node_children::iterator iterator = children.find(name);
115 if(iterator == children.end())
117 if(parent == 0)
118 return false;
119 return parent->find_entity(name, output);
122 output = iterator->second;
123 return true;