[206] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
[167] | 3 | #include <jenga/include/common/logger.h>
|
---|
| 4 |
|
---|
[182] | 5 | #include <jenga/include/smoothie/Smoothie.h>
|
---|
| 6 | #include <jenga/include/smoothie/LexicalAnalysis.h>
|
---|
| 7 |
|
---|
[199] | 8 | #include <Compiler.h>
|
---|
[206] | 9 | #include <../Enum.h>
|
---|
[195] | 10 |
|
---|
[4] | 11 | #include "common.h"
|
---|
| 12 |
|
---|
| 13 | CEnumParent **ppobj_EnumParent;
|
---|
| 14 | int iEnumParentNum;
|
---|
| 15 |
|
---|
| 16 | CEnumMember::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 | }
|
---|
| 22 | CEnumMember::~CEnumMember(){
|
---|
| 23 | if(m_name) HeapDefaultFree(m_name);
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 |
|
---|
[103] | 27 | CEnumParent::CEnumParent( const NamespaceScopes &namespaceScopes, const char *buffer,int nowLine)
|
---|
| 28 | : namespaceScopes( namespaceScopes )
|
---|
| 29 | {
|
---|
[4] | 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 |
|
---|
[103] | 52 | name = temporary;
|
---|
[4] | 53 |
|
---|
| 54 | if(buffer[i]=='\0'){
|
---|
[75] | 55 | SetError(22,"Enum",nowLine);
|
---|
[4] | 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'){
|
---|
[75] | 78 | SetError(22,"Enum",nowLine);
|
---|
[4] | 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 | }
|
---|
| 105 | CEnumParent::~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 |
|
---|
| 115 | void CEnumParent::InitEnum(void){
|
---|
| 116 | ppobj_EnumParent=(CEnumParent **)HeapAlloc(hHeap,0,1);
|
---|
| 117 | iEnumParentNum=0;
|
---|
| 118 |
|
---|
[88] | 119 | const char *source = Smoothie::Lexical::source.GetBuffer();
|
---|
| 120 |
|
---|
[103] | 121 | // 名前空間管理
|
---|
[199] | 122 | NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
|
---|
[103] | 123 | namespaceScopes.clear();
|
---|
| 124 |
|
---|
| 125 | int i2;
|
---|
| 126 | char temporary[VN_SIZE];
|
---|
| 127 | for(int i=0;;i++){
|
---|
[88] | 128 | if(source[i]=='\0') break;
|
---|
[4] | 129 |
|
---|
[103] | 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 |
|
---|
[88] | 154 | if(source[i]==1&&source[i+1]==ESC_ENUM){
|
---|
[4] | 155 | if(i>=2){
|
---|
[88] | 156 | if(source[i-2]==1&&source[i-1]==ESC_CONST) continue;
|
---|
[4] | 157 | }
|
---|
| 158 | ppobj_EnumParent=(CEnumParent **)HeapReAlloc(hHeap,0,ppobj_EnumParent,(iEnumParentNum+1)*sizeof(CEnumParent *));
|
---|
[103] | 159 | ppobj_EnumParent[iEnumParentNum]=new CEnumParent( namespaceScopes, source+i,i);
|
---|
[4] | 160 | iEnumParentNum++;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | void 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 |
|
---|
| 172 | char *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 |
|
---|
[103] | 181 | for(int i=0;i<iEnumParentNum;i++){
|
---|
[4] | 182 | CEnumParent *parent;
|
---|
| 183 | parent=ppobj_EnumParent[i];
|
---|
| 184 |
|
---|
[103] | 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());
|
---|
[4] | 191 | length+=lstrlen(buffer+length);
|
---|
| 192 | lstrcpy(buffer+length,"\tInherits EnumBase\n");
|
---|
| 193 | length+=lstrlen(buffer+length);
|
---|
[103] | 194 | sprintf(buffer+length,"\tSub %s(value As Long,lpszName As LPSTR)\n",parent->GetName().c_str());
|
---|
[92] | 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);
|
---|
[4] | 200 | lstrcpy(buffer+length,"Public\n");
|
---|
| 201 | length+=lstrlen(buffer+length);
|
---|
[103] | 202 | sprintf(buffer+length,"\tSub %s()\n",parent->GetName().c_str());
|
---|
[4] | 203 | length+=lstrlen(buffer+length);
|
---|
| 204 | if(parent->iEnumMemberNum){
|
---|
[92] | 205 | sprintf(buffer+length,"\t\tEnumBase(%d,\"%s\")\n",
|
---|
| 206 | parent->ppobj_EnumMember[0]->m_value,
|
---|
| 207 | parent->ppobj_EnumMember[0]->m_name);
|
---|
[4] | 208 | length+=lstrlen(buffer+length);
|
---|
| 209 | }
|
---|
| 210 | lstrcpy(buffer+length,"\tEnd Sub\n");
|
---|
| 211 | length+=lstrlen(buffer+length);
|
---|
[103] | 212 | sprintf(buffer+length,"\tSub ~%s()\n",parent->GetName().c_str());
|
---|
[4] | 213 | length+=lstrlen(buffer+length);
|
---|
| 214 | lstrcpy(buffer+length,"\tEnd Sub\n");
|
---|
| 215 | length+=lstrlen(buffer+length);
|
---|
| 216 |
|
---|
[103] | 217 | for(int i2=0;i2<parent->iEnumMemberNum;i2++){
|
---|
[4] | 218 | CEnumMember *member;
|
---|
| 219 | member=parent->ppobj_EnumMember[i2];
|
---|
| 220 |
|
---|
[92] | 221 | sprintf(buffer+length,"\tStatic %s As %s(%d,\"%s\")\n",
|
---|
[4] | 222 | member->m_name,
|
---|
[103] | 223 | parent->GetName().c_str(),
|
---|
[92] | 224 | member->m_value,
|
---|
| 225 | member->m_name);
|
---|
[4] | 226 | length+=lstrlen(buffer+length);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[92] | 229 | /*
|
---|
[29] | 230 | sprintf(buffer+length,"\tOverride Function ToString() As String\n",parent->TypeName);
|
---|
[4] | 231 | length+=lstrlen(buffer+length);
|
---|
[92] | 232 | lstrcpy(buffer+length,"\t\tSelect Case value\n");
|
---|
[4] | 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 |
|
---|
[92] | 248 |
|
---|
[4] | 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 |
|
---|
[92] | 256 | sprintf(buffer+length,"\tSub Operator= (ByRef value As String)\n",parent->TypeName);
|
---|
[4] | 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");
|
---|
[92] | 272 | length+=lstrlen(buffer+length);
|
---|
[4] | 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");
|
---|
[92] | 279 | length+=lstrlen(buffer+length);*/
|
---|
[4] | 280 |
|
---|
| 281 | lstrcpy(buffer+length,"End Class\n");
|
---|
| 282 | length+=lstrlen(buffer+length);
|
---|
| 283 |
|
---|
[103] | 284 | BOOST_FOREACH( const string &namespaceStr, parent->GetNamespaceScopes() ){
|
---|
| 285 | lstrcpy( buffer+length, "End Namespace\n" );
|
---|
| 286 | length+=lstrlen(buffer+length);
|
---|
| 287 | }
|
---|
[4] | 288 |
|
---|
[103] | 289 |
|
---|
[4] | 290 | //バッファ領域が足りなくなった場合はバッファを増量する
|
---|
| 291 | if(length>MaxSize){
|
---|
| 292 | MaxSize+=65535;
|
---|
| 293 | buffer=(char *)HeapReAlloc(hHeap,0,buffer,MaxSize+65535);
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[92] | 297 | // ログを生成
|
---|
[206] | 298 | Jenga::Common::Logger logger( Jenga::Common::Environment::GetAppDir() + "\\enum_generated.log", false );
|
---|
[167] | 299 | logger << buffer << endl;
|
---|
[92] | 300 |
|
---|
[4] | 301 | return buffer;
|
---|
| 302 | }
|
---|