00001
00002 #ifndef _LGI_SCRIPTING_H_
00003 #define _LGI_SCRIPTING_H_
00004
00005 #include "GVariant.h"
00006
00007 class GScriptContext;
00008 class GScriptEngine;
00009 class GScriptEnginePrivate;
00010 class GCompiledCode;
00011
00012 typedef GArray<GVariant*> ArgumentArray;
00013 typedef bool (GScriptContext::*ScriptCmd)(GVariant *Ret, ArgumentArray &Args);
00014
00016 enum GFuncType
00017 {
00019 NullFunc,
00021 HostFunc,
00023 ScriptFunc,
00025 ExternFunc
00026 };
00027
00028 struct GFunc
00029 {
00030 GFuncType Type;
00031 char *Method;
00032
00033 GFunc(const char *m = 0, GFuncType t = NullFunc)
00034 {
00035 Type = t;
00036 Method = NewStr(m);
00037 }
00038
00039 virtual ~GFunc()
00040 {
00041 DeleteArray(Method);
00042 }
00043
00044 virtual bool Call(GScriptContext *Ctx, GVariant *Ret, ArgumentArray &Args) = 0;
00045 };
00046
00047 struct GHostFunc : public GFunc
00048 {
00049 GScriptContext *Context;
00050 char *Args;
00051 ScriptCmd Func;
00052
00053 GHostFunc(const char *method, const char *args, ScriptCmd proc) : GFunc(method, HostFunc)
00054 {
00055 Args = NewStr(args);
00056 Func = proc;
00057 }
00058
00059 ~GHostFunc()
00060 {
00061 DeleteArray(Args);
00062 }
00063
00064 bool Call(GScriptContext *Ctx, GVariant *Ret, ArgumentArray &Args);
00065 };
00066
00067 class GScriptUtils
00068 {
00069 public:
00070 virtual ~GScriptUtils() {}
00071
00072 int atoi(char16 *s);
00073 int64 atoi64(char16 *s);
00074 double atof(char16 *s);
00075 int htoi(char16 *s);
00076 };
00077
00079 class GScriptContext : public GScriptUtils
00080 {
00081 public:
00082 virtual ~GScriptContext() {}
00083
00084 virtual GHostFunc *GetCommands() = 0;
00085 virtual void SetEngine(GScriptEngine *Eng) = 0;
00086 virtual char *GetIncludeFile(char *FileName) = 0;
00087 virtual GAutoString GetDataFolder() { return GAutoString(); }
00088
00089
00090
00091
00092
00093
00094 };
00095
00097 class GScriptEngine
00098 {
00099 friend class SystemFunctions;
00100
00101 protected:
00102 virtual GCompiledCode *GetCurrentCode() { return 0; }
00103
00104 public:
00105 virtual ~GScriptEngine() {}
00106
00107 #define NotImplmented { LgiAssert(!"Not implemented"); }
00108 #define NotImplmentedRet0 { LgiAssert(!"Not implemented"); return 0; }
00109
00111 virtual void Empty() NotImplmented
00112
00114 virtual bool Compile(char *Script, bool Add = false) NotImplmentedRet0
00115
00117 virtual bool Run() NotImplmentedRet0
00118
00120 virtual bool RunTemporary(char *Script) NotImplmentedRet0
00121
00123 virtual bool EvaluateExpression(GVariant *Result, GDom *VariableSource, char *Expression) NotImplmentedRet0
00124
00126 virtual GVariant *Var(char16 *name, bool create = true) NotImplmentedRet0
00127
00129 virtual GStringPipe *GetTerm() NotImplmentedRet0
00130
00132 virtual bool CallMethod(const char *Method, GVariant *Ret, ArgumentArray &Args) NotImplmentedRet0
00133
00135 virtual void DumpVariables() NotImplmented
00136 };
00137
00139 class GScriptEngine1 : public GScriptEngine
00140 {
00141 friend class GScriptEval;
00142
00143 GScriptEnginePrivate *d;
00144
00145 public:
00146 GScriptEngine1(GViewI *parent, GScriptContext *context);
00147 ~GScriptEngine1();
00148
00149 void Empty();
00150 bool Compile(char *Script, bool Add = false);
00151 bool Run();
00152 bool RunTemporary(char *Script);
00153 bool EvaluateExpression(GVariant *Result, GDom *VariableSource, char *Expression);
00154 GVariant *Var(char16 *name, bool create = true);
00155 GStringPipe *GetTerm();
00156 bool CallMethod(const char *Method, GVariant *Ret, ArgumentArray &Args);
00157 void DumpVariables();
00158 };
00159
00161 class GScriptEngine2 : public GScriptEngine
00162 {
00163 class GScriptEnginePrivate2 *d;
00164
00165 GCompiledCode *GetCurrentCode();
00166
00167 public:
00168 GScriptEngine2(GViewI *parent, GScriptContext *context);
00169 ~GScriptEngine2();
00170
00171 void Empty();
00172 bool Compile(char *Script, bool Add = false);
00173 bool Run();
00174 bool RunTemporary(char *Script);
00175 bool EvaluateExpression(GVariant *Result, GDom *VariableSource, char *Expression);
00176 GVariant *Var(char16 *name, bool create = true);
00177 GStringPipe *GetTerm();
00178 bool CallMethod(const char *Method, GVariant *Ret, ArgumentArray &Args);
00179 void DumpVariables();
00180 };
00181
00182 #endif
00183