[206] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
[182] | 3 | #include <jenga/include/smoothie/Smoothie.h>
|
---|
| 4 |
|
---|
[199] | 5 | #include <Compiler.h>
|
---|
[206] | 6 | #include <../Enum.h>
|
---|
[195] | 7 |
|
---|
[4] | 8 | #include "common.h"
|
---|
| 9 |
|
---|
| 10 | CEnumParent **ppobj_EnumParent;
|
---|
| 11 | int iEnumParentNum;
|
---|
| 12 |
|
---|
| 13 | CEnumMember::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 | }
|
---|
| 19 | CEnumMember::~CEnumMember(){
|
---|
| 20 | if(m_name) HeapDefaultFree(m_name);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 |
|
---|
[103] | 24 | CEnumParent::CEnumParent( const NamespaceScopes &namespaceScopes, const char *buffer,int nowLine)
|
---|
| 25 | : namespaceScopes( namespaceScopes )
|
---|
| 26 | {
|
---|
[4] | 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 |
|
---|
[103] | 49 | name = temporary;
|
---|
[4] | 50 |
|
---|
| 51 | if(buffer[i]=='\0'){
|
---|
[75] | 52 | SetError(22,"Enum",nowLine);
|
---|
[4] | 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] | 75 | SetError(22,"Enum",nowLine);
|
---|
[4] | 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 | }
|
---|
| 102 | CEnumParent::~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 |
|
---|
| 112 | void CEnumParent::InitEnum(void){
|
---|
| 113 | ppobj_EnumParent=(CEnumParent **)HeapAlloc(hHeap,0,1);
|
---|
| 114 | iEnumParentNum=0;
|
---|
| 115 |
|
---|
[280] | 116 | const char *source = compiler.GetObjectModule().GetCurrentSource().GetBuffer();
|
---|
[88] | 117 |
|
---|
[103] | 118 | // 名前空間管理
|
---|
[199] | 119 | NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
|
---|
[103] | 120 | namespaceScopes.clear();
|
---|
| 121 |
|
---|
| 122 | int i2;
|
---|
| 123 | char temporary[VN_SIZE];
|
---|
| 124 | for(int i=0;;i++){
|
---|
[88] | 125 | if(source[i]=='\0') break;
|
---|
[4] | 126 |
|
---|
[103] | 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 |
|
---|
[88] | 151 | if(source[i]==1&&source[i+1]==ESC_ENUM){
|
---|
[4] | 152 | if(i>=2){
|
---|
[88] | 153 | if(source[i-2]==1&&source[i-1]==ESC_CONST) continue;
|
---|
[4] | 154 | }
|
---|
| 155 | ppobj_EnumParent=(CEnumParent **)HeapReAlloc(hHeap,0,ppobj_EnumParent,(iEnumParentNum+1)*sizeof(CEnumParent *));
|
---|
[103] | 156 | ppobj_EnumParent[iEnumParentNum]=new CEnumParent( namespaceScopes, source+i,i);
|
---|
[4] | 157 | iEnumParentNum++;
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | void 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 |
|
---|
[322] | 169 | char *CEnumParent::GenerateSourceCode(void){
|
---|
[4] | 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 |
|
---|
[103] | 178 | for(int i=0;i<iEnumParentNum;i++){
|
---|
[4] | 179 | CEnumParent *parent;
|
---|
| 180 | parent=ppobj_EnumParent[i];
|
---|
| 181 |
|
---|
[103] | 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());
|
---|
[4] | 188 | length+=lstrlen(buffer+length);
|
---|
| 189 | lstrcpy(buffer+length,"\tInherits EnumBase\n");
|
---|
| 190 | length+=lstrlen(buffer+length);
|
---|
[103] | 191 | sprintf(buffer+length,"\tSub %s(value As Long,lpszName As LPSTR)\n",parent->GetName().c_str());
|
---|
[92] | 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);
|
---|
[4] | 197 | lstrcpy(buffer+length,"Public\n");
|
---|
| 198 | length+=lstrlen(buffer+length);
|
---|
[103] | 199 | sprintf(buffer+length,"\tSub %s()\n",parent->GetName().c_str());
|
---|
[4] | 200 | length+=lstrlen(buffer+length);
|
---|
| 201 | if(parent->iEnumMemberNum){
|
---|
[92] | 202 | sprintf(buffer+length,"\t\tEnumBase(%d,\"%s\")\n",
|
---|
| 203 | parent->ppobj_EnumMember[0]->m_value,
|
---|
| 204 | parent->ppobj_EnumMember[0]->m_name);
|
---|
[4] | 205 | length+=lstrlen(buffer+length);
|
---|
| 206 | }
|
---|
| 207 | lstrcpy(buffer+length,"\tEnd Sub\n");
|
---|
| 208 | length+=lstrlen(buffer+length);
|
---|
[103] | 209 | sprintf(buffer+length,"\tSub ~%s()\n",parent->GetName().c_str());
|
---|
[4] | 210 | length+=lstrlen(buffer+length);
|
---|
| 211 | lstrcpy(buffer+length,"\tEnd Sub\n");
|
---|
| 212 | length+=lstrlen(buffer+length);
|
---|
| 213 |
|
---|
[103] | 214 | for(int i2=0;i2<parent->iEnumMemberNum;i2++){
|
---|
[4] | 215 | CEnumMember *member;
|
---|
| 216 | member=parent->ppobj_EnumMember[i2];
|
---|
| 217 |
|
---|
[92] | 218 | sprintf(buffer+length,"\tStatic %s As %s(%d,\"%s\")\n",
|
---|
[4] | 219 | member->m_name,
|
---|
[103] | 220 | parent->GetName().c_str(),
|
---|
[92] | 221 | member->m_value,
|
---|
| 222 | member->m_name);
|
---|
[4] | 223 | length+=lstrlen(buffer+length);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[327] | 226 | sprintf(buffer+length,"\tFunction Operator or (enumBase As %s) As %s\n",parent->GetName().c_str(),parent->GetName().c_str());
|
---|
| 227 | length+=lstrlen(buffer+length);
|
---|
| 228 | sprintf(buffer+length,"\t\tReturn New %s(This.value or enumBase.value, \"custom\")\n",parent->GetName().c_str());
|
---|
| 229 | length+=lstrlen(buffer+length);
|
---|
| 230 | lstrcpy(buffer+length,"\tEnd Function\n");
|
---|
| 231 | length+=lstrlen(buffer+length);
|
---|
| 232 |
|
---|
| 233 | sprintf(buffer+length,"\tFunction Operator and (enumBase As %s) As %s\n",parent->GetName().c_str(),parent->GetName().c_str());
|
---|
| 234 | length+=lstrlen(buffer+length);
|
---|
| 235 | sprintf(buffer+length,"\t\tReturn New %s(This.value and enumBase.value, \"custom\")\n",parent->GetName().c_str());
|
---|
| 236 | length+=lstrlen(buffer+length);
|
---|
| 237 | lstrcpy(buffer+length,"\tEnd Function\n");
|
---|
| 238 | length+=lstrlen(buffer+length);
|
---|
| 239 |
|
---|
[92] | 240 | /*
|
---|
[29] | 241 | sprintf(buffer+length,"\tOverride Function ToString() As String\n",parent->TypeName);
|
---|
[4] | 242 | length+=lstrlen(buffer+length);
|
---|
[92] | 243 | lstrcpy(buffer+length,"\t\tSelect Case value\n");
|
---|
[4] | 244 | length+=lstrlen(buffer+length);
|
---|
| 245 | for(i2=0;i2<parent->iEnumMemberNum;i2++){
|
---|
| 246 | CEnumMember *member;
|
---|
| 247 | member=parent->ppobj_EnumMember[i2];
|
---|
| 248 |
|
---|
| 249 | sprintf(buffer+length,"\t\t\tCase %d\n",member->m_value);
|
---|
| 250 | length+=lstrlen(buffer+length);
|
---|
| 251 | sprintf(buffer+length,"\t\t\t\tReturn \"%s\"\n",member->m_name);
|
---|
| 252 | length+=lstrlen(buffer+length);
|
---|
| 253 | }
|
---|
| 254 | lstrcpy(buffer+length,"\t\tEnd Select\n");
|
---|
| 255 | length+=lstrlen(buffer+length);
|
---|
| 256 | lstrcpy(buffer+length,"\tEnd Function\n");
|
---|
| 257 | length+=lstrlen(buffer+length);
|
---|
| 258 |
|
---|
[92] | 259 |
|
---|
[4] | 260 | sprintf(buffer+length,"\tSub Operator= (ByRef value As %s)\n",parent->TypeName);
|
---|
| 261 | length+=lstrlen(buffer+length);
|
---|
| 262 | lstrcpy(buffer+length,"\t\tThis.Copy(ByVal VarPtr(value))\n");
|
---|
| 263 | length+=lstrlen(buffer+length);
|
---|
| 264 | lstrcpy(buffer+length,"\tEnd Sub\n");
|
---|
| 265 | length+=lstrlen(buffer+length);
|
---|
| 266 |
|
---|
[92] | 267 | sprintf(buffer+length,"\tSub Operator= (ByRef value As String)\n",parent->TypeName);
|
---|
[4] | 268 | length+=lstrlen(buffer+length);
|
---|
| 269 | lstrcpy(buffer+length,"\t\tSelect Case value\n");
|
---|
| 270 | length+=lstrlen(buffer+length);
|
---|
| 271 | for(i2=0;i2<parent->iEnumMemberNum;i2++){
|
---|
| 272 | CEnumMember *member;
|
---|
| 273 | member=parent->ppobj_EnumMember[i2];
|
---|
| 274 |
|
---|
| 275 | sprintf(buffer+length,"\t\t\tCase \"%s\"\n",member->m_name);
|
---|
| 276 | length+=lstrlen(buffer+length);
|
---|
| 277 | sprintf(buffer+length,"\t\t\t\tThis=%s.%s\n",parent->TypeName,member->m_name);
|
---|
| 278 | length+=lstrlen(buffer+length);
|
---|
| 279 | }
|
---|
| 280 | lstrcpy(buffer+length,"\t\tEnd Select\n");
|
---|
| 281 | length+=lstrlen(buffer+length);
|
---|
| 282 | lstrcpy(buffer+length,"\tEnd Sub\n");
|
---|
[92] | 283 | length+=lstrlen(buffer+length);
|
---|
[4] | 284 |
|
---|
| 285 | sprintf(buffer+length,"\tSub Operator= (value As Long)\n",parent->TypeName);
|
---|
| 286 | length+=lstrlen(buffer+length);
|
---|
| 287 | lstrcpy(buffer+length,"\t\tm_Value=value\n");
|
---|
| 288 | length+=lstrlen(buffer+length);
|
---|
| 289 | lstrcpy(buffer+length,"\tEnd Sub\n");
|
---|
[92] | 290 | length+=lstrlen(buffer+length);*/
|
---|
[4] | 291 |
|
---|
| 292 | lstrcpy(buffer+length,"End Class\n");
|
---|
| 293 | length+=lstrlen(buffer+length);
|
---|
| 294 |
|
---|
[103] | 295 | BOOST_FOREACH( const string &namespaceStr, parent->GetNamespaceScopes() ){
|
---|
| 296 | lstrcpy( buffer+length, "End Namespace\n" );
|
---|
| 297 | length+=lstrlen(buffer+length);
|
---|
| 298 | }
|
---|
[4] | 299 |
|
---|
[103] | 300 |
|
---|
[4] | 301 | //バッファ領域が足りなくなった場合はバッファを増量する
|
---|
| 302 | if(length>MaxSize){
|
---|
| 303 | MaxSize+=65535;
|
---|
| 304 | buffer=(char *)HeapReAlloc(hHeap,0,buffer,MaxSize+65535);
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[302] | 308 | #ifdef _DEBUG
|
---|
[92] | 309 | // ログを生成
|
---|
[206] | 310 | Jenga::Common::Logger logger( Jenga::Common::Environment::GetAppDir() + "\\enum_generated.log", false );
|
---|
[167] | 311 | logger << buffer << endl;
|
---|
[302] | 312 | #endif
|
---|
[92] | 313 |
|
---|
[4] | 314 | return buffer;
|
---|
| 315 | }
|
---|