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