source: dev/BasicCompiler_Common/Enum.cpp@ 99

Last change on this file since 99 was 92, checked in by dai_9181, 18 years ago

列挙型クラスの自動生成コードを修正した(派生クラスでのToStringメソッドを廃止し、サイズを軽減した)。

File size: 6.7 KB
Line 
1#include "common.h"
2
3CEnumParent **ppobj_EnumParent;
4int iEnumParentNum;
5
6CEnumMember::CEnumMember(char *name,int value){
7 m_name=(char *)HeapAlloc(hHeap,0,lstrlen(name)+1);
8 lstrcpy(m_name,name);
9
10 m_value=value;
11}
12CEnumMember::~CEnumMember(){
13 if(m_name) HeapDefaultFree(m_name);
14}
15
16
17CEnumParent::CEnumParent(const char *buffer,int nowLine){
18 ppobj_EnumMember=(CEnumMember **)HeapAlloc(hHeap,0,1);
19 iEnumMemberNum=0;
20
21 int i=0,i2;
22
23 if(!(buffer[i]==1&&buffer[i+1]==ESC_ENUM)) return;
24 i+=2;
25
26 //列挙体の名前を取得
27 char temporary[VN_SIZE];
28 for(i2=0;;i++,i2++){
29 if(IsCommandDelimitation(buffer[i])){
30 temporary[i2]=0;
31 break;
32 }
33 if(!IsVariableChar(buffer[i])){
34 SetError(1,NULL,i);
35 break;
36 }
37 temporary[i2]=buffer[i];
38 }
39
40 TypeName=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
41 lstrcpy(TypeName,temporary);
42
43 if(buffer[i]=='\0'){
44 SetError(22,"Enum",nowLine);
45 return;
46 }
47
48 int NextValue=0;
49 while(1){
50 i++;
51
52 if(buffer[i]==1&&buffer[i+1]==ESC_ENDENUM) break;
53
54 for(i2=0;;i2++,i++){
55 if(IsCommandDelimitation(buffer[i])){
56 temporary[i2]=0;
57 break;
58 }
59 if(buffer[i]=='='){
60 temporary[i2]=0;
61 break;
62 }
63 temporary[i2]=buffer[i];
64 }
65 if(temporary[0]=='\0'){
66 if(buffer[i]=='\0'){
67 SetError(22,"Enum",nowLine);
68 break;
69 }
70 continue;
71 }
72
73 if(buffer[i]!='='){
74 NextValue++;
75 }
76 else{
77 char temp2[VN_SIZE];
78 for(i++,i2=0;;i2++,i++){
79 if(IsCommandDelimitation(buffer[i])){
80 temp2[i2]=0;
81 break;
82 }
83 temp2[i2]=buffer[i];
84 }
85 NextValue=atoi(temp2);
86 }
87
88 //メンバを追加
89 ppobj_EnumMember=(CEnumMember **)HeapReAlloc(hHeap,0,ppobj_EnumMember,(iEnumMemberNum+1)*sizeof(CEnumMember *));
90 ppobj_EnumMember[iEnumMemberNum]=new CEnumMember(temporary,NextValue);
91 iEnumMemberNum++;
92 }
93}
94CEnumParent::~CEnumParent(){
95 HeapDefaultFree(TypeName);
96
97 int i;
98 for(i=0;i<iEnumMemberNum;i++){
99 delete ppobj_EnumMember[i];
100 }
101 HeapDefaultFree(ppobj_EnumMember);
102}
103
104
105
106void CEnumParent::InitEnum(void){
107 ppobj_EnumParent=(CEnumParent **)HeapAlloc(hHeap,0,1);
108 iEnumParentNum=0;
109
110 const char *source = Smoothie::Lexical::source.GetBuffer();
111
112 int i;
113 for(i=0;;i++){
114 if(source[i]=='\0') break;
115
116 if(source[i]==1&&source[i+1]==ESC_ENUM){
117 if(i>=2){
118 if(source[i-2]==1&&source[i-1]==ESC_CONST) continue;
119 }
120 ppobj_EnumParent=(CEnumParent **)HeapReAlloc(hHeap,0,ppobj_EnumParent,(iEnumParentNum+1)*sizeof(CEnumParent *));
121 ppobj_EnumParent[iEnumParentNum]=new CEnumParent(source+i,i);
122 iEnumParentNum++;
123 }
124 }
125}
126void CEnumParent::DestroyEnum(void){
127 int i;
128 for(i=0;i<iEnumParentNum;i++){
129 delete ppobj_EnumParent[i];
130 }
131 HeapDefaultFree(ppobj_EnumParent);
132}
133
134char *CEnumParent::GenerateCodes(void){
135 int i,i2;
136
137 char *buffer;
138 int MaxSize,length;
139 MaxSize=65535;
140 buffer=(char *)HeapAlloc(hHeap,0,MaxSize+65535);
141 length=0;
142
143 buffer[0]=0;
144
145 for(i=0;i<iEnumParentNum;i++){
146 CEnumParent *parent;
147 parent=ppobj_EnumParent[i];
148
149 sprintf(buffer+length,"Class Enum %s\n",parent->TypeName);
150 length+=lstrlen(buffer+length);
151 lstrcpy(buffer+length,"\tInherits EnumBase\n");
152 length+=lstrlen(buffer+length);
153 sprintf(buffer+length,"\tSub %s(value As Long,lpszName As LPSTR)\n",parent->TypeName);
154 length+=lstrlen(buffer+length);
155 lstrcpy(buffer+length,"\t\tEnumBase(value,lpszName)\n");
156 length+=lstrlen(buffer+length);
157 lstrcpy(buffer+length,"\tEnd Sub\n");
158 length+=lstrlen(buffer+length);
159 lstrcpy(buffer+length,"Public\n");
160 length+=lstrlen(buffer+length);
161 sprintf(buffer+length,"\tSub %s()\n",parent->TypeName);
162 length+=lstrlen(buffer+length);
163 if(parent->iEnumMemberNum){
164 sprintf(buffer+length,"\t\tEnumBase(%d,\"%s\")\n",
165 parent->ppobj_EnumMember[0]->m_value,
166 parent->ppobj_EnumMember[0]->m_name);
167 length+=lstrlen(buffer+length);
168 }
169 lstrcpy(buffer+length,"\tEnd Sub\n");
170 length+=lstrlen(buffer+length);
171 sprintf(buffer+length,"\tSub ~%s()\n",parent->TypeName);
172 length+=lstrlen(buffer+length);
173 lstrcpy(buffer+length,"\tEnd Sub\n");
174 length+=lstrlen(buffer+length);
175
176 for(i2=0;i2<parent->iEnumMemberNum;i2++){
177 CEnumMember *member;
178 member=parent->ppobj_EnumMember[i2];
179
180 sprintf(buffer+length,"\tStatic %s As %s(%d,\"%s\")\n",
181 member->m_name,
182 parent->TypeName,
183 member->m_value,
184 member->m_name);
185 length+=lstrlen(buffer+length);
186 }
187
188 /*
189 sprintf(buffer+length,"\tOverride Function ToString() As String\n",parent->TypeName);
190 length+=lstrlen(buffer+length);
191 lstrcpy(buffer+length,"\t\tSelect Case value\n");
192 length+=lstrlen(buffer+length);
193 for(i2=0;i2<parent->iEnumMemberNum;i2++){
194 CEnumMember *member;
195 member=parent->ppobj_EnumMember[i2];
196
197 sprintf(buffer+length,"\t\t\tCase %d\n",member->m_value);
198 length+=lstrlen(buffer+length);
199 sprintf(buffer+length,"\t\t\t\tReturn \"%s\"\n",member->m_name);
200 length+=lstrlen(buffer+length);
201 }
202 lstrcpy(buffer+length,"\t\tEnd Select\n");
203 length+=lstrlen(buffer+length);
204 lstrcpy(buffer+length,"\tEnd Function\n");
205 length+=lstrlen(buffer+length);
206
207
208 sprintf(buffer+length,"\tSub Operator= (ByRef value As %s)\n",parent->TypeName);
209 length+=lstrlen(buffer+length);
210 lstrcpy(buffer+length,"\t\tThis.Copy(ByVal VarPtr(value))\n");
211 length+=lstrlen(buffer+length);
212 lstrcpy(buffer+length,"\tEnd Sub\n");
213 length+=lstrlen(buffer+length);
214
215 sprintf(buffer+length,"\tSub Operator= (ByRef value As String)\n",parent->TypeName);
216 length+=lstrlen(buffer+length);
217 lstrcpy(buffer+length,"\t\tSelect Case value\n");
218 length+=lstrlen(buffer+length);
219 for(i2=0;i2<parent->iEnumMemberNum;i2++){
220 CEnumMember *member;
221 member=parent->ppobj_EnumMember[i2];
222
223 sprintf(buffer+length,"\t\t\tCase \"%s\"\n",member->m_name);
224 length+=lstrlen(buffer+length);
225 sprintf(buffer+length,"\t\t\t\tThis=%s.%s\n",parent->TypeName,member->m_name);
226 length+=lstrlen(buffer+length);
227 }
228 lstrcpy(buffer+length,"\t\tEnd Select\n");
229 length+=lstrlen(buffer+length);
230 lstrcpy(buffer+length,"\tEnd Sub\n");
231 length+=lstrlen(buffer+length);
232
233 sprintf(buffer+length,"\tSub Operator= (value As Long)\n",parent->TypeName);
234 length+=lstrlen(buffer+length);
235 lstrcpy(buffer+length,"\t\tm_Value=value\n");
236 length+=lstrlen(buffer+length);
237 lstrcpy(buffer+length,"\tEnd Sub\n");
238 length+=lstrlen(buffer+length);*/
239
240 lstrcpy(buffer+length,"End Class\n");
241 length+=lstrlen(buffer+length);
242
243
244 //バッファ領域が足りなくなった場合はバッファを増量する
245 if(length>MaxSize){
246 MaxSize+=65535;
247 buffer=(char *)HeapReAlloc(hHeap,0,buffer,MaxSize+65535);
248 }
249 }
250
251 // ログを生成
252 Smoothie::Logger::PutFile( "enum_generated.log", buffer );
253
254 return buffer;
255}
Note: See TracBrowser for help on using the repository browser.