source: dev/trunk/abdev/BasicCompiler_Common/src/Enum.cpp@ 420

Last change on this file since 420 was 407, checked in by dai_9181, 17 years ago

Enum.cpp/Enum.hを移動した

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