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

Last change on this file since 526 was 523, checked in by dai_9181, 17 years ago

ヘッダファイルを整理中

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