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

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