koscript_context.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "koscript_context.h"
00021 #include "koscript_parsenode.h"
00022 #include "koscript_property.h"
00023
00024 #include <stdio.h>
00025
00026
00027
00028
00029
00030
00031
00032 KSContext::KSContext()
00033 {
00034 m_bLeftExpr = false;
00035 m_bReturning = false;
00036 }
00037
00038 KSContext::KSContext( KSContext& c, bool leftexpr )
00039 {
00040 setScope( c );
00041 m_bLeftExpr = leftexpr;
00042 m_bReturning = false;
00043 }
00044
00045 KSContext::~KSContext()
00046 {
00047
00048 m_exception = 0;
00049 m_value = 0;
00050 m_extraData = 0;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 KSException::KSException( const QString& _type, const KSValue::Ptr& _ptr, int _line )
00060 {
00061 m_type = new KSValue( _type );
00062 m_value = _ptr;
00063 if ( _line >= 0 )
00064 m_lines.append( _line );
00065 }
00066
00067 KSException::KSException( const QString& _type, const QString& _val, int _line )
00068 {
00069 m_type = new KSValue( _type );
00070 m_value = new KSValue( _val );
00071 if ( _line >= 0 )
00072 m_lines.append( _line );
00073 }
00074
00075 KSException::KSException( const KSValue::Ptr& _type, const KSValue::Ptr& _ptr, int _line )
00076 {
00077 m_type = _type;
00078 m_value = _ptr;
00079 if ( _line >= 0 )
00080 m_lines.append( _line );
00081 }
00082
00083 void KSException::print( KSContext& context )
00084 {
00085 printf("%s\n",toString( context ).local8Bit().data());
00086 }
00087
00088 QString KSException::toString( KSContext& context )
00089 {
00090 QString out("Exception '%1'\n%3\n");
00091
00092 if ( m_value )
00093 out = out.arg( m_type->toString( context ) ).arg( m_value->toString( context ) );
00094 else
00095 out = out.arg( m_type->toString( context ) ).arg( "" );
00096
00097 QValueList<int>::ConstIterator it = lines().begin();
00098 for( ; it != lines().end(); ++it )
00099 {
00100 QString l( "\nLine %1" );
00101 l = l.arg( *it );
00102 out += l;
00103 }
00104
00105 return out;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114 KSScope::KSScope( const KSNamespace* globalSpace, KSModule* module )
00115 {
00116 Q_ASSERT( globalSpace );
00117
00118 m_module = module;
00119 m_globalSpace = globalSpace;
00120 if ( m_module )
00121 m_moduleSpace = m_module->nameSpace();
00122 else
00123 m_moduleSpace = 0;
00124 m_localScope = 0;
00125 }
00126
00127 KSScope::KSScope( const KSScope& s ) : QShared()
00128 {
00129 m_module = s.m_module;
00130 m_globalSpace = s.m_globalSpace;
00131 m_moduleSpace = s.m_moduleSpace;
00132 m_localScope = s.m_localScope;
00133 }
00134
00135 KSValue* KSScope::object( const QString& name, bool _insert )
00136 {
00137 int len = name.length();
00138
00139
00140 if ( len > 2 && name[0] == ':' && name[1] == ':' )
00141 {
00142
00143 }
00144 else if ( name.find( ':' ) == -1 )
00145 {
00146 if ( m_localScope )
00147 {
00148 KSValue *v = m_localScope->object( name, false );
00149 if ( v )
00150 return v;
00151 }
00152
00153 if ( m_moduleSpace )
00154 {
00155 KSNamespace::Iterator it = m_moduleSpace->find( name );
00156 if ( it != m_moduleSpace->end() )
00157 return it.data();
00158 }
00159
00160 KSNamespace::ConstIterator it = m_globalSpace->find( name );
00161 if ( it != m_globalSpace->end() )
00162 {
00163 KSSharedPtr<KSValue> ptr( it.data() );
00164 return ptr;
00165 }
00166
00167
00168
00169 if( m_moduleSpace )
00170 {
00171 KSNamespace::ConstIterator it = m_moduleSpace->begin();
00172 for( ; it != m_moduleSpace->end(); ++it )
00173 if( it.key().lower() == name.lower() )
00174 {
00175 KSSharedPtr<KSValue> ptr( it.data() );
00176 return ptr;
00177 }
00178 }
00179
00180 it = m_globalSpace->begin();
00181 for( ; it != m_globalSpace->end(); ++it )
00182 if( it.key().lower() == name.lower() )
00183 {
00184 KSSharedPtr<KSValue> ptr( it.data() );
00185 return ptr;
00186 }
00187
00188 if ( !_insert )
00189 return 0;
00190
00191 KSValue::Ptr v = new KSValue();
00192 v->setMode( KSValue::LeftExpr );
00193 addObject( name, v );
00194 return v;
00195 }
00196 else
00197 {
00198
00199 }
00200
00201 return 0;
00202 }
00203
00204 void KSScope::addObject( const QString& name, const KSValue::Ptr& value )
00205 {
00206 if( m_localScope )
00207 m_localScope->addObject( name, value );
00208 else if( m_module )
00209 m_module->addObject( name, value );
00210 else
00211 Q_ASSERT( 0 );
00212 }
00213
00214
00215
00216
00217
00218
00219
00220 KSModule::KSModule( KSInterpreter* i, const QString& name, KSParseNode* code )
00221 : QShared(), m_name( name ), m_code( code ), m_interpreter( i )
00222 {
00223 }
00224
00225 KSModule::~KSModule()
00226 {
00227 if ( m_code )
00228 delete m_code;
00229 }
00230
00231 void KSModule::addObject( const QString& name, const KSValue::Ptr& v )
00232 {
00233 m_space.insert( name, v );
00234 }
00235
00236 void KSModule::removeObject( const QString& name )
00237 {
00238 m_space.remove( name );
00239 }
00240
00241
00242 KSValue::Ptr KSModule::member( KSContext& context, const QString& name )
00243 {
00244 KSNamespace::Iterator it = m_space.find( name );
00245 if ( it == m_space.end() )
00246 {
00247 if ( context.leftExpr() )
00248 {
00249 this->ref();
00250 KSValue::Ptr ptr( new KSValue( new KSProperty( this, name ) ) );
00251 ptr->setMode( KSValue::LeftExpr );
00252 return ptr;
00253
00254
00255
00256 }
00257
00258 QString tmp( "Unknown symbol '%1' in object of module '%2'" );
00259 context.setException( new KSException( "UnknownName", tmp.arg( name ).arg( m_name ) ) );
00260 return 0;
00261 }
00262
00263 return it.data();
00264 }
00265
00266 bool KSModule::setMember( KSContext&, const QString& name, const KSValue::Ptr& v )
00267 {
00268 m_space.insert( name, v );
00269
00270 return true;
00271 }
00272
00273 bool KSModule::eval( KSContext& context )
00274 {
00275 if ( !m_code )
00276 return false;
00277
00278 return m_code->eval( context );
00279 }
00280
00281 KSValue* KSModule::object( const QString& name )
00282 {
00283 KSNamespace::Iterator nit = m_space.find( name );
00284 if ( nit != m_space.end() )
00285 return nit.data();
00286
00287 return 0;
00288 }
00289
00290 void KSModule::setCode( KSParseNode* node )
00291 {
00292 if ( m_code )
00293 delete m_code;
00294
00295 m_code = node;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304 KSValue* KSSubScope::object( const QString& name, bool insert )
00305 {
00306 QPtrListIterator<KSNamespace> it( m_spaces );
00307 it.toLast();
00308 for( ; it.current(); --it )
00309 {
00310 KSNamespace::Iterator nit = it.current()->find( name );
00311 if ( nit != it.current()->end() )
00312 return nit.data();
00313 }
00314
00315 if ( !insert )
00316 return 0;
00317
00318 KSValue* v = new KSValue();
00319 v->setMode( KSValue::LeftExpr );
00320 addObject( name, v );
00321 return v;
00322 }
00323
00324 void KSSubScope::addObject( const QString& name, const KSValue::Ptr& value )
00325 {
00326 m_spaces.getLast()->insert( name, value );
00327 }
This file is part of the documentation for lib Library Version 1.3.5.