123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #include "KSGSExpressionEvaluator.h"
- #include "../Leser/KSGSLeser.h"
- #include "../Befehl/KSGSKlasse.h"
- #include "../Error/Error.h"
- #include <iostream>
- using namespace KSGScript;
- KSGSExpressionEvaluatorImpl::KSGSExpressionEvaluatorImpl()
- : ReferenceCounter()
- {
- functions = new RCArray< KSGSFunktion >();
- classes = new RCArray< KSGSKlasse >();
- params = new Framework::RCArray<KSGSVariable>();
- variables = new RCArray< KSGSVariable >();
- expression = "";
- }
- KSGSExpressionEvaluatorImpl::~KSGSExpressionEvaluatorImpl()
- {
- params->release();
- functions->release();
- classes->release();
- variables->release();
- }
- void KSGSExpressionEvaluatorImpl::compile()
- {
- functions->leeren();
- classes->leeren();
- variables->leeren();
- Framework::Text script = "func double start(";
- Iterator<Text*> name = paramNames->begin();
- for( Iterator<KSGSVariable*> it = params->begin(); it && name; it = it.next(), name = name.next() )
- {
- switch( it->getTyp() )
- {
- case KSGS_BOOL:
- script += "bool " + name;
- break;
- case KSGS_INT:
- script += "int " + name;
- break;
- case KSGS_DOUBLE:
- script += "double " + name;
- break;
- case KSGS_ARRAY:
- script += "Array " + name;
- break;
- case KSGS_THREAD:
- script += "Thread " + name;
- break;
- case KSGS_TEXT:
- script += "Text " + name;
- break;
- case KSGS_BILD:
- script += "Bild " + name;
- break;
- case KSGS_MAUSEREIGNIS:
- script += "MausEreignis " + name;
- break;
- case KSGS_TASTATUREREIGNIS:
- script += "TastaturEreignis " + name;
- break;
- case KSGS_TEXTFELD:
- script += "TextFeld " + name;
- break;
- case KSGS_KNOPF:
- script += "Knopf " + name;
- break;
- case KSGS_FENSTER:
- script += "Fenster " + name;
- break;
- case KSGS_BILDO:
- script += "BildZ " + name;
- break;
- case KSGS_ANIMATION2DDATA:
- script += "Animation2DData " + name;
- break;
- case KSGS_ANIMATION2D:
- script += "Animation2D " + name;
- break;
- }
- if( it.hasNext() )
- script += ", ";
- }
- script += Text( ") { return " ) + expression + Text( "; }" );
- TextReader* expressionReader = new TextReader( new Text( script ) );
- KSGSLeser* reader = new KSGSLeser( expressionReader, this, "expression" );
- if( !reader->laden() )
- {
- expressionReader->release();
- return;
- }
- Array< KSGSVariableDef* >* varDefs = new Array< KSGSVariableDef* >();
- if( !reader->compile( classes, functions, varDefs ) )
- {
- varDefs->release();
- expressionReader->release();
- return;
- }
- reader->release();
- int vAnz = varDefs->getEintragAnzahl();
- for( int i = 0; i < vAnz; i++ )
- {
- KSGSVariable* var = KSGSKlasseInstanz::erstellVariable( this, varDefs->get( i ) );
- if( var )
- variables->add( var );
- delete varDefs->get( i );
- }
- varDefs->release();
- expressionReader->release();
- }
- KSGSVariable* KSGSExpressionEvaluatorImpl::createParameter( Framework::Text name, int type )
- {
- this->paramNames->add( new Text( name ) );
- KSGSVariable* var = KSGSKlasseInstanz::erstellVariable( this, new KSGSVariableDef{ type, 0, 0, "" } );
- this->params->add( dynamic_cast<KSGSVariable*>(var->getThis()) );
- if( this->expression.getLength() > 0 )
- compile();
- return var;
- }
- KSGSVariable* KSGSExpressionEvaluatorImpl::getParameter( Framework::Text name )
- {
- int vAnz = paramNames->getEintragAnzahl();
- for( int i = 0; i < vAnz; i++ )
- {
- if( paramNames->z( i )->istGleich( name ) )
- return params->get( i );
- }
- return 0;
- }
- void KSGSExpressionEvaluatorImpl::setExpression( Framework::Text expression )
- {
- this->expression = expression;
- compile();
- }
- KSGSVariable* KSGSExpressionEvaluatorImpl::evaluate()
- {
- if( functions->getEintragAnzahl() > 0 )
- return startFunktion( functions->z( 0 )->getId(), dynamic_cast<RCArray<KSGSVariable>*>(params->getThis()) );
- return 0;
- }
- void KSGSExpressionEvaluatorImpl::logNachricht( char* n )
- {
- std::cout << n << "\n";
- }
- Framework::Text* KSGSExpressionEvaluatorImpl::convertPfad( char* pf )
- {
- Text* ret = new Text( pf );
- ret->ersetzen( '\\', '/' );
- if( ret->getText()[ 0 ] == '/' )
- {
- ret->remove( 0 );
- return ret;
- }
- return ret;
- }
- void KSGSExpressionEvaluatorImpl::setVariable( int id, KSGSVariable* var )
- {
- variables->set( var, id );
- }
- KSGSVariable* KSGSExpressionEvaluatorImpl::startFunktion( int id, Framework::RCArray< KSGSVariable >* parameter )
- {
- if( !functions->z( id ) || functions->z( id )->getSichtbarkeit() != 0 )
- {
- error( 19, {}, this );
- parameter->release();
- return 0;
- }
- KSGSFunktionInstanz* inst = functions->z( id )->erstellInstanz( dynamic_cast<KSGScriptProcessor*>(getThis()), 0, parameter );
- KSGSVariable* ret = inst->startFunktion();
- inst->release();
- return ret;
- }
- KSGSVariable* KSGSExpressionEvaluatorImpl::erstellKlassenInstanz( int id )
- {
- int anz = classes->getEintragAnzahl();
- for( int i = 0; i < anz; i++ )
- {
- if( classes->z( i ) && classes->z( i )->getId() == id )
- return classes->z( i )->erstellInstanz( this );
- }
- error( 2, {}, this );
- return 0;
- }
- KSGSVariable* KSGSExpressionEvaluatorImpl::getVariable( int id ) const
- {
- if( !variables || !variables->z( id ) )
- {
- error( 17, {}, (KSGScriptProcessor*)this );
- return 0;
- }
- return variables->get( id );
- }
- int KSGSExpressionEvaluatorImpl::getScriptId() const
- {
- return 0;
- }
- Framework::Bildschirm* KSGSExpressionEvaluatorImpl::zBildschirm() const
- {
- return 0;
- }
- Framework::Schrift* KSGSExpressionEvaluatorImpl::zSchrift() const
- {
- return 0;
- }
- int KSGSExpressionEvaluatorImpl::getFunktionId( const char* name ) const
- {
- int anz = functions->getEintragAnzahl();
- for( int i = 0; i < anz; i++ )
- {
- if( functions->z( i )->hatName( name ) )
- return i;
- }
- return -1;
- }
- bool KSGSExpressionEvaluatorImpl::istBeendet( int scrId ) const
- {
- return false;
- }
- KSGSVariable* KSGSExpressionEvaluatorImpl::callback( Framework::RCArray< KSGSVariable >* parameter ) const
- {
- parameter->release();
- return 0;
- }
|