source: dev/trunk/abdev/BasicCompiler_Common/src/Const.cpp@ 303

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