private void CheckForEnumMembers(BfsExpGroup group, IBfsDataBlock block)
{
//For each expression variable in the expression group
for (int index = 0; index < group.Members.Count; index++)
{
//Recursively visit the sub-expressions
IBfsExpNode node = group.Members[index];
if (node is BfsExpGroup)
{
CheckForEnumMembers(node as BfsExpGroup, block);
continue;
}
//Ignore irrelevant objects (operators and known variables)
if (!(node is BfsExpressionVariable))
continue;
BfsExpressionVariable expressionvar = node as BfsExpressionVariable;
if (!(expressionvar.LastField is BfsExpressionUnknown))
continue;
//Only interested in resolving the BfsExpressionUnknowns
BfsExpressionUnknown unknown = expressionvar.LastField as BfsExpressionUnknown;
BfsExpressionVariable candidatevar = null;
bool resolvedTheVar = false;
BfsOperator op = null;
if (index >= 2 && group.Members[index - 2] is BfsExpressionVariable)
{
op = group.Members[index - 1] as BfsOperator;
candidatevar = group.Members[index - 2] as BfsExpressionVariable;
}
else if (index + 2 < group.Members.Count && group.Members[index + 2] is BfsExpressionVariable)
{
op = group.Members[index + 1] as BfsOperator;
candidatevar = group.Members[index + 2] as BfsExpressionVariable;
}
if ( candidatevar != null
&& candidatevar.LastField is BfsStructField
&& (candidatevar.LastField as BfsStructField).FieldType is BfsNamedType)
{
BfsStructField field = candidatevar.LastField as BfsStructField;
BfsNamedType fieldtype = field.FieldType as BfsNamedType;
if (fieldtype.DataBlock is BfsEnum && (fieldtype.DataBlock as BfsEnum).EnumAliases.ContainsKey(unknown.Name))
{
resolvedTheVar = true;
BfsEnumFieldAlias enumalias = (fieldtype.DataBlock as BfsEnum).EnumAliases[unknown.Name];
BfsEnumAliasExp aliasExp = new BfsEnumAliasExp();
aliasExp.EnumBlock = fieldtype.DataBlock as BfsEnum;
aliasExp.Alias = enumalias;
aliasExp.SourceRange = unknown.SourceRange;
//Only allow equality operators on EnumAliases
if (op.Operator == "==" || op.Operator == "!=")
{
group.Members.Insert(index, aliasExp);
group.Members.Remove(node);
}
else
BfsCompiler.ReportError(op.SourceRange,
"The operator '"+op.Operator+"' cannot be used with the enum alias '"+enumalias.Name+"'. Only == and != are allowed.");
expressionvar.LastField = aliasExp;
}
}
if(!resolvedTheVar)
BfsCompiler.ReportError(unknown.SourceRange,
"Unresolved variable name: '" + unknown.Name + "'");
}
}