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

Last change on this file since 214 was 206, checked in by dai_9181, 17 years ago

コード全体のリファクタリングを実施

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