source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/Const.cpp@ 509

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

Symbolクラスをab_commonプロジェクトに移動した。

File size: 6.0 KB
RevLine 
[206]1#include "stdafx.h"
2
[193]3#include <Compiler.h>
4
[509]5using namespace ActiveBasic::Compiler;
[4]6
[7]7double CConst::GetDoubleData(){
8 double dbl;
9 memcpy(&dbl,&i64data,sizeof(_int64));
10 return dbl;
11}
12
[206]13void AddConst( const NamespaceScopes &namespaceScopes, char *buffer ){
[4]14 int i;
15
16 //名前を取得
[206]17 char name[VN_SIZE];
[4]18 for(i=0;;i++){
19 if(buffer[i]=='\0'){
[465]20 compiler.errorMessenger.Output(10,"Const",cp);
[4]21 return;
22 }
23 if(buffer[i]=='='||buffer[i]=='('){
[206]24 name[i]=0;
[4]25 break;
26 }
[206]27 name[i]=buffer[i];
[4]28 }
29
[7]30 //重複チェック
[265]31 if( compiler.GetObjectModule().meta.GetGlobalConstMacros().IsExist( name )
32 || compiler.GetObjectModule().meta.GetGlobalConsts().IsExist( name ) )
[206]33 {
[465]34 compiler.errorMessenger.Output(15,name,cp);
[7]35 return;
36 }
37
[4]38 if(buffer[i] == '('){
39 //定数マクロ
40
[265]41 compiler.GetObjectModule().meta.GetGlobalConstMacros().Add( namespaceScopes, name, buffer + i );
[4]42 }
43 else{
44 //一般の定数
[206]45 char *expression = buffer + i + 1;
[4]46
[265]47 compiler.GetObjectModule().meta.GetGlobalConsts().Add( namespaceScopes, name, expression );
[4]48 }
49}
50
[206]51void Consts::Add( const NamespaceScopes &namespaceScopes, const string &name , char *Expression)
52{
[7]53 _int64 i64data;
[75]54 Type resultType;
55 if( !StaticCalculation(false, Expression, 0, &i64data, resultType) ){
56 //変数の場合
57 //何もしない(実行領域コンパイル時にdim宣言として扱う)
58 return;
59 }
[4]60
[75]61 //リテラル値の場合
62 //登録を行う
[7]63
[103]64 CConst *newconst = new CConst(namespaceScopes, name, resultType, i64data);
[7]65
[206]66 //ハッシュリストに追加
67 Put( newconst );
[4]68}
[206]69void Consts::Add(const NamespaceScopes &namespaceScopes, const string &name, int value){
[103]70 CConst *newconst = new CConst( namespaceScopes, name, value);
[7]71
[206]72 //ハッシュリストに追加
73 Put( newconst );
[7]74}
75
[206]76CConst *Consts::GetObjectPtr( const string &name ){
[103]77 char ObjName[VN_SIZE]; //オブジェクト変数
78 char NestMember[VN_SIZE]; //入れ子メンバ
[206]79 bool isObjectMember = SplitMemberName( name.c_str(), ObjName, NestMember );
[103]80
[206]81 CConst *pConst = GetHashArrayElement( NestMember );
82 while( pConst )
83 {
[509]84 if( pConst->IsEqualSymbol( LexicalAnalyzer::FullNameToSymbol( name ) ) )
[206]85 {
86 break;
87 }
88 pConst = pConst->GetChainNext();
[7]89 }
90
91 return pConst;
92}
93
94
[254]95int Consts::GetBasicType(const char *Name){
[7]96 CConst *pConst = GetObjectPtr(Name);
97
98 if(!pConst) return 0;
99
[103]100 return pConst->GetType().GetBasicType();
[7]101}
[254]102_int64 Consts::GetWholeData(const char *Name){
[7]103 CConst *pConst = GetObjectPtr(Name);
104
105 if(!pConst) return 0;
106
107 return pConst->GetWholeData();
108}
[254]109double Consts::GetDoubleData(const char *Name){
[7]110 CConst *pConst = GetObjectPtr(Name);
111
112 if(!pConst) return 0;
113
114 return pConst->GetDoubleData();
115}
[254]116bool Consts::IsStringPtr( const char *Name ){
[103]117 CConst *pConst = GetObjectPtr(Name);
118
119 if(!pConst) return false;
120
121 const Type &type = pConst->GetType();
122
123 return ( type.GetBasicType() == typeOfPtrChar && type.GetIndex() == LITERAL_STRING );
124}
[206]125
126
127bool ConstMacro::GetCalcBuffer( const char *parameterStr, char *dest ) const
128{
129 extern HANDLE hHeap;
130 int i2,i3,i4,num;
131 char temporary[VN_SIZE];
132 char *pParms[MAX_PARMS];
133 num=0;
134 i2=0;
135 while(1){
136 i2=GetOneParameter(parameterStr,i2,temporary);
137
138 pParms[num]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
139 lstrcpy(pParms[num],temporary);
140
141 num++;
142 if(parameterStr[i2]=='\0') break;
143 }
144 if( num != this->GetParameters().size() ){
145 extern int cp;
146 for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
[465]147 compiler.errorMessenger.Output(10,GetName().c_str(),cp);
[206]148 lstrcpy(dest,"0");
149 return 1;
150 }
151
152 i2=0;
153 i4=0;
154 while(1){
155
156 //数式内の項を取得
157 for(i3=0;;i2++,i3++){
158 if(!IsVariableChar( this->GetExpression()[i2] )){
159 temporary[i3]=0;
160 break;
161 }
162 temporary[i3] = this->GetExpression()[i2];
163 }
164
165 //パラメータと照合する
166 for( i3=0; i3<(int)this->GetParameters().size(); i3++ ){
167 if( this->GetParameters()[i3] == temporary ) break;
168 }
169
170 if( i3 == (int)this->GetParameters().size() ){
171 //パラメータでないとき
172 lstrcpy(dest+i4,temporary);
173 i4+=lstrlen(temporary);
174 }
175 else{
176 //パラメータのとき
177 lstrcpy(dest+i4,pParms[i3]);
178 i4+=lstrlen(pParms[i3]);
179 }
180
181 //演算子をコピー
182 for(;;i2++,i4++){
183 if( this->GetExpression()[i2] == 1 ){
184 dest[i4++] = this->GetExpression()[i2++];
185 dest[i4] = this->GetExpression()[i2];
186 continue;
187 }
188 if(IsVariableTopChar( this->GetExpression()[i2] )) break;
189 dest[i4] = this->GetExpression()[i2];
190 if( this->GetExpression()[i2] == '\0' ) break;
191 }
192
193 if( this->GetExpression()[i2] == '\0' ) break;
194 }
195
196 for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
197
198 return 1;
199}
200
201// マクロ定数を追加するための関数
202void ConstMacros::Add( const NamespaceScopes &namespaceScopes, const std::string &name, const char *parameterStr )
203{
204 std::vector<std::string> parameters;
205
206 int i = 0;
207 if(parameterStr[i]!='(')
208 {
[465]209 compiler.errorMessenger.OutputFatalError();
[206]210 return;
211 }
212
213 char temporary[VN_SIZE];
214 int i2;
215 for(i++,i2=0;;i++,i2++){
216 if(parameterStr[i]=='\0'){
[465]217 compiler.errorMessenger.Output(1,NULL,cp);
[206]218 return;
219 }
220 if(parameterStr[i]==','||parameterStr[i]==')'){
221 temporary[i2]=0;
222
223 parameters.push_back( temporary );
224
225 if(parameterStr[i]==')'){
226 i++;
227 if(parameterStr[i]!='='){
228 extern int cp;
[465]229 compiler.errorMessenger.Output(1,NULL,cp);
[206]230 return;
231 }
232 break;
233 }
234
235 i2=-1;
236 continue;
237 }
238 temporary[i2]=parameterStr[i];
239 }
240
241 //データ
242 lstrcpy(temporary,parameterStr+i+1);
243
244 Put( new ConstMacro( namespaceScopes, name, parameters, temporary ) );
245}
246ConstMacro *ConstMacros::Find( const std::string &name ){
247 char ObjName[VN_SIZE]; //オブジェクト変数
248 char NestMember[VN_SIZE]; //入れ子メンバ
249 bool isObjectMember = SplitMemberName( name.c_str(), ObjName, NestMember );
250
251 ConstMacro *pConstMacro = GetHashArrayElement( NestMember );
252 while( pConstMacro )
253 {
[509]254 if( pConstMacro->IsEqualSymbol( LexicalAnalyzer::FullNameToSymbol( name ) ) )
[206]255 {
256 break;
257 }
258 pConstMacro = pConstMacro->GetChainNext();
259 }
260
261 return pConstMacro;
262}
Note: See TracBrowser for help on using the repository browser.