1 /* "CodeWorker":	a scripting language for parsing and generating text.
2 
3 Copyright (C) 1996-1997, 1999-2002 C�dric Lemaire
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 To contact the author: codeworker@free.fr
20 */
21 
22 #ifndef _ExprScriptFunction_h_
23 #define _ExprScriptFunction_h_
24 
25 #include <list>
26 #include <vector>
27 #include <map>
28 #include <string>
29 
30 #include "ExprScriptExpression.h"
31 
32 namespace CodeWorker {
33 	class ScpStream;
34 
35 	class DtaScriptVariable;
36 	class GrfFunction;
37 	class GrfBlock;
38 	class ExprScriptFunction;
39 
40 	enum EXPRESSION_TYPE {
41 		UNKNOWN_EXPRTYPE = -1,
42 		VALUE_EXPRTYPE = 0,
43 		NODE_EXPRTYPE = 1,
44 		ITERATOR_EXPRTYPE = 2,
45 		REFERENCE_EXPRTYPE = 3,
46 		SCRIPTFILE_EXPRTYPE = 8, // common script
47 		SCRIPTFILE_PATTERN_EXPRTYPE = 9,
48 		SCRIPTFILE_FREE_EXPRTYPE = 10,
49 		SCRIPTFILE_BNF_EXPRTYPE = 11,
50 		SCRIPTFILE_TRANSLATE_EXPRTYPE = 12,
51 		ARRAY_EXPRTYPE = 16 // for specifying an array type
52 	};
53 
54 	typedef ExprScriptFunction* (*CREATE_FUNCTION)(GrfBlock&);
55 
56 
57 	class DtaFunctionInfo {
58 	public:
59 		CREATE_FUNCTION constructor;
60 		unsigned int* pCounter;
61 	};
62 
63 
64 	class ExprScriptFunction : public ExprScriptExpression {
65 	protected:
66 		std::vector<ExprScriptExpression*> _parameters;
67 		ExprScriptExpression* _pTemplate;
68 		GrfFunction* _pPrototype;
69 		bool _bIsExternal;
70 
71 	public:
72 		ExprScriptFunction(GrfFunction* pPrototype = NULL);
73 		virtual ~ExprScriptFunction();
74 
isAFunctionExpression()75 		virtual bool isAFunctionExpression() const { return true; }
isAGenerateFunction()76 		virtual bool isAGenerateFunction() const { return false; }
isAParseFunction()77 		virtual bool isAParseFunction() const { return false; }
78 
79 		void addParameter(ExprScriptExpression* pParameter);
80 		void clearParameters();
getParameters()81 		const std::vector<ExprScriptExpression*>& getParameters() const { return _parameters; }
82 		virtual EXPRESSION_TYPE getParameterType(unsigned int iIndex) const;
83 		virtual ExprScriptExpression* getDefaultParameter(unsigned int iIndex) const;
84 		virtual unsigned int getArity() const;
85 		virtual unsigned int getMinArity() const;
86 		virtual const char* getName() const;
getUserBody()87 		inline GrfFunction* getUserBody() const { return _pPrototype; }
setTemplate(ExprScriptExpression * pTemplate)88 		inline void setTemplate(ExprScriptExpression* pTemplate) { _pTemplate = pTemplate; }
89 		virtual int getThisPosition() const;
90 		virtual void initializationDone();
91 
92 		virtual std::string getValue(DtaScriptVariable& visibility) const;
93 
94 		virtual EXPRESSION_RETURN_TYPE compileCpp(CppCompilerEnvironment& theCompilerEnvironment) const;
95 		virtual bool compileCppBoolean(CppCompilerEnvironment& theCompilerEnvironment, bool bNegative) const;
96 		virtual bool compileCppString(CppCompilerEnvironment& theCompilerEnvironment) const;
97 
98 		virtual std::string toString() const;
99 
100 		static ExprScriptFunction* create(GrfBlock& block, ScpStream& script, const std::string& sFunction, const std::string& sTemplate, bool bGenericKey);
101 		static ExprScriptFunction* createMethod(GrfBlock& block, ScpStream& script, const std::string& sFunction, const std::string& sTemplate, bool bGenericKey);
102 		static std::map<std::string, DtaFunctionInfo>& getFunctionRegister();
103 		static void clearCounters();
104 	};
105 }
106 
107 #endif
108 
109