1 /*
2   GUIDO Library
3   Copyright (C) 2006-2008  Grame
4 
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9 
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14 
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19   Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20   research@grame.fr
21 
22 */
23 
24 #ifndef __guidoexpression__
25 #define __guidoexpression__
26 
27 #include <string>
28 
29 #include "export.h"
30 #include "ctree.h"
31 #include "functor.h"
32 #include "guidoExprEnum.h"
33 #include "gar_smartpointer.h"
34 #include "visitable.h"
35 #include "visitor.h"
36 
37 namespace guidolang
38 {
39 
40 class guidoexpression;
41 class guidovalue;
42 class guidoEnv;
43 
44 typedef guido::SMARTP<guidoexpression> 	Sguidoexpression;
45 typedef guido::SMARTP<guidovalue>		Sguidovalue;
46 typedef guido::SMARTP<guidoEnv>			SguidoEnv;
47 
48 //______________________________________________________________________________
49 /*!
50 \brief The base class for guido language expressions.
51 */
52 class export guidoexpression : public guido::ctree<guidoexpression>, public guido::visitable
53 {
54     protected:
55 		std::string	fName;
56 		int			fType;
57 
guidoexpression(int type)58 				 guidoexpression(int type) : fType(type) {}
~guidoexpression()59 		virtual ~guidoexpression() {}
60 		virtual	void	basicprint(std::ostream& os);
61 
62 	public:
getName()63 		virtual const std::string&	getName() const		{ return fName; }
getType()64 		virtual int					getType() const		{ return fType; }
65 
66 		virtual void		acceptIn(guido::basevisitor& visitor);
67 		virtual void		acceptOut(guido::basevisitor& visitor);
68 		virtual	void		print(std::ostream& os);
69 
70 		virtual Sguidoexpression	replace (const Sguidoexpression& exp, const Sguidoexpression& with);
71 //		virtual Sguidovalue			suspend(SguidoEnv env);
72 		virtual Sguidovalue			eval(SguidoEnv env);
73 
74 		//________________________________________________________________________
75 		virtual Sguidoexpression getArg(unsigned int n) const;
76 		virtual bool operator ==(const Sguidoexpression& i) const		{ return false; }
77 		virtual bool operator !=(const Sguidoexpression& i) const		{ return !(*this == i); }
78 
setName(const std::string & name)79 		void setName(const std::string& name)	{ fName = name; }
80 };
81 
82 export std::ostream& operator << (std::ostream& os, const Sguidoexpression& elt);
83 
84 //______________________________________________________________________________
85 /*
86 \brief	A template class to type all guido expressions with integers.
87 */
88 template <int elt> class guidonode : public guidoexpression
89 {
90 	public:
create()91 		static guido::SMARTP<guidonode<elt> > create()
92 			{ guidonode<elt>* o = new guidonode<elt>(elt); assert(o!=0); return o; }
93 
acceptIn(guido::basevisitor & v)94 		virtual void acceptIn(guido::basevisitor& v) {
95 			if (guido::visitor<guido::SMARTP<guidonode<elt> > >* p = dynamic_cast<guido::visitor<guido::SMARTP<guidonode<elt> > >*>(&v)) {
96 				guido::SMARTP<guidonode<elt> > sptr = this;
97 				p->visitStart(sptr);
98 			}
99 			else guidoexpression::acceptIn(v);
100 		}
acceptOut(guido::basevisitor & v)101         virtual void acceptOut(guido::basevisitor& v) {
102 			if (guido::visitor<guido::SMARTP<guidonode<elt> > >* p = dynamic_cast<guido::visitor<guido::SMARTP<guidonode<elt> > >*>(&v)) {
103 				guido::SMARTP<guidonode<elt> > sptr = this;
104 				p->visitEnd(sptr);
105 			}
106 			else guidoexpression::acceptOut(v);
107 		}
108 
109  		virtual bool operator ==(const Sguidoexpression& e) const {
110 			if (e->getType() == kNamed)
111 				return *this == e->getArg(0);
112 
113 			if (dynamic_cast<guidonode<elt>*>((guidoexpression*)(e))) {
114 				for (int i=0; i < size(); i++) {
115 					Sguidoexpression argi = getArg(i);
116 					Sguidoexpression eargi = e->getArg(i);
117 					if (!argi || !eargi || (argi != eargi))
118 						return false;
119 				}
120 				return true;
121 			}
122 			return false;
123 		}
124 
125 	protected:
guidonode(int type)126 				 guidonode(int type) : guidoexpression(type) {}
~guidonode()127 		virtual ~guidonode() {}
128 
129 };
130 
131 } // namespace
132 
133 #endif
134