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.h,v 1.37 2003/03/18 19:36:13 simkin_cvs 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 
00068   skMethodDefNode * parseString(const skString& location,const skString& code,skExecutableContext& ctxt);       
00080   skMethodDefNode * parseExternalParams(const skString& location,skStringList& paramNames,const skString& code,skExecutableContext& ctxt);
00094   void executeString(const skString& location,skiExecutable * obj,const skString& code,skRValueArray&  args,skRValue& return_value,skMethodDefNode ** parseTree,skExecutableContext& ctxt);
00095 
00109   void executeStringExternalParams(const skString& location,skiExecutable * obj,skStringList& paramNames,const skString& code,skRValueArray&  args,skRValue& r,skMethodDefNode ** keepParseTree,skExecutableContext& ctxt);
00110 
00121   void executeParseTree(const skString& location,skiExecutable * obj,skMethodDefNode * parseTree,skRValueArray&  args,skRValue& return_value,skExecutableContext& ctxt);
00122 
00123 
00134   skExprNode * parseExpression(const skString& location,const skString& expression,skExecutableContext& ctxt);  
00146   void evaluateExpression(const skString& location,skiExecutable * obj,
00147                       const skString& expression,skRValueTable&  vars,
00148                       skRValue& return_value,skExecutableContext& ctxt);
00149 
00161   void evaluateExpression(const skString& location,skiExecutable * obj,
00162                       skExprNode * expression,skRValueTable&  vars,
00163                       skRValue& return_value,skExecutableContext& ctxt);
00164 
00165   //------------------------
00166   // Global Variable methods
00167   //------------------------
00168    
00174   void addGlobalVariable(const skString& name,skRValue value);
00179   void removeGlobalVariable(const skString& name);
00187   bool findGlobalVariable(const skString& name,skRValue& return_value);
00188 
00193   const skRValueTable& getGlobalVariables() const;
00194 
00195   //--------------------------------------------------------
00196   // Interpreter is an Executable which exposes some fields
00197   //--------------------------------------------------------
00198     
00203   virtual bool setValue(const skString& s,const skString& attribute,const skRValue& v);
00209   virtual bool getValue(const skString& s,const skString& attribute,skRValue& v);
00219   virtual bool method(const skString& method_name,skRValueArray& arguments,skRValue& return_value,skExecutableContext& ctxt);
00220 
00225   virtual void getInstanceVariables(skRValueTable& table);
00226     
00227   //------------------------
00228   // Tracing methods
00229   //------------------------
00230 
00234   void trace(const skString& msg);
00238   void setTraceCallback(skTraceCallback * callback);
00239 
00243   void setStatementStepper(skStatementStepper * stepper);
00244 
00250   void runtimeError(skStackFrame& ctxt,const skString& msg); 
00251 
00252   //---------------------------
00253   // Constructor and Destructor
00254   //---------------------------
00255     
00259   skInterpreter();
00263   ~skInterpreter();
00264     
00265  
00269   static skNull g_Null;
00270 
00271  private:
00272 
00273   //--------------------
00274   // copying not allowed
00275   //--------------------
00276 
00280   skInterpreter(const skInterpreter&);
00284   skInterpreter& operator=(const skInterpreter&);
00285 
00286 
00287     // Expression evaluation
00288   
00293   skRValue evaluate(skStackFrame& frame,skExprNode * n);
00298   skRValue evalMethod(skStackFrame& frame,skIdListNode * ids);
00308   void makeMethodCall(skStackFrame& frame,skRValue& robject,const skString& method_name,skExprNode * array_index, const skString& attribute,skExprListNode * exprs,skRValue& ret);
00309   
00310     // Statement execution
00311 
00316   void executeAssignStat(skStackFrame& frame,skAssignNode * n);
00317   
00318     // the statements below return true to halt further processing (i.e. a return statement has been encountered)
00319 
00326   bool executeStat(skStackFrame& frame,skStatNode * pstat,skRValue& r);
00327 
00334   bool executeStats(skStackFrame& frame,skStatListNode * n,skRValue& r);
00335 
00342   bool executeReturnStat(skStackFrame& frame,skReturnNode * n,skRValue& r);
00343 
00350   bool executeIfStat(skStackFrame& frame,skIfNode * n,skRValue& r);
00351 
00358   bool executeWhileStat(skStackFrame& frame,skWhileNode * n,skRValue& r);
00359 
00366   bool executeSwitchStat(skStackFrame& frame,skSwitchNode * n,skRValue& r);
00367 
00374   bool executeForEachStat(skStackFrame& frame,skForEachNode * n,skRValue& r);
00375 
00382   bool executeForStat(skStackFrame& frame,skForNode * n,skRValue& r);
00383 
00384   // Misc runtime routines
00385 
00391     void addLocalVariable(skRValueTable& var,const skString& name,skRValue value); 
00398     skString checkIndirectId(skStackFrame& frame,const skString& name); 
00406   skRValue findValue(skStackFrame& frame,const skString& name,skExprNode * array_index,const skString& attribute); 
00412   void followIdList(skStackFrame& frame,skIdListNode * idList,skRValue& object); 
00413 
00414 
00423   bool extractFieldArrayValue(skStackFrame& frame,skRValue& robject,const skString& field_name,skExprNode * array_index,const skString& attrib,skRValue& ret);
00431   bool extractArrayValue(skStackFrame& frame,skRValue& robject,skExprNode * array_index,const skString& attrib,skRValue& ret) ;
00439   bool extractValue(skStackFrame& frame,skRValue& robject,const skString& name,const skString& attrib,skRValue& ret) ;
00440 
00448   bool insertArrayValue(skStackFrame& frame,skRValue& robject, skExprNode * array_index,const skString& attr,const skRValue& value);
00456   bool insertValue(skStackFrame& frame,skRValue& robject,const skString& name, const skString& attr,const skRValue& value);
00457 
00458   // Variables
00460   skRValueTable m_GlobalVars; 
00462   bool m_Tracing; 
00464   skTraceCallback * m_TraceCallback; 
00466   skStatementStepper * m_StatementStepper; // the statement stepper
00467 };      
00468 //---------------------------------------------------
00469 inline skString skInterpreter::checkIndirectId(skStackFrame& frame,const skString& name)
00470 //---------------------------------------------------
00471 {
00472   // look for an initial "@" in a field name, and de-reference it if necessary
00473   skString ret=name;
00474   if (name.at(0)=='@'){
00475     ret=name.substr(1,name.length()-1);
00476     skRValue new_name=findValue(frame,ret,0,skString());
00477     ret=new_name.str();
00478   }
00479   return ret;
00480 }       
00481 
00482 
00483 #endif
00484 
00485 

Generated on Tue Mar 18 19:46:18 2003 for Simkin by doxygen1.3-rc1