1. 程式人生 > >Vczh Library++3.0託管語言語法樹完成

Vczh Library++3.0託管語言語法樹完成

    經歷了大約一個多星期的挑選,Vczh Library++3.0託管語言的語法樹大約就定下來了。跟本地語言有一個預設的語法叫NativeX一樣,這次的語法就叫ManagedX了。當然ManagedX只是設計來直接反映託管語言語法樹的一門語言,如果願意的話,這個語法樹還能被編譯成其他長得像(嗯,只能是像,因為必須是託管的,有類沒指標等)Basic啦Pascal那樣子的東西。

    我將ManagedX的語法分為兩部分。第一部分是基礎部分,第二部分則是擴充套件部分。將他們分開的依據就是,一個基於擴充套件部分的程式,總是可以被重寫為一個功能完全一致但是僅基於基礎部分的比較複雜的程式。舉一個簡單的例子。我們知道lambda表示式在一門面向物件語言中並不是必需的,它只是一個語法糖。如果我們在面嚮物件語言中把函式指標統統抽象成一個基類或者介面(就跟C#的Delegate一樣)的話,一個lambda表示式總是可以被表達成一個新的型別,然後只需要重新組織一下生成該lambda表示式的函式的區域性變數,就可以把lambda表達是去掉了。當然lambda表示式作為一個語法來說是必需的,因為它大大提高了開發效率。但是對於編譯過程來說又不是必需的,因為它總是可以用自動重寫的方法來去掉。

    因此ManagedX的編譯過程就比較清晰了:
    1、我們接受一個基於擴充套件部分的程式,然後經過第一輪的語法分析,得到語法樹上面所有節點的型別資訊和引用資訊。
    2、根據分析出來的資訊,將該程式重寫為僅基於基礎部分的程式。在重寫的過程中會使得資訊不會丟失(特別是ManagedX支援反射,而反射卻可以支援擴充套件部分,所以需要做很多小動作來維持重寫前後的程式執行上的一致性)
    3、對重寫出來的新程式重新進行語法分析。
    4、根據分析出來的資訊,將它編譯成
NativeX


    從這裡打架就可以知道為什麼我之前要做NativeX了,因為實現了NativeX之後,以後實現很多新的複雜語言會十分省事。執行時的虛擬機器做好了,函式庫也有了一部分,還有基於二進位制編譯結果的多檔案支援啦、泛型啦、concept mapping啦、外接函式外掛啦、還有將來會做的debugger,和編譯到X86的過程,都可以直接被新的語言所享受。在這裡ManagedX的開發過程就僅僅是一個前端的開發過程了,因為後端的第一階段已經完成了,至少可以直接使用。

    下面貼一下ManagedX那將近900行的語法樹:(Library\Scripting\ManagedLanguage\ManagedLanguageExpression.h)
  1
 /***********************************************************************
  2 Vczh Library++ 3.0
  3 Developer: 陳梓瀚(vczh)
  4 Scripting::ManagedLanguage
  5   6 Classes:
  7 ***********************************************************************/  8   9 #ifndef VCZH_SCRIPTING_MANAGEDLANGUAGE_MANAGEDLANGUAGEEXPRESSION
 10 #define VCZH_SCRIPTING_MANAGEDLANGUAGE_MANAGEDLANGUAGEEXPRESSION 11  12 #include "..\..\Pointer.h" 13 #include "..\..\String.h" 14 #include "..\..\Collections\List.h" 15 #include "..\..\Collections\Dictionary.h" 16 #include "..\Common\AlgorithmDeclaration.h" 17  18 namespace vl
 19 {
 20 namespace scripting
 21     {
 22 namespace managedlanguage
 23         {
 24 struct ManagedPosition
 25             {
 26                 vint                                        start;
 27                 vint                                        lineStart;
 28                 vint                                        lineIndex;
 29                 vint                                        codeIndex;
 30  31                 ManagedPosition();
 32             };
 33  34 class ManagedLanguageElement : public Object, private NotCopyable
 35             {
 36 public:
 37                 ManagedPosition                                position;
 38             };
 39  40 /***********************************************************************
 41 Basic Constructions
 42 ***********************************************************************/ 43  44 class ManagedType : public ManagedLanguageElement
 45             {
 46 public:
 47                 ALGORITHM_TARGET_ROOT(ManagedType)
 48             };
 49  50 class ManagedExpression : public ManagedLanguageElement
 51             {
 52 public:
 53                 ALGORITHM_TARGET_ROOT(ManagedExpression)
 54             };
 55  56 class ManagedStatement : public ManagedLanguageElement
 57             {
 58 public:
 59                 ALGORITHM_TARGET_ROOT(ManagedStatement)
 60             };
 61  62 class ManagedDeclaration : public ManagedLanguageElement
 63             {
 64 public:
 65                 ALGORITHM_TARGET_ROOT(ManagedDeclaration)
 66             };
 67  68 class ManagedProgram : public ManagedLanguageElement
 69             {
 70 public:
 71                 collections::List<Ptr<ManagedDeclaration>>    declarations;
 72             };
 73  74 /***********************************************************************
 75 Extended Constructions
 76 ***********************************************************************/ 77  78 class ManagedExtendedType : public ManagedType
 79             {
 80 public:
 81                 ALGORITHM_ACCEPT_DECLARATION(ManagedType)
 82  83                 ALGORITHM_TARGET_ROOT(ManagedExtendedType)
 84             };
 85  86 class ManagedExtendedExpression : public ManagedExpression
 87             {
 88 public:
 89                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
 90  91                 ALGORITHM_TARGET_ROOT(ManagedExtendedExpression)
 92             };
 93  94 class ManagedExtendedStatement : public ManagedStatement
 95             {
 96 public:
 97                 ALGORITHM_ACCEPT_DECLARATION(ManagedStatement)
 98  99                 ALGORITHM_TARGET_ROOT(ManagedExtendedStatement)
100             };
101 102 class ManagedExtendedDeclaration : public ManagedDeclaration
103             {
104 public:
105                 ALGORITHM_ACCEPT_DECLARATION(ManagedDeclaration)
106 107                 ALGORITHM_TARGET_ROOT(ManagedExtendedDeclaration)
108             };
109 110 /***********************************************************************
111 Basic Types
112 ***********************************************************************/113 114 class ManagedGenericArgumentType : public ManagedType
115             {
116 public:
117                 ALGORITHM_ACCEPT_DECLARATION(ManagedType)
118 119                 WString                                        name;
120             };
121 122 class ManagedReferencedType : public ManagedType
123             {
124 public:
125                 ALGORITHM_ACCEPT_DECLARATION(ManagedType)
126 127                 WString                                        name;
128             };
129 130 class ManagedMemberType : public ManagedType
131             {
132 public:
133                 ALGORITHM_ACCEPT_DECLARATION(ManagedType)
134 135                 Ptr<ManagedType>                            operand;
136                 WString                                        member;
137             };
138 139 class ManagedInstantiatedGenericType : public ManagedType
140             {
141 public:
142                 ALGORITHM_ACCEPT_DECLARATION(ManagedType)
143 144                 Ptr<ManagedType>                            elementType;
145                 collections::List<Ptr<ManagedType>>            argumentTypes;
146             };
147 148 #define MANAGED_TYPE_TARGETS(P, F)\149             F(P, ManagedGenericArgumentType)\
150             F(P, ManagedReferencedType)\
151             F(P, ManagedMemberType)\
152             F(P, ManagedInstantiatedGenericType)\
153             F(P, ManagedExtendedType)\
154 155             DEFINE_ALGORITHM_INTERFACE(ManagedType, MANAGED_TYPE_TARGETS)
156 157 /***********************************************************************
158 Extended Types
159 ***********************************************************************/160 161 class ManagedArrayType : public ManagedExtendedType
162             {
163 public:
164                 ALGORITHM_ACCEPT_DECLARATION(ManagedExtendedType)
165 166                 Ptr<ManagedType>                            elementType;
167 int                                            dimensionCount;
168             };
169 170 class ManagedFunctionType : public ManagedExtendedType
171             {
172 public:
173                 ALGORITHM_ACCEPT_DECLARATION(ManagedExtendedType)
174 175                 Ptr<ManagedType>                            returnType;
176                 collections::List<Ptr<ManagedType>>            parameterTypes;
177             };
178 179 class ManagedEventType : public ManagedExtendedType
180             {
181 public:
182                 ALGORITHM_ACCEPT_DECLARATION(ManagedExtendedType)
183 184                 Ptr<ManagedType>                            functionType;
185             };
186 187 class ManagedAutoReferType : public ManagedExtendedType
188             {
189 public:
190                 ALGORITHM_ACCEPT_DECLARATION(ManagedExtendedType)
191             };
192 193 #define MANAGED_EXTENDED_TYPE_TARGETS(P, F)\194             F(P, ManagedArrayType)\
195             F(P, ManagedFunctionType)\
196             F(P, ManagedEventType)\
197             F(P, ManagedAutoReferType)\
198 199             DEFINE_ALGORITHM_INTERFACE(ManagedExtendedType, MANAGED_EXTENDED_TYPE_TARGETS)
200 201 /***********************************************************************
202 Basic Expressions
203 ***********************************************************************/204 205 class ManagedPrimitiveExpression : public ManagedExpression
206             {
207             };
208 209 class ManagedNullExpression : public ManagedPrimitiveExpression
210             {
211 public:
212                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
213             };
214 215 class ManagedIntegerExpression : public ManagedPrimitiveExpression
216             {
217 public:
218                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
219 220 bool                                        sign;
221                 union
222                 {
223                     signed __int64                            signedInteger;
224                     unsigned __int64                        unsignedInteger;
225                 }                                            value;
226             };
227 228 class ManagedFloatExpression : public ManagedPrimitiveExpression
229             {
230 public:
231                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
232 233 bool                                        doublePrecision;
234 double                                        value;
235             };
236 237 class ManagedBooleanExpression : public ManagedPrimitiveExpression
238             {
239 public:
240                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
241 242 bool                                        value;
243             };
244 245 class ManagedCharExpression : public ManagedPrimitiveExpression
246             {
247 public:
248                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
249 250                 wchar_t                                        value;
251             };
252 253 class ManagedStringExpression : public ManagedPrimitiveExpression
254             {
255 public:
256                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
257 258                 WString                                        value;
259             };
260 261 class ManagedReferenceExpression : public ManagedExpression
262             {
263 public:
264                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
265 266                 WString                                        name;
267             };
268 269 class ManagedMemberExpression : public ManagedExpression
270             {
271 public:
272                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
273 274                 Ptr<ManagedExpression>                        operand;
275                 WString                                        member;
276             };
277 278 class ManagedInstanciatedExpression : public ManagedExpression
279             {
280 public:
281                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
282 283                 Ptr<ManagedExpression>                        operand;
284                 collections::List<Ptr<ManagedType>>            argumentTypes;
285             };
286 287 class ManagedInvokeExpression : public ManagedExpression
288             {
289 public:
290                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
291 292                 Ptr<ManagedExpression>                        function;
293                 collections::List<Ptr<ManagedExpression>>    arguments;
294                 collections::List<WString>                    defaultParameterNames;
295                 collections::List<Ptr<ManagedExpression>>    defaultParameterValues;
296             };
297 298 class ManagedNewExpression : public ManagedExpression
299             {
300 public:
301                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
302 303                 Ptr<ManagedType>                            objectType;
304                 collections::List<Ptr<ManagedExpression>>    arguments;
305                 collections::List<WString>                    defaultParameterNames;
306                 collections::List<Ptr<ManagedExpression>>    defaultParameterValues;
307                 collections::List<WString>                    propertyNames;
308                 collections::List<Ptr<ManagedExpression>>    propertyValues;
309             };
310 311 class ManagedFunctionResultExpression : public ManagedExpression
312             {
313 public:
314                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
315             };
316 317 class ManagedCastingExpression : public ManagedExpression
318             {
319 public:
320                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
321 322                 Ptr<ManagedExpression>                        operand;
323                 Ptr<ManagedType>                            type;
324             };
325 326 class ManagedThisExpression : public ManagedExpression
327             {
328 public:
329                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
330             };
331 332 class ManagedBaseExpression : public ManagedExpression
333             {
334 public:
335                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
336             };
337 338 class ManagedAssignmentExpression : public ManagedExpression
339             {
340 public:
341                 ALGORITHM_ACCEPT_DECLARATION(ManagedExpression)
342             };
343 344 #define MANAGED_EXPRESSION_TARGETS(P, F)\345             F(P, ManagedNullExpression)\
346             F(P, ManagedIntegerExpression)\
347             F(P, ManagedFloatExpression)\
348             F(P, ManagedBooleanExpression)\
349             F(P, ManagedCharExpression)\
350             F(P, ManagedStringExpression)\
351             F(P, ManagedReferenceExpression)\
352             F(P, ManagedMemberExpression)\
353             F(P, ManagedInstanciatedExpression)\
354             F(P, ManagedInvokeExpression)\
355             F(P, ManagedNewExpression)\
356             F(P, ManagedFunctionResultExpression)\
357             F(P, ManagedCastingExpression)\
358             F(P, ManagedThisExpression)\
359             F(P, ManagedBaseExpression)\
360             F(P, ManagedAssignmentExpression)\
361             F(P, ManagedExtendedExpression)\
362 363             DEFINE_ALGORITHM_INTERFACE(ManagedExpression, MANAGED_EXPRESSION_TARGETS)
364 365 /***********************************************************************
366 Extended Expressions
367 ***********************************************************************/368 369 class ManagedLambdaExpression : public ManagedExtendedExpression
370             {
371 public:
372                 ALGORITHM_ACCEPT_DECLARATION(ManagedExtendedExpression)
373 374                 collections::List<Ptr<ManagedType>>            parameterTypes;
375                 collections::List<WString>                    parameterNames;
376                 Ptr<ManagedType>                            returnType;
377                 Ptr<ManagedStatement>                        body;
378             };
379 380 class ManagedChoiceExpression : public ManagedExtendedExpression
381             {
382 public:
383                 ALGORITHM_ACCEPT_DECLARATION(ManagedExtendedExpression)
384 385                 Ptr<ManagedExpression>                        condition;
386                 Ptr<ManagedExpression>                        trueExpression;
387                 Ptr<ManagedExpression>                        falseExpression;
388             };
389 390 class ManagedNullChoiceExpression : public ManagedExtendedExpression
391             {
392 public:
393                 ALGORITHM_ACCEPT_DECLARATION(ManagedExtendedExpression)
394 395                 Ptr<ManagedExpression>                        valueExpression;
396                 Ptr<ManagedExpression>                        candidateExpression;
397             };
398 399 class ManagedTypeofExpression : public ManagedExtendedExpression
400             {
401 public:
402                 ALGORITHM_ACCEPT_DECLARATION(ManagedExtendedExpression)
403 404                 Ptr<ManagedType>                            type;
405             };
406 407 #define MANAGED_EXTENDED_EXPRESSION_TARGETS(P, F)\408             F(P, ManagedLambdaExpression)\
409             F(P, ManagedChoiceExpression)\
410             F(P, ManagedNullChoiceExpression)\
411             F(P, ManagedTypeofExpression)\
412 413             DEFINE_ALGORITHM_INTERFACE(ManagedExtendedExpression, MANAGED_EXTENDED_EXPRESSION_TARGETS)
414 415 /***********************************************************************
416 Basic Statements
417 ***********************************************************************/418 419 class ManagedEmptyStatement : public ManagedStatement
420             {
421 public:
422                 ALGORITHM_ACCEPT_DECLARATION(ManagedStatement)
423             };
424