KSGSExpressionEvaluator.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "KSGSExpressionEvaluator.h"
  2. #include "..\Leser\KSGSLeser.h"
  3. #include "..\Befehl\KSGSKlasse.h"
  4. #include "..\Error\Error.h"
  5. #include <iostream>
  6. using namespace KSGScript;
  7. KSGSExpressionEvaluatorImpl::KSGSExpressionEvaluatorImpl()
  8. : ReferenceCounter()
  9. {
  10. functions = new RCArray< KSGSFunktion >();
  11. classes = new RCArray< KSGSKlasse >();
  12. params = new Framework::RCArray<KSGSVariable>();
  13. variables = new RCArray< KSGSVariable >();
  14. expression = "";
  15. }
  16. KSGSExpressionEvaluatorImpl::~KSGSExpressionEvaluatorImpl()
  17. {
  18. params->release();
  19. functions->release();
  20. classes->release();
  21. variables->release();
  22. }
  23. void KSGSExpressionEvaluatorImpl::compile()
  24. {
  25. functions->leeren();
  26. classes->leeren();
  27. variables->leeren();
  28. Framework::Text script = "func double start(";
  29. Iterator<Text *> name = paramNames->getIterator();
  30. for( Iterator<KSGSVariable *> it = params->getIterator(); it && name; it = it.next(), name = name.next() )
  31. {
  32. switch( it->getTyp() )
  33. {
  34. case KSGS_BOOL:
  35. script += "bool " + name;
  36. break;
  37. case KSGS_INT:
  38. script += "int " + name;
  39. break;
  40. case KSGS_DOUBLE:
  41. script += "double " + name;
  42. break;
  43. case KSGS_ARRAY:
  44. script += "Array " + name;
  45. break;
  46. case KSGS_THREAD:
  47. script += "Thread " + name;
  48. break;
  49. case KSGS_TEXT:
  50. script += "Text " + name;
  51. break;
  52. case KSGS_BILD:
  53. script += "Bild " + name;
  54. break;
  55. case KSGS_MAUSEREIGNIS:
  56. script += "MausEreignis " + name;
  57. break;
  58. case KSGS_TASTATUREREIGNIS:
  59. script += "TastaturEreignis " + name;
  60. break;
  61. case KSGS_TEXTFELD:
  62. script += "TextFeld " + name;
  63. break;
  64. case KSGS_KNOPF:
  65. script += "Knopf " + name;
  66. break;
  67. case KSGS_FENSTER:
  68. script += "Fenster " + name;
  69. break;
  70. case KSGS_BILDO:
  71. script += "BildZ " + name;
  72. break;
  73. case KSGS_ANIMATION2DDATA:
  74. script += "Animation2DData " + name;
  75. break;
  76. case KSGS_ANIMATION2D:
  77. script += "Animation2D " + name;
  78. break;
  79. }
  80. if( it.hasNext() )
  81. script += ", ";
  82. }
  83. script += ") { return " + expression + Text( "; }" );
  84. TextReader *expressionReader = new TextReader( new Text( script ) );
  85. KSGSLeser *reader = new KSGSLeser( expressionReader, this, "expression" );
  86. if( !reader->laden() )
  87. {
  88. expressionReader->release();
  89. return;
  90. }
  91. Array< KSGSVariableDef * > *varDefs = new Array< KSGSVariableDef * >();
  92. if( !reader->compile( classes, functions, varDefs ) )
  93. {
  94. varDefs->release();
  95. expressionReader->release();
  96. return;
  97. }
  98. reader->release();
  99. int vAnz = varDefs->getEintragAnzahl();
  100. for( int i = 0; i < vAnz; i++ )
  101. {
  102. KSGSVariable *var = KSGSKlasseInstanz::erstellVariable( this, varDefs->get( i ) );
  103. if( var )
  104. variables->add( var );
  105. delete varDefs->get( i );
  106. }
  107. varDefs->release();
  108. expressionReader->release();
  109. }
  110. KSGSVariable *KSGSExpressionEvaluatorImpl::createParameter( Framework::Text name, int type )
  111. {
  112. this->paramNames->add( new Text( name ) );
  113. KSGSVariable *var = KSGSKlasseInstanz::erstellVariable( this, new KSGSVariableDef{ type, 0, 0, "" } );
  114. this->params->add( (KSGSVariable *)var->getThis() );
  115. if( this->expression.getLength() > 0 )
  116. compile();
  117. return var;
  118. }
  119. KSGSVariable *KSGSExpressionEvaluatorImpl::getParameter( Framework::Text name )
  120. {
  121. int vAnz = paramNames->getEintragAnzahl();
  122. for( int i = 0; i < vAnz; i++ )
  123. {
  124. if( paramNames->z( i )->istGleich( name ) )
  125. return params->get( i );
  126. }
  127. return 0;
  128. }
  129. void KSGSExpressionEvaluatorImpl::setExpression( Framework::Text expression )
  130. {
  131. this->expression = expression;
  132. compile();
  133. }
  134. KSGSVariable *KSGSExpressionEvaluatorImpl::evaluate()
  135. {
  136. if( functions->getEintragAnzahl() > 0 )
  137. return startFunktion( functions->z( 0 )->getId(), ( RCArray<KSGSVariable>* )params->getThis() );
  138. return 0;
  139. }
  140. void KSGSExpressionEvaluatorImpl::logNachricht( char *n )
  141. {
  142. std::cout << n << "\n";
  143. }
  144. Framework::Text *KSGSExpressionEvaluatorImpl::convertPfad( char *pf )
  145. {
  146. Text *ret = new Text( pf );
  147. ret->ersetzen( '\\', '/' );
  148. if( ret->getText()[ 0 ] == '/' )
  149. {
  150. ret->remove( 0 );
  151. return ret;
  152. }
  153. return ret;
  154. }
  155. void KSGSExpressionEvaluatorImpl::setVariable( int id, KSGSVariable *var )
  156. {
  157. variables->set( var, id );
  158. }
  159. KSGSVariable *KSGSExpressionEvaluatorImpl::startFunktion( int id, Framework::RCArray< KSGSVariable > *parameter )
  160. {
  161. if( !functions->z( id ) || functions->z( id )->getSichtbarkeit() != 0 )
  162. {
  163. error( 19, {}, this );
  164. parameter->release();
  165. return 0;
  166. }
  167. KSGSFunktionInstanz *inst = functions->z( id )->erstellInstanz( (KSGScriptProcessor *)getThis(), 0, parameter );
  168. KSGSVariable *ret = inst->startFunktion();
  169. inst->release();
  170. return ret;
  171. }
  172. KSGSVariable *KSGSExpressionEvaluatorImpl::erstellKlassenInstanz( int id )
  173. {
  174. int anz = classes->getEintragAnzahl();
  175. for( int i = 0; i < anz; i++ )
  176. {
  177. if( classes->z( i ) && classes->z( i )->getId() == id )
  178. return classes->z( i )->erstellInstanz( this );
  179. }
  180. error( 2, {}, this );
  181. return 0;
  182. }
  183. KSGSVariable *KSGSExpressionEvaluatorImpl::getVariable( int id ) const
  184. {
  185. if( !variables || !variables->z( id ) )
  186. {
  187. error( 17, {}, (KSGScriptProcessor *)this );
  188. return 0;
  189. }
  190. return variables->get( id );
  191. }
  192. int KSGSExpressionEvaluatorImpl::getScriptId() const
  193. {
  194. return 0;
  195. }
  196. Framework::Bildschirm *KSGSExpressionEvaluatorImpl::zBildschirm() const
  197. {
  198. return 0;
  199. }
  200. Framework::Schrift *KSGSExpressionEvaluatorImpl::zSchrift() const
  201. {
  202. return 0;
  203. }
  204. int KSGSExpressionEvaluatorImpl::getFunktionId( const char *name ) const
  205. {
  206. int anz = functions->getEintragAnzahl();
  207. for( int i = 0; i < anz; i++ )
  208. {
  209. if( functions->z( i )->hatName( name ) )
  210. return i;
  211. }
  212. return -1;
  213. }
  214. bool KSGSExpressionEvaluatorImpl::istBeendet( int scrId ) const
  215. {
  216. return false;
  217. }
  218. KSGSVariable *KSGSExpressionEvaluatorImpl::rückruf( Framework::RCArray< KSGSVariable > *parameter ) const
  219. {
  220. parameter->release();
  221. return 0;
  222. }