source: dev/trunk/abdev/BasicCompiler_Common/Enum.cpp@ 340

Last change on this file since 340 was 327, checked in by dai_9181, 17 years ago

デリゲートのベースがほぼ実装できた

File size: 8.8 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4#include <jenga/include/smoothie/LexicalAnalysis.h>
5
6#include <Compiler.h>
7#include <../Enum.h>
8
9#include "common.h"
10
11CEnumParent **ppobj_EnumParent;
12int iEnumParentNum;
13
14CEnumMember::CEnumMember(char *name,int value){
15 m_name=(char *)HeapAlloc(hHeap,0,lstrlen(name)+1);
16 lstrcpy(m_name,name);
17
18 m_value=value;
19}
20CEnumMember::~CEnumMember(){
21 if(m_name) HeapDefaultFree(m_name);
22}
23
24
25CEnumParent::CEnumParent( const NamespaceScopes &namespaceScopes, const char *buffer,int nowLine)
26 : namespaceScopes( namespaceScopes )
27{
28 ppobj_EnumMember=(CEnumMember **)HeapAlloc(hHeap,0,1);
29 iEnumMemberNum=0;
30
31 int i=0,i2;
32
33 if(!(buffer[i]==1&&buffer[i+1]==ESC_ENUM)) return;
34 i+=2;
35
36 //列挙体の名前を取得
37 char temporary[VN_SIZE];
38 for(i2=0;;i++,i2++){
39 if(IsCommandDelimitation(buffer[i])){
40 temporary[i2]=0;
41 break;
42 }
43 if(!IsVariableChar(buffer[i])){
44 SetError(1,NULL,i);
45 break;
46 }
47 temporary[i2]=buffer[i];
48 }
49
50 name = temporary;
51
52 if(buffer[i]=='\0'){
53 SetError(22,"Enum",nowLine);
54 return;
55 }
56
57 int NextValue=0;
58 while(1){
59 i++;
60
61 if(buffer[i]==1&&buffer[i+1]==ESC_ENDENUM) break;
62
63 for(i2=0;;i2++,i++){
64 if(IsCommandDelimitation(buffer[i])){
65 temporary[i2]=0;
66 break;
67 }
68 if(buffer[i]=='='){
69 temporary[i2]=0;
70 break;
71 }
72 temporary[i2]=buffer[i];
73 }
74 if(temporary[0]=='\0'){
75 if(buffer[i]=='\0'){
76 SetError(22,"Enum",nowLine);
77 break;
78 }
79 continue;
80 }
81
82 if(buffer[i]!='='){
83 NextValue++;
84 }
85 else{
86 char temp2[VN_SIZE];
87 for(i++,i2=0;;i2++,i++){
88 if(IsCommandDelimitation(buffer[i])){
89 temp2[i2]=0;
90 break;
91 }
92 temp2[i2]=buffer[i];
93 }
94 NextValue=atoi(temp2);
95 }
96
97 //メンバを追加
98 ppobj_EnumMember=(CEnumMember **)HeapReAlloc(hHeap,0,ppobj_EnumMember,(iEnumMemberNum+1)*sizeof(CEnumMember *));
99 ppobj_EnumMember[iEnumMemberNum]=new CEnumMember(temporary,NextValue);
100 iEnumMemberNum++;
101 }
102}
103CEnumParent::~CEnumParent(){
104 int i;
105 for(i=0;i<iEnumMemberNum;i++){
106 delete ppobj_EnumMember[i];
107 }
108 HeapDefaultFree(ppobj_EnumMember);
109}
110
111
112
113void CEnumParent::InitEnum(void){
114 ppobj_EnumParent=(CEnumParent **)HeapAlloc(hHeap,0,1);
115 iEnumParentNum=0;
116
117 const char *source = compiler.GetObjectModule().GetCurrentSource().GetBuffer();
118
119 // 名前空間管理
120 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
121 namespaceScopes.clear();
122
123 int i2;
124 char temporary[VN_SIZE];
125 for(int i=0;;i++){
126 if(source[i]=='\0') break;
127
128 if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
129 for(i+=2,i2=0;;i2++,i++){
130 if( IsCommandDelimitation( source[i] ) ){
131 temporary[i2]=0;
132 break;
133 }
134 temporary[i2]=source[i];
135 }
136 namespaceScopes.push_back( temporary );
137
138 continue;
139 }
140 else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
141 if( namespaceScopes.size() <= 0 ){
142 SetError(12, "End Namespace", i );
143 }
144 else{
145 namespaceScopes.pop_back();
146 }
147
148 i += 2;
149 continue;
150 }
151
152 if(source[i]==1&&source[i+1]==ESC_ENUM){
153 if(i>=2){
154 if(source[i-2]==1&&source[i-1]==ESC_CONST) continue;
155 }
156 ppobj_EnumParent=(CEnumParent **)HeapReAlloc(hHeap,0,ppobj_EnumParent,(iEnumParentNum+1)*sizeof(CEnumParent *));
157 ppobj_EnumParent[iEnumParentNum]=new CEnumParent( namespaceScopes, source+i,i);
158 iEnumParentNum++;
159 }
160 }
161}
162void CEnumParent::DestroyEnum(void){
163 int i;
164 for(i=0;i<iEnumParentNum;i++){
165 delete ppobj_EnumParent[i];
166 }
167 HeapDefaultFree(ppobj_EnumParent);
168}
169
170char *CEnumParent::GenerateSourceCode(void){
171 char *buffer;
172 int MaxSize,length;
173 MaxSize=65535;
174 buffer=(char *)HeapAlloc(hHeap,0,MaxSize+65535);
175 length=0;
176
177 buffer[0]=0;
178
179 for(int i=0;i<iEnumParentNum;i++){
180 CEnumParent *parent;
181 parent=ppobj_EnumParent[i];
182
183 BOOST_FOREACH( const string &namespaceStr, parent->GetNamespaceScopes() ){
184 sprintf(buffer+length,"Namespace %s\n",namespaceStr.c_str());
185 length+=lstrlen(buffer+length);
186 }
187
188 sprintf(buffer+length,"Class Enum %s\n",parent->GetName().c_str());
189 length+=lstrlen(buffer+length);
190 lstrcpy(buffer+length,"\tInherits EnumBase\n");
191 length+=lstrlen(buffer+length);
192 sprintf(buffer+length,"\tSub %s(value As Long,lpszName As LPSTR)\n",parent->GetName().c_str());
193 length+=lstrlen(buffer+length);
194 lstrcpy(buffer+length,"\t\tEnumBase(value,lpszName)\n");
195 length+=lstrlen(buffer+length);
196 lstrcpy(buffer+length,"\tEnd Sub\n");
197 length+=lstrlen(buffer+length);
198 lstrcpy(buffer+length,"Public\n");
199 length+=lstrlen(buffer+length);
200 sprintf(buffer+length,"\tSub %s()\n",parent->GetName().c_str());
201 length+=lstrlen(buffer+length);
202 if(parent->iEnumMemberNum){
203 sprintf(buffer+length,"\t\tEnumBase(%d,\"%s\")\n",
204 parent->ppobj_EnumMember[0]->m_value,
205 parent->ppobj_EnumMember[0]->m_name);
206 length+=lstrlen(buffer+length);
207 }
208 lstrcpy(buffer+length,"\tEnd Sub\n");
209 length+=lstrlen(buffer+length);
210 sprintf(buffer+length,"\tSub ~%s()\n",parent->GetName().c_str());
211 length+=lstrlen(buffer+length);
212 lstrcpy(buffer+length,"\tEnd Sub\n");
213 length+=lstrlen(buffer+length);
214
215 for(int i2=0;i2<parent->iEnumMemberNum;i2++){
216 CEnumMember *member;
217 member=parent->ppobj_EnumMember[i2];
218
219 sprintf(buffer+length,"\tStatic %s As %s(%d,\"%s\")\n",
220 member->m_name,
221 parent->GetName().c_str(),
222 member->m_value,
223 member->m_name);
224 length+=lstrlen(buffer+length);
225 }
226
227 sprintf(buffer+length,"\tFunction Operator or (enumBase As %s) As %s\n",parent->GetName().c_str(),parent->GetName().c_str());
228 length+=lstrlen(buffer+length);
229 sprintf(buffer+length,"\t\tReturn New %s(This.value or enumBase.value, \"custom\")\n",parent->GetName().c_str());
230 length+=lstrlen(buffer+length);
231 lstrcpy(buffer+length,"\tEnd Function\n");
232 length+=lstrlen(buffer+length);
233
234 sprintf(buffer+length,"\tFunction Operator and (enumBase As %s) As %s\n",parent->GetName().c_str(),parent->GetName().c_str());
235 length+=lstrlen(buffer+length);
236 sprintf(buffer+length,"\t\tReturn New %s(This.value and enumBase.value, \"custom\")\n",parent->GetName().c_str());
237 length+=lstrlen(buffer+length);
238 lstrcpy(buffer+length,"\tEnd Function\n");
239 length+=lstrlen(buffer+length);
240
241 /*
242 sprintf(buffer+length,"\tOverride Function ToString() As String\n",parent->TypeName);
243 length+=lstrlen(buffer+length);
244 lstrcpy(buffer+length,"\t\tSelect Case value\n");
245 length+=lstrlen(buffer+length);
246 for(i2=0;i2<parent->iEnumMemberNum;i2++){
247 CEnumMember *member;
248 member=parent->ppobj_EnumMember[i2];
249
250 sprintf(buffer+length,"\t\t\tCase %d\n",member->m_value);
251 length+=lstrlen(buffer+length);
252 sprintf(buffer+length,"\t\t\t\tReturn \"%s\"\n",member->m_name);
253 length+=lstrlen(buffer+length);
254 }
255 lstrcpy(buffer+length,"\t\tEnd Select\n");
256 length+=lstrlen(buffer+length);
257 lstrcpy(buffer+length,"\tEnd Function\n");
258 length+=lstrlen(buffer+length);
259
260
261 sprintf(buffer+length,"\tSub Operator= (ByRef value As %s)\n",parent->TypeName);
262 length+=lstrlen(buffer+length);
263 lstrcpy(buffer+length,"\t\tThis.Copy(ByVal VarPtr(value))\n");
264 length+=lstrlen(buffer+length);
265 lstrcpy(buffer+length,"\tEnd Sub\n");
266 length+=lstrlen(buffer+length);
267
268 sprintf(buffer+length,"\tSub Operator= (ByRef value As String)\n",parent->TypeName);
269 length+=lstrlen(buffer+length);
270 lstrcpy(buffer+length,"\t\tSelect Case value\n");
271 length+=lstrlen(buffer+length);
272 for(i2=0;i2<parent->iEnumMemberNum;i2++){
273 CEnumMember *member;
274 member=parent->ppobj_EnumMember[i2];
275
276 sprintf(buffer+length,"\t\t\tCase \"%s\"\n",member->m_name);
277 length+=lstrlen(buffer+length);
278 sprintf(buffer+length,"\t\t\t\tThis=%s.%s\n",parent->TypeName,member->m_name);
279 length+=lstrlen(buffer+length);
280 }
281 lstrcpy(buffer+length,"\t\tEnd Select\n");
282 length+=lstrlen(buffer+length);
283 lstrcpy(buffer+length,"\tEnd Sub\n");
284 length+=lstrlen(buffer+length);
285
286 sprintf(buffer+length,"\tSub Operator= (value As Long)\n",parent->TypeName);
287 length+=lstrlen(buffer+length);
288 lstrcpy(buffer+length,"\t\tm_Value=value\n");
289 length+=lstrlen(buffer+length);
290 lstrcpy(buffer+length,"\tEnd Sub\n");
291 length+=lstrlen(buffer+length);*/
292
293 lstrcpy(buffer+length,"End Class\n");
294 length+=lstrlen(buffer+length);
295
296 BOOST_FOREACH( const string &namespaceStr, parent->GetNamespaceScopes() ){
297 lstrcpy( buffer+length, "End Namespace\n" );
298 length+=lstrlen(buffer+length);
299 }
300
301
302 //バッファ領域が足りなくなった場合はバッファを増量する
303 if(length>MaxSize){
304 MaxSize+=65535;
305 buffer=(char *)HeapReAlloc(hHeap,0,buffer,MaxSize+65535);
306 }
307 }
308
309#ifdef _DEBUG
310 // ログを生成
311 Jenga::Common::Logger logger( Jenga::Common::Environment::GetAppDir() + "\\enum_generated.log", false );
312 logger << buffer << endl;
313#endif
314
315 return buffer;
316}
Note: See TracBrowser for help on using the repository browser.