示例#1
0
        private void Print(int a, int b, operationDelegate operation)
        {
            int result = operation(a, b);

            Console.WriteLine($"Imprmiendo : {result}");
        }
示例#2
0
		public Operation setOperation(operationDelegate operation)
		{
			this.operation = operation;
			return this;
		}
示例#3
0
        public static IValue ParseValue(string text, int sectionCharIndex, ParsingStatus status)
        {
            if (text.Length != 0)
            {
                // check if this expression is an atomic expression
                var value = AtomicExpressionValue.ParseValue(text, sectionCharIndex, status);
                if (value != null)
                {
                    return(value);
                }

                // if the expression is not an atomic one, it must be longer than 1 character
                if (text.Length == 1)
                {
                    return(null);
                }

                // check if this expression is an atomic expression preceded by a numeric sign
                if (text[0] == '+' || text[0] == '-')
                {
                    value = AtomicExpressionValue.ParseValue(text.Substring(1), sectionCharIndex + 1, status);
                    if (value != null)
                    {
                        return(text[0] == '-' ? new NumberValue(value.GetSign(status.LocationCounter).Invert(), value.GetMagnitude(status.LocationCounter)) : value);
                    }
                }

                // check if this expression contains one or more binary operations
                if (text.Length >= 3)
                {
                    int               operatorPosition = -1;
                    string            operatorText     = null;
                    operationDelegate operatorDelegate = null;

                    // find the last operator included the expression, if any
                    foreach (KeyValuePair <string, operationDelegate> pair in mBinaryOperations)
                    {
                        string currentOperator = pair.Key;

                        var operatorIndex = text.LastIndexOf(currentOperator, text.Length - 2, text.Length - 2, StringComparison.Ordinal);
                        // this find only counts if it is closer to the end of the expression than the previous find
                        if (operatorIndex > (operatorPosition + 1))
                        {
                            operatorPosition = operatorIndex;
                            operatorText     = currentOperator;
                            operatorDelegate = pair.Value;
                        }
                    }

                    if (operatorPosition == -1)
                    {
                        return(null);
                    }

                    int rightTermStartIndex = operatorPosition + operatorText.Length;
                    // the left term can itself be an expression, so parse it as one (recursively)
                    var left = ParseValue(text.Substring(0, operatorPosition), sectionCharIndex, status);
                    // the right term must be an atomic expression
                    var right = AtomicExpressionValue.ParseValue(text.Substring(rightTermStartIndex), sectionCharIndex + rightTermStartIndex, status);

                    if (left != null && right != null)
                    {
                        // both terms were successfully parsed. Perform the operation and return the result
                        return(operatorDelegate(left, right, status.LocationCounter));
                    }
                }
            }
            return(null);
        }