随笔-341  评论-2670  文章-0  trackbacks-0
    为了让宿主程序可以更加清楚一份Kernel FP代码的内容,我今天为Kernel FP添加了反射的API。

    Kernel FP的脚本引擎分为VL_KfpSymbol和VL_KfpMachine。其中VL_KfpSymbol负责编译代码和构造VL_KfpMachine。VL_KfpMachine的运行可以脱离VL_KfpSymbol,VL_KfpSymbol再次添加代码并编译的时候,构造的VL_KfpMachine并不受到影响。先前VL_KfpMachine已经有了小部分反射,主要用于枚举各种对象的运行时ID。这次VL_KfpSymbol的反射则是用于查看已经添加的代码的结构。

    反射API如下:
  1 /*********************************************************************************************************
  2 反射
  3 *********************************************************************************************************/
  4 
  5         class VL_KfpAsmDecl : public VL_Base
  6         {
  7         public:
  8             virtual VUnicodeString                GetName()=0;
  9             virtual VUnicodeString                GetOwnerUnitName()=0;
 10         };
 11 
 12         class VL_KfpAsmTypeDecl : public VL_KfpAsmDecl
 13         {
 14             friend class VL_KfpAsmUnit;
 15         protected:
 16             VL_KfpIdTypeDeclaration::Ptr    FInternalObject;
 17 
 18             VL_KfpAsmTypeDecl(VL_KfpIdTypeDeclaration::Ptr Object);
 19         public:
 20             VL_KfpAsmTypeDecl();
 21             ~VL_KfpAsmTypeDecl();
 22 
 23             VUnicodeString                        GetName();
 24             VUnicodeString                        GetOwnerUnitName();
 25             VInt                                GetTypeParameterCount();
 26             VUnicodeString                        GetTypeParameter(VInt Index);
 27             VL_KfpIdType::Ptr                    GetReferencedType();
 28         };
 29 
 30         class VL_KfpAsmCtorDecl : public VL_KfpAsmDecl
 31         {
 32             friend class VL_KfpAsmUnit;
 33         protected:
 34             VL_KfpIdCtorDeclaration::Ptr        FInternalObject;
 35 
 36             VL_KfpAsmCtorDecl(VL_KfpIdCtorDeclaration::Ptr Object);
 37         public:
 38             VL_KfpAsmCtorDecl();
 39             ~VL_KfpAsmCtorDecl();
 40 
 41             VUnicodeString                        GetName();
 42             VUnicodeString                        GetOwnerUnitName();
 43             VUnicodeString                        GetOwnerTypeName();
 44             VL_KfpIdType::Ptr                    GetType();
 45         };
 46 
 47         class VL_KfpAsmExpectedDecl : public VL_KfpAsmDecl
 48         {
 49             friend class VL_KfpAsmUnit;
 50         protected:
 51             VL_KfpIdExpectedDeclaration::Ptr    FInternalObject;
 52 
 53             VL_KfpAsmExpectedDecl(VL_KfpIdExpectedDeclaration::Ptr Object);
 54         public:
 55             VL_KfpAsmExpectedDecl();
 56             ~VL_KfpAsmExpectedDecl();
 57 
 58             VUnicodeString                        GetName();
 59             VUnicodeString                        GetOwnerUnitName();
 60             VInt                                GetTypeParameterCount();
 61             VUnicodeString                        GetTypeParameter(VInt Index);
 62             VL_KfpIdType::Ptr                    GetType();
 63         };
 64 
 65         class VL_KfpAsmFunctionDecl : public VL_KfpAsmDecl
 66         {
 67             friend class VL_KfpAsmUnit;
 68         protected:
 69             VL_KfpIdFunctionDeclaration::Ptr    FInternalObject;
 70 
 71             VL_KfpAsmFunctionDecl(VL_KfpIdFunctionDeclaration::Ptr Object);
 72         public:
 73             VL_KfpAsmFunctionDecl();
 74             ~VL_KfpAsmFunctionDecl();
 75 
 76             VUnicodeString                        GetName();
 77             VUnicodeString                        GetOwnerUnitName();
 78             VInt                                GetTypeParameterCount();
 79             VUnicodeString                        GetTypeParameter(VInt Index);
 80             VL_KfpIdType::Ptr                    GetType();
 81             VBool                                IsExternalFunction();
 82             VUnicodeString                        GetAlias();
 83         };
 84 
 85         class VL_KfpAsmUnit : public VL_Base
 86         {
 87             friend class VL_KfpAsmProgram;
 88         protected:
 89             VL_KfpIdTable::Ptr                    FTable;
 90 
 91             VL_KfpAsmUnit(VL_KfpIdTable::Ptr Table);
 92         public:
 93             VL_KfpAsmUnit();
 94             ~VL_KfpAsmUnit();
 95 
 96             VUnicodeString                        GetName();
 97             VInt                                GetImportCount();
 98             VUnicodeString                        GetImport(VInt Index);
 99             VInt                                GetTypeDeclarationCount();
100             VL_KfpAsmTypeDecl                    GetTypeDeclaration(VInt Index);
101             VInt                                GetCtorDeclarationCount();
102             VL_KfpAsmCtorDecl                    GetCtorDeclaration(VInt Index);
103             VInt                                GetExpectedDeclarationCount();
104             VL_KfpAsmExpectedDecl                GetExpectedDeclaration(VInt Index);
105             VInt                                GetFunctionDeclarationCount();
106             VL_KfpAsmFunctionDecl                GetFunctionDeclaration(VInt Index);
107         };
108 
109         class VL_KfpAsmProgram : public VL_Base
110         {
111             friend class VL_KfpSymbol;
112         protected:
113             typedef VL_AutoPtr<VL_KfpAsmProgram>                    Ptr;
114             VL_KfpCodeUnitAnalyser*                FAnalyser;
115 
116             VL_KfpAsmProgram(VL_KfpCodeUnitAnalyser* Analyser);
117         public:
118             VL_KfpAsmProgram();
119             ~VL_KfpAsmProgram();
120 
121             VInt                                GetUnitCount();
122             VL_KfpAsmUnit                        GetUnit(VInt Index);
123         };
124 
125 /*********************************************************************************************************
126 脚本引擎
127 *********************************************************************************************************/
128 
129         class VL_KfpSymbol : public VL_Base
130         {
131         protected:
132             VL_KfpCodeProvider                    FProvider;
133             VL_KfpCodeUnitAnalyser                FAnalyser;
134             VL_KfpCodeUnit::List                FCodeUnits;
135             VBool                                FErrorState;
136             VL_KfpAsmProgram::Ptr                FProgram;
137         public:
138             VL_KfpSymbol();
139             ~VL_KfpSymbol();
140 
141             VL_KfpCodeUnit::Ptr                    AddUnit(VUnicodeString Code , VL_KfpError::List& Errors);
142             void                                AddUnit(KfpUnit& Unit);
143             VBool                                PreCompile(VL_KfpError::List& Errors);
144             VBool                                IsErrorState();
145             VBool                                HasCodeUnit();
146             VL_KfpMachine::Ptr                    CreateMachine();
147             VUnicodeString                        GetReport();
148             VUnicodeString                        GetDebugInformation();
149             VL_KfpAsmProgram*                    GetProgramForReflection();
150         };

    以下为使用方法。这是一个函数,从VL_KfpSymbol对象开始反射并打印出所反射到的内容:
 1 void ReflectSymbol(VL_KfpSymbol* Symbol)
 2 {
 3     for(VInt i=0;i<Symbol->GetProgramForReflection()->GetUnitCount();i++)
 4     {
 5         VL_KfpAsmUnit Unit=Symbol->GetProgramForReflection()->GetUnit(i);
 6         GetConsole()->Write(L"Unit : "+Unit.GetName()+L"\r\n");
 7         for(VInt j=0;j<Unit.GetImportCount();j++)
 8         {
 9             GetConsole()->Write(L"  Import : "+Unit.GetImport(j)+L"\r\n");
10         }
11         for(VInt j=0;j<Unit.GetTypeDeclarationCount();j++)
12         {
13             VL_KfpAsmTypeDecl TypeDecl=Unit.GetTypeDeclaration(j);
14             GetConsole()->Write(L"    Type Declaration : "+TypeDecl.GetOwnerUnitName()+L"."+TypeDecl.GetName()+L"\r\n");
15             GetConsole()->Write(L"      Parameters : ");
16             for(VInt k=0;k<TypeDecl.GetTypeParameterCount();k++)
17             {
18                 GetConsole()->Write(TypeDecl.GetTypeParameter(k)+L" ");
19             }
20             GetConsole()->Write(L"\r\n");
21             GetConsole()->Write(L"      Type :"+TypeDecl.GetReferencedType()->GenerateCode(L"")+L"\r\n");
22         }
23         for(VInt j=0;j<Unit.GetCtorDeclarationCount();j++)
24         {
25             VL_KfpAsmCtorDecl CtorDecl=Unit.GetCtorDeclaration(j);
26             GetConsole()->Write(L"    Ctor Declaration : "+CtorDecl.GetOwnerUnitName()+L"."+CtorDecl.GetName()+L"\r\n");
27             GetConsole()->Write(L"      Source : "+CtorDecl.GetOwnerTypeName()+L"\r\n");
28             GetConsole()->Write(L"      Type :"+CtorDecl.GetType()->GenerateCode(L"")+L"\r\n");
29         }
30         for(VInt j=0;j<Unit.GetExpectedDeclarationCount();j++)
31         {
32             VL_KfpAsmExpectedDecl ExpectedDecl=Unit.GetExpectedDeclaration(j);
33             GetConsole()->Write(L"    Expected Declaration : "+ExpectedDecl.GetOwnerUnitName()+L"."+ExpectedDecl.GetName()+L"\r\n");
34             GetConsole()->Write(L"      Parameters : ");
35             for(VInt k=0;k<ExpectedDecl.GetTypeParameterCount();k++)
36             {
37                 GetConsole()->Write(ExpectedDecl.GetTypeParameter(k)+L" ");
38             }
39             GetConsole()->Write(L"\r\n");
40             GetConsole()->Write(L"      Type :"+ExpectedDecl.GetType()->GenerateCode(L"")+L"\r\n");
41         }
42         for(VInt j=0;j<Unit.GetFunctionDeclarationCount();j++)
43         {
44             VL_KfpAsmFunctionDecl FunctionDecl=Unit.GetFunctionDeclaration(j);
45             GetConsole()->Write(L"    Function Declaration : "+FunctionDecl.GetOwnerUnitName()+L"."+FunctionDecl.GetName()+L"\r\n");
46             GetConsole()->Write(L"      Parameters : ");
47             for(VInt k=0;k<FunctionDecl.GetTypeParameterCount();k++)
48             {
49                 GetConsole()->Write(FunctionDecl.GetTypeParameter(k)+L" ");
50             }
51             GetConsole()->Write(L"\r\n");
52             GetConsole()->Write(L"      Type :"+FunctionDecl.GetType()->GenerateCode(L"")+L"\r\n");
53             if(FunctionDecl.IsExternalFunction())
54             {
55             GetConsole()->Write(L"      Alias :"+FunctionDecl.GetAlias()+L"\r\n");
56             }
57         }
58         GetConsole()->Write(L"按[ENTER]继续");
59         GetConsole()->WaitForEnter();
60     }
61 }

    对于宿主程序来说,反射主要用于检查连接函数的类型,并根据不同的类型使用不同的求值方法进行求值。
posted on 2008-12-20 06:10 陈梓瀚(vczh) 阅读(1410) 评论(0)  编辑 收藏 引用 所属分类: 脚本技术

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理