Back to index

texmacs  1.0.7.15
environment.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : environment.hpp
00004 * DESCRIPTION: abstract environments for style rewriting
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef ENVIRONMENT_H
00013 #define ENVIRONMENT_H
00014 #include "tree.hpp"
00015 
00016 //#define CLASSICAL_MACRO_EXPANSION
00017 #define ALTERNATIVE_MACRO_EXPANSION
00018 
00019 /******************************************************************************
00020 * Helper functions which should be moved elsewhere
00021 ******************************************************************************/
00022 
00023 inline int
00024 weak_hash (tree t) {
00025   tree_rep* rep= t.operator -> ();
00026   return hash ((void*) rep);
00027 }
00028 
00029 inline bool
00030 weak_equal (tree t1, tree t2) {
00031   tree_rep* rep1= t1.operator -> ();
00032   tree_rep* rep2= t2.operator -> ();
00033   return rep1 == rep2;
00034 }
00035 
00036 inline int
00037 round_pow2 (int i) {
00038   int n=1;
00039   while (n<i) n <<= 1;
00040   return n;
00041 }
00042 
00043 /******************************************************************************
00044 * Abstract environments
00045 ******************************************************************************/
00046 
00047 class environment;
00048 class environment_rep: public concrete_struct {
00049 public:
00050   inline environment_rep () {}
00051   inline virtual ~environment_rep () {}
00052 
00053   virtual bool contains (int key) = 0;
00054   virtual tree read (int key) = 0;
00055   virtual void write (int key, const tree& val) = 0;
00056   virtual void remove (int key) = 0;
00057   virtual void print (const string& prefix) = 0;
00058 
00059   inline bool contains (const string& key) {
00060     return contains ((int) make_tree_label (key)); }
00061   inline tree read (const string& key) {
00062     return read ((int) make_tree_label (key)); }
00063   inline void write (const string& key, const tree& val) {
00064     write ((int) make_tree_label (key), val); }
00065   inline void remove (const string& key) {
00066     remove ((int) make_tree_label (key)); }
00067 
00068   friend class environment;
00069 };
00070 
00071 class environment {
00072   ABSTRACT_NULL(environment);
00073   inline tree operator [] (int key) {
00074     return rep->read (key); }
00075   inline tree operator [] (const string& key) {
00076     return rep->read (key); }
00077   inline friend environment_rep* as_pointer (const environment& env) {
00078     return env.rep; }
00079   inline friend int weak_hash (environment env) {
00080     return hash ((void*) env.rep); }
00081   inline friend bool weak_equal (environment env1, environment env2) {
00082     return env1.rep == env2.rep; }
00083 };
00084 ABSTRACT_NULL_CODE(environment);
00085 
00086 void test_environments ();
00087 
00088 #endif // defined ENVIRONMENT_H