随笔-341  评论-2670  文章-0  trackbacks-0
    这一次的语法分析器,我使用Vczh Combinator Parser,按照文法的定义,将字符串转换成了语法树。Vczh Combinator Parser构造较大规模的编译器的时候还是较为吃力,主要原因出在Visual C++竟然限制一个类型全名不能超过某个长度T_T……这几天都写毕业论文去了,没什么时间可以拼这个分析器。下一次我还是用回大二的时候开发的Syngram好了。

    于是我使用了一些囧方法回避了这个长度限制问题——将一个文法切成几段。下面是代码:
  1 #include "VL_JIT_CMinusSyntax.h"
  2 #include "..\..\..\Data\Grammar2\Combinator\VL_CpKernel.h"
  3 #include "..\..\..\Data\Grammar2\Combinator\VL_CpLexer.h"
  4 
  5 using namespace vl::grammar;
  6 
  7 namespace vl
  8 {
  9     namespace jit
 10     {
 11         namespace cminus
 12         {
 13 
 14 /*********************************************************************************************************
 15 记号
 16 signed unsigned byte short int double float char wchar bool struct
 17 null true false
 18 void extern typedef if else while do break continue return alias
 19 *********************************************************************************************************/
 20 
 21             enum VLE_CMinusTokenTag
 22             {
 23                 vcttPrimIdentifier,
 24                 vcttPrimFloat,
 25                 vcttPrimInteger,
 26                 vcttPrimChar,
 27                 vcttPrimWChar,
 28                 vcttPrimString,
 29                 vcttPrimWString,
 30 
 31                 vcttKeySigned,
 32                 vcttKeyUnsigned,
 33                 vcttKeyByte,
 34                 vcttKeyShort,
 35                 vcttKeyInt,
 36                 vcttKeyDouble,
 37                 vcttKeyFloat,
 38                 vcttKeyChar,
 39                 vcttKeyWChar,
 40                 vcttKeyBool,
 41                 vcttKeyStruct,
 42                 vcttKeyNull,
 43                 vcttKeyTrue,
 44                 vcttKeyFalse,
 45                 vcttKeyVoid,
 46                 vcttKeyExtern,
 47                 vcttKeyTypedef,
 48                 vcttKeyIf,
 49                 vcttKeyElse,
 50                 vcttKeyWhile,
 51                 vcttKeyDo,
 52                 vcttKeyBreak,
 53                 vcttKeyContinue,
 54                 vcttKeyReturn,
 55                 vcttKeyAlias,
 56                 vcttKeyConst,
 57 
 58                 vcttSymbolDoubleBracket,
 59                 vcttSymbolLeftBracket,
 60                 vcttSymbolRightBracket,
 61                 vcttSymbolLeftArray,
 62                 vcttSymbolRightArray,
 63                 vcttSymbolLeftBlock,
 64                 vcttSymbolRightBlock,
 65                 vcttSymbolComma,
 66                 vcttSymbolSemicolon,
 67 
 68                 vcttOpMul,
 69                 vcttOpDiv,
 70                 vcttOpMod,
 71                 vcttOpAdd,
 72                 vcttOpSub,
 73                 vcttOpLeftShift,
 74                 vcttOpRightShift,
 75                 vcttOpLess,
 76                 vcttOpLessAndEqual,
 77                 vcttOpGreater,
 78                 vcttOpGreaterAndEqual,
 79                 vcttOpEqual,
 80                 vcttOpNotEqual,
 81                 vcttOpBitAnd,
 82                 vcttOpBitOr,
 83                 vcttOpBitXor,
 84                 vcttOpBitNot,
 85                 vcttOpAnd,
 86                 vcttOpOr,
 87                 vcttOpXor,
 88                 vcttOpNot,
 89                 
 90                 vcttAssign,
 91                 vcttAssignAdd,
 92                 vcttAssignSub,
 93                 vcttAssignMul,
 94                 vcttAssignDiv,
 95                 vcttAssignMod,
 96                 vcttAssignBitAnd,
 97                 vcttAssignBitOr,
 98                 vcttAssignBitXor,
 99                 vcttAssignAnd,
100                 vcttAssignOr,
101                 vcttAssignXor,
102                 vcttAssignLeftShift,
103                 vcttAssignRightShift
104             };
105 
106 /*********************************************************************************************************
107 词法分析器
108 *********************************************************************************************************/
109 
110             void BuildLexer(VL_CpLexer& Lexer)
111             {
112                 Lexer
113                     <<Token(true,_Blank,2147483647)
114                     <<Token(true,_CppComment,2147483647)
115                     <<Token(true,_CComment,2147483647)
116                     <<Token(false,L"signed",            vcttKeySigned)
117                     <<Token(false,L"unsigned",            vcttKeyUnsigned)
118                     <<Token(false,L"byte",                vcttKeyByte)
119                     <<Token(false,L"short",                vcttKeyShort)
120                     <<Token(false,L"int",                vcttKeyInt)
121                     <<Token(false,L"double",            vcttKeyDouble)
122                     <<Token(false,L"float",                vcttKeyFloat)
123                     <<Token(false,L"char",                vcttKeyChar)
124                     <<Token(false,L"wchar",                vcttKeyWChar)
125                     <<Token(false,L"bool",                vcttKeyBool)
126                     <<Token(false,L"struct",            vcttKeyStruct)
127                     <<Token(false,L"null",                vcttKeyNull)
128                     <<Token(false,L"true",                vcttKeyTrue)
129                     <<Token(false,L"false",                vcttKeyFalse)
130                     <<Token(false,L"void",                vcttKeyVoid)
131                     <<Token(false,L"extern",            vcttKeyExtern)
132                     <<Token(false,L"typedef",            vcttKeyTypedef)
133                     <<Token(false,L"if",                vcttKeyIf)
134                     <<Token(false,L"else",                vcttKeyElse)
135                     <<Token(false,L"while",                vcttKeyWhile)
136                     <<Token(false,L"do",                vcttKeyDo)
137                     <<Token(false,L"break",                vcttKeyBreak)
138                     <<Token(false,L"continue",            vcttKeyContinue)
139                     <<Token(false,L"return",            vcttKeyReturn)
140                     <<Token(false,L"alias",                vcttKeyAlias)
141                     <<Token(false,L"const",                vcttKeyConst)
142                     <<Token(false,_Char,                vcttPrimChar)
143                     <<Token(false,_WChar,                vcttPrimWChar)
144                     <<Token(false,_String,                vcttPrimString)
145                     <<Token(false,_WString,                vcttPrimWString)
146                     <<Token(false,_Name,                vcttPrimIdentifier)
147                     <<Token(false,_UnsignedStrictFloat,    vcttPrimFloat)
148                     <<Token(false,_UnsignedInteger,        vcttPrimInteger)
149                     <<Token(false,L"[]",                vcttSymbolDoubleBracket)
150                     <<Token(false,L"(",                    vcttSymbolLeftBracket)
151                     <<Token(false,L")",                    vcttSymbolRightBracket)
152                     <<Token(false,L"[",                    vcttSymbolLeftArray)
153                     <<Token(false,L"]",                    vcttSymbolRightArray)
154                     <<Token(false,L"{",                    vcttSymbolLeftBlock)
155                     <<Token(false,L"}",                    vcttSymbolRightBlock)
156                     <<Token(false,L",",                    vcttSymbolComma)
157                     <<Token(false,L";",                    vcttSymbolSemicolon)
158                     <<Token(false,L"+=",                vcttAssignAdd)
159                     <<Token(false,L"-=",                vcttAssignSub)
160                     <<Token(false,L"*=",                vcttAssignMul)
161                     <<Token(false,L"/=",                vcttAssignDiv)
162                     <<Token(false,L"%=",                vcttAssignMod)
163                     <<Token(false,L"&=",                vcttAssignBitAnd)
164                     <<Token(false,L"|=",                vcttAssignBitOr)
165                     <<Token(false,L"^=",                vcttAssignBitXor)
166                     <<Token(false,L"&&=",                vcttAssignAnd)
167                     <<Token(false,L"||=",                vcttAssignOr)
168                     <<Token(false,L"^^=",                vcttAssignXor)
169                     <<Token(false,L"<<=",                vcttAssignLeftShift)
170                     <<Token(false,L">>=",                vcttAssignRightShift)
171                     <<Token(false,L"*",                    vcttOpMul)
172                     <<Token(false,L"/",                    vcttOpDiv)
173                     <<Token(false,L"%",                    vcttOpMod)
174                     <<Token(false,L"+",                    vcttOpAdd)
175                     <<Token(false,L"-",                    vcttOpSub)
176                     <<Token(false,L"<<",                vcttOpLeftShift)
177                     <<Token(false,L">>",                vcttOpRightShift)
178                     <<Token(false,L"<",                    vcttOpLess)
179                     <<Token(false,L"<=",                vcttOpLessAndEqual)
180                     <<Token(false,L">",                    vcttOpGreater)
181                     <<Token(false,L">=",                vcttOpGreaterAndEqual)
182                     <<Token(false,L"==",                vcttOpEqual)
183                     <<Token(false,L"!=",                vcttOpNotEqual)
184                     <<Token(false,L"&&",                vcttOpAnd)
185                     <<Token(false,L"||",                vcttOpOr)
186                     <<Token(false,L"^^",                vcttOpXor)
187                     <<Token(false,L"!",                    vcttOpNot)
188                     <<Token(false,L"&",                    vcttOpBitAnd)
189                     <<Token(false,L"|",                    vcttOpBitOr)
190                     <<Token(false,L"^",                    vcttOpBitXor)
191                     <<Token(false,L"~",                    vcttOpBitNot)
192                     <<Token(false,L"=",                    vcttAssign)
193                     ;
194             }
195 
196 /*********************************************************************************************************
197 语法分析器
198 *********************************************************************************************************/
199 
200             namespace Handlers
201             {
202                 VL_CMinusExpressionInfo ToInfo(VL_CpToken Input)
203                 {
204                     VL_CMinusExpressionInfo Info;
205                     Info.Token=VUnicodeString(Input.Start,Input.Length);
206                     Info.Line=Input.Line;
207                     return Info;
208                 }
209 
210 /*********************************************************************************************************
211 类型
212 *********************************************************************************************************/
213 
214                 VL_CMinusType::Ptr ToNamedType(VL_CpToken Input)
215                 {
216                     VL_CMinusNamedType* Type=new VL_CMinusNamedType;
217                     Type->Info=ToInfo(Input);
218                     Type->Name=Type->Info.Token;
219                     return Type;
220                 }
221 
222                 VL_CMinusType::Ptr ToNamedType2(const VL_CpPair<VL_CpToken , VL_CpToken>& Input)
223                 {
224                     VL_CMinusNamedType* Type=new VL_CMinusNamedType;
225                     Type->Info=ToInfo(Input.First);
226                     Type->Name=VUnicodeString(Input.First.Start,Input.First.Length)+L" "+VUnicodeString(Input.Second.Start,Input.Second.Length);
227                     return Type;
228                 }
229 
230                 VL_CMinusType::Ptr ToStructType(const VL_CpPair<VL_CpToken , VL_CpList<VL_CpPair<VL_CMinusType::Ptr , VL_CpToken>>>& Input)
231                 {
232                     VL_CMinusType::NamedList Members;
233                     VL_CpList<VL_CpPair<VL_CMinusType::Ptr , VL_CpToken>>::Node::Ptr Current=Input.Second.Head;
234                     while(Current)
235                     {
236                         VL_Fullpair<VUnicodeString , VL_CMinusType::Ptr> Pair;
237                         Pair.Key=VUnicodeString(Current->Data.Second.Start,Current->Data.Second.Length);
238                         Pair.Value=Current->Data.First;
239                         Members.Add(Pair);
240                         Current=Current->Next;
241                     }
242                     VL_CMinusType::Ptr Type=new VL_CMinusStructType(Members);
243                     Type->Info=ToInfo(Input.First);
244                     return Type;
245                 }
246 
247                 VL_CMinusType::Ptr ToArrayType(VL_CpToken Input)
248                 {
249                     VL_CMinusType::Ptr Type=new VL_CMinusArrayType(0,VUnicodeString(Input.Start,Input.Length).ToInt());
250                     return Type;
251                 }
252 
253                 VL_CMinusType::Ptr ToPointerType(VL_CpToken Input)
254                 {
255                     VL_CMinusType::Ptr Type=new VL_CMinusPointerType(0);
256                     return Type;
257                 }
258 
259                 VL_CMinusType::Ptr ToFunctionType(const VL_CpNullable<VL_CpPair<VL_CMinusType::Ptr , VL_CpList<VL_CMinusType::Ptr>>>& Input)
260                 {
261                     VL_CMinusType::List Parameters;
262                     if(!Input.IsNull)
263                     {
264                         Parameters.Add(Input.Data.First);
265                         VL_CpList<VL_CMinusType::Ptr>::Node::Ptr Current=Input.Data.Second.Head;
266                         while(Current)
267                         {
268                             Parameters.Add(Current->Data);
269                             Current=Current->Next;
270                         }
271                     }
272                     VL_CMinusType::Ptr Type=new VL_CMinusFunctionType(0,Parameters);
273                     return Type;
274                 }
275 
276                 VL_CMinusType::Ptr ToType(const VL_CpPair<VL_CMinusType::Ptr , VL_CpList<VL_CMinusType::Ptr>>& Input)
277                 {
278                     VL_CMinusType::Ptr Type=Input.First;
279                     VL_CpList<VL_CMinusType::Ptr>::Node::Ptr Current=Input.Second.Head;
280                     while(Current)
281                     {
282                         if(VL_AutoPtr<VL_CMinusArrayType> ArrayType=Current->Data)
283                         {
284                             ArrayType->Type=Type;
285                             ArrayType->Info=Type->Info;
286                             Type=ArrayType;
287                         }
288                         else if(VL_AutoPtr<VL_CMinusPointerType> PointerType=Current->Data)
289                         {
290                             PointerType->Type=Type;
291                             PointerType->Info=Type->Info;
292                             Type=PointerType;
293                         }
294                         else if(VL_AutoPtr<VL_CMinusFunctionType> FunctionType=Current->Data)
295                         {
296                             FunctionType->ReturnType=Type;
297                             FunctionType->Info=Type->Info;
298                             Type=FunctionType;
299                         }
300                         Current=Current->Next;
301                     }
302                     return Type;
303                 }
304 
305 /*********************************************************************************************************
306 表达式
307 *********************************************************************************************************/
308 
309                 VWChar ReadChar(PCWChar& Input)
310                 {
311                     if(*Input==L'\\')
312                     {
313                         switch(VWChar Char=*(++Input)++)
314                         {
315                         case L't':
316                             return L'\t';
317                         case L'r':
318                             return L'\r';
319                         case L'n':
320                             return L'\n';
321                         case L'0':
322                             return L'\0';
323                         default:
324                             return Char;
325                         }
326                     }
327                     else
328                     {
329                         return *Input++;
330                     }
331                 }
332 
333                 VL_CMinusExpression::Ptr ToPrimitiveExpression(VL_CpToken Token)
334                 {
335                     VL_CMinusExpression::Ptr Exp;
336                     switch(Token.ID)
337                     {
338                     case vcttPrimFloat:
339                         Exp=new VL_CMinusPrimitiveExpression(VUnicodeString(Token.Start,Token.Length).ToDouble());
340                         break;
341                     case vcttPrimInteger:
342                         Exp=new VL_CMinusPrimitiveExpression(VUnicodeString(Token.Start,Token.Length).ToInt());
343                         break;
344                     case vcttPrimChar:
345                         {
346                             PCWChar Input=Token.Start+1;
347                             Exp=new VL_CMinusPrimitiveExpression(ReadChar(Input),false);
348                         }
349                         break;
350                     case vcttPrimWChar:
351                         {
352                             PCWChar Input=Token.Start+2;
353                             Exp=new VL_CMinusPrimitiveExpression(ReadChar(Input),true);
354                         }
355                         break;
356                     case vcttPrimString:
357                         {
358                             PWChar Buffer=new VWChar[Token.Length+1];
359                             PWChar Write=Buffer;
360                             PCWChar Read=Token.Start+1;
361                             while(*Read!=L'\"')
362                             {
363                                 *Write++=ReadChar(Read);
364                             }
365                             *Write=L'\0';
366                             Exp=new VL_CMinusPrimitiveExpression(Buffer,false);
367                             delete[] Buffer;
368                         }
369                         break;
370                     case vcttPrimWString:
371                         {
372                             PWChar Buffer=new VWChar[Token.Length+1];
373                             PWChar Write=Buffer;
374                             PCWChar Read=Token.Start+2;
375                             while(*Read!=L'\"')
376                             {
377                                 *Write++=ReadChar(Read);
378                             }
379                             *Write=L'\0';
380                             Exp=new VL_CMinusPrimitiveExpression(Buffer,true);
381                             delete[] Buffer;
382                         }
383                         break;
384                     case vcttPrimIdentifier:
385                         Exp=new VL_CMinusPrimitiveExpression(VUnicodeString(Token.Start,Token.Length));
386                         break;
387                     case vcttKeyTrue:
388                         Exp=new VL_CMinusPrimitiveExpression(true);
389                         break;
390                     case vcttKeyFalse:
391                         Exp=new VL_CMinusPrimitiveExpression(false);
392                         break;
393                     case vcttKeyNull:
394                         Exp=new VL_CMinusPrimitiveExpression;
395                         break;
396                     }
397                     Exp->Info=ToInfo(Token);
398                     return Exp;
399                 }
400 
401                 VL_CMinusExpression::Ptr ToUnaryExpression(const VL_CpPair<VL_CpToken , VL_CMinusExpression::Ptr>& Input)
402                 {
403                     VL_CMinusExpression::Ptr Exp;
404                     switch(Input.First.ID)
405                     {
406                     case vcttOpAdd:
407                         Exp=new VL_CMinusUnaryExpression(vcuoPositive,Input.Second);
408                         break;
409                     case vcttOpSub:
410                         Exp=new VL_CMinusUnaryExpression(vcuoNegative,Input.Second);
411                         break;
412                     case vcttOpNot:
413                         Exp=new VL_CMinusUnaryExpression(vcuoNot,Input.Second);
414                         break;
415                     case vcttOpBitNot:
416                         Exp=new VL_CMinusUnaryExpression(vcuoBitNot,Input.Second);
417                         break;
418                     case vcttOpAnd:
419                         Exp=new VL_CMinusUnaryExpression(vcuoAddress,Input.Second);
420                         break;
421                     case vcttOpMul:
422                         Exp=new VL_CMinusUnaryExpression(vcuoValue,Input.Second);
423                         break;
424                     }
425                     Exp->Info=ToInfo(Input.First);
426                     return Exp;
427                 }
428 
429                 VL_CMinusExpression::Ptr ToTypeCastExpression(const VL_CpPair<VL_CMinusType::Ptr , VL_CMinusExpression::Ptr>& Input)
430                 {
431                     VL_CMinusExpression::Ptr Exp=new VL_CMinusTypeCastExpression(Input.First,Input.Second);
432                     Exp->Info=Input.First->Info;
433                     return Exp;
434                 }
435 
436                 VL_AutoPtr<VL_CMinusExpression::List> ToExpressionList(const VL_CpNullable<VL_CpPair<VL_CMinusExpression::Ptr , VL_CpList<VL_CMinusExpression::Ptr>>>& Input)
437                 {
438                     VL_AutoPtr<VL_CMinusExpression::List> Values=new VL_CMinusExpression::List;
439                     if(!Input.IsNull)
440                     {
441                         Values->Add(Input.Data.First);
442                         VL_CpList<VL_CMinusExpression::Ptr>::Node::Ptr Current=Input.Data.Second.Head;
443                         while(Current)
444                         {
445                             Values->Add(Current->Data);
446                             Current=Current->Next;
447                         }
448                     }
449                     return Values;
450                 }
451 
452                 VL_CMinusExpression::Ptr ToArrayBuildingExpression(const VL_CpPair<VL_CMinusType::Ptr , VL_AutoPtr<VL_CMinusExpression::List>>& Input)
453                 {
454                     VL_CMinusExpression::Ptr Exp=new VL_CMinusArrayBuildingExpression(Input.First,*Input.Second.Object());
455                     Exp->Info=Input.First->Info;
456                     return Exp;
457                 }
458 
459                 VL_CMinusExpression::Ptr ToStructBuildingExpression(const VL_CpPair<VL_CMinusType::Ptr , VL_AutoPtr<VL_CMinusExpression::List>>& Input)
460                 {
461                     VL_CMinusExpression::Ptr Exp=new VL_CMinusArrayBuildingExpression(Input.First,*Input.Second.Object());
462                     Exp->Info=Input.First->Info;
463                     return Exp;
464                 }
465 
466                 VL_CMinusExpression::Ptr ToInvokeExpression(VL_AutoPtr<VL_CMinusExpression::List> Input)
467                 {
468                     return new VL_CMinusInvokeExpression(0,*Input.Object());
469                 }
470 
471                 VL_CMinusExpression::Ptr ToSubScriptExpression(VL_CMinusExpression::Ptr Input)
472                 {
473                     return new VL_CMinusBinaryExpression(vcboSubScript,0,Input);
474                 }
475 
476                 VL_CMinusExpression::Ptr ToExp0(const VL_CpPair<VL_CMinusExpression::Ptr , VL_CpList<VL_CMinusExpression::Ptr>>& Input)
477                 {
478                     VL_CMinusExpression::Ptr Exp=Input.First;
479                     VL_CpList<VL_CMinusExpression::Ptr>::Node::Ptr Current=Input.Second.Head;
480                     while(Current)
481                     {
482                         if(VL_AutoPtr<VL_CMinusInvokeExpression> Invoke=Current->Data)
483                         {
484                             Invoke->Function=Exp;
485                             Invoke->Info=Exp->Info;
486                             Exp=Invoke;
487                         }
488                         else if(VL_AutoPtr<VL_CMinusBinaryExpression> SubScript=Current->Data)
489                         {
490                             SubScript->Left=Exp;
491                             SubScript->Info=Exp->Info;
492                             Exp=SubScript;
493                         }
494                         Current=Current->Next;
495                     }
496                     return Exp;
497                 }
498 
499                 VL_CMinusExpression::Ptr ToExpX(const VL_CpPair<VL_CMinusExpression::Ptr , VL_CpList<VL_CpPair<VL_CpToken , VL_CMinusExpression::Ptr>>>& Input)
500                 {
501                     VL_CMinusExpression::Ptr Exp=Input.First;
502                     VL_CpList<VL_CpPair<VL_CpToken , VL_CMinusExpression::Ptr>>::Node::Ptr Current=Input.Second.Head;
503                     while(Current)
504                     {
505 #define CASE(NAME) case vcttOp##NAME: Exp=new VL_CMinusBinaryExpression(vcbo##NAME,Exp,Current->Data.Second); Exp->Info=ToInfo(Current->Data.First); break
506                         switch(Current->Data.First.ID)
507                         {
508                             CASE(Mul);
509                             CASE(Div);
510                             CASE(Mod);
511                             CASE(Add);
512                             CASE(Sub);
513                             CASE(LeftShift);
514                             CASE(RightShift);
515                             CASE(Less);
516                             CASE(LessAndEqual);
517                             CASE(Greater);
518                             CASE(GreaterAndEqual);
519                             CASE(Equal);
520                             CASE(NotEqual);
521                             CASE(BitAnd);
522                             CASE(BitOr);
523                             CASE(BitXor);
524                             CASE(And);
525                             CASE(Or);
526                             CASE(Xor);
527                         }
528 #undef CASE
529                         Current=Current->Next;
530                     }
531                     return Exp;
532                 }
533 
534 /*********************************************************************************************************
535 语句
536 *********************************************************************************************************/
537 
538                 VL_CMinusStatement::Ptr ToVariableStatement(const VL_CpPair<VL_CpPair<VL_CpPair<VL_CpNullable<VL_CpToken> , VL_CMinusType::Ptr> , VL_CpToken> , VL_CpNullable<VL_CMinusExpression::Ptr>>& Input)
539                 {
540                     VL_CMinusStatement::Ptr Stat=new VL_CMinusVariableStatement(!Input.First.First.First.IsNull,VUnicodeString(Input.First.Second.Start,Input.First.Second.Length),Input.First.First.Second,Input.Second.IsNull?0:Input.Second.Data);
541                     Stat->Info=Input.First.First.Second->Info;
542                     return Stat;
543                 }
544 
545                 VL_CMinusStatement::Ptr ToAssignmentStatement(const VL_CpPair<VL_CMinusExpression::Ptr , VL_CpNullable<VL_CpPair<VL_CpToken , VL_CMinusExpression::Ptr>>>& Input)
546                 {
547                     VL_CMinusStatement::Ptr Stat;
548                     if(Input.Second.IsNull)
549                     {
550                         Stat=new VL_CMinusExpressionStatement(Input.First);
551                     }
552                     else
553                     {
554                         VLE_CMinusAssignmentOperator Operator=vcaoAssignment;
555                         switch(Input.Second.Data.First.ID)
556                         {
557 #define CASE(NAME) case vcttAssign##NAME: Operator=vcao##NAME; break
558                         case vcttAssign:
559                             Operator=vcaoAssignment;
560                             break;
561                         CASE(Add);
562                         CASE(Sub);
563                         CASE(Mul);
564                         CASE(Div);
565                         CASE(Mod);
566                         CASE(BitAnd);
567                         CASE(BitOr);
568                         CASE(BitXor);
569                         CASE(And);
570                         CASE(Or);
571                         CASE(Xor);
572                         CASE(LeftShift);
573                         CASE(RightShift);
574 #undef CASE
575                         }
576                         Stat=new VL_CMinusAssignmentStatement(Operator,Input.First,Input.Second.Data.Second);
577                     }
578                     Stat->Info=Input.First->Info;
579                     return Stat;
580                 }
581 
582                 VL_CMinusStatement::Ptr ToBreakStatement(VL_CpToken Token)
583                 {
584                     VL_CMinusStatement::Ptr Stat=new VL_CMinusBreakStatement;
585                     Stat->Info=ToInfo(Token);
586                     return Stat;
587                 }
588 
589                 VL_CMinusStatement::Ptr ToContinueStatement(VL_CpToken Token)
590                 {
591                     VL_CMinusStatement::Ptr Stat=new VL_CMinusContinueStatement;
592                     Stat->Info=ToInfo(Token);
593                     return Stat;
594                 }
595 
596                 VL_CMinusStatement::Ptr ToReturnStatement(const VL_CpPair<VL_CpToken , VL_CpNullable<VL_CMinusExpression::Ptr>>& Input)
597                 {
598                     VL_CMinusStatement::Ptr Stat=new VL_CMinusReturnStatement(Input.Second.IsNull?0:Input.Second.Data);
599                     Stat->Info=ToInfo(Input.First);
600                     return Stat;
601                 }
602 
603                 VL_CMinusStatement::Ptr ToBlockStatement(const VL_CpPair<VL_CpToken , VL_CpList<VL_CMinusStatement::Ptr>>& Input)
604                 {
605                     VL_CMinusStatement::List Statements;
606                     VL_CpList<VL_CMinusStatement::Ptr>::Node::Ptr Current=Input.Second.Head;
607                     while(Current)
608                     {
609                         Statements.Add(Current->Data);
610                         Current=Current->Next;
611                     }
612                     VL_CMinusStatement::Ptr Stat=new VL_CMinusBlockStatement(Statements);
613                     Stat->Info=ToInfo(Input.First);
614                     return Stat;
615                 }
616 
617                 VL_CMinusStatement::Ptr ToWhileDoStatement(const VL_CpPair<VL_CMinusExpression::Ptr , VL_CMinusStatement::Ptr>& Input)
618                 {
619                     VL_CMinusStatement::Ptr Stat=new VL_CMinusWhileStatement(Input.First,Input.Second);
620                     Stat->Info=Input.First->Info;
621                     return Stat;
622                 }
623 
624                 VL_CMinusStatement::Ptr ToDoWhileStatement(const VL_CpPair<VL_CMinusStatement::Ptr , VL_CMinusExpression::Ptr>& Input)
625                 {
626                     VL_CMinusStatement::Ptr Stat=new VL_CMinusDoWhileStatement(Input.Second,Input.First);
627                     Stat->Info=Input.First->Info;
628                     return Stat;
629                 }
630 
631                 VL_CMinusStatement::Ptr ToIfStatement(const VL_CpPair<VL_CpPair<VL_CMinusExpression::Ptr , VL_CMinusStatement::Ptr> , VL_CpNullable<VL_CMinusStatement::Ptr>>& Input)
632                 {
633                     VL_CMinusStatement::Ptr Stat=new VL_CMinusIfStatement(Input.First.First,Input.First.Second,Input.Second.IsNull?0:Input.Second.Data);
634                     Stat->Info=Input.First.First->Info;
635                     return Stat;
636                 }
637 
638 /*********************************************************************************************************
639 声明
640 *********************************************************************************************************/
641 
642                 VL_CMinusDeclaration::Ptr ToTypeDeclaration(const VL_CpPair<VL_CMinusType::Ptr , VL_CpToken>& Input)
643                 {
644                     VL_CMinusDeclaration::Ptr Decl=new VL_CMinusTypeDeclaration(VUnicodeString(Input.Second.Start,Input.Second.Length),Input.First);
645                     Decl->Info=ToInfo(Input.Second);
646                     return Decl;
647                 }
648 
649                 VL_CMinusDeclaration::Ptr ToVariableDeclaration(const VL_CpPair<VL_CpPair<VL_CpPair<VL_CpNullable<VL_CpToken> , VL_CMinusType::Ptr> , VL_CpToken> , VL_CpNullable<VL_CMinusExpression::Ptr>>& Input)
650                 {
651                     VL_CMinusDeclaration::Ptr Decl=new VL_CMinusVariableDeclaration(VUnicodeString(Input.First.Second.Start,Input.First.Second.Length),!Input.First.First.First.IsNull,Input.First.First.Second,Input.Second.IsNull?0:Input.Second.Data);
652                     Decl->Info=Input.First.First.Second->Info;
653                     return Decl;
654                 }
655 
656                 VL_AutoPtr<VL_CMinusType::List> ToTypeList(const VL_CpNullable<VL_CpPair<VL_CMinusType::Ptr , VL_CpList<VL_CMinusType::Ptr>>>& Input)
657                 {
658                     VL_AutoPtr<VL_CMinusType::List> Types=new VL_CMinusType::List;
659                     if(!Input.IsNull)
660                     {
661                         Types->Add(Input.Data.First);
662                         VL_CpList<VL_CMinusType::Ptr>::Node::Ptr Current=Input.Data.Second.Head;
663                         while(Current)
664                         {
665                             Types->Add(Current->Data);
666                             Current=Current->Next;
667                         }
668                     }
669                     return Types;
670                 }
671 
672                 struct NamedListPair
673                 {
674                     VL_Fullpair<VUnicodeString , VL_CMinusType::Ptr> Pair;
675                 };
676 
677                 NamedListPair ToNamedListPair(const VL_CpPair<VL_CMinusType::Ptr , VL_CpToken>& Input)
678                 {
679                     NamedListPair Pair;
680                     Pair.Pair.Key=VUnicodeString(Input.Second.Start,Input.Second.Length);
681                     Pair.Pair.Value=Input.First;
682                     return Pair;
683                 }
684 
685                 VL_AutoPtr<VL_CMinusType::NamedList> ToTypeNamedList(const VL_CpNullable<VL_CpPair<NamedListPair , VL_CpList<NamedListPair>>>& Input)
686                 {
687                     VL_AutoPtr<VL_CMinusType::NamedList> Types=new VL_CMinusType::NamedList;
688                     if(!Input.IsNull)
689                     {
690                         Types->Add(Input.Data.First.Pair);
691                         VL_CpList<NamedListPair>::Node::Ptr Current=Input.Data.Second.Head;
692                         while(Current)
693                         {
694                             Types->Add(Current->Data.Pair);
695                             Current=Current->Next;
696                         }
697                     }
698                     return Types;
699                 }
700 
701                 struct TypeNamePair
702                 {
703                     VL_CMinusType::Ptr    Type;
704                     VUnicodeString        Name;
705                 };
706 
707                 struct FunctionHeader
708                 {
709                     TypeNamePair                            TypeName;
710                     VL_AutoPtr<VL_CMinusType::NamedList>    Parameters;
711                 };
712 
713                 struct ExternHeader
714                 {
715                     TypeNamePair                            TypeName;
716                     VL_AutoPtr<VL_CMinusType::List>            Parameters;
717                 };
718 
719                 TypeNamePair ToTypeNamePair(const VL_CpPair<VL_CMinusType::Ptr , VL_CpToken>& Input)
720                 {
721                     TypeNamePair Pair;
722                     Pair.Type=Input.First;
723                     Pair.Name=VUnicodeString(Input.Second.Start,Input.Second.Length);
724                     return Pair;
725                 }
726 
727                 FunctionHeader ToFunctionHeader(const VL_CpPair<TypeNamePair , VL_AutoPtr<VL_CMinusType::NamedList>>& Input)
728                 {
729                     FunctionHeader Header;
730                     Header.TypeName=Input.First;
731                     Header.Parameters=Input.Second;
732                     return Header;
733                 }
734 
735                 ExternHeader ToExternHeader(const VL_CpPair<TypeNamePair , VL_AutoPtr<VL_CMinusType::List>>& Input)
736                 {
737                     ExternHeader Header;
738                     Header.TypeName=Input.First;
739                     Header.Parameters=Input.Second;
740                     return Header;
741                 }
742 
743                 VL_CMinusDeclaration::Ptr ToFunctionDeclaration(const VL_CpPair<FunctionHeader , VL_CMinusStatement::Ptr>& Input)
744                 {
745                     VL_CMinusDeclaration::Ptr Decl=new VL_CMinusFunctionDeclaration(Input.First.TypeName.Name,Input.First.TypeName.Type,*Input.First.Parameters.Object(),Input.Second);
746                     Decl->Info=Input.First.TypeName.Type->Info;
747                     return Decl;
748                 }
749 
750                 VL_CMinusDeclaration::Ptr ToExternDeclaration(const VL_CpPair<ExternHeader , VL_CpToken>& Input)
751                 {
752                     VUnicodeString Alias=VUnicodeString(Input.Second.Start,Input.Second.Length);
753                     VL_CMinusDeclaration::Ptr Decl=new VL_CMinusExternalFunctionDeclaration(Input.First.TypeName.Name,Input.First.TypeName.Type,*Input.First.Parameters.Object(),Alias);
754                     Decl->Info=Input.First.TypeName.Type->Info;
755                     return Decl;
756                 }
757 
758 /*********************************************************************************************************
759 程序
760 *********************************************************************************************************/
761 
762                 VL_CMinusProgram::Ptr ToProgram(const VL_CpList<VL_CMinusDeclaration::Ptr>& Input)
763                 {
764                     VL_CMinusProgram::Ptr Program=new VL_CMinusProgram;
765                     VL_CpList<VL_CMinusDeclaration::Ptr>::Node::Ptr Current=Input.Head;
766                     while(Current)
767                     {
768                         Program->Declarations.Add(Current->Data);
769                         Current=Current->Next;
770                     }
771                     return Program;
772                 }
773             }
774             using namespace Handlers;
775 
776             VL_CMinusProgram::Ptr CompileToCMinusProgram(VUnicodeString Code , VL_CMinusError& Error)
777             {
778                 VL_CpLexer Lexer;
779                 BuildLexer(Lexer);
780 
781                 _Wrapper<VL_CpTokenNodePtr , VL_CMinusType::Ptr> Type;
782                 {
783                     typedef _Terminal<VL_CpTokenNodePtr , VL_CMinusType::Ptr>                        TypeTerminal;
784                     typedef _Terminal<VL_CpTokenNodePtr , VL_CpList<VL_CpPair<VL_CMinusType::Ptr , VL_CpToken>>> StructBodyTerminal;
785 
786                     TypeTerminal        NamedType1        = ToNamedType <<= (Token(vcttPrimIdentifier)||Toks(L"void")||Toks(L"char")||Toks(L"wchar"));
787                     TypeTerminal        NamedType2        = ToNamedType <<= (Toks(L"byte")||Toks(L"short")||Toks(L"int"));
788                     TypeTerminal        NamedType3        = ToNamedType2 <<= (Toks(L"signed")||Toks(L"unsigned")) + (Toks(L"byte")||Toks(L"short")||Toks(L"int"));
789 
790                     StructBodyTerminal    StructBody        = **(Type + Token(vcttPrimIdentifier) < Toks(L";"));
791                     TypeTerminal        StructType        = ToStructType <<= Toks(L"struct"+ (Toks(L"{"> StructBody < Toks(L"}"));
792 
793                     TypeTerminal        PointerType        = ToPointerType <<= Toks(L"*");
794                     TypeTerminal        ArrayType        = ToArrayType <<= (Toks(L"["> Token(vcttPrimInteger) < Toks(L"]"));
795 
796                     TypeTerminal        FunctionBody    = ToFunctionType <<= Optx(Type + **(Toks(L","> Type));
797                     TypeTerminal        FunctionType    = Toks(L"("> FunctionBody < Toks(L")");
798 
799                     TypeTerminal        NamedType        = NamedType1||NamedType2||NamedType3||StructType;
800                     TypeTerminal        ComplexType        = PointerType||ArrayType||FunctionType;
801 
802                     Type = ToType <<= (NamedType + **ComplexType);
803                 }
804 
805                 _Wrapper<VL_CpTokenNodePtr , VL_CMinusExpression::Ptr> Exp;
806                 {
807                     typedef _Terminal<VL_CpTokenNodePtr , VL_CMinusExpression::Ptr>                    ExpTerminal;
808                     typedef _Terminal<VL_CpTokenNodePtr , VL_AutoPtr<VL_CMinusExpression::List>>    ExpListTerminal;
809                     typedef _Terminal<VL_CpTokenNodePtr , VL_CpToken>                                ExpOp;
810 
811                     ExpTerminal            Primitive1        = ToPrimitiveExpression <<= (
812                                                             Token(vcttPrimFloat)||
813                                                             Token(vcttPrimInteger)||
814                                                             Token(vcttPrimChar)||
815                                                             Token(vcttPrimWChar)||
816                                                             Token(vcttPrimString)||
817                                                             Token(vcttPrimWString)
818                                                             );
819                     ExpTerminal            Primitive2        = ToPrimitiveExpression <<= (
820                                                             Token(vcttPrimIdentifier)||
821                                                             Token(vcttKeyTrue)||
822                                                             Token(vcttKeyFalse)||
823                                                             Token(vcttKeyNull)
824                                                             );
825                     ExpTerminal            Unary            = ToUnaryExpression <<= (Tokc(L"+-!~&*"+ Exp);
826                     ExpTerminal            Bracketed        = Toks(L"("> Exp < Toks(L"(");
827                     ExpTerminal            Cast            = ToTypeCastExpression <<= ((Toks(L"("> Type < Toks(L"(")) + Exp);
828                     ExpListTerminal        ExpList            = ToExpressionList <<= Optx(Exp + **(Tokc(L","> Exp));
829                     ExpTerminal            Array            = ToArrayBuildingExpression <<= (
830                                                             Type + (Toks(L"[]"+ Toks(L"{"> ExpList < Toks(L"}"))
831                                                             );
832                     ExpTerminal            Struct            = ToStructBuildingExpression <<= (
833                                                             Type + (Toks(L"{"> ExpList < Toks(L"}"))
834                                                             );
835 
836                     ExpTerminal            SolidType1        = Primitive1||Primitive2||Unary;
837                     ExpTerminal            SolidType2        = Bracketed||Cast||Array||Struct;
838                     ExpTerminal            SolidType        = SolidType1||SolidType2;
839 
840                     ExpTerminal            Invoke            = ToInvokeExpression <<= (Toks(L"("> ExpList < Toks(L")"));
841                     ExpTerminal            SubScript        = ToSubScriptExpression <<= (Toks(L"["> Exp < Toks(L"]"));
842                     ExpTerminal            Exp0            = ToExp0 <<= (SolidType + **(Invoke||SubScript));
843 
844                     ExpOp                Compare            = Toks(L"<")||Toks(L"<=")||Toks(L">")||Toks(L">=")||Toks(L"==")||Toks(L"!=");
845 
846                     ExpTerminal            Exp1            = ToExpX <<= (Exp0 + **(Tokc(L"*/%"+ Exp0));
847                     ExpTerminal            Exp2            = ToExpX <<= (Exp1 + **(Tokc(L"+-"+ Exp1));
848                     ExpTerminal            Exp3            = ToExpX <<= (Exp2 + **((Toks(L"<<"|| Toks(L">>")) + Exp2));
849                     ExpTerminal            Exp4            = ToExpX <<= (Exp3 + **(Compare + Exp3));
850                     ExpTerminal            Exp5            = ToExpX <<= (Exp4 + **(Toks(L"&"+ Exp4));
851                     ExpTerminal            Exp6            = ToExpX <<= (Exp5 + **(Toks(L"^"+ Exp5));
852                     ExpTerminal            Exp7            = ToExpX <<= (Exp6 + **(Toks(L"|"+ Exp6));
853                     ExpTerminal            Exp8            = ToExpX <<= (Exp7 + **(Toks(L"&&"+ Exp7));
854                     ExpTerminal            Exp9            = ToExpX <<= (Exp8 + **(Toks(L"^^"+ Exp8));
855                     Exp                                    = ToExpX <<= (Exp9 + **(Toks(L"||"+ Exp9));
856                 }
857                 _Wrapper<VL_CpTokenNodePtr , VL_CMinusStatement::Ptr> Stat;
858                 {
859                     typedef _Terminal<VL_CpTokenNodePtr , VL_CMinusStatement::Ptr>                    StatTerminal;
860                     typedef _Terminal<VL_CpTokenNodePtr , VL_CpNullable<VL_CMinusExpression::Ptr>>    ExpTerminalOpt;
861                     typedef _Terminal<VL_CpTokenNodePtr , VL_CMinusExpression::Ptr>                    ExpTerminal;
862                     typedef _Terminal<VL_CpTokenNodePtr , VL_CpToken>                                StatOp;
863 
864                     ExpTerminalOpt        EqualExpOpt        = Optx(Toks(L"="> Exp < Toks(L";"));
865                     StatTerminal        Variable        = ToVariableStatement <<= (Optx(Toks(L"const")) + Type + Token(vcttPrimIdentifier) + EqualExpOpt);
866 
867                     StatOp                StatOpEqual        = Token(vcttAssign) || Token(vcttAssignAdd)|| Token(vcttAssignSub)|| Token(vcttAssignMul)|| Token(vcttAssignDiv)|| Token(vcttAssignMod);
868                     StatOp                StatOpBit        = Token(vcttAssignLeftShift) || Token(vcttAssignRightShift)|| Token(vcttAssignBitAnd)|| Token(vcttAssignBitOr)|| Token(vcttAssignBitXor);
869                     StatOp                StatOpBool        = Token(vcttAssignAnd)|| Token(vcttAssignOr)|| Token(vcttAssignXor);
870                     StatOp                StatOpAssign    = StatOpEqual || StatOpBit || StatOpBool;
871                     StatTerminal        Assignment        = ToAssignmentStatement <<= (Exp + Optx(StatOpAssign + Exp) < Toks(L";"));
872 
873                     StatTerminal        Break            = ToBreakStatement <<= (Toks(L"break"< Toks(L";"));
874                     StatTerminal        Continue        = ToContinueStatement <<= (Toks(L"continue"< Toks(L";"));
875                     StatTerminal        Return            = ToReturnStatement <<= (Toks(L"return"+ Optx(Exp) < Toks(L";"));
876                     StatTerminal        Block            = ToBlockStatement <<= (Toks(L"{"+ **(Stat) < Toks(L"}"));
877 
878                     ExpTerminal            WhileExpr        = Toks(L"while"> Toks(L"("> Exp < Toks(L")");
879                     StatTerminal        WhileDo            = ToWhileDoStatement <<= (WhileExpr + Stat);
880                     StatTerminal        DoWhile            = ToDoWhileStatement <<= (Toks(L"do"> Stat + WhileExpr < Toks(L";"));
881 
882                     ExpTerminal            IfExpr            = Toks(L"if"> Toks(L"("> Exp < Toks(L")");
883                     StatTerminal        If                = ToIfStatement <<= (IfExpr + Stat + Optx(Toks(L"else"> Stat));
884 
885                     StatTerminal        Control            = WhileDo || DoWhile || If || Break || Continue || Return;
886                     StatTerminal        NonControl        = Variable || Assignment || Block;
887                     Stat                                = Control || NonControl;
888                 }
889                 _Wrapper<VL_CpTokenNodePtr , VL_CMinusDeclaration::Ptr> Decl;
890                 {
891                     typedef _Terminal<VL_CpTokenNodePtr , VL_CMinusDeclaration::Ptr>                DeclTerminal;
892                     typedef _Terminal<VL_CpTokenNodePtr , VL_CpNullable<VL_CMinusExpression::Ptr>>    ExpTerminalOpt;
893                     typedef _Terminal<VL_CpTokenNodePtr , VL_AutoPtr<VL_CMinusType::List>>            TypeListTerminal;
894                     typedef _Terminal<VL_CpTokenNodePtr , VL_AutoPtr<VL_CMinusType::NamedList>>        TypeMapTerminal;
895                     typedef _Terminal<VL_CpTokenNodePtr , NamedListPair>                            TypePairTerminal;
896                     typedef _Terminal<VL_CpTokenNodePtr , TypeNamePair>                                TypeNameTerminal;
897                     typedef _Terminal<VL_CpTokenNodePtr , FunctionHeader>                            FuncHeadTerminal;
898                     typedef _Terminal<VL_CpTokenNodePtr , ExternHeader>                                ExternHeadTerminal;
899 
900                     DeclTerminal        TypeDecl        = ToTypeDeclaration <<= (Toks(L"typedef"> (Type + Token(vcttPrimIdentifier)) < Toks(L";"));
901 
902                     ExpTerminalOpt        EqualExpOpt        = Optx(Toks(L"="> Exp < Toks(L";"));
903                     DeclTerminal        VarDecl            = ToVariableDeclaration <<= (Optx(Toks(L"const")) + Type + Token(vcttPrimIdentifier) + EqualExpOpt);
904 
905                     TypeListTerminal    TypeList        = ToTypeList <<= Optx(Type + **(Toks(L","> Type));
906                     TypePairTerminal    TypePair        = ToNamedListPair <<= (Type + Token(vcttPrimIdentifier));
907                     TypeMapTerminal        TypeMap            = ToTypeNamedList <<= Optx(TypePair + **(Toks(L","> TypePair));
908                     TypeListTerminal    BracketTypeList    = Toks(L"("> TypeList < Toks(L")");
909                     TypeMapTerminal        BracketTypeMap    = Toks(L"("> TypeMap < Toks(L")");
910 
911                     TypeNameTerminal    TypeName        = ToTypeNamePair<<= (Type + Token(vcttPrimIdentifier));
912                     FuncHeadTerminal    FuncHead        = ToFunctionHeader <<= (TypeName + BracketTypeMap);
913                     ExternHeadTerminal    ExternHead        = ToExternHeader <<= (TypeName + BracketTypeList);
914                     DeclTerminal        FuncDecl        = ToFunctionDeclaration <<= (FuncHead + Stat);
915                     DeclTerminal        ExternDecl        = ToExternDeclaration <<= ((Toks(L"extern"> ExternHead) + (Toks(L"alias"> Token(vcttPrimString)));
916 
917                     Decl                                = TypeDecl || VarDecl || ExternDecl || FuncDecl;
918                 }
919                 _Wrapper<VL_CpTokenNodePtr , VL_CMinusProgram::Ptr> Program;
920                 {
921                     Program                                = ToProgram <<= **Decl;
922                 }
923 
924                 VL_CpParser<VL_CpTokenNodePtr , VL_CMinusProgram::Ptr> Parser=Program;
925 
926                 try
927                 {
928                     VL_CpLexer::_Result LexResult=Lexer.Parse(Code.Buffer());
929                     if(LexResult.Second.First)
930                     {
931                         Error.Info.Line=LexResult.Second.Second;
932                         Error.Message=L"遇到不可识别的记号。";
933                     }
934                     else
935                     {
936                         VL_CpPair<VL_CMinusProgram::Ptr , VL_CpTokenNodePtr> SynResult=Parser.Parse(LexResult.First.Head).Head->Data;
937                         if(SynResult.Second)
938                         {
939                             Error.Info.Line=SynResult.Second->Data.Line;
940                             Error.Info.Token=VUnicodeString(SynResult.Second->Data.Start,SynResult.Second->Data.Length);
941                             Error.Message=L"记号附近发生语法错误。";
942                         }
943                         else
944                         {
945                             return SynResult.First;
946                         }
947                     }
948                 }
949                 catch(const VL_CpBasicException<VL_CpTokenNodePtr>& e)
950                 {
951                     Error.Info.Line=e.Input->Data.Line;
952                     Error.Info.Token=VUnicodeString(e.Input->Data.Start,e.Input->Data.Length);
953                     Error.Message=L"记号附近发生语法错误。";
954                 }
955 
956                 return 0;
957             }
958         }
959     }
960 }
posted on 2009-05-03 19:25 陈梓瀚(vczh) 阅读(2764) 评论(11)  编辑 收藏 引用 所属分类: JIT

评论:
# re: JIT脚本引擎:实现CMinus语法分析器 2009-05-04 02:33 | shongbee2
哇,好晕。呵呵。。膜拜一下。。我继续努力  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2009-05-04 04:45 | Davy.xu
很早在此就拜读大师的代码。本人也写过一个脚本,语法词法直接学习lua的,个人觉得最难的莫过于虚拟机,虚拟机里最难的地方,莫过于脚本和navive函数的交叉迭代调用的处理。
另外,我认为lua支持多返回值是在性能和结构上的优化,如果一个脚本不支持多返回值,脚本只能是个玩具。
多返回值的尾调用栈处理也是极为麻烦的事情

  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2009-05-04 04:54 | 陈梓瀚(vczh)
1:这次我的脚本生成机器码,native不native什么的已经没区别了。
2:多个返回值跟返回struct一样,不是什么问题。
3:所以,尾调用栈的问题是不存在的。  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2009-05-04 15:04 | dopoco
所为学习尚可,如若则不如做些实用的项目。  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2009-05-04 17:17 | 陈梓瀚(vczh)
@dopoco
研究一个东西也是有成本的。不过个人觉得做这个比那些“实用的项目”欢乐得多。  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器[未登录] 2009-05-04 20:15 | eXile
在实用主义覆盖一切角落的国度,人们都会变得鼠目寸光。  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2009-05-05 04:52 | 阿黄
@eXile
此所谓应用科学和理论科学的矛盾,不用过多计较
觉着好久行了
实用主义也是给理论主义一个提供想法的好阶段之一
  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2012-11-26 18:37 | 吴刚
TypeTerminal NamedType1 = ToNamedType <<= (Token(vcttPrimIdentifier)||Toks(L"void")||Toks(L"char")||Toks(L"wchar"));

这句是什么意思啊 <<= 我看了 好像没有重载操作符 希望能够解惑 是不是MSC编译器的特殊用法(我解释GCC比较多)  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2012-11-27 00:54 | 吴刚
VL_CpParser<VL_CpTokenNodePtr , VL_CMinusProgram::Ptr> Parser=Program; 好吧 前面的看懂了 这里又看不懂了  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2012-11-27 01:12 | 吴刚
好吧 是用了隐式类型转换加操作符重载 没看懂的时候想骂人 为什么写这么非主流的代码出来 看懂了发现还是蛮好懂的 看来跟大牛还是有很大的差距的  回复  更多评论
  
# re: JIT脚本引擎:实现CMinus语法分析器 2012-11-27 05:25 | 陈梓瀚(vczh)
@吴刚
现在我已经有新版本的parser combinator实现了,写出来的代码要比这个好看,尽管我在也没有update这个CMinus……  回复  更多评论
  

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理