随笔-341  评论-2670  文章-0  trackbacks-0
    今天写了一些CMinus程序供语法分析器分析,然后程序从语法树重新生成格式化后的代码,两边比较检查优先级什么的是否正确处理。下面是CMinus写的冒泡排序、菲薄纳气数列、链表操作函数以及其他程序:

    下面的代码旨在测试所有种类的类型、表达式、语句和声明。
  1 void BubbleSort(int* Input , int Count)
  2 {
  3   int i=0;
  4   while(i<Count-1)
  5   {
  6     int j=Count-2;
  7     while(j>=i)
  8     {
  9       if(Input[j]>Input[j+1])
 10       {
 11         int Temp=Input[j];
 12         Input[j]=Input[j+1];
 13         Input[j+1]=Temp;
 14       }
 15       j-=1;
 16     }
 17     i+=1;
 18   }
 19 }
 20 
 21 int Fab(int Index)
 22 {
 23   if(Index<2)return 1;
 24   else return Fab(Index-1)+Fab(Index-2);
 25 }
 26 
 27 typedef struct
 28 {
 29   int Data;
 30   Node* Next;
 31 } Node;
 32 
 33 extern void* malloc(int) alias "malloc";
 34 extern void free(void*) alias "free";
 35 
 36 Node* CreateNodes()
 37 {
 38   Node* Head=(Node*)malloc(sizeof(Node));
 39   *Head=Node{0,null};
 40   Node* Tail=Head;
 41   int Current=1;
 42   do{
 43     Node* NewNode=(Node*)malloc(sizeof(Node));
 44     *NewNode=Node{Current,null};
 45     Tail->Next=NewNode;
 46     Tail=NewNode;
 47   }while(Current<=4);
 48   return Head;
 49 }
 50 
 51 void FreeNodes(Node* Head)
 52 {
 53   while(Head!=null)
 54   {
 55     Node* Next=Head->Next;
 56     free(Head);
 57     Head=Next;
 58   }
 59 }
 60 
 61 void CreateAndFreeNodes()
 62 {
 63   FreeNodes(CreateNodes());
 64 }
 65 
 66 int SubRev(int A , int B)
 67 {
 68   return -A+B;
 69 }
 70 
 71 int Sum()
 72 {
 73   int[5] Number=int[]{1,2,3,4,5};
 74   int Result=0;
 75   int Index=0;
 76   while(true)
 77   {
 78     Result+=Number[Index];
 79     Index+=1;
 80     if(Index==5)break;
 81     else continue;
 82   }
 83   return Result;
 84 }
 85 
 86 int GlobalA;
 87 int GlobalB=0;
 88 const int GlobalC;
 89 const int GlobalD=0;
 90 
 91 void Local()
 92 {
 93   int LocalA;
 94   int LocalB=0;
 95   const int LocalC;
 96   const int LocalD=0;
 97 }
 98 
 99 typedef int(int,int) IntFunc;
100 
101 int Add(int a,int b)
102 {
103   return a+b;
104 }
105 
106 int Sub(int a,int b)
107 {
108   return a-b;
109 }
110 
111 void RunAll(int a,int b)
112 {
113   IntFunc[2] Funcs=IntFunc[]{&Add,&Sub};
114   int[2] Nums;
115   int Index=0;
116   while(Index<2)
117   {
118     Nums[Index]=Funcs[Index](a,b);
119     Index+=1;
120   }
121 }

    下面是格式化的结果:
  1 void BubbleSort (int* Input, int Count)
  2 {
  3   int i = 0;
  4   while ((i < (Count - 1)))
  5   {
  6     int j = (Count - 2);
  7     while ((j >= i))
  8     {
  9       if ((Input[j] > Input[(j + 1)]))
 10       {
 11         int Temp = Input[j];
 12         Input[j] = Input[(j + 1)];
 13         Input[(j + 1)] = Temp;
 14       }
 15       j -= 1;
 16     }
 17     i += 1;
 18   }
 19 }
 20 
 21 int Fab (int Index)
 22 {
 23   if ((Index < 2))
 24     return 1;
 25   else
 26     return (Fab((Index - 1)) + Fab((Index - 2)));
 27 }
 28 
 29 typedef struct
 30 {
 31   int Data;
 32   Node* Next;
 33 } Node;
 34 
 35 extern void* malloc (int) alias "malloc";
 36 
 37 extern void free (void*) alias "free";
 38 
 39 Node* CreateNodes ()
 40 {
 41   Node* Head = (Node*)malloc(sizeof(Node));
 42   *Head = Node{0null};
 43   Node* Tail = Head;
 44   int Current = 1;
 45   do
 46   {
 47     Node* NewNode = (Node*)malloc(sizeof(Node));
 48     *NewNode = Node{Current, null};
 49     Tail->Next = NewNode;
 50     Tail = NewNode;
 51   }
 52   while ((Current <= 4));
 53   return Head;
 54 }
 55 
 56 void FreeNodes (Node* Head)
 57 {
 58   while ((Head != null))
 59   {
 60     Node* Next = Head->Next;
 61     free(Head);
 62     Head = Next;
 63   }
 64 }
 65 
 66 void CreateAndFreeNodes ()
 67 {
 68   FreeNodes(CreateNodes());
 69 }
 70 
 71 int SubRev (int A, int B)
 72 {
 73   return (-+ B);
 74 }
 75 
 76 int Sum ()
 77 {
 78   int[5] Number = int[]{12345};
 79   int Result = 0;
 80   int Index = 0;
 81   while (true)
 82   {
 83     Result += Number[Index];
 84     Index += 1;
 85     if ((Index == 5))
 86       break;
 87     else
 88       continue;
 89   }
 90   return Result;
 91 }
 92 
 93 int GlobalA;
 94 
 95 int GlobalB = 0;
 96 
 97 const int GlobalC;
 98 
 99 const int GlobalD = 0;
100 
101 void Local ()
102 {
103   int LocalA;
104   int LocalB = 0;
105   const int LocalC;
106   const int LocalD = 0;
107 }
108 
109 typedef int(intint) IntFunc;
110 
111 int Add (int a, int b)
112 {
113   return (a + b);
114 }
115 
116 int Sub (int a, int b)
117 {
118   return (a - b);
119 }
120 
121 void RunAll (int a, int b)
122 {
123   IntFunc[2] Funcs = IntFunc[]{&Add, &Sub};
124   int[2] Nums;
125   int Index = 0;
126   while ((Index < 2))
127   {
128     Nums[Index] = Funcs[Index](a, b);
129     Index += 1;
130   }
131 }
posted on 2009-05-04 19:44 陈梓瀚(vczh) 阅读(1954) 评论(1)  编辑 收藏 引用 所属分类: JIT

评论:
# re: JIT脚本引擎:CMinus大部分语法测试通过 2009-05-04 20:07 | baihacker
我是路过的  回复  更多评论
  

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