1 2 /****************************************************************************** 3 * MODULE : environment.hpp 4 * DESCRIPTION: abstract environments for style rewriting 5 * COPYRIGHT : (C) 2006 Joris van der Hoeven 6 ******************************************************************************* 7 * This software falls under the GNU general public license version 3 or later. 8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE 9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>. 10 ******************************************************************************/ 11 12 #ifndef ENVIRONMENT_H 13 #define ENVIRONMENT_H 14 #include "tree.hpp" 15 16 //#define CLASSICAL_MACRO_EXPANSION 17 #define ALTERNATIVE_MACRO_EXPANSION 18 19 /****************************************************************************** 20 * Helper functions which should be moved elsewhere 21 ******************************************************************************/ 22 23 inline int weak_hash(tree t)24weak_hash (tree t) { 25 tree_rep* rep= t.operator -> (); 26 return hash ((void*) rep); 27 } 28 29 inline bool weak_equal(tree t1,tree t2)30weak_equal (tree t1, tree t2) { 31 tree_rep* rep1= t1.operator -> (); 32 tree_rep* rep2= t2.operator -> (); 33 return rep1 == rep2; 34 } 35 36 inline int round_pow2(int i)37round_pow2 (int i) { 38 int n=1; 39 while (n<i) n <<= 1; 40 return n; 41 } 42 43 /****************************************************************************** 44 * Abstract environments 45 ******************************************************************************/ 46 47 class environment; 48 class environment_rep: public concrete_struct { 49 public: environment_rep()50 inline environment_rep () {} ~environment_rep()51 inline virtual ~environment_rep () {} 52 53 virtual bool contains (int key) = 0; 54 virtual tree read (int key) = 0; 55 virtual void write (int key, const tree& val) = 0; 56 virtual void remove (int key) = 0; 57 virtual void print (const string& prefix) = 0; 58 contains(const string & key)59 inline bool contains (const string& key) { 60 return contains ((int) make_tree_label (key)); } read(const string & key)61 inline tree read (const string& key) { 62 return read ((int) make_tree_label (key)); } write(const string & key,const tree & val)63 inline void write (const string& key, const tree& val) { 64 write ((int) make_tree_label (key), val); } remove(const string & key)65 inline void remove (const string& key) { 66 remove ((int) make_tree_label (key)); } 67 68 friend class environment; 69 }; 70 71 class environment { 72 ABSTRACT_NULL(environment); operator [](int key)73 inline tree operator [] (int key) { 74 return rep->read (key); } operator [](const string & key)75 inline tree operator [] (const string& key) { 76 return rep->read (key); } as_pointer(const environment & env)77 inline friend environment_rep* as_pointer (const environment& env) { 78 return env.rep; } weak_hash(environment env)79 inline friend int weak_hash (environment env) { 80 return hash ((void*) env.rep); } weak_equal(environment env1,environment env2)81 inline friend bool weak_equal (environment env1, environment env2) { 82 return env1.rep == env2.rep; } 83 }; 84 ABSTRACT_NULL_CODE(environment); 85 86 void test_environments (); 87 88 #endif // defined ENVIRONMENT_H 89