Index: trunk/abdev/BasicCompiler_Common/src/Const.cpp
===================================================================
--- trunk/abdev/BasicCompiler_Common/Const.cpp	(revision 199)
+++ trunk/abdev/BasicCompiler_Common/src/Const.cpp	(revision 206)
@@ -1,60 +1,7 @@
+#include "stdafx.h"
+
 #include <Compiler.h>
-#include <NamespaceSupporter.h>
-
-#include "common.h"
-#include OPCODE_H_PATH	//opcode.h
-
-
-//シングルトンオブジェクト
-CDBConst CDBConst::obj;
-
-
-bool ConstBase::IsEqualSymbol( const NamespaceScopes &namespaceScopes, const string &name ) const
-{
-	if( GetName() != name ){
-		return false;
-	}
-	return compiler.GetNamespaceSupporter().IsSameAreaNamespace( this->namespaceScopes, namespaceScopes );
-}
-bool ConstBase::IsEqualSymbol( const string &fullName ) const
-{
-	char AreaName[VN_SIZE] = "";		//オブジェクト変数
-	char NestName[VN_SIZE] = "";		//入れ子メンバ
-	bool isNest = CClass::SplitName( fullName.c_str(), AreaName, NestName );
-
-	return IsEqualSymbol( NamespaceScopes( AreaName ), NestName );
-}
-
-
-
-
-CConst::CConst( const NamespaceScopes &namespaceScopes, const string &name, const Type &newType, _int64 i64data)
-	: ConstBase( namespaceScopes, name )
-	, type( newType )
-	, i64data( i64data )
-	, pNext( NULL )
-{
-}
-CConst::CConst( const NamespaceScopes &namespaceScopes, const string &name, int value)
-	: ConstBase( namespaceScopes, name )
-	, type( Type(DEF_LONG) )
-	, i64data( value )
-	, pNext( NULL )
-{
-}
-CConst::~CConst(){
-	//連結先のオブジェクトを破棄
-	if(pNext){
-		delete pNext;
-		pNext = 0;
-	}
-}
-
-Type CConst::GetType(){
-	return type;
-}
-_int64 CConst::GetWholeData(){
-	return i64data;
-}
+
+
 double CConst::GetDoubleData(){
 	double dbl;
@@ -63,51 +10,9 @@
 }
 
-
-
-CConstMacro::CConstMacro( const NamespaceScopes &namespaceScopes, const string &name, char *Expression)
-	: ConstBase( namespaceScopes, name )
-{
-}
-CConstMacro::~CConstMacro(){
-}
-
-
-
-
-
-CDBConst::CDBConst(){
-	memset(this,0,sizeof(CDBConst));
-
-	ppHash = (CConst **)calloc(MAX_HASH*sizeof(CConst *),1);
-	Init();
-}
-CDBConst::~CDBConst(){
-	Free();
-
-	free(ppHash);
-}
-
-//解放
-void CDBConst::Free(){
+void AddConst( const NamespaceScopes &namespaceScopes, char *buffer ){
 	int i;
-	for(i=0; i<MAX_HASH; i++){
-		if(ppHash[i]){
-			delete ppHash[i];
-			ppHash[i] = 0;
-		}
-	}
-}
-
-//初期化
-void CDBConst::Init(){
-	Free();
-}
-
-void AddConstData(char *Command);
-void CDBConst::Add( const NamespaceScopes &namespaceScopes, char *buffer ){
-	int i;
 
 	//名前を取得
-	char Name[VN_SIZE];
+	char name[VN_SIZE];
 	for(i=0;;i++){
 		if(buffer[i]=='\0'){
@@ -116,13 +21,15 @@
 		}
 		if(buffer[i]=='='||buffer[i]=='('){
-			Name[i]=0;
+			name[i]=0;
 			break;
 		}
-		Name[i]=buffer[i];
+		name[i]=buffer[i];
 	}
 
 	//重複チェック
-	if(GetBasicType(Name)){
-		SetError(15,Name,cp);
+	if( compiler.GetMeta().GetGlobalConstMacros().IsExist( name )
+		|| compiler.GetMeta().GetGlobalConsts().IsExist( name ) )
+	{
+		SetError(15,name,cp);
 		return;
 	}
@@ -131,32 +38,16 @@
 		//定数マクロ
 
-		//未完成
-		AddConstData(buffer);
+		compiler.GetMeta().GetGlobalConstMacros().Add( namespaceScopes, name, buffer + i );
 	}
 	else{
 		//一般の定数
-		char *Expression = buffer + i + 1;
-
-		AddConst( namespaceScopes, Name,Expression);
-	}
-}
-
-void CDBConst::AddConst( const string &name, CConst *newconst){
-	int key = hash_default(name.c_str());
-
-	//ハッシュリストに追加
-	if(ppHash[key]){
-		CConst *pConst = ppHash[key];
-		while(pConst->pNext){
-			pConst = pConst->pNext;
-		}
-
-		pConst->pNext = newconst;
-	}
-	else{
-		ppHash[key] = newconst;
-	}
-}
-void CDBConst::AddConst( const NamespaceScopes &namespaceScopes, const string &name , char *Expression){
+		char *expression = buffer + i + 1;
+
+		compiler.GetMeta().GetGlobalConsts().Add( namespaceScopes, name, expression );
+	}
+}
+
+void Consts::Add( const NamespaceScopes &namespaceScopes, const string &name , char *Expression)
+{
 	_int64 i64data;
 	Type resultType;
@@ -172,28 +63,27 @@
 	CConst *newconst = new CConst(namespaceScopes, name, resultType, i64data);
 
-	AddConst( name, newconst);
-}
-void CDBConst::AddConst(const NamespaceScopes &namespaceScopes, const string &name, int value){
+	//ハッシュリストに追加
+	Put( newconst );
+}
+void Consts::Add(const NamespaceScopes &namespaceScopes, const string &name, int value){
 	CConst *newconst = new CConst( namespaceScopes, name, value);
 
-	AddConst(name, newconst);
-}
-
-CConst *CDBConst::GetObjectPtr( const string &name ){
+	//ハッシュリストに追加
+	Put( newconst );
+}
+
+CConst *Consts::GetObjectPtr( const string &name ){
 	char ObjName[VN_SIZE];		//オブジェクト変数
 	char NestMember[VN_SIZE];	//入れ子メンバ
-	bool isObjectMember = CClass::SplitName( name.c_str(), ObjName, NestMember );
-
-	//ハッシュ値を取得
-	int key;
-	key=hash_default( NestMember );
-
-	//格納位置を取得
-	CConst *pConst;
-	pConst=ppHash[key];
-	while(pConst){
-		if( pConst->IsEqualSymbol( name ) ) break;
-
-		pConst=pConst->pNext;
+	bool isObjectMember = SplitMemberName( name.c_str(), ObjName, NestMember );
+
+	CConst *pConst = GetHashArrayElement( NestMember );
+	while( pConst )
+	{
+		if( pConst->IsEqualSymbol( name ) )
+		{
+			break;
+		}
+		pConst = pConst->GetChainNext();
 	}
 
@@ -202,5 +92,5 @@
 
 
-int CDBConst::GetBasicType(char *Name){
+int Consts::GetBasicType(char *Name){
 	CConst *pConst = GetObjectPtr(Name);
 
@@ -209,5 +99,5 @@
 	return pConst->GetType().GetBasicType();
 }
-_int64 CDBConst::GetWholeData(char *Name){
+_int64 Consts::GetWholeData(char *Name){
 	CConst *pConst = GetObjectPtr(Name);
 
@@ -216,5 +106,5 @@
 	return pConst->GetWholeData();
 }
-double CDBConst::GetDoubleData(char *Name){
+double Consts::GetDoubleData(char *Name){
 	CConst *pConst = GetObjectPtr(Name);
 
@@ -223,5 +113,5 @@
 	return pConst->GetDoubleData();
 }
-bool CDBConst::IsStringPtr( char *Name ){
+bool Consts::IsStringPtr( char *Name ){
 	CConst *pConst = GetObjectPtr(Name);
 
@@ -232,2 +122,140 @@
 	return ( type.GetBasicType() == typeOfPtrChar && type.GetIndex() == LITERAL_STRING );
 }
+
+
+bool ConstMacro::GetCalcBuffer( const char *parameterStr, char *dest ) const
+{
+	extern HANDLE hHeap;
+	int i2,i3,i4,num;
+	char temporary[VN_SIZE];
+	char *pParms[MAX_PARMS];
+	num=0;
+	i2=0;
+	while(1){
+		i2=GetOneParameter(parameterStr,i2,temporary);
+
+		pParms[num]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
+		lstrcpy(pParms[num],temporary);
+
+		num++;
+		if(parameterStr[i2]=='\0') break;
+	}
+	if( num != this->GetParameters().size() ){
+		extern int cp;
+		for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
+		SetError(10,GetName().c_str(),cp);
+		lstrcpy(dest,"0");
+		return 1;
+	}
+
+	i2=0;
+	i4=0;
+	while(1){
+
+		//数式内の項を取得
+		for(i3=0;;i2++,i3++){
+			if(!IsVariableChar( this->GetExpression()[i2] )){
+				temporary[i3]=0;
+				break;
+			}
+			temporary[i3] = this->GetExpression()[i2];
+		}
+
+		//パラメータと照合する
+		for( i3=0; i3<(int)this->GetParameters().size(); i3++ ){
+			if( this->GetParameters()[i3] == temporary ) break;
+		}
+
+		if( i3 == (int)this->GetParameters().size() ){
+			//パラメータでないとき
+			lstrcpy(dest+i4,temporary);
+			i4+=lstrlen(temporary);
+		}
+		else{
+			//パラメータのとき
+			lstrcpy(dest+i4,pParms[i3]);
+			i4+=lstrlen(pParms[i3]);
+		}
+
+		//演算子をコピー
+		for(;;i2++,i4++){
+			if( this->GetExpression()[i2] == 1 ){
+				dest[i4++] = this->GetExpression()[i2++];
+				dest[i4] = this->GetExpression()[i2];
+				continue;
+			}
+			if(IsVariableTopChar( this->GetExpression()[i2] )) break;
+			dest[i4] = this->GetExpression()[i2];
+			if( this->GetExpression()[i2] == '\0' ) break;
+		}
+
+		if( this->GetExpression()[i2] == '\0' ) break;
+	}
+
+	for(i2=0;i2<num;i2++) HeapDefaultFree(pParms[i2]);
+
+	return 1;
+}
+
+// マクロ定数を追加するための関数
+void ConstMacros::Add( const NamespaceScopes &namespaceScopes, const std::string &name, const char *parameterStr )
+{
+	std::vector<std::string> parameters;
+
+	int i = 0;
+	if(parameterStr[i]!='(')
+	{
+		SetError();
+		return;
+	}
+
+	char temporary[VN_SIZE];
+	int i2;
+	for(i++,i2=0;;i++,i2++){
+		if(parameterStr[i]=='\0'){
+			SetError(1,NULL,cp);
+			return;
+		}
+		if(parameterStr[i]==','||parameterStr[i]==')'){
+			temporary[i2]=0;
+
+			parameters.push_back( temporary );
+
+			if(parameterStr[i]==')'){
+				i++;
+				if(parameterStr[i]!='='){
+					extern int cp;
+					SetError(1,NULL,cp);
+					return;
+				}
+				break;
+			}
+
+			i2=-1;
+			continue;
+		}
+		temporary[i2]=parameterStr[i];
+	}
+
+	//データ
+	lstrcpy(temporary,parameterStr+i+1);
+
+	Put( new ConstMacro( namespaceScopes, name, parameters, temporary ) );
+}
+ConstMacro *ConstMacros::Find( const std::string &name ){
+	char ObjName[VN_SIZE];		//オブジェクト変数
+	char NestMember[VN_SIZE];	//入れ子メンバ
+	bool isObjectMember = SplitMemberName( name.c_str(), ObjName, NestMember );
+
+	ConstMacro *pConstMacro = GetHashArrayElement( NestMember );
+	while( pConstMacro )
+	{
+		if( pConstMacro->IsEqualSymbol( name ) )
+		{
+			break;
+		}
+		pConstMacro = pConstMacro->GetChainNext();
+	}
+
+	return pConstMacro;
+}
