Esempio n. 1
0
        public bool makeComparison(int lineNumber, bool doChangeValue = true)
        {
            if (counterVariable.variableType != VariableTypes.number)
            {
                ErrorMessage.sendErrorMessage(lineNumber, ErrorType.ForLoop, ForLoopErrorType.counterVariableIsNotNumber.ToString(), new string[1] {
                    counterVariable.name
                });
            }

            if (doChangeValue)
            {
                counterVariable.setValue(counterVariable.getNumber() + incrementValue);
            }
            bool doLoop = ComparisonOperatorParser.makeComparison(counterVariable, base.rightValue, base.theComparisonType, lineNumber);


            getTargetScope().scopeVariables.addVariable(counterVariable, getTargetScope().scopeParser, lineNumber);
            if (doLoop == false && doChangeValue)
            {
                counterVariable.setValue(endValue - incrementValue);
            }

            return(doLoop);
        }
        public static bool parseExpression(Logic[] logicOrder, int lineNumber, Scope currentScope)
        {
            #region findOperators
            int operatorLow    = -1;
            int operatorHigh   = -1;
            int operatorAmount = 0;
            for (int i = 0; i < logicOrder.Length; i++)
            {
                if (isStatementOperator(logicOrder [i]))
                {
                    if (operatorLow == -1)
                    {
                        operatorLow = i;
                    }
                    else
                    {
                        operatorHigh = i;
                    }
                    operatorAmount++;
                }
            }


            #endregion


            if (operatorAmount > 2 && operatorHigh - operatorLow != 1)
            {
                ErrorMessage.sendErrorMessage(lineNumber, "I detta moment är det inte tillåtet att gör flera jämförelser direkt efter varandra. Använd \"and\" och \"or\" istället.");
            }
            if (operatorAmount == 0)
            {
                return(handleOnlyValueStatement(logicOrder, lineNumber, currentScope));
            }

            if (operatorAmount == 2)
            {
                if (operatorHigh - operatorLow != 1)
                {
                    ErrorMessage.sendErrorMessage(lineNumber, "Operatorerna måste komma direkt efter varandra");
                }
            }
            else
            {
                operatorHigh = operatorLow;
            }



            Logic[] leftSide  = new Logic[operatorLow];
            Logic[] rightSide = new Logic[logicOrder.Length - 1 - operatorHigh];
            Logic[] operators = new Logic[operatorAmount];
            setSidesOfStatement(logicOrder, leftSide, rightSide, operators, operatorLow, operatorHigh);


            Variable       firstSum     = SumParser.parseIntoSum(leftSide, lineNumber, currentScope);
            Variable       secondSum    = SumParser.parseIntoSum(rightSide, lineNumber, currentScope);
            ComparisonType operatorType = ComparisonOperatorParser.parseOperators(operators);

            if (operatorType == ComparisonType.unknown)
            {
                if (operators[0].currentType == WordTypes.equalSign)
                {
                    ErrorMessage.sendErrorMessage(lineNumber, "Fel vid tilldelning. Kom ihåg att använda == om du vill jämföra om två värden är lika.");
                }
                else
                {
                    ErrorMessage.sendErrorMessage(lineNumber, "Operatorerna går inte att tyda");
                }
            }

            if (firstSum.variableType == VariableTypes.unknown || secondSum.variableType == VariableTypes.unknown)
            {
                ErrorMessage.sendErrorMessage(lineNumber, "Korrupt värde");
            }

            if (firstSum.variableType != secondSum.variableType)
            {
                var firstSumType  = SumParser.TypeToString(firstSum.variableType);
                var secondSumType = SumParser.TypeToString(secondSum.variableType);

                string errorMessage;

                if (firstSumType == "" || secondSumType == "")
                {
                    errorMessage = "Misslyckades med att jämföra " + firstSum.variableType + " med " + secondSum.variableType;
                }
                else
                {
                    errorMessage = "Kan inte jämföra " + firstSumType + " med " + secondSumType + ".";
                }

                ErrorMessage.sendErrorMessage(lineNumber, errorMessage);
            }

            if (ComparisonOperatorParser.checkSumsToOperator(firstSum.variableType, operatorType, lineNumber))
            {
                bool resultOfComparison = ComparisonOperatorParser.makeComparison(firstSum, secondSum, operatorType, lineNumber);
                return(resultOfComparison);
            }

            ErrorMessage.sendErrorMessage(lineNumber, "Korrupt expression!");
            return(false);
        }