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

Last change on this file since 293 was 265, checked in by dai_9181, 17 years ago
File size: 5.7 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4
5
6double CConst::GetDoubleData(){
7 double dbl;
8 memcpy(&dbl,&i64data,sizeof(_int64));
9 return dbl;
10}
11
12void AddConst( const NamespaceScopes &namespaceScopes, char *buffer ){
13 int i;
14
15 //名前を取得
16 char name[VN_SIZE];
17 for(i=0;;i++){
18 if(buffer[i]=='\0'){
19 SetError(10,"Const",cp);
20 return;
21 }
22 if(buffer[i]=='='||buffer[i]=='('){
23 name[i]=0;
24 break;
25 }
26 name[i]=buffer[i];
27 }
28
29 //重複チェック
30 if( compiler.GetObjectModule().meta.GetGlobalConstMacros().IsExist( name )
31 || compiler.GetObjectModule().meta.GetGlobalConsts().IsExist( name ) )
32 {
33 SetError(15,name,cp);
34 return;
35 }
36
37 if(buffer[i] == '('){
38 //定数マクロ
39
40 compiler.GetObjectModule().meta.GetGlobalConstMacros().Add( namespaceScopes, name, buffer + i );
41 }
42 else{
43 //一般の定数
44 char *expression = buffer + i + 1;
45
46 compiler.GetObjectModule().meta.GetGlobalConsts().Add( namespaceScopes, name, expression );
47 }
48}
49
50void Consts::Add( const NamespaceScopes &namespaceScopes, const string &name , char *Expression)
51{
52 _int64 i64data;
53 Type resultType;
54 if( !StaticCalculation(false, Expression, 0, &i64data, resultType) ){
55 //変数の場合
56 //何もしない(実行領域コンパイル時にdim宣言として扱う)
57 return;
58 }
59
60 //リテラル値の場合
61 //登録を行う
62
63 CConst *newconst = new CConst(namespaceScopes, name, resultType, i64data);
64
65 //ハッシュリストに追加
66 Put( newconst );
67}
68void Consts::Add(const NamespaceScopes &namespaceScopes, const string &name, int value){
69 CConst *newconst = new CConst( namespaceScopes, name, value);
70
71 //ハッシュリストに追加
72 Put( newconst );
73}
74
75CConst *Consts::GetObjectPtr( const string &name ){
76 char ObjName[VN_SIZE]; //オブジェクト変数
77 char NestMember[VN_SIZE]; //入れ子メンバ
78 bool isObjectMember = SplitMemberName( name.c_str(), ObjName, NestMember );
79
80 CConst *pConst = GetHashArrayElement( NestMember );
81 while( pConst )
82 {
83 if( pConst->IsEqualSymbol( name ) )
84 {
85 break;
86 }
87 pConst = pConst->GetChainNext();
88 }
89
90 return pConst;
91}
92
93
94int Consts::GetBasicType(const char *Name){
95 CConst *pConst = GetObjectPtr(Name);
96
97 if(!pConst) return 0;
98
99 return pConst->GetType().GetBasicType();
100}
101_int64 Consts::GetWholeData(const char *Name){
102 CConst *pConst = GetObjectPtr(Name);
103
104 if(!pConst) return 0;
105
106 return pConst->GetWholeData();
107}
108double Consts::GetDoubleData(const char *Name){
109 CConst *pConst = GetObjectPtr(Name);
110
111 if(!pConst) return 0;
112
113 return pConst->GetDoubleData();
114}
115bool Consts::IsStringPtr( const char *Name ){
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}
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.