ASSIGN
FIXunknown
c_cpp
4 years ago
9.8 kB
10
Indexable
void SymbolTable::KiemThu(string Temp, string TypeNameFunction, int CurrentScope, int TheIndexmost, int &Counting, int &Position)
{
string THeIndentiferName;
string THEIndentifierTmp;
int IndexPointing = 0;
int PositionsOfFun[100];
int Theindexof
// int TheFirstpos
= TypeNameFunction.find('(');
THeIndentiferName = TypeNameFunction.substr(0, Theindexof);
THEIndentifierTmp = TypeNameFunction.substr(Theindexof);
Data TheFunctionof = searching(THeIndentiferName, CurrentScope, TheIndexmost, Counting);
Position = TheIndexmost;
if (TheFunctionof.IndentifierName == "Empty")
{
throw Undeclared(THeIndentiferName);
}
if (TheFunctionof.TheList == "")
throw TypeMismatch(Temp);
if (TheFunctionof.TypeOf != "")
{
if (TheFunctionof.TheList.size() == 1)
{
if (THEIndentifierTmp.size() > 2)
throw TypeMismatch(Temp);
}
else
{
for (char i : THEIndentifierTmp)
{
if (i == ',')
{
IndexPointing++;
}
}
if (IndexPointing + 2 != (int)TheFunctionof.TheList.size())
throw TypeMismatch(Temp);
IndexPointing = 0;
for (int i = 0; i < (int)THEIndentifierTmp.size(); ++i)
{
if (THEIndentifierTmp[i] == '(' || THEIndentifierTmp[i] == ')' || THEIndentifierTmp[i] == ',')
{
PositionsOfFun[IndexPointing] = i;
IndexPointing++;
}
}
int BienDem = 0;
for (int i = 0; i < IndexPointing - 1; ++i)
{
string Subtitute = THEIndentifierTmp.substr(PositionsOfFun[i] + 1, PositionsOfFun[i + 1] - PositionsOfFun[i] - 1);
if (Subtitute[0] >= '0' && Subtitute[0] <= '9')
{
if (TheFunctionof.TheList[BienDem] != 'N')
throw TypeMismatch(Temp);
}
else if (Subtitute[0] == '\'')
{
if (TheFunctionof.TheList[BienDem] != 'S')
throw TypeMismatch(Temp);
}
else
{
Data THEIndentifierTmp = searching(Subtitute, CurrentScope, TheIndexmost, Counting);
if (THEIndentifierTmp.IndentifierName == "Empty")
throw Undeclared(Subtitute);
if (THEIndentifierTmp.TheList != "")
throw TypeMismatch(Temp);
if (THEIndentifierTmp.TypeOf == "number" && TheFunctionof.TheList[BienDem] == 'S')
throw TypeMismatch(Temp);
if (THEIndentifierTmp.TypeOf == "string" && TheFunctionof.TheList[BienDem] == 'N')
throw TypeMismatch(Temp);
if (THEIndentifierTmp.TypeOf == "")
throw TypeCannotBeInferred(Temp);
}
++BienDem;
}
}
}
else
{
if (TheFunctionof.TheList.size() == 1)
{
if (THEIndentifierTmp.size() > 2)
throw TypeMismatch(Temp);
}
else
{
for (char i : THEIndentifierTmp)
{
if (i == ',')
{
IndexPointing++;
}
}
if ((int)((IndexPointing + 2)*7) != (int)TheFunctionof.TheList.size())
throw TypeMismatch(Temp);
IndexPointing = 0;
for (int i = 0; i < (int)THEIndentifierTmp.size(); ++i)
{
if (THEIndentifierTmp[i] == '(' || THEIndentifierTmp[i] == ')' || THEIndentifierTmp[i] == ',')
{
PositionsOfFun[IndexPointing] = i;
IndexPointing++;
}
}
int BienDemMot = 0;
for (int i = 0; i < IndexPointing - 1; ++i)
{
string Subtitute = THEIndentifierTmp.substr(PositionsOfFun[i] + 1, PositionsOfFun[i + 1] - PositionsOfFun[i] - 1);
if (Subtitute[0] >= '0' && Subtitute[0] <= '9')
{
hashtable.Array[TheIndexmost].TheList[BienDemMot] = 'N';
}
else if (Subtitute[0] == '\'')
{
hashtable.Array[TheIndexmost].TheList[BienDemMot] = 'S';
}
else
{
Data THEIndentifierTmp = searching(Subtitute, CurrentScope, TheIndexmost, Counting);
if (THEIndentifierTmp.IndentifierName == "Empty")
throw Undeclared(Subtitute);
if (THEIndentifierTmp.TheList != "")
throw TypeMismatch(Temp);
// if (THEIndentifierTmp.TypeOf == "")
// throw TypeCannotBeInferred(Temp);
else
{
if (THEIndentifierTmp.TypeOf == "string")
{
hashtable.Array[TheIndexmost].TheList[BienDemMot] = 'S';
}
else
hashtable.Array[TheIndexmost].TheList[BienDemMot] = 'N';
}
}
BienDemMot++;
}
}
}
}
Data SymbolTable::searching(string IndentifierName, int CurrentScope, int Position, int Counting)
{
while (CurrentScope >= 0)
{
Counting = 0;
string encodeName1 = to_string(CurrentScope);
for (int i : IndentifierName)
encodeName1 += to_string(i - 48);
//element.SolveCode = encodeName1;
string Subnet = encodeName1;
//(IndentifierName, CurrentScope);
for (auto i = 0; i < hashtable.SizeOf; i++)
{
auto k = hashtable.Hash(Subnet, i, Const_c1, Const_c2, SizeTable_m);
if (hashtable.Array[k].SolveCode == Subnet && hashtable.Level[k] == 1)
{
Position = k;
return hashtable.Array[k];
}
Counting++;
}
CurrentScope--;
}
return Data();
}
else if (regex_match(Temp, CheckAssignTheFunction))
{
/* cout << "Func" << endl;
int SizeOf = Temp.size();
string TempSubAssign = Temp.substr(7, SizeOf);
int Space_Position = TempSubAssign.find(" ") + 7;
IndentifierName = Temp.substr(7, Space_Position - 7);
TypeName = Temp.substr(Space_Position + 1, (SizeOf - Space_Position)); */
if(KeyOrNot(Temp))
throw InvalidInstruction(Temp);
string IndentifierNameFunction = "";
string TypeNameFunction = "";
int SizeOfName = Temp.size();
string TempSubAssign = Temp.substr(7, SizeOfName);
int Space_Position = TempSubAssign.find(" ") + 7;
IndentifierNameFunction = Temp.substr(7, Space_Position - 7);
TypeNameFunction = Temp.substr(Space_Position + 1, (SizeOfName - Space_Position));
//string TypeNameFunction, IndentifierNameFunction;
int Counting = 0;
int PositionOfVar = 0;
int Position_fake = 0;
int Position = 0;
Data TheTempOf = searching(IndentifierNameFunction, CurrentScope, Position, Counting);
int TheIndexmost = 0;
/////////////////////////////////////////////////////////
KiemThu(Temp, TypeNameFunction, CurrentScope, Position_fake, Counting, Position);
//////////////////////////////////////////////////////
Data CheckingIndex = searching(IndentifierNameFunction, CurrentScope, PositionOfVar, Counting);
if (CheckingIndex.IndentifierName == "Empty")
{
throw Undeclared(IndentifierNameFunction);
}
if (CheckingIndex.TheList != "")
{
throw TypeMismatch(Temp);
}
if (hashtable.Array[PositionOfVar].TypeOf == "" && hashtable.Array[Position].TypeOf == "")
{
throw TypeCannotBeInferred(Temp);
}
if (hashtable.Array[PositionOfVar].TypeOf == "" && hashtable.Array[Position].TypeOf == "void")
{
throw TypeMismatch(Temp);
}
if (hashtable.Array[PositionOfVar].TypeOf != "" && hashtable.Array[Position].TypeOf != "")
{
if (hashtable.Array[PositionOfVar].TypeOf != hashtable.Array[Position].TypeOf)
throw TypeMismatch(Temp);
}
if (hashtable.Array[PositionOfVar].TypeOf == "" && hashtable.Array[Position].TypeOf != "")
{
hashtable.Array[PositionOfVar].TypeOf = hashtable.Array[Position].TypeOf;
}
if (hashtable.Array[PositionOfVar].TypeOf != "" && hashtable.Array[Position].TypeOf == "")
{
hashtable.Array[Position].TypeOf = hashtable.Array[PositionOfVar].TypeOf;
}
cout << Counting << endl;
}Editor is loading...