本文整理汇总了C#中Group.IndexOf方法的典型用法代码示例。如果您正苦于以下问题:C# Group.IndexOf方法的具体用法?C# Group.IndexOf怎么用?C# Group.IndexOf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Group
的用法示例。
在下文中一共展示了Group.IndexOf方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseIfGroup
/// <summary>
/// Parses an if statement.
/// </summary>
/// <param name="group">The group to parse.</param>
/// <returns>
/// If a statement or block of code is executed, then the result of that code is returned, otherwise the
/// boolean result of the logical expression is returned. A TException is returned when there is an error.
/// </returns>
TType ParseIfGroup(Group group)
{
/* BNF for if statement:
* <if-statement> ::= 'if' <condition> ',' ( <statements> | <block-no-end> 'else' <statements> )
* <block-no-end> ::= <new-line> (<statement> <new-line>)*
* The 'if' is assumed to have already been checked for
*/
if (group.Count < 2)
return new TException(this, "Statement could not be evaluated",
"if statement must be given a condition");
int commaIndex = group.IndexOf(",");
if (commaIndex < 0)
return new TException(this, "if statment invalid", "comma required after condition");
Group conditionGroup = new Group(null);
conditionGroup.AddRange(group.GetRange(1, commaIndex - 1));
TType value = ParseGroup(conditionGroup);
if (value is TException) return value;
TBoolean result = value as TBoolean;
if (result == null)
{
bool success = false;
TVariable variable = value as TVariable;
if (variable != null)
{
result = variable.Value as TBoolean;
if (result != null) success = true;
}
if (!success)
return new TException(this, "Condition does not evaluate to a boolean value", "yes or no");
}
if (group.Count > commaIndex + 1) // If there is no block after the 'if' <condition> ','
{
// If there is an else and it's at the end of the line, get a block for the else
int elseIndex = group.IndexOf("else", commaIndex + 1);
TBlock elseBlock = null;
if (elseIndex == group.Count - 1)
{
TException exception;
elseBlock = new TBlock(this, out exception, false);
if (exception != null) return exception;
}
if (result.Value)
{
// Execute the code between the comma and the end of the statement or the else (if any).
// If there isn't anything to execute, then a block is created (near the end of this method)
Group statementGroup = new Group(null);
if (elseIndex < 0)
statementGroup.AddRange(group.GetRange(commaIndex + 1, group.Count - (commaIndex + 1)));
else
statementGroup.AddRange(group.GetRange(commaIndex + 1, elseIndex - (commaIndex + 1)));
if (statementGroup.Count > 0) return ParseGroup(statementGroup);
}
else if (elseIndex >= 0)
{
if (elseBlock == null)
{
Group statementGroup = new Group(null);
statementGroup.AddRange(group.GetRange(elseIndex + 1, group.Count - (elseIndex + 1)));
if (statementGroup.Count > 0) return ParseGroup(statementGroup);
}
else
{
bool exitFromFunction, breakUsed;
return elseBlock.Execute(this, out exitFromFunction, out breakUsed);
}
}
else return result;
}
{ // This code is executed if there is a block after the 'if' <condition> ',' instead of a single statement
TException exception;
TBlock block = new TBlock(this, out exception, true);
if (exception != null) return exception;
bool exitFromFunction, breakUsed;
if (result.Value) return block.Execute(this, out exitFromFunction, out breakUsed, true);
if (block.HasElse()) return block.Execute(this, out exitFromFunction, out breakUsed, false);
}
return result;
}
示例2: ParseCommasOfGroup
/// <summary>
/// Parses the commas in the group, converting a comma separated list into a TArgumentList, and replacing the
/// list in the group with it.
/// </summary>
/// <param name="group">The group to parse.</param>
/// <returns>A TException on failure, otherwise null.</returns>
TException ParseCommasOfGroup(Group group)
{
int index;
while ((index = group.IndexOf(",")) >= 0)
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term ','");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TArgumentList argList = a as TArgumentList;
if (argList == null)
{
argList = new TArgumentList();
argList.Add(a);
argList.Add(b);
}
else argList.Add(b);
group[index - 1] = argList;
group.RemoveRange(index, 2);
}
return null;
}
示例3: ParseComparisonsOfGroup
/// <summary>
/// Parses the logical comparisons of the group. Replaces the operators and operands in the group with their
/// results.
/// </summary>
/// <param name="group">The group to parse.</param>
/// <returns>A TException on failure, otherwise null.</returns>
TException ParseComparisonsOfGroup(Group group)
{
int index;
while ((index = group.IndexOf("=")) >= 0)
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term '='");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TType result = Operations.Equal(this, a, b, true);
if (result == null) return new TException(this, "Comparison operation failed", "reason unknown");
if (result is TException) return result as TException;
group[index - 1] = result;
group.RemoveRange(index, 2);
}
while ((index = group.IndexOf("~=")) >= 0) // Approximately equal (by rounding or within ~0.5)
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term '~='");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TType result = Operations.Equal(this, a, b, false);
if (result is TException) return result as TException;
group[index - 1] = result;
group.RemoveRange(index, 2);
}
while ((index = group.IndexOf("/=")) >= 0) // Not equal
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term '/='");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TType result = Operations.NotEqual(this, a, b);
if (result == null) return new TException(this, "Comparison operation failed", "reason unknown");
if (result is TException) return result as TException;
group[index - 1] = result;
group.RemoveRange(index, 2);
}
while ((index = group.IndexOf("<")) >= 0)
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term '<'");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TType result = Operations.Math.Inequality(this, a, b, "<");
if (result == null) return new TException(this, "Less than comparison failed", "reason unknown");
if (result is TException) return result as TException;
group[index - 1] = result;
group.RemoveRange(index, 2);
}
while ((index = group.IndexOf(">")) >= 0)
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term '<'");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TType result = Operations.Math.Inequality(this, a, b, ">");
if (result == null)
return new TException(this, "Greater than comparison failed", "reason unknown");
if (result is TException) return result as TException;
group[index - 1] = result;
group.RemoveRange(index, 2);
}
while ((index = group.IndexOf("<=")) >= 0)
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term '<'");
TType a = TType.Parse(this, group[index - 1]);
//.........这里部分代码省略.........
示例4: ParseBidmasOfGroup
/// <summary>
/// Parses all of the arithmetic in the group, according to BIDMAS (although ignoring Brackets, because
/// they're taken care of elsewhere), replacing the operands and operators with the results.
/// </summary>
/// <param name="group">The group to parse.</param>
/// <returns>A TException on failure, otherwise null.</returns>
TException ParseBidmasOfGroup(Group group)
{
// For every while loop, convert the values either side of the operator to the relevant TType, and attempt
// to perform the operation on them. Then replace the operands and operator in the group with the result
int index;
while ((index = group.IndexOf("^")) >= 0) // Indicies
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term '^'");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TType result = Operations.Math.Pow(this, a, b);
if (result == null) return new TException(this, "Exponentiation operation failed", "reason unknown");
if (result is TException) return result as TException;
group[index - 1] = result;
group.RemoveRange(index, 2);
}
while (true) // Division and Multiplication
{
// Find out which operator comes first in order to parse the expression from left to right
int divIndex = group.IndexOf("/"), mulIndex = group.IndexOf("*");
if (divIndex < 0) index = mulIndex;
else if (mulIndex < 0) index = divIndex;
else index = divIndex < mulIndex ? divIndex : mulIndex;
if (index < 0) break;
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term '" + group[index] + "'");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TType result;
if ((string)group[index] == "/")
{
result = Operations.Math.Divide(this, a, b);
if (result == null) return new TException(this, "Division operation failed", "reason unknown");
}
else
{
result = Operations.Math.Multiply(this, a, b);
if (result == null)
return new TException(this, "Multiplication operation failed", "reason unknown");
}
if (result is TException) return result as TException;
group[index - 1] = result;
group.RemoveRange(index, 2);
}
while (true) // Addition and Subtraction
{
int addIndex = group.IndexOf("+"), subIndex = group.IndexOf("-");
if (addIndex < 0) index = subIndex;
else if (subIndex < 0) index = addIndex;
else index = addIndex < subIndex ? addIndex : subIndex;
if (index < 0) break;
char operatorChar = ((string)group[index])[0];
if (index - 1 < 0)
{ // There's no number before the operator, so perform a unary operation
TException exception = ParseUnaryArtitmetic(group, index, operatorChar);
if (exception != null) return exception;
continue;
}
else if (index + 1 >= group.Count)
return new TException(this, "Invalid expression term '" + group[index] + "'");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException)
{
string possibleOperator = group[index - 1] as string;
if (possibleOperator != null)
{
if (RESERVED_SYMBOLS.Contains(possibleOperator))
{
TException exception = ParseUnaryArtitmetic(group, index, operatorChar);
if (exception != null) return exception;
continue;
}
}
return a as TException;
}
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
//.........这里部分代码省略.........
示例5: ParseAssignmentGroup
/// <summary>
/// Parses an assignment operation.
/// </summary>
/// <param name="group">The group to parse.</param>
/// <returns>The assigned variable or function on success, otherwise a TException.</returns>
TType ParseAssignmentGroup(Group group)
{
/* BNF for assignment:
* <assignment> ::= 'let' ( <variable-assignment> | <function-declaration> )
* <var-assignment> ::= <identifier> '=' <expression>
* <func-declaration> ::= <identifier> '(' { <parameters> } ')' '=' <statements>
* <parameters> ::= <identifier> { ',' <identifier> }*
* <statements> ::= <block> | <statement>
* <block> ::= <new-line> (<statement> <new-line>)* 'end'
* You can probably guess what <identifier>, <statement> and <new-line> are
* The 'let' is assumed to have already been checked for
*/
int equalsIndex = group.IndexOf("=");
if (equalsIndex < 0) return new TException(this, "Variable or function could not be assigned a value");
// Could be assigning a dereferenced variable
TException exception = ParseReferencesOfGroup(group, equalsIndex);
if (exception != null) return exception;
if (group.Count == 1) return new TException(this, "Could not assign variable", "no variable name given");
string variableName = group[1] as string;
TVariable existingVariable = null;
if (variableName == null)
{
exception = new TException(this, "Could not assign variable", "invalid variable name given");
// Check if an existing variable or function is being assigned
Group groupToParse = group[1] as Group;
TType value = group[1] as TType;
if (groupToParse != null) value = ParseGroup(groupToParse);
if (value == null) return exception;
TVariable variable = value as TVariable;
if (variable != null) existingVariable = variable;
else
{
TFunction function = value as TFunction;
if (function != null) variableName = function.Name;
else return exception;
}
}
if (group.Count == 2) return new TException(this, "Variable could not be assigned a value");
string assignmentOperator = group[2] as string;
if (assignmentOperator == null) // Now we assume that we're dealing with a function declaration
{
Group paramGroup = group[2] as Group;
if (paramGroup == null) // The user probably just wanted to assign a variable but made a typo
return new TException(this, "Variable could not be assigned a value",
"value to assign to variable must be given");
// Get the identifiers of all the parameters within the brackets, keeping strict watch on comma usage
TParameterList paramList = new TParameterList();
bool commaExpected = false;
for (int i = 0; i < paramGroup.Count; ++i)
{
if (commaExpected && (i == paramGroup.Count - 1))
return new TException(this, "Parameters could not be parsed", "last parameter missing");
string paramName = paramGroup[i] as string;
if (commaExpected && (paramName != ",")) paramName = null;
if (paramName == null) return new TException(this, "Parameters could not be parsed",
"invalid parameter name given");
if (!commaExpected) paramList.Add(paramName);
commaExpected = !commaExpected;
}
exception = new TException(this, "Function could not be given a body", "function body must be given");
if (group.Count == 3) return exception;
assignmentOperator = group[3] as string;
if (assignmentOperator == null) return exception;
else if (assignmentOperator != "=") return exception;
TFunction function;
if (group.Count == 4) // statement is just 'let <i><params> =', so get a block
{
TBlock block = new TBlock(this, out exception, false);
if (exception != null) return exception;
function = new TFunction(variableName ?? existingVariable.Identifier, block,
paramList.ParameterNames, null);
}
else // Create a single line function
{
Group funcBody = new Group(null);
funcBody.AddRange(group.GetRange(4, group.Count - 4));
function = new TFunction(variableName ?? existingVariable.Identifier, funcBody.ToString(),
paramList.ParameterNames, null);
}
exception = TFunction.AddFunction(this, function);
//.........这里部分代码省略.........
示例6: ParseWhileGroup
/// <summary>
/// Parses a while loop statement.
/// </summary>
/// <param name="group">The group to parse.</param>
/// <returns>TNil on success, otherwise a TException.</returns>
TType ParseWhileGroup(Group group)
{
/* BNF for while loop:
* <while-loop> ::= 'while' <condition> ',' ( <statement> | <block> )
* The 'while' is assumed to have already been checked for
*/
if (group.Count == 1) return new TException(this, "Statement could not be evaluated",
"while loop must be given a condition");
int commaIndex = group.IndexOf(",");
if (commaIndex < 0) return new TException(this, "while loop invalid", "comma required after condition");
if (group.Count == 1)
return new TException(this,
"Statement could not be evaluated", "while loop must be given a condition");
Group conditionGroup = new Group(null);
conditionGroup.AddRange(group.GetRange(1, commaIndex - 1));
Group statementGroup = null;
TBlock block = null;
if (group.Count > commaIndex + 1) // If there is a statement after the comma, use that statement
{
statementGroup = new Group(null);
statementGroup.AddRange(group.GetRange(commaIndex + 1, group.Count - (commaIndex + 1)));
}
else // Otherwise get a block
{
TException exception;
block = new TBlock(this, out exception, false);
if (exception != null) return exception;
}
TType returnValue = TNil.Instance;
while (true)
{
// Parse the condition, and if it's true run the block or single statement, otherwise return MNil
TType value = ParseGroup((Group)conditionGroup.Clone());
if (value is TException) return value;
TBoolean result = value as TBoolean;
if (result == null)
{
bool success = false;
TVariable variable = value as TVariable;
if (variable != null)
{
result = variable.Value as TBoolean;
if (result != null) success = true;
}
if (!success)
return new TException(this, "Condition does not evaluate to a boolean value", "yes or no");
}
if (result.Value)
{
bool breakUsed = false;
if (statementGroup != null) returnValue = ParseGroup((Group)statementGroup.Clone());
else
{
bool exitFromFunction;
returnValue = block.Execute(this, out exitFromFunction, out breakUsed);
if (exitFromFunction) return returnValue;
}
if ((returnValue is TException) || breakUsed) return returnValue;
}
else return returnValue;
if (!alive) return TNil.Instance;
}
}
示例7: ParseModulusBracketsOfGroup
/// <summary>
/// Parses a group, searching for any modulus brackets and replacing the modulus brackets and their contents
/// with the absolute values.
/// </summary>
/// <param name="group">The group to parse.</param>
/// <returns>A TException on failure, otherwise null.</returns>
TException ParseModulusBracketsOfGroup(Group group)
{
int index;
while ((index = group.IndexOf(TType.MODULUS_CHARACTER.ToString())) >= 0)
{
TException exception = new TException(this, "Modulus brackets not closed", "another | required");
if (index + 2 >= group.Count) return exception;
if (group[index + 2] is string)
{
if ((string)group[index + 2] != TType.MODULUS_CHARACTER.ToString()) return exception;
}
else return exception;
TType value = TType.Parse(this, group[index + 1]);
if (value is TException) return value as TException;
TType result = Operations.Math.Modulus(this, value);
if (result == null) return new TException(this, "Modulus operation failed", "reason unknown");
if (result is TException) return result as TException;
group[index] = result;
group.RemoveRange(index + 1, 2);
}
return null;
}
示例8: ParseLogicalOperatorsOfGroup
/// <summary>
/// Parses the logical operators of the group. Replaces the operators and operands with their results.
/// </summary>
/// <param name="group">The group to parse.</param>
/// <returns>A TException on failure, otherwise null.</returns>
TException ParseLogicalOperatorsOfGroup(Group group)
{
int index;
while ((index = group.IndexOf("and")) >= 0)
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term ','");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TBoolean aBool = a as TBoolean;
if (aBool == null)
return new TException(this, "Left hand side of expression must be a boolean value", "yes or no");
TBoolean bBool = b as TBoolean;
if (bBool == null)
return new TException(this, "Right hand side of expression must be a boolean value", "yes or no");
group[index - 1] = new TBoolean(aBool.Value && bBool.Value);
group.RemoveRange(index, 2);
}
while ((index = group.IndexOf("or")) >= 0)
{
if ((index - 1 < 0) || (index + 1 >= group.Count))
return new TException(this, "Invalid expression term ','");
TType a = TType.Parse(this, group[index - 1]);
if (a is TException) return a as TException;
TType b = TType.Parse(this, group[index + 1]);
if (b is TException) return b as TException;
TBoolean aBool = a as TBoolean;
if (aBool == null)
return new TException(this, "Left hand side of expression must be a boolean value", "yes or no");
TBoolean bBool = b as TBoolean;
if (bBool == null)
return new TException(this, "Right hand side of expression must be a boolean value", "yes or no");
group[index - 1] = new TBoolean(aBool.Value || bBool.Value);
group.RemoveRange(index, 2);
}
return null;
}