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

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

・GetConstInfo関数を廃止し、LexicalAnalyzer::CollectConstsメソッドを追加。

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