MathTypeLibrary(libmath++)  0.0.3
matcher.h
1 
2 // Math Type Library
3 // $Id: matcher.h,v 1.3 2002/05/07 12:38:55 cparpart Exp $
4 // (This file contains the expression tree specific template members)
5 //
6 // Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public License
19 // along with this library; see the file COPYING.LIB. If not, write to
20 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA 02111-1307, USA.
23 #ifndef libmath_matcher_h
24 #define libmath_matcher_h
25 
26 #include <map>
27 #include <list>
28 #include <memory>
29 #include <string>
30 
31 namespace math {
32 
33 template<class T>
35 public:
38 
40  void define(const std::string& AId, const TNode<T> *ANode);
42  bool defined(const std::string& AId) const;
44  const TNode<T> *get(const std::string& AId) const;
45 
47  void mark(const TNode<T> *ANode);
48 
50  bool contains(const TNode<T> *ANode) const;
51 
52 private:
53  typedef std::map<std::string, const TNode<T> *> TAnyMap;
54  typedef std::list<const TNode<T> *> TNodeList;
55 
56  TAnyMap FAnyMap;
57  TNodeList FNodeList;
58 };
59 
61 // The match template tree
62 
63 template <class T>
64 class TMatch {
65 public:
66  virtual ~TMatch() {}
67  virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const = 0;
68 };
69 
70 template <class T>
71 class TNumMatch : public TMatch<T> {
72 public:
73  TNumMatch(const T& ANum);
74 
75  virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
76 
77 private:
78  T FNumber;
79 };
80 
81 template <class T>
82 class TAnyMatch : public TMatch<T> {
83 public:
84  TAnyMatch(const std::string& AId);
85 
86  virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
87 
88 private:
89  std::string FIdent;
90 };
91 
94 template <class T>
95 class T2Match : public TMatch<T> {
96 protected:
97  T2Match(TMatch<T> *ALeft, TMatch<T> *ARight);
98  ~T2Match();
99 
100  // gets additional match methods for share soon
101 
102  typedef std::list<TMatch<T> *> TList;
103 
104  TList FPatterns;
105 };
106 
107 template <class T>
108 class TPlusMatch : public T2Match<T> {
109 public:
110  TPlusMatch(TMatch<T> *ALeft, TMatch<T> *ARight, ...);
111 
112  virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
113 };
114 
115 template <class T>
116 class TMulMatch : public T2Match<T> {
117 public:
118  TMulMatch(TMatch<T> *ALeft, TMatch<T> *ARight, ...);
119 
120  virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
121 };
122 
123 template <class T>
124 class TNegMatch : public TMatch<T> {
125 public:
126  TNegMatch(TMatch<T> *ANode);
127 
128  virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
129 
130 private:
131  std::auto_ptr<TMatch<T> > FNode;
132 };
133 
134 template <class T>
135 class TDivMatch : public TMatch<T> {
136 public:
137  TDivMatch(TMatch<T> *ALeft, TMatch<T> *ARight);
138 
139  virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
140 
141 private:
142  std::auto_ptr<TMatch<T> > FLeft;
143  std::auto_ptr<TMatch<T> > FRight;
144 };
145 
146 template <class T>
147 class TPowMatch : public TMatch<T> {
148 public:
149  TPowMatch(TMatch<T> *ABase, TMatch<T> *AExp);
150 
151  virtual bool match(const TNode<T> *AExpr, TMatchRegistry<T> *AReg) const;
152 
153 private:
154  std::auto_ptr<TMatch<T> > FBase;
155  std::auto_ptr<TMatch<T> > FExp;
156 };
157 
175 template<class T>
176 class TMatcher : public TNodeVisitor<T> {
177 public:
178  typedef std::map<std::string, TNode<T> > TResult;
179 
183  static bool matchExact(const TMatch<T> *AMatch, const TNode<T> *AExpr,
184  TMatchRegistry<T> *AReg = 0);
185 
190  static bool match(const TMatch<T> *AMatch, const TNode<T> *AExpr,
191  TMatchRegistry<T> *AReg = 0);
192 
196  static unsigned match(const std::string& AMatch, const TNode<T> *AExpr,
197  TResult& AResult);
198 
199 private:
200  TMatcher(const TMatch<T> *AMatch, const TNode<T> *ANode,
201  TMatchRegistry<T> *AReg = 0);
202 
203 private:
204  const TMatch<T> *FMatch;
205  const TNode<T> *FExpr;
206 
207 private:
208  virtual void visit(TNumberNode<T> *);
209  virtual void visit(TSymbolNode<T> *);
210  virtual void visit(TParamNode<T> *);
211 
212  virtual void visit(TPlusNode<T> *);
213  virtual void visit(TNegNode<T> *);
214 
215  virtual void visit(TMulNode<T> *);
216  virtual void visit(TDivNode<T> *);
217 
218  virtual void visit(TPowNode<T> *);
219  virtual void visit(TSqrtNode<T> *);
220 
221  virtual void visit(TSinNode<T> *);
222  virtual void visit(TCosNode<T> *);
223  virtual void visit(TTanNode<T> *);
224  virtual void visit(TLnNode<T> *);
225 
226  virtual void visit(TFuncNode<T> *);
227  virtual void visit(TIfNode<T> *);
228 
229  virtual void visit(TEquNode<T> *);
230  virtual void visit(TUnEquNode<T> *);
231  virtual void visit(TGreaterNode<T> *);
232  virtual void visit(TLessNode<T> *);
233  virtual void visit(TGreaterEquNode<T> *);
234  virtual void visit(TLessEquNode<T> *);
235 };
236 
237 } // namespace math
238 
239 #include <math++/matcher.tcc>
240 
241 #endif