koscript_context.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __KSCRIPT_CONTEXT_H__
00021 #define __KSCRIPT_CONTEXT_H__
00022
00023 #include "koscript_value.h"
00024 #include "koscript_ptr.h"
00025
00026 #include <qshared.h>
00027 #include <qstring.h>
00028 #include <qptrlist.h>
00029
00030 class KSContext;
00031 class KSParseNode;
00032 class KSInterpreter;
00033
00034 typedef QMap<QString,KSValue::Ptr> KSNamespace;
00035
00041 class KSModule : public QShared
00042 {
00043 public:
00044 typedef KSSharedPtr<KSModule> Ptr;
00045
00046 KSModule( KSInterpreter*, const QString& name, KSParseNode* = 0 );
00047 virtual ~KSModule();
00048
00053 virtual bool eval( KSContext& );
00054
00062 virtual KSValue::Ptr member( KSContext&, const QString& name );
00066 virtual bool setMember( KSContext&, const QString& name, const KSValue::Ptr& v );
00067
00071 virtual bool isPebbles() const { return FALSE; }
00072
00076 QString name() const { return m_name; }
00077
00084 KSNamespace* nameSpace() { return &m_space; }
00085
00095 KSValue* object( const QString& name );
00102 void addObject( const QString& name, const KSValue::Ptr& v );
00109 void removeObject( const QString& name );
00113 KSInterpreter* interpreter() { return m_interpreter; }
00114
00115 protected:
00116 void setCode( KSParseNode* node );
00117
00118 private:
00119 QString m_name;
00120 KSNamespace m_space;
00121 KSParseNode* m_code;
00122 KSInterpreter* m_interpreter;
00123 };
00124
00125
00126 class KSSubScope
00127 {
00128 public:
00129 KSSubScope() { }
00130 KSSubScope( KSNamespace* n ) { m_spaces.append( n ); }
00131
00137 KSValue* object( const QString& name, bool insert = FALSE );
00141 void addObject( const QString& name, const KSValue::Ptr& );
00142
00146 void pushNamespace( KSNamespace* nspace ) { m_spaces.append( nspace ); }
00147
00151 void popNamespace() { m_spaces.removeLast(); }
00152
00153 private:
00154 QPtrList<KSNamespace> m_spaces;
00155 };
00156
00157
00158 class KSScope : public QShared
00159 {
00160 public:
00161 typedef KSSharedPtr<KSScope> Ptr;
00162
00166 KSScope( const KSNamespace* globalSpace, KSModule *module );
00167 KSScope( const KSScope& s );
00168
00169 void pushLocalScope( KSSubScope* scope ) { Q_ASSERT( m_localScope == 0 ); m_localScope = scope; }
00170 KSSubScope* popLocalScope() { KSSubScope* s = m_localScope; m_localScope = 0; return s; }
00171 KSSubScope* localScope() { return m_localScope; }
00172
00173 void pushModule( KSModule* m ) { Q_ASSERT( m_module == 0 ); m_module = m; m_moduleSpace = m->nameSpace(); }
00174 KSModule* popModule() { KSModule* n = m_module; m_module = 0; return n; }
00175
00176 KSModule* module() { return m_module; }
00177
00185 KSValue* object( const QString& name, bool insert = FALSE );
00192 void addObject( const QString& name, const KSValue::Ptr& );
00193
00194 private:
00195 KSModule* m_module;
00196 const KSNamespace* m_globalSpace;
00200 KSNamespace* m_moduleSpace;
00201 KSSubScope* m_localScope;
00202 };
00203
00204 class KSException : public QShared
00205 {
00206 public:
00207 typedef KSSharedPtr<KSException> Ptr;
00208
00209 KSException( const QString& _type, const KSValue::Ptr& _ptr, int _line = -1 );
00210 KSException( const QString& _type, const QString& _val, int _line = -1 );
00211 KSException( const KSValue::Ptr& _type, const KSValue::Ptr& ptr, int _line = -1 );
00212 ~KSException() { }
00213
00214 const QValueList<int>& lines() { return m_lines; }
00215 void addLine( int l ) { if ( m_lines.isEmpty() ) m_lines.append( l ); else if ( m_lines.last() != l ) m_lines.append( l ); }
00216
00217 QString toString( KSContext& context );
00218 void print( KSContext& context );
00219
00220 KSValue* type() { return m_type; }
00221 KSValue* value() { return m_value; }
00222
00223 private:
00224 KSValue::Ptr m_type;
00225 KSValue::Ptr m_value;
00226 QValueList<int> m_lines;
00227 };
00228
00229 class KSContext
00230 {
00231 public:
00232 KSContext();
00233 KSContext( KSContext& c, bool leftexpr = false );
00234 ~KSContext();
00235
00236 void setValue( const KSValue::Ptr& p ) { m_value = p; }
00242 void setValue( KSValue* p ) { m_value = p; }
00248 KSValue* value() { return m_value; }
00254 KSValue* shareValue() { if ( !m_value ) return 0; m_value->ref(); return m_value; }
00255
00259 void setExtraData( KSValue* p ) { m_extraData = p; }
00260
00264 KSValue* extraData() { return m_extraData; }
00265
00266
00267 void setException( KSContext& c ) { m_exception = c.exception(); if ( c.exception() ) c.exception()->ref(); }
00268 void setException( KSException::Ptr& p ) { m_exception = p; }
00269 void setException( KSException* p ) { m_exception = p; }
00270 KSException* exception() { return m_exception; }
00271 KSException* shareException() { if ( !m_exception ) return 0; m_exception->ref(); return m_exception; }
00272
00273 void setScope( KSContext& c ) { m_scope = c.scope(); if ( c.scope() ) c.scope()->ref(); }
00274 void setScope( KSScope::Ptr& p ) { m_scope = p; }
00275 void setScope( KSScope* p ) { m_scope = p; }
00276 KSScope* scope() { return m_scope; }
00277 KSValue* object( const QString& _name ) { if ( !!m_scope ) return m_scope->object( _name, m_bLeftExpr ); return 0; }
00278
00279 void setLeftExpr( bool b ) { m_bLeftExpr = b; }
00280 bool leftExpr() { return m_bLeftExpr; }
00281
00285 KSModule* module() { return m_scope->module(); }
00286
00287 KSInterpreter* interpreter() { return m_scope->module()->interpreter(); }
00288
00295 bool returnFlag() const { return m_bReturning; }
00301 void clearReturnFlag() { m_bReturning = false; }
00308 void setReturnFlag( bool b = true ) { m_bReturning = b; }
00309
00310 int tmpInt;
00311
00312 private:
00313 KSValue::Ptr m_value;
00314 KSValue::Ptr m_extraData;
00315 KSException::Ptr m_exception;
00316 KSScope::Ptr m_scope;
00317 bool m_bLeftExpr;
00318 bool m_bReturning;
00319 };
00320
00321 #endif
This file is part of the documentation for lib Library Version 1.3.5.