source: dev/BasicCompiler_Common/Const.cpp@ 87

Last change on this file since 87 was 75, checked in by dai_9181, 18 years ago

TYPEINFO→Typeへのリファクタリングを実施。64bitはほぼ完了。32bitが全般的に未完成。

File size: 3.5 KB
Line 
1#include "common.h"
2#include OPCODE_H_PATH //opcode.h
3
4
5//シングルトンオブジェクト
6CDBConst CDBConst::obj;
7
8
9CConstBase::CConstBase(char *Name){
10 this->Name = (char *)malloc(lstrlen(Name)+1);
11 lstrcpy(this->Name, Name);
12}
13CConstBase::~CConstBase(){
14 free(Name);
15 Name=0;
16}
17
18char *CConstBase::GetName(){
19 return Name;
20}
21
22
23
24CConst::CConst(char *Name, int Type, _int64 i64data):CConstBase(Name)
25{
26 this->Type = Type;
27 this->i64data = i64data;
28
29 //連結リストを初期化
30 pNext = 0;
31}
32CConst::CConst(char *Name, int value):CConstBase(Name)
33{
34 Type = DEF_LONG;
35 i64data = value;
36
37 //連結リストを初期化
38 pNext = 0;
39}
40CConst::~CConst(){
41 //連結先のオブジェクトを破棄
42 if(pNext){
43 delete pNext;
44 pNext = 0;
45 }
46}
47
48int CConst::GetType(){
49 return Type;
50}
51_int64 CConst::GetWholeData(){
52 return i64data;
53}
54double CConst::GetDoubleData(){
55 double dbl;
56 memcpy(&dbl,&i64data,sizeof(_int64));
57 return dbl;
58}
59
60
61
62CConstMacro::CConstMacro(char *Name, char *Expression):CConstBase(Name)
63{
64}
65CConstMacro::~CConstMacro(){
66}
67
68
69
70
71
72CDBConst::CDBConst(){
73 memset(this,0,sizeof(CDBConst));
74
75 ppHash = (CConst **)calloc(MAX_HASH*sizeof(CConst *),1);
76 Init();
77}
78CDBConst::~CDBConst(){
79 Free();
80
81 free(ppHash);
82}
83
84//解放
85void CDBConst::Free(){
86 int i;
87 for(i=0; i<MAX_HASH; i++){
88 if(ppHash[i]){
89 delete ppHash[i];
90 ppHash[i] = 0;
91 }
92 }
93}
94
95//初期化
96void CDBConst::Init(){
97 Free();
98}
99
100void AddConstData(char *Command);
101void CDBConst::Add(char *buffer){
102 int i;
103
104 //名前を取得
105 char Name[VN_SIZE];
106 for(i=0;;i++){
107 if(buffer[i]=='\0'){
108 SetError(10,"Const",cp);
109 return;
110 }
111 if(buffer[i]=='='||buffer[i]=='('){
112 Name[i]=0;
113 break;
114 }
115 Name[i]=buffer[i];
116 }
117
118 //重複チェック
119 if(GetType(Name)){
120 SetError(15,Name,cp);
121 return;
122 }
123
124 if(buffer[i] == '('){
125 //定数マクロ
126
127 //未完成
128 AddConstData(buffer);
129 }
130 else{
131 //一般の定数
132 char *Expression = buffer + i + 1;
133
134 AddConst(Name,Expression);
135 }
136}
137
138void CDBConst::AddConst(char *Name, CConst *newconst){
139 int key = hash_default(Name);
140
141 //ハッシュリストに追加
142 if(ppHash[key]){
143 CConst *pConst = ppHash[key];
144 while(pConst->pNext){
145 pConst = pConst->pNext;
146 }
147
148 pConst->pNext = newconst;
149 }
150 else{
151 ppHash[key] = newconst;
152 }
153}
154void CDBConst::AddConst(char *Name, char *Expression){
155 _int64 i64data;
156 Type resultType;
157 if( !StaticCalculation(false, Expression, 0, &i64data, resultType) ){
158 //変数の場合
159 //何もしない(実行領域コンパイル時にdim宣言として扱う)
160 return;
161 }
162
163 //リテラル値の場合
164 //登録を行う
165
166 CConst *newconst = new CConst(Name, resultType.GetBasicType(), i64data);
167
168 AddConst(Name, newconst);
169}
170void CDBConst::AddConst(char *Name, int value){
171 CConst *newconst = new CConst(Name, value);
172
173 AddConst(Name, newconst);
174}
175
176CConst *CDBConst::GetObjectPtr(char *Name){
177 //ハッシュ値を取得
178 int key;
179 key=hash_default(Name);
180
181 //格納位置を取得
182 CConst *pConst;
183 pConst=ppHash[key];
184 while(pConst){
185 if(lstrcmp(pConst->GetName(),Name)==0) break;
186
187 pConst=pConst->pNext;
188 }
189
190 return pConst;
191}
192
193
194int CDBConst::GetType(char *Name){
195 CConst *pConst = GetObjectPtr(Name);
196
197 if(!pConst) return 0;
198
199 return pConst->GetType();
200}
201_int64 CDBConst::GetWholeData(char *Name){
202 CConst *pConst = GetObjectPtr(Name);
203
204 if(!pConst) return 0;
205
206 return pConst->GetWholeData();
207}
208double CDBConst::GetDoubleData(char *Name){
209 CConst *pConst = GetObjectPtr(Name);
210
211 if(!pConst) return 0;
212
213 return pConst->GetDoubleData();
214}
Note: See TracBrowser for help on using the repository browser.