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

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

LexicalAnalyzer::ConstMacroToExpressionメソッドを実装。

File size: 6.1 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( 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 consts.Add( namespaceScopes, name, expression );
176 }
177 else
178 {
179 // 定数マクロ
180 const char *params = temporary + i2;
181 constMacros.Add( namespaceScopes, name, params );
182 }
183
184 if(source[i]=='\0') break;
185 }
186 else{
187 int result = JumpStatement( source, i );
188 if( result == -1 ){
189 //エラー
190 return;
191 }
192 else if( result == 1 ){
193 //ジャンプした場合
194 i--;
195 }
196 }
197 }
198 }
199
200 // イテレータを初期化
201 compiler.GetObjectModule().meta.GetGlobalConsts().Iterator_Init();
202 compiler.GetObjectModule().meta.GetGlobalConstMacros().Iterator_Init();
203}
204
205bool LexicalAnalyzer::ConstMacroToExpression( const ConstMacro &constMacro, const char *parameterStr, char *dest )
206{
207 extern HANDLE hHeap;
208 int i2,i3,i4,num;
209 char temporary[VN_SIZE];
210 char *pParms[MAX_PARMS];
211 num=0;
212 i2=0;
213 while(1){
214 i2=GetOneParameter(parameterStr,i2,temporary);
215
216 pParms[num]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
217 lstrcpy(pParms[num],temporary);
218
219 num++;
220 if(parameterStr[i2]=='\0') break;
221 }
222 if( num != constMacro.GetParameters().size() ){
223 extern int cp;
224 for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
225 compiler.errorMessenger.Output(10,constMacro.GetName().c_str(),cp);
226 lstrcpy(dest,"0");
227 return true;
228 }
229
230 i2=0;
231 i4=0;
232 while(1){
233
234 //数式内の項を取得
235 for(i3=0;;i2++,i3++){
236 if(!IsVariableChar( constMacro.GetExpression()[i2] )){
237 temporary[i3]=0;
238 break;
239 }
240 temporary[i3] = constMacro.GetExpression()[i2];
241 }
242
243 //パラメータと照合する
244 for( i3=0; i3<(int)constMacro.GetParameters().size(); i3++ ){
245 if( constMacro.GetParameters()[i3] == temporary ) break;
246 }
247
248 if( i3 == (int)constMacro.GetParameters().size() ){
249 //パラメータでないとき
250 lstrcpy(dest+i4,temporary);
251 i4+=lstrlen(temporary);
252 }
253 else{
254 //パラメータのとき
255 lstrcpy(dest+i4,pParms[i3]);
256 i4+=lstrlen(pParms[i3]);
257 }
258
259 //演算子をコピー
260 for(;;i2++,i4++){
261 if( constMacro.GetExpression()[i2] == 1 ){
262 dest[i4++] = constMacro.GetExpression()[i2++];
263 dest[i4] = constMacro.GetExpression()[i2];
264 continue;
265 }
266 if(IsVariableTopChar( constMacro.GetExpression()[i2] )) break;
267 dest[i4] = constMacro.GetExpression()[i2];
268 if( constMacro.GetExpression()[i2] == '\0' ) break;
269 }
270
271 if( constMacro.GetExpression()[i2] == '\0' ) break;
272 }
273
274 for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
275
276 return true;
277}
Note: See TracBrowser for help on using the repository browser.