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

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