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 #ifdef WIN32
23 #pragma warning (disable : 4786)
24 #endif
25 
26 #include "UtlException.h"
27 #include "UtlTrace.h"
28 #include "ScpStream.h"
29 #include "UtlTimer.h"
30 
31 #include "DtaScriptVariable.h"
32 #include "GrfExecutionContext.h"
33 #include "CppCompilerEnvironment.h"
34 #include "GrfCommand.h"
35 
36 namespace CodeWorker {
37 	GrfExecutionContext* GrfCommand::_pExecutionContext = NULL;
38 
GrfCommand(GrfBlock * pParent)39 	GrfCommand::GrfCommand(GrfBlock* pParent) : _pParent(pParent), _sParsingFilePtr(NULL), _iFileLocation(-1), _iCounter(0), _pTimer(NULL) {}
40 
~GrfCommand()41 	GrfCommand::~GrfCommand() {}
42 
accept(DtaVisitor & visitor,DtaVisitorEnvironment & env)43 	void GrfCommand::accept(DtaVisitor& visitor, DtaVisitorEnvironment& env) {
44 		throw UtlException("internal error: GrfCommand::accept() MUST be overloaded in sub classes!");
45 	}
46 
getFunctionName() const47 	const char* GrfCommand::getFunctionName() const { return NULL; }
isAPredefinedFunction() const48 	bool GrfCommand::isAPredefinedFunction() const { return (getFunctionName() != NULL); }
isABNFCommand() const49 	bool GrfCommand::isABNFCommand() const { return false; }
50 
applyRecursively(APPLY_ON_COMMAND_FUNCTION apply)51 	void GrfCommand::applyRecursively(APPLY_ON_COMMAND_FUNCTION apply) {
52 		apply(this);
53 	}
54 
setParsingInformation(const char * sName,ScpStream & stream)55 	void GrfCommand::setParsingInformation(const char* sName, ScpStream& stream) {
56 		if (sName != NULL) {
57 			_sParsingFilePtr = sName;
58 			_iFileLocation = stream.getInputLocation();
59 		}
60 	}
61 
callBeforeExecutionCBK(DtaScriptVariable & visibility)62 	void GrfCommand::callBeforeExecutionCBK(DtaScriptVariable& visibility) {
63 		callRecursiveBeforeExecutionCBK(getCurrentExecutionContext(), visibility);
64 	}
65 
callRecursiveBeforeExecutionCBK(GrfExecutionContext * pContext,DtaScriptVariable & visibility)66 	void GrfCommand::callRecursiveBeforeExecutionCBK(GrfExecutionContext* pContext, DtaScriptVariable& visibility) {
67 		if (pContext != NULL) {
68 			callRecursiveBeforeExecutionCBK(pContext->getLastExecutionContext(), visibility);
69 			pContext->handleBeforeExecutionCBK(this, visibility);
70 		}
71 	}
72 
callAfterExecutionCBK(DtaScriptVariable & visibility)73 	void GrfCommand::callAfterExecutionCBK(DtaScriptVariable& visibility) {
74 		GrfExecutionContext* pContext = getCurrentExecutionContext();
75 		while (pContext != NULL) {
76 			pContext->handleAfterExecutionCBK(this, visibility);
77 			pContext = pContext->getLastExecutionContext();
78 		}
79 	}
80 
callAfterExceptionCBK(DtaScriptVariable & visibility,UtlException & exception)81 	void GrfCommand::callAfterExceptionCBK(DtaScriptVariable& visibility, UtlException& exception) {
82 		GrfExecutionContext* pContext = getCurrentExecutionContext();
83 		while (pContext != NULL) {
84 			pContext->handleAfterExceptionCBK(this, visibility, exception);
85 			pContext = pContext->getLastExecutionContext();
86 		}
87 	}
88 
execute(DtaScriptVariable & visibility)89 	SEQUENCE_INTERRUPTION_LIST GrfCommand::execute(DtaScriptVariable& visibility) {
90 		UTLTRACE_STACK_INSTRUCTION(_sParsingFilePtr, _iFileLocation);
91 		SEQUENCE_INTERRUPTION_LIST result;
92 		if (_pExecutionContext != NULL) {
93 			callBeforeExecutionCBK(visibility);
94 			try {
95 				result = executeInternal(visibility);
96 			} catch(UtlException& exception) {
97 				callAfterExceptionCBK(visibility, exception);
98 				throw/* UtlException(exception)*/;
99 			}
100 			callAfterExecutionCBK(visibility);
101 		} else {
102 			result = executeInternal(visibility);
103 		}
104 		return result;
105 	}
106 
clearTimer()107 	void GrfCommand::clearTimer() {
108 		if (_pTimer != NULL) _pTimer->clear();
109 	}
110 
startTimer()111 	void GrfCommand::startTimer() {
112 		if (_pTimer == NULL) _pTimer = new UtlTimer;
113 		_pTimer->start();
114 	}
115 
stopTimer()116 	void GrfCommand::stopTimer() {
117 		if (_pTimer != NULL) _pTimer->stop();
118 	}
119 
getTimeInMillis() const120 	long GrfCommand::getTimeInMillis() const {
121 		if ((_iCounter == 0) || (_pTimer == NULL)) return 0L; // little bug when the timer has never started
122 		return _pTimer->getTimeInMillis();
123 	}
124 
toString() const125 	std::string GrfCommand::toString() const {
126 		return "<unknown command>";
127 	}
128 
compileCpp(CppCompilerEnvironment & theCompilerEnvironment) const129 	void GrfCommand::compileCpp(CppCompilerEnvironment& theCompilerEnvironment) const {
130 		CW_BODY_INDENT << "<unhandled command '" << getFunctionName() << "'>";
131 		CW_BODY_ENDL;
132 	}
133 }
134