Esempio n. 1
0
 public override void Validate(out ParserError error)
 {
     error = ParserError.Empty;
 }
Esempio n. 2
0
 public abstract void Validate(out ParserError error);
Esempio n. 3
0
 public abstract object GetValue(Dictionary <GridCoordinates, object> childCells, out ParserError error);
Esempio n. 4
0
        private LinkedListNode <ILexem> HandleBinarOperation(LinkedListNode <ILexem> initNode, out ParserError error)
        {
            var curOpToken = initNode.Value as OperationToken;

            var prevNode = initNode.Previous;
            var nextNode = initNode.Next;

            if (prevNode == null)
            {
                error = new ParserError($"Can't find left argument of {curOpToken.ActualValue} at: {curOpToken.Position}");
                return(initNode);
            }

            if (nextNode == null)
            {
                error = new ParserError($"Can't find right argument of {curOpToken.ActualValue} at: {curOpToken.Position}");
                return(initNode);
            }

            if (!(prevNode.Value is OpRand leftOpRand))
            {
                error = new ParserError($"Wrong left operand of {curOpToken.ActualValue} at: {curOpToken.Position}");
                return(initNode);
            }

            if (!(nextNode.Value is OpRand rightOpRand))
            {
                error = new ParserError($"Wrong right operand of {curOpToken.ActualValue} at: {curOpToken.Position}");
                return(initNode);
            }

            prevNode.List.Remove(prevNode);
            nextNode.List.Remove(nextNode);

            return(Replace(initNode, new BinarOp(curOpToken.Value, leftOpRand, rightOpRand, out error)));
        }
Esempio n. 5
0
        public override object GetValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
        {
            try {
                error = new ParserError("Something went wrong in expression");

                if (expressionComponents.First.Value is OpRand opRand)
                {
                    return(opRand.GetValue(childCells, out error));
                }

                if (expressionComponents.First.Value is StringToken strToken)
                {
                    error = ParserError.Empty;
                    return(strToken.Value);
                }

                return(null);
            } catch {
                error = new ParserError("Wrong expression");
                return(null);
            }
        }
Esempio n. 6
0
 public override void Validate(out ParserError error)
 {
     Argument.Validate(out error);
 }
Esempio n. 7
0
        private object GetDivideValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
        {
            error = ParserError.Empty;
            if (leftValue is Decimal leftNum && rightValue is Decimal rightNum)
            {
                if (rightNum == 0)
                {
                    error = new ParserError("Omg, it was so predictable...\nBut it's just an error\n Infinities are bad");
                    return(null);
                }

                return(leftNum / rightNum);
            }

            error = new ParserError($"{OpName} operation must have number type arguments:\n" +
                                    $" {leftValue.GetType().ToString()} and {rightValue.GetType().ToString()} were found.");
            return(null);
        }
Esempio n. 8
0
        private LinkedListNode <ILexem> HandleSubExpression(LinkedListNode <ILexem> lParNode, LinkedListNode <ILexem> rParNode, out ParserError error)
        {
            Debug.Assert(lParNode != null && rParNode != null);
            Debug.Assert(lParNode.Value is SeparatorToken lSep && lSep.Value == SeparatorToken.Separator.LParen);
            Debug.Assert(rParNode.Value is SeparatorToken rSep && rSep.Value == SeparatorToken.Separator.RParen);
            Debug.Assert(lParNode.List == rParNode.List);

            var tokens        = lParNode.List;
            var subExprLexems = new LinkedList <ILexem>();

            var curNode = lParNode.Next;

            while (curNode != rParNode)
            {
                var nxtNode = curNode.Next;
                subExprLexems.AddLast(curNode.Value);

                tokens.Remove(curNode);
                curNode = nxtNode;
            }

            curNode = tokens.AddBefore(rParNode, new Expression(subExprLexems, out error));
            tokens.Remove(lParNode);
            tokens.Remove(rParNode);

            return(curNode);
        }
Esempio n. 9
0
        private LinkedListNode <ILexem> HandleStringTerm(LinkedListNode <ILexem> initNode, out ParserError error)
        {
            error = ParserError.Empty;
            var posibleError = new ParserError($"Unexpected token: \" at {((Token)initNode.Value).Position}");

            var tokens  = initNode.List;
            var strNode = initNode.Next;

            if (strNode == null)
            {
                error = posibleError;
                return(initNode);
            }

            if (!(strNode.Value is StringToken strToken))
            {
                error = posibleError;
                return(initNode);
            }

            tokens.Remove(initNode);
            strNode = Replace(strNode, new Term(strToken));

            var closeQuote = strNode.Next;

            if (closeQuote == null || !IsQuote(closeQuote.Value))
            {
                error = posibleError;
                return(initNode);
            }

            tokens.Remove(closeQuote);
            return(strNode);
        }
Esempio n. 10
0
        public BinarOp(OperationToken.Operation opName, OpRand leftOpRand, OpRand rightOpRand, out ParserError error)
        {
            error = ParserError.Empty;

            this.OpName      = opName;
            this.LeftOpRand  = leftOpRand;
            this.RightOpRand = rightOpRand;
        }
Esempio n. 11
0
        private object GetLessEqValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
        {
            error = ParserError.Empty;

            if (leftValue is bool || rightValue is bool)
            {
                error = new ParserError($"{OpName} operation chant have bool arguments");
            }

            if (leftValue is string leftStr && rightValue is string rightStr)
            {
                return(leftStr.Length <= rightStr.Length);
            }

            if (leftValue is string leftStr1 && rightValue is Decimal rightNum1)
            {
                return(leftStr1.Length <= rightNum1);
            }

            if (leftValue is Decimal leftNum && rightValue is Decimal rightNum)
            {
                return(leftNum <= rightNum);
            }

            error = new ParserError($"{OpName} operation has wrong argument types");
            return(null);
        }
Esempio n. 12
0
 private object GetEqualValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
 {
     error = ParserError.Empty;
     return(leftValue.Equals(rightValue));
 }
Esempio n. 13
0
        public override object GetValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
        {
            leftValue = LeftOpRand.GetValue(childCells, out error);
            if (!error.IsEmpty)
            {
                return(null);
            }

            rightValue = RightOpRand.GetValue(childCells, out error);
            if (!error.IsEmpty)
            {
                return(null);
            }

            try {
                switch (OpName)
                {
                case OperationToken.Operation.Plus: return(GetPlusValue(childCells, out error));

                case OperationToken.Operation.Minus: return(GetMinusValue(childCells, out error));

                case OperationToken.Operation.Mult: return(GetMultValue(childCells, out error));

                case OperationToken.Operation.Divide: return(GetDivideValue(childCells, out error));

                case OperationToken.Operation.Mod: return(GetModValue(childCells, out error));

                case OperationToken.Operation.Div: return(GetDivValue(childCells, out error));

                case OperationToken.Operation.More: return(GetMoreValue(childCells, out error));

                case OperationToken.Operation.MoreEq: return(GetMoreEqValue(childCells, out error));

                case OperationToken.Operation.Less: return(GetLessValue(childCells, out error));

                case OperationToken.Operation.LessEq: return(GetLessEqValue(childCells, out error));

                case OperationToken.Operation.Equal: return(GetEqualValue(childCells, out error));

                case OperationToken.Operation.UnEqual: return(GetUnEqualValue(childCells, out error));

                default: {
                    error = new ParserError($"Strange binar operation{OpName.ToString()}");
                    return(null);
                }
                }
            } catch (Exception e) {
                error = new ParserError(e.Message);
                return(null);
            }
        }
Esempio n. 14
0
        private object GetMinusValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
        {
            error = ParserError.Empty;
            if (leftValue is Decimal leftNum && rightValue is Decimal rightNum)
            {
                return(leftNum - rightNum);
            }

            error = new ParserError($"{OpName} operation must have number type arguments:\n" +
                                    $" {leftValue.GetType().ToString()} and {rightValue.GetType().ToString()} were found.");
            return(null);
        }
Esempio n. 15
0
        public override object GetValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
        {
            error = ParserError.Empty;
            if (IsLogic)
            {
                return(AsLogic);
            }
            if (IsString)
            {
                return(AsString);
            }
            if (IsNumber)
            {
                return(AsNumber);
            }

            if (Value is CellNameToken cellName)
            {
                if (!childCells.ContainsKey(cellName.Value))
                {
                    (string x, string y) = cellName.Value.GetStringCoords();
                    error = new ParserError($"Don't know the value of {x + y} cell.");
                    return(null);
                }

                return(childCells[cellName.Value]);
            }

            error = new ParserError($"Strange term at {Value.Position}");
            return(null);
        }
Esempio n. 16
0
        private LinkedListNode <ILexem> HandleIfOperation(LinkedListNode <ILexem> curNode, out ParserError error)
        {
            var argumentNode = curNode.Next;

            if (argumentNode == null)
            {
                error = new ParserError($"Can't find IF arguments at: {((Token)curNode.Value).Position}");
                return(curNode);
            }

            if (!(argumentNode.Value is Expression argExpr))
            {
                error = new ParserError($"IF must has arguments at: {((Token)curNode.Value).Position}");
                return(curNode);
            }

            curNode.List.Remove(curNode);

            var oprands = new OpRandsList(argExpr, out error);

            if (!error.IsEmpty)
            {
                return(argumentNode);
            }

            return(Replace(argumentNode, new UnarOp(OperationToken.Operation.If, oprands, out error)));
        }
Esempio n. 17
0
        private object GetMinusValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
        {
            object argValue = Argument.GetValue(childCells, out error);

            if (!error.IsEmpty)
            {
                return(null);
            }

            if (argValue is Decimal decValue)
            {
                return(-decValue);
            }

            error = new ParserError($"Unar {OpName.ToString()} operation has wrong argument type");
            return(null);
        }
Esempio n. 18
0
        private LinkedListNode <ILexem> HandleUnarOperation(LinkedListNode <ILexem> curNode, out ParserError error)
        {
            var currentOperation = ((OperationToken)curNode.Value).Value;

            var nextNode = curNode.Next;

            if (nextNode == null)
            {
                error = new ParserError($"Can't find unar plus' argument at: {((Token)curNode.Value).Position}");
                return(curNode);
            }

            if (curNode.Previous != null && curNode.Previous.Value is OpRand)
            {
                error = ParserError.Empty;
                return(curNode);
            }

            curNode.List.Remove(curNode);
            var nextOpRand = nextNode.Value as OpRand;

            if (nextOpRand == null)
            {
                error = new ParserError($"Can't find Unar {currentOperation.ToString()} argument");
                return(curNode);
            }

            return(Replace(nextNode, new UnarOp(currentOperation, nextOpRand, out error)));
        }
Esempio n. 19
0
        public override object GetValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
        {
            try {
                switch (OpName)
                {
                case OperationToken.Operation.If: return(GetIfValue(childCells, out error));

                case OperationToken.Operation.Plus: return(GetPlusValue(childCells, out error));

                case OperationToken.Operation.Minus: return(GetMinusValue(childCells, out error));

                case OperationToken.Operation.Not: return(GetNotValue(childCells, out error));

                default: {
                    error = new ParserError($"Unexpected UnarOperation: {OpName.ToString()}");
                    return(null);
                }
                }
            } catch (Exception e) {
                error = new ParserError(e.Message);
                return(null);
            }
        }
Esempio n. 20
0
 public override object GetValue(Dictionary <GridCoordinates, object> childCells, out ParserError error)
 {
     error = new ParserError("Can't find value of OperandList");
     return(null);
 }