Marsyas
0.6.0-alpha
|
00001 /* 00002 ** Copyright (C) 1998-2007 George Tzanetakis <gtzan@cs.uvic.ca> 00003 ** 00004 ** This program is free software; you can redistribute it and/or modify 00005 ** it under the terms of the GNU General Public License as published by 00006 ** the Free Software Foundation; either version 2 of the License, or 00007 ** (at your option) any later version. 00008 ** 00009 ** This program is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 ** GNU General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU General Public License 00015 ** along with this program; if not, write to the Free Software 00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #ifndef MARSYAS_SYM_TBL_H 00020 #define MARSYAS_SYM_TBL_H 00021 00022 #include <marsyas/expr/ExVal.h> 00023 #include <marsyas/expr/ExCommon.h> 00024 #include <marsyas/common_header.h> 00025 00026 #include <string> 00027 #include <map> 00028 #include <vector> 00029 #include <cstddef> 00030 00031 namespace Marsyas 00032 { 00041 class ExNode; 00042 class ExNode_Fun; 00043 00060 class ExRecord : public ExRefCount { 00061 private: 00062 int kind_; 00063 std::string name_; 00064 ExVal value_; 00065 bool reserved_; 00066 00067 std::map<std::string,std::string> syms_aliases_; 00068 std::map<std::string,ExRecord*> syms_; 00069 00070 std::vector<std::string> imports_; 00071 00072 private: 00073 void split_on(std::string p, char c, std::string& hd, std::string& tl, bool keep=false); 00074 void rsplit_on(std::string p, char c, std::string& hd, std::string& tl); 00075 ExRecord* find_sym(std::string nm); 00076 00077 public: 00078 ExRecord(); 00079 ExRecord(int kind); 00080 ExRecord(int kind, ExFun* fun, bool reserved); 00081 ExRecord(int kind, std::string name, ExVal& value, bool reserved); 00082 00083 virtual ~ExRecord(); 00084 00085 std::string getType(std::string nm=""); 00086 std::string getElemType(std::string nm=""); 00087 int getKind(std::string nm=""); 00088 bool is_reserved(std::string nm=""); 00089 std::size_t size() {return syms_.size();} 00090 00091 void setValue(ExVal& v, std::string path="", int elem_pos=-1); 00092 ExVal getValue(std::string path=""); 00093 ExRecord* getRecord(std::string nm); 00094 ExFun* getFunctionCopy(std::string nm=""); 00095 bool is_list(); 00096 bool is_seq(); 00097 bool params_compare(std::string a, std::string b); 00098 // addRecord expects the name of the symbol, for functions this includes 00099 // parameter type information used to differentiate it from other functions 00100 // aliases may be specified using the | symbol as so: 00101 // this "Real|R.log|ln(mrs_real)" adds the single symbol: 00102 // Real.log(mrs_real) 00103 // and the aliases: 00104 // R ~> Real 00105 // ln ~> log 00106 // allowing the four possible ways of making the same call: 00107 // Real.log(mrs_real) 00108 // Real.ln(mrs_real) 00109 // R.log(mrs_real) 00110 // R.ln(mrs_real) 00111 // If parameters are to appear on the path then only one set is allowed and 00112 // must appear at the end as in the above examples. 00113 void addAliases(std::string path, std::string name); 00114 void addRecord(std::string path, ExRecord* sym); 00115 ExRecord* rmvRecord(std::string path); 00116 void addReserved(std::string path, ExFun* f); 00117 void addReserved(std::string path, ExVal v, std::string nm="", int kind=T_CONST); 00118 00119 void import(std::string); 00120 void rmv_import(std::string); 00121 }; 00122 00123 class ExSymTbl : public ExRefCount { 00124 /*** setup a naming scheme that prefixes an id for each variable name 00125 so that outputting bytecode is easier with variable names ***/ 00126 std::vector<ExRecord*> rho_; 00127 ExRecord* curr_; 00128 unsigned int env_id; 00129 public: 00130 ExSymTbl() : ExRefCount() {env_id=0; curr_=NULL;} 00131 virtual ~ExSymTbl(); 00132 00133 void block_open(); 00134 void block_close(); 00135 void addTable(ExRecord* r); 00136 00137 std::size_t size() {return rho_.size();} 00138 void setValue(ExVal& v, std::string path); 00139 00140 ExVal getValue(std::string path); 00141 ExRecord* getRecord(std::string nm); 00142 ExFun* getFunctionCopy(std::string nm); 00143 00144 void addRecord(std::string path, ExRecord* sym); 00145 ExRecord* rmvRecord(std::string path); 00146 void addReserved(std::string path, ExFun* f); 00147 void addReserved(std::string path, ExVal v, std::string nm="", int kind=T_CONST); 00148 00149 void import(std::string); 00150 void rmv_import(std::string); 00151 }; 00152 00153 }//namespace Marsyas 00154 00155 #endif 00156