source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/LexicalAnalyzer_Const.cpp@ 716

Last change on this file since 716 was 637, checked in by dai_9181, 16 years ago

リンカの依存関係解決モジュールを製作中

File size: 6.4 KB
Line 
1#include "stdafx.h"
2
3using namespace ActiveBasic::Compiler;
4
5void LexicalAnalyzer::AddConstEnum( Consts &consts, const NamespaceScopes &namespaceScopes, const char *buffer )
6{
7 extern int cp;
8 int i=0,i2;
9
10 if(!(buffer[i]==1&&buffer[i+1]==ESC_ENUM)) return;
11 i+=2;
12
13 //列挙体の名前を取得
14 char temporary[VN_SIZE];
15 for(i2=0;;i++,i2++){
16 if(IsCommandDelimitation(buffer[i])){
17 temporary[i2]=0;
18 break;
19 }
20 if(!IsVariableChar(buffer[i])){
21 compiler.errorMessenger.Output(1,NULL,i);
22 break;
23 }
24 temporary[i2]=buffer[i];
25 }
26
27 if(buffer[i]=='\0'){
28 compiler.errorMessenger.Output(22,"Enum",cp);
29 return;
30 }
31
32 int NextValue=0;
33 while(1){
34 i++;
35
36 if(buffer[i]==1&&buffer[i+1]==ESC_ENDENUM) break;
37
38 for(i2=0;;i2++,i++){
39 if(IsCommandDelimitation(buffer[i])){
40 temporary[i2]=0;
41 break;
42 }
43 if(buffer[i]=='='){
44 temporary[i2]=0;
45 break;
46 }
47 temporary[i2]=buffer[i];
48 }
49 if(temporary[0]=='\0'){
50 if(buffer[i]=='\0'){
51 compiler.errorMessenger.Output(22,"Enum",cp);
52 break;
53 }
54 continue;
55 }
56
57 if(buffer[i]!='='){
58 NextValue++;
59 }
60 else{
61 char temp2[VN_SIZE];
62 for(i++,i2=0;;i2++,i++){
63 if(IsCommandDelimitation(buffer[i])){
64 temp2[i2]=0;
65 break;
66 }
67 temp2[i2]=buffer[i];
68 }
69
70 _int64 i64data;
71 StaticCalculation(true, temp2,DEF_LONG,&i64data,Type());
72 NextValue=(int)i64data;
73 }
74
75 //定数を追加
76 consts.Add( Symbol( namespaceScopes, temporary ), NextValue);
77 }
78}
79
80void LexicalAnalyzer::CollectConsts( const char *source, Consts &consts, ConstMacros &constMacros )
81{
82 ////////////////////////////////////////////
83 // Const命令の情報を取得
84 ////////////////////////////////////////////
85
86 int i2;
87 char temporary[1024];
88
89 // 名前空間管理
90 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
91 namespaceScopes.clear();
92
93 for(int i=0;;i++){
94 if( source[i] == '\0' ) break;
95
96 if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
97 for(i+=2,i2=0;;i2++,i++){
98 if( IsCommandDelimitation( source[i] ) ){
99 temporary[i2]=0;
100 break;
101 }
102 temporary[i2]=source[i];
103 }
104 namespaceScopes.push_back( temporary );
105
106 continue;
107 }
108 else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
109 if( namespaceScopes.size() <= 0 ){
110 compiler.errorMessenger.Output(12, "End Namespace", i );
111 }
112 else{
113 namespaceScopes.pop_back();
114 }
115
116 i += 2;
117 continue;
118 }
119
120 if( source[i] == 1 ){
121 if(source[i]==1&&source[i+1]==ESC_CONST){
122 i+=2;
123
124 extern int cp;
125 cp=i; //エラー用
126
127
128 if(source[i]==1&&source[i+1]==ESC_ENUM){
129 AddConstEnum( consts, namespaceScopes, source+i);
130 continue;
131 }
132
133 for(i2=0;;i++,i2++){
134 if(source[i]=='\"'){
135 temporary[i2]=source[i];
136 for(i++,i2++;;i++,i2++){
137 temporary[i2]=source[i];
138 if(source[i]=='\"') break;
139 }
140 continue;
141 }
142 if(IsCommandDelimitation(source[i])){
143 temporary[i2]=0;
144 break;
145 }
146 temporary[i2]=source[i];
147 }
148
149 //名前を取得
150 char name[VN_SIZE];
151 for(i2=0;;i2++){
152 if(temporary[i2]=='\0'){
153 compiler.errorMessenger.Output(10,"Const",cp);
154 return;
155 }
156 if(temporary[i2]=='='||temporary[i2]=='('){
157 name[i2]=0;
158 break;
159 }
160 name[i2]=temporary[i2];
161 }
162
163 //重複チェック
164 if( compiler.GetObjectModule().meta.GetGlobalConstMacros().IsExistDuplicationKeyName( name )
165 || compiler.GetObjectModule().meta.GetGlobalConsts().IsExistDuplicationKeyName( name ) )
166 {
167 compiler.errorMessenger.Output(15,name,cp);
168 return;
169 }
170
171 if( temporary[i2] == '=' )
172 {
173 // 定数
174 const char *expression = temporary + i2 + 1;
175
176 _int64 i64data;
177 Type resultType;
178 if( StaticCalculation(false, expression, 0, &i64data, resultType) )
179 {
180 consts.Add( Symbol( namespaceScopes, name ), i64data, resultType );
181 }
182 }
183 else
184 {
185 // 定数マクロ
186 const char *params = temporary + i2;
187 if( !constMacros.Add( Symbol( namespaceScopes, name ), params ) )
188 {
189 compiler.errorMessenger.Output( 1, NULL, i );
190 }
191 }
192
193 if(source[i]=='\0') break;
194 }
195 else{
196 int result = JumpStatement( source, i );
197 if( result == -1 ){
198 //エラー
199 return;
200 }
201 else if( result == 1 ){
202 //ジャンプした場合
203 i--;
204 }
205 }
206 }
207 }
208
209 // イテレータを初期化
210 compiler.GetObjectModule().meta.GetGlobalConsts().Iterator_Init();
211 compiler.GetObjectModule().meta.GetGlobalConstMacros().Iterator_Init();
212}
213
214bool LexicalAnalyzer::ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char *dest )
215{
216 extern HANDLE hHeap;
217 int i2,i3,i4,num;
218 char temporary[VN_SIZE];
219 char *pParms[MAX_PARMS];
220 num=0;
221 i2=0;
222 while(1){
223 i2=GetOneParameter(parameterStr,i2,temporary);
224
225 pParms[num]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
226 lstrcpy(pParms[num],temporary);
227
228 num++;
229 if(parameterStr[i2]=='\0') break;
230 }
231 if( num != constMacro.GetParameters().size() ){
232 extern int cp;
233 for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
234 compiler.errorMessenger.Output(10,constMacro.GetName().c_str(),cp);
235 lstrcpy(dest,"0");
236 return true;
237 }
238
239 i2=0;
240 i4=0;
241 while(1){
242
243 //数式内の項を取得
244 for(i3=0;;i2++,i3++){
245 if(!IsVariableChar( constMacro.GetExpression()[i2] )){
246 temporary[i3]=0;
247 break;
248 }
249 temporary[i3] = constMacro.GetExpression()[i2];
250 }
251
252 //パラメータと照合する
253 for( i3=0; i3<(int)constMacro.GetParameters().size(); i3++ ){
254 if( constMacro.GetParameters()[i3] == temporary ) break;
255 }
256
257 if( i3 == (int)constMacro.GetParameters().size() ){
258 //パラメータでないとき
259 lstrcpy(dest+i4,temporary);
260 i4+=lstrlen(temporary);
261 }
262 else{
263 //パラメータのとき
264 lstrcpy(dest+i4,pParms[i3]);
265 i4+=lstrlen(pParms[i3]);
266 }
267
268 //演算子をコピー
269 for(;;i2++,i4++){
270 if( constMacro.GetExpression()[i2] == 1 ){
271 dest[i4++] = constMacro.GetExpression()[i2++];
272 dest[i4] = constMacro.GetExpression()[i2];
273 continue;
274 }
275 if(IsVariableTopChar( constMacro.GetExpression()[i2] )) break;
276 dest[i4] = constMacro.GetExpression()[i2];
277 if( constMacro.GetExpression()[i2] == '\0' ) break;
278 }
279
280 if( constMacro.GetExpression()[i2] == '\0' ) break;
281 }
282
283 for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
284
285 return true;
286}
Note: See TracBrowser for help on using the repository browser.