00001
00002
00003
00004 #ifndef DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOL_H
00005 #define DEV_W3_ORG__2001_BLINDFOLD_SRC_SYMBOL_H
00006 #include "config.h"
00007
00008 #include <cassert>
00009 #include <string>
00010 #include <hash_map>
00011 #include <vector>
00012
00013 class VariableScope;
00014 class SymbolTable;
00015
00041 class Symbol {
00042
00043 public:
00044
00045
00046 public:
00047 Symbol(SymbolTable* table = 0, int index = 0);
00048 Symbol(const Symbol& other);
00049 const Symbol& operator=(const Symbol& other);
00050 bool operator==(const Symbol& other) const
00051 { return _table == other._table && _index == other._index; }
00052 bool operator<(const Symbol& other) const { return _index < other._index; }
00053 size_t hash() const { return _index; }
00054
00055 friend std::ostream& operator<<(std::ostream& s, const Symbol& me);
00056 std::ostream& print_to(std::ostream& stream) const;
00057 ~Symbol();
00058
00059
00060
00061 enum Type { VARIABLE, CONSTANT, LITERAL, NULL_SYMBOL, NUM_TYPES };
00062
00063 Symbol(Type type, const char* name);
00064
00065 static const Symbol null;
00066
00067 #if 0
00068
00083 static Symbol variable(VariableScope* getScope(), const char* name=0) {
00084 return Symbol(VARIABLE, name);
00085 }
00086 #endif
00087
00088 static Symbol constant(const char* name) {
00089 return Symbol(CONSTANT, name);
00090 }
00091
00095 static Symbol literal(const char* name) {
00096 return Symbol(LITERAL, name);
00097 }
00098
00099 typedef unsigned int SType;
00100 typedef unsigned int SIndex;
00101
00102 SType getType() const;
00103
00104 bool isVar() const { return getType() == VARIABLE; }
00105 bool isLit() const { return getType() == LITERAL; }
00106
00107 operator bool() const { return getType() != NULL_SYMBOL; }
00108
00109 SIndex index() const { return _index; }
00110 SymbolTable& table() const { return *_table; }
00111 const char* text() const;
00112
00113 bool isBound() const {
00114 return isVar() && getBinding();
00115 }
00116
00117 void bind(Symbol other);
00118
00119 Symbol getBinding() const;
00120
00121 bool compareOrBind(Symbol other) {
00122 NOT_IMPLEMENTED;
00123 }
00124
00125 static SymbolTable global;
00126
00127 private:
00128
00129
00130 SymbolTable* _table;
00131 SIndex _index;
00132
00133 #if 0
00134 SymbolTable::Record& record() {
00135 return(_table->record());
00136 }
00137 #endif
00138
00139 };
00140
00141
00142 STANDARD_EXTERNAL(Symbol)
00143
00144 #include "SymbolTable.h"
00145
00146 inline Symbol::Symbol(SymbolTable* table = 0, int index = 0)
00147 : _table(table), _index(index)
00148 {
00149 if (_table) _table->keep(_index);
00150 }
00151
00152 inline Symbol::Symbol(const Symbol& other)
00153 : _table(other._table), _index(other._index)
00154 {
00155 if (_table) _table->keep(_index);
00156 }
00157
00158 inline const Symbol& Symbol::operator=(const Symbol& other)
00159 {
00160 if (_table) _table->release(_index);
00161 _table=other._table;
00162 _index=other._index;
00163 if (_table) _table->keep(_index);
00164 return other;
00165 }
00166
00167 inline Symbol::~Symbol()
00168 {
00169 if (_table) _table->release(_index);
00170 }
00171
00172 inline Symbol::SType Symbol::getType() const
00173 {
00174 if (_table == 0) return NULL_SYMBOL;
00175 return _table->type(_index);
00176 }
00177
00178 inline const char* Symbol::text() const
00179 {
00180 if (_table == 0) return "-null-";
00181 return _table->text(_index);
00182 }
00183
00184 inline void Symbol::bind(Symbol other) {
00185 _table->records[_index].boundTo = other;
00186 }
00187
00188 inline Symbol Symbol::getBinding() const {
00189 return _table->records[_index].boundTo;
00190 }
00191
00192
00193 #endif // -*-C++-*-
00194