Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

skInterpreter.h

00001 /*
00002   Copyright 1996-2003
00003   Simon Whiteside
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 * $Id: skInterpreter_8h-source.html,v 1.7 2004/12/17 21:31:15 sdw Exp $
00020 */
00021 #ifndef skINTERPRETER_H
00022 #define skINTERPRETER_H
00023 
00024 #include "skRValueTable.h"
00025 #include "skiExecutable.h"
00026 #include "skNull.h"
00027 #include "skParseNode.h"
00028 #include "skExecutableIterator.h"
00029 #include "skRValueTable.h"
00030 
00031 class CLASSEXPORT skRValueArray;
00032 class CLASSEXPORT skExprNode;
00033 class CLASSEXPORT skStringList;
00034 class CLASSEXPORT skMethodDefNode;
00035 class CLASSEXPORT skTraceCallback;
00036 class CLASSEXPORT skStatementStepper;
00037 
00038 #ifndef EXCEPTIONS_DEFINED
00039 #include "skScriptError.h"
00040 #endif
00041 
00042 class CLASSEXPORT skStackFrame;
00043 
00051 class CLASSEXPORT skInterpreter : public skExecutable
00052 { 
00053  public:
00054   //------------------------
00055   // Parsing and Executing methods
00056   //------------------------
00057 
00069   IMPORT_C skMethodDefNode * parseString(const skString& location,const skString& code,skExecutableContext& ctxt);       
00082   IMPORT_C skMethodDefNode * parseExternalParams(const skString& location,skStringList& paramNames,const skString& code,skExecutableContext& ctxt);
00097   IMPORT_C void executeString(const skString& location,skiExecutable * obj,const skString& code,skRValueArray&  args,skRValue& return_value,skMethodDefNode ** parseTree,skExecutableContext& ctxt);
00098 
00113   IMPORT_C void executeStringExternalParams(const skString& location,skiExecutable * obj,skStringList& paramNames,const skString& code,skRValueArray&  args,skRValue& r,skMethodDefNode ** keepParseTree,skExecutableContext& ctxt);
00114 
00126   IMPORT_C void executeParseTree(const skString& location,skiExecutable * obj,skMethodDefNode * parseTree,skRValueArray&  args,skRValue& return_value,skExecutableContext& ctxt);
00127 
00128 
00141   IMPORT_C void evaluateExpression(const skString& location,skiExecutable * obj,
00142                       const skString& expression,skRValueTable&  vars,
00143                       skRValue& return_value,skExecutableContext& ctxt);
00156 #ifdef EXECUTE_PARSENODES
00157   IMPORT_C void evaluateExpression(const skString& location,skiExecutable * obj,
00158                       skExprNode * expression,skRValueTable&  vars,
00159                       skRValue& return_value,skExecutableContext& ctxt);
00160 #else
00161   IMPORT_C void evaluateExpression(const skString& location,skiExecutable * obj,
00162                           skCompiledCode& code,skRValueTable&  vars,
00163                           skRValue& return_value,skExecutableContext& ctxt);
00164 #endif
00165 
00166   //------------------------
00167   // Global Variable methods
00168   //------------------------
00169    
00176   IMPORT_C void addGlobalVariable(const skString& name,const skRValue& value);
00181   IMPORT_C void removeGlobalVariable(const skString& name);
00182 #ifdef __SYMBIAN32__
00183 
00190   IMPORT_C void addGlobalVariable(const TDesC& name,const skRValue& value);
00196   IMPORT_C void removeGlobalVariable(const TDesC& name);
00197 #endif
00198 
00205   IMPORT_C bool findGlobalVariable(const skString& name,skRValue& return_value);
00206 
00211   IMPORT_C const skRValueTable& getGlobalVariables() const;
00212 
00213   //--------------------------------------------------------
00214   // Interpreter is an Executable which exposes some fields
00215   //--------------------------------------------------------
00216     
00221   virtual IMPORT_C bool setValue(const skString& s,const skString& attribute,const skRValue& v);
00227   virtual IMPORT_C bool getValue(const skString& s,const skString& attribute,skRValue& v);
00239   virtual IMPORT_C bool method(const skString& method_name,skRValueArray& arguments,skRValue& return_value,skExecutableContext& ctxt);
00240 
00246   virtual void getInstanceVariables(skRValueTable& table);
00247     
00248   //------------------------
00249   // Tracing methods
00250   //------------------------
00251 
00255   IMPORT_C void trace(const skString& msg);
00259   IMPORT_C void trace(const Char * msg);
00263   IMPORT_C void setTraceCallback(skTraceCallback * callback);
00264 
00268   IMPORT_C void setStatementStepper(skStatementStepper * stepper);
00269 
00276   IMPORT_C void runtimeError(skStackFrame& ctxt,const skString& msg); 
00283   IMPORT_C void runtimeError(skStackFrame& ctxt,const Char * msg); 
00284 
00285   //---------------------------
00286   // Constructor and Destructor
00287   //---------------------------
00288     
00294   IMPORT_C skInterpreter();
00298   virtual IMPORT_C ~skInterpreter();
00304   IMPORT_C void init();
00305     
00306   inline skNull& getNull();
00307 
00308  private:
00309 
00310   //--------------------
00311   // copying not allowed
00312   //--------------------
00313 
00317   skInterpreter(const skInterpreter&);
00321   skInterpreter& operator=(const skInterpreter&);
00322 
00325   enum StatementReturnCode {SRC_CONTINUE=0,SRC_RETURN=1,SRC_BREAK=2};
00326 #ifdef EXECUTE_PARSENODES
00327 
00338   skExprNode * parseExpression(const skString& location,const skString& expression,skExecutableContext& ctxt);  
00339 #else
00340 
00351   skCompiledExprNode * parseExpression(const skString& location,const skString& expression,skExecutableContext& ctxt);  
00352 #endif
00353     // Expression evaluation
00354   
00360 #ifdef EXECUTE_PARSENODES
00361   skRValue evaluate(skStackFrame& frame,skExprNode * n);
00362 #else
00363   skRValue evaluate(skStackFrame& frame,skCompiledCode& code,USize& pc);
00364 #endif
00365 
00370 #ifdef EXECUTE_PARSENODES
00371   skRValue evalMethod(skStackFrame& frame,skIdListNode * ids);
00372 #else
00373   skRValue evalMethod(skStackFrame& frame,skCompiledCode& code,USize& pc);
00374 #endif
00375 
00385 #ifdef EXECUTE_PARSENODES
00386   void makeMethodCall(skStackFrame& frame,skRValue& robject,const skString& method_name,skExprNode * array_index, const skString& attribute,skExprListNode * exprs,skRValue& ret);
00387 #else
00388   void makeMethodCall(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& robject,const skString& method_name,bool has_array_index,const skString& attribute, skRValue& ret);
00389 #endif
00390   
00391     // Statement execution
00392 
00398 #ifdef EXECUTE_PARSENODES
00399   void executeAssignStat(skStackFrame& frame,skAssignNode * n);
00400 #else
00401   void executeAssignStat(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& r);
00402 #endif
00403   
00404     // the statements below return true to halt further processing (i.e. a return statement has been encountered)
00405 
00413 #ifdef EXECUTE_PARSENODES
00414   StatementReturnCode executeStats(skStackFrame& frame,skStatListNode * n,skRValue& r);
00415 #else
00416 
00426   StatementReturnCode executeStats(skStackFrame& frame,skCompiledCode& code,USize& pc,int& num_bytes,bool execute_bytes,skRValue& r);
00427 #endif
00428 
00435 #ifdef EXECUTE_PARSENODES
00436   StatementReturnCode executeReturnStat(skStackFrame& frame,skReturnNode * n,skRValue& r);
00437 #else
00438   StatementReturnCode executeReturnStat(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& r,bool has_expr);
00439 #endif
00440 
00447 #ifdef EXECUTE_PARSENODES
00448   StatementReturnCode executeBreakStat(skStackFrame& frame,skBreakNode * n,skRValue& r);
00449 #else
00450   StatementReturnCode executeBreakStat(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& r);
00451 #endif
00452 
00460 #ifdef EXECUTE_PARSENODES
00461   StatementReturnCode executeIfStat(skStackFrame& frame,skIfNode * n,skRValue& r);
00462 #else
00463   StatementReturnCode executeIfStat(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& r,bool has_else);
00464 #endif
00465 
00473 #ifdef EXECUTE_PARSENODES
00474   StatementReturnCode executeWhileStat(skStackFrame& frame,skWhileNode * n,skRValue& r);
00475 #else
00476   StatementReturnCode executeWhileStat(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& r);
00477 #endif
00478 
00486 #ifdef EXECUTE_PARSENODES
00487   StatementReturnCode executeSwitchStat(skStackFrame& frame,skSwitchNode * n,skRValue& r);
00488 #else
00489   StatementReturnCode executeSwitchStat(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& r,bool has_default);
00490 #endif
00491 
00499 #ifdef EXECUTE_PARSENODES
00500   StatementReturnCode executeForEachStat(skStackFrame& frame,skForEachNode * n,skRValue& r);
00501 #else
00502   StatementReturnCode executeForEachStat(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& r,USize id_index);
00503 #endif
00504 
00512 #ifdef EXECUTE_PARSENODES
00513   StatementReturnCode executeForStat(skStackFrame& frame,skForNode * n,skRValue& r);
00514 #else
00515   StatementReturnCode executeForStat(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& r,bool has_step);
00516 #endif
00517 
00518   // Misc runtime routines
00519 
00526   void addLocalVariable(skRValueTable& var,const skString& name,skRValue value); 
00534   inline skString checkIndirectId(skStackFrame& frame,const skString& name); 
00543 #ifdef EXECUTE_PARSENODES
00544   skRValue findValue(skStackFrame& frame,const skString& name,skExprNode * array_index,const skString& attribute); 
00545 #else
00546   skRValue findValue(skStackFrame& frame,const skString& name,const skRValue * array_index,const skString& attribute); 
00547 #endif
00548 
00554 #ifdef EXECUTE_PARSENODES
00555   void followIdList(skStackFrame& frame,skIdListNode * idList,skRValue& object); 
00556 #else
00557   void followIdList(skStackFrame& frame,skCompiledCode& code,USize& pc,int num_ids,skRValue& object); 
00558 #endif
00559 
00560 
00570 #ifdef EXECUTE_PARSENODES
00571   bool extractFieldArrayValue(skStackFrame& frame,skRValue& robject,const skString& field_name,skExprNode * array_index,const skString& attrib,skRValue& ret);
00572 #else
00573   bool extractFieldArrayValue(skStackFrame& frame,skRValue& robject,const skString& field_name,const skRValue& array_index,const skString& attrib,skRValue& ret);
00574 #endif
00575 
00582 #ifdef EXECUTE_PARSENODES
00583   bool extractArrayValue(skStackFrame& frame,skRValue& robject,skExprNode * array_index,const skString& attrib,skRValue& ret) ;
00584 #else
00585   bool extractArrayValue(skStackFrame& frame,skRValue& robject,const skRValue& array_index,const skString& attrib,skRValue& ret) ;
00586 #endif
00587 
00595   bool extractValue(skStackFrame& frame,skRValue& robject,const skString& name,const skString& attrib,skRValue& ret) ;
00596 
00605 #ifdef EXECUTE_PARSENODES
00606   bool insertArrayValue(skStackFrame& frame,skRValue& robject, skExprNode * array_index,const skString& attr,const skRValue& value);
00607 #else
00608   bool insertArrayValue(skStackFrame& frame,skCompiledCode& code,USize& pc,skRValue& robject, const skString& attr,const skRValue& value);
00609 #endif
00610 
00618   bool insertValue(skStackFrame& frame,skRValue& robject,const skString& name, const skString& attr,const skRValue& value);
00619 
00620 #ifndef EXECUTE_PARSENODES
00621   void getIdNode(skCompiledCode& code,USize& pc,skString& id,bool& has_array,bool& is_method);
00622   void getIdNodes(skCompiledCode& code,USize& pc,int& num_ids,skString& attribute);
00623 #endif
00624 
00625   // Variables
00627   skRValueTable m_GlobalVars; 
00629   bool m_Tracing; 
00631   skTraceCallback * m_TraceCallback; 
00633   skStatementStepper * m_StatementStepper; // the statement stepper
00637   skNull m_Null;
00638 };      
00639 //---------------------------------------------------
00640 inline skString skInterpreter::checkIndirectId(skStackFrame& frame,const skString& name)
00641 //---------------------------------------------------
00642 {
00643   // look for an initial "@" in a field name, and de-reference it if necessary
00644   skString ret=name;
00645   if (name.at(0)=='@'){
00646     ret=name.substr(1,name.length()-1);
00647     skRValue new_name=findValue(frame,ret,0,skString());
00648     ret=new_name.str();
00649   }
00650   return ret;
00651 }       
00652 //---------------------------------------------------
00653 inline skNull& skInterpreter::getNull()
00654 //---------------------------------------------------
00655 {
00656   return m_Null;
00657 }
00658 
00659 xskNAMED_LITERAL(OnANonObject,skSTR(" on a non-object\n"));
00660 
00661 #endif
00662 
00663 

Generated on Fri Dec 17 20:28:26 2004 for Simkin C++ for Symbian by doxygen1.3