00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef VM_H
00021 #define VM_H
00022
00023 #include "config.h"
00024 #include "scanner.h"
00025
00026 #ifndef BUILDING_DOX
00027 # include <boost/smart_ptr.hpp>
00028 # include <iostream>
00029 # include <set>
00030 # include <string>
00031 # include <vector>
00032 #endif
00033
00037 struct Value {
00038 enum VType {
00039 V_NULL = 0,
00040 V_BOOL,
00041 V_INT,
00042 V_DOUBLE,
00043 V_STRING
00044 };
00045 VType type;
00046
00047 bool boolVal;
00048 int intVal;
00049 double doubleVal;
00050 std::string stringVal;
00051
00052 Value();
00053 };
00054 std::ostream& operator<< (std::ostream &, const Value::VType &);
00055 std::ostream& operator<< (std::ostream &, const Value &);
00056 typedef SHARED_PTR(Value) PValue;
00057 typedef SHARED_PTR(const Value) PConstValue;
00058
00062 class ValueFactory {
00063 public:
00064 static PValue create();
00065 static PValue create(bool);
00066 static PValue create(int);
00067 static PValue create(double);
00068 static PValue create(const std::string &);
00069 private:
00070 ValueFactory();
00071 };
00072
00077 struct Var {
00078 std::string name;
00079 Value value;
00080 Token defined;
00081
00083 bool used;
00084
00086 bool initialized;
00087
00088 Var();
00089 };
00090 typedef SHARED_PTR(Var) PVar;
00091
00095 class VarSet {
00096 public:
00097 VarSet();
00098 ~VarSet();
00099
00101 VarSet &operator= (const VarSet &);
00102
00103 bool add(PVar);
00104 unsigned size() const;
00105 PVar operator[] (unsigned);
00106 PVar operator[] (const std::string &);
00107
00109 const PVar operator[] (unsigned pos) const {
00110 return const_cast<VarSet *>(this)
00111 -> operator[] (pos);
00112 }
00114 const PVar operator[] (const std::string &name) const {
00115 return const_cast<VarSet *>(this)
00116 -> operator[] (name);
00117 }
00118 private:
00119 struct Private;
00120 Private *d;
00121
00123 VarSet(const VarSet &);
00124 };
00125
00129 struct FncDeclaration {
00130 FncDeclaration();
00131 FncDeclaration(const FncDeclaration &);
00132 FncDeclaration& operator= (const FncDeclaration &);
00133 virtual ~FncDeclaration();
00134
00136 Var self;
00137
00139 VarSet args;
00140 };
00142 bool operator== (const FncDeclaration &, const FncDeclaration &);
00144 bool operator!= (const FncDeclaration &, const FncDeclaration &);
00145
00146 class Vm;
00147 class CmdList;
00148 typedef SHARED_PTR(CmdList) PCmdList;
00149
00153 struct FncDefinition: public FncDeclaration {
00155 FncDefinition(Vm *vm);
00156
00157
00158
00159
00160
00161 FncDefinition(const FncDefinition &);
00162 virtual ~FncDefinition();
00163
00164 Vm *const vm;
00165 VarSet vars;
00166 const PCmdList cmdList;
00167
00168 private:
00170 FncDefinition& operator= (const FncDefinition &);
00171 };
00173 bool chkUnused(FncDefinition *);
00174
00179 class FncSet {
00180 public:
00181 FncSet();
00182 ~FncSet();
00183
00189 bool addDeclaration(FncDeclaration *);
00190
00195 FncDeclaration* getDeclaration(const std::string &);
00196
00203 bool addDefinition(FncDefinition *);
00204
00209 FncDefinition* getDefinition(const std::string &);
00210
00212 typedef STD_VECTOR(const FncDefinition *) TVector;
00213
00219 void getAllDefinitions(TVector &);
00220 private:
00221 struct Private;
00222 Private *d;
00223 };
00224
00229 class ICmd {
00230 public:
00231 virtual ~ICmd() { }
00232
00234 virtual bool exec(FncDefinition *) = 0;
00235
00237 virtual void toStream(std::ostream &) const = 0;
00238 };
00239 typedef SHARED_PTR(ICmd) PCmd;
00240
00245 class CmdList: public ICmd {
00246 public:
00247 CmdList();
00248 virtual ~CmdList();
00249 virtual bool exec(FncDefinition *);
00250 virtual void toStream(std::ostream &) const;
00251
00253 void add(PCmd);
00254 private:
00255 struct Private;
00256 Private *d;
00257 };
00258 typedef SHARED_PTR(CmdList) PCmdList;
00259
00264 class ValueStack {
00265 public:
00266 ValueStack(Vm *vm);
00267 ~ValueStack();
00268
00269 bool isEmpty() const;
00270 void push(PValue);
00271 PValue pop();
00272
00278 bool push(PValue, const Token &);
00279
00286 bool pushFromVar(const Var &, const Token &);
00287
00293 bool pop(PValue &, const Token &);
00294
00301 bool popToVar(Var &, const Token &);
00302
00303 private:
00304 struct Private;
00305 Private *d;
00306 };
00307
00313 class CalleeSet {
00314 public:
00315 typedef STD_VECTOR(std::string) TStringList;
00316 typedef STD_VECTOR(Token) TTokenList;
00317 CalleeSet();
00318 ~CalleeSet();
00319
00325 void add(std::string name, Token tCall);
00326
00328 bool isCalled(std::string name);
00329
00331 void getNames(TStringList &);
00332
00334 bool getCalls(std::string name, TTokenList &);
00335
00336 private:
00337 struct Private;
00338 Private *d;
00339 };
00340
00344 struct Vm {
00345 const std::string fileName;
00346 VarSet glVars;
00347 FncSet fncSet;
00348 ValueStack vmStack;
00349 CalleeSet calleeSet;
00350 int callDepth;
00351 int maxCallDepth;
00352 static const int DEF_MAX_CALL_DEPTH = 1024;
00353
00354 Vm(std::string fileName_):
00355 fileName(fileName_),
00356 vmStack(this),
00357 callDepth(0),
00358 maxCallDepth(DEF_MAX_CALL_DEPTH)
00359 {
00360 }
00361 };
00362
00367 class VmRunner: public IErrorSensitive {
00368 public:
00369 VmRunner(Vm *vm);
00370 virtual ~VmRunner();
00371 virtual bool hasError() const;
00372 bool run();
00373 private:
00374 struct Private;
00375 Private *d;
00376 };
00377
00378 #endif