private List <String> PromoteIntegers(List <String> Operands)
        {
            if (!EXEExecutionGlobals.AllowPromotionOfIntegerToReal)
            {
                return(Operands);
            }

            bool ContainsReal    = false;
            bool ContainsInteger = false;

            foreach (String Operand in Operands)
            {
                ContainsReal    |= EXETypes.RealTypeName.Equals(EXETypes.DetermineVariableType("", Operand));
                ContainsInteger |= EXETypes.IntegerTypeName.Equals(EXETypes.DetermineVariableType("", Operand));

                if (ContainsReal && ContainsInteger)
                {
                    break;
                }
            }

            if (!ContainsReal || !ContainsInteger)
            {
                return(Operands);
            }

            return(Operands.Select(x => EXETypes.AdjustAssignedValue(EXETypes.RealTypeName, x)).ToList());
        }
        public override Boolean Execute(OALProgram OALProgram, EXEScope Scope)
        {
            //Console.WriteLine("Assignment is being executed");
            Boolean Result = false;

            String AssignedValue = this.AssignedExpression.Evaluate(Scope, OALProgram.ExecutionSpace);

            if (AssignedValue == null)
            {
                return(Result);
            }

            // If we are assigning to a variable
            if (this.AttributeName == null)
            {
                //Console.WriteLine("Assigning value: " + AssignedValue + " to variable " + this.VariableName);

                EXEPrimitiveVariable Variable = Scope.FindPrimitiveVariableByName(this.VariableName);
                // If the variable doesnt exist, we simply create it
                if (Variable == null)
                {
                    //Console.WriteLine("Creating new var " + this.VariableName);
                    Result = Scope.AddVariable(new EXEPrimitiveVariable(this.VariableName, AssignedValue));
                }
                //If variable exists and its type is UNDEFINED
                else if (EXETypes.UnitializedName.Equals(Variable.Type))
                {
                    //Console.WriteLine("Assigning to unitialized var" + this.VariableName);
                    Result = Variable.AssignValue(Variable.Name, AssignedValue);
                }
                // If the variable exists and is primitive
                else if (!EXETypes.ReferenceTypeName.Equals(Variable.Type))
                {
                    //Console.WriteLine("Assigning to initialized var" + this.VariableName);
                    // If the types don't match, this fails and returns false
                    AssignedValue = EXETypes.AdjustAssignedValue(Variable.Type, AssignedValue);
                    Result        = Variable.AssignValue("", AssignedValue);
                }

                // Variable exists and is not primitive. What to do, what to do?
                // We do nothing, we CANNOT ASSIGN TO HANDLES!!!
            }
            // We are assigning to an attribute of a variable
            else
            {
                EXEReferenceEvaluator RefEvaluator = new EXEReferenceEvaluator();
                Result = RefEvaluator.SetAttributeValue(this.VariableName, this.AttributeName, Scope, OALProgram.ExecutionSpace, AssignedValue);
                //Console.WriteLine("Tried to assign " + AssignedValue + " to " + this.VariableName + "." + this.AttributeName);
            }

            //Console.WriteLine("Assignment Result: " + Result);
            return(Result);
        }
Beispiel #3
0
        //SetUloh1
        // Similar as task above, but this time we set the attribute value to "NewValue" parameter
        // But it's not that easy, you need to check if attribute type and NewValue type are the same (e.g. both are integer)
        // To do that, you need to find the referencing variable's class (via Scope) and then the attribute's type (vie ExecutionSpace)
        // When you know the type of attribute, use EXETypes.IsValidValue to see if you can or cannot assign that value to that attribute
        // You assign it in Scope
        // Return if you could assign it or not
        // EXETypes.determineVariableType()
        public Boolean SetAttributeValue(String ReferencingVariableName, String AttributeName, EXEScope Scope, CDClassPool ExecutionSpace, String NewValue)
        {
            EXEReferencingVariable ReferencingVariable = Scope.FindReferencingVariableByName(ReferencingVariableName);

            if (ReferencingVariable == null)
            {
                return(false);
            }

            CDClassInstance ClassInstance = ExecutionSpace.GetClassInstanceById(ReferencingVariable.ClassName, ReferencingVariable.ReferencedInstanceId);

            if (ClassInstance == null)
            {
                return(false);
            }

            CDClass Class = ExecutionSpace.getClassByName(ReferencingVariable.ClassName);

            if (Class == null)
            {
                return(false);
            }

            CDAttribute Attribute = Class.GetAttributeByName(AttributeName);

            if (Attribute == null)
            {
                return(false);
            }

            String NewValueType = EXETypes.DetermineVariableType(null, NewValue);

            if (!EXETypes.CanBeAssignedToAttribute(AttributeName, Attribute.Type, NewValueType))
            {
                return(false);
            }

            ClassInstance.SetAttribute(AttributeName, EXETypes.AdjustAssignedValue(Attribute.Type, NewValue));

            return(true);
        }
        // SetUloh1
        // Here you get operator and operands, and you need to return the result. Check the unit tests to see what this is about
        public String Evaluate(String Operator, List <String> InOperands)
        {
            List <String> Operands = PromoteIntegers(InOperands);

            if (!this.CanBeEvaluated(Operator, Operands))
            {
                return(null);
            }

            //Console.WriteLine("Can be evaluated");

            //get variable type
            String VariableType = EXETypes.DetermineVariableType(null, Operands[0]);


            switch (Operator)
            {
            case "+":     //int, real, string
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //add numbers in list and return result
                    return(IntList.Aggregate((a, x) => a + x).ToString());
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //add numbers in list and return result
                    return(EXETypes.AdjustAssignedValue(EXETypes.RealTypeName, DoubleList.Aggregate((a, x) => a + x).ToString()));
                }
                if (String.Equals(EXETypes.StringTypeName, VariableType))
                {
                    //add string in list and return result
                    //remove \" from Operands
                    List <String> StringList = Operands.Select(s => s.Replace("\"", "")).ToList();

                    //add strings in list and return result
                    return(CreateEXETypeString(StringList.Aggregate((a, x) => a + x).ToString()));
                }
                break;

            case "-":     //int, real
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //sub numbers in list and return result
                    return(IntList.Aggregate((a, x) => a - x).ToString());
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //sub numbers in list and return result
                    return(EXETypes.AdjustAssignedValue(EXETypes.RealTypeName, DoubleList.Aggregate((a, x) => a - x).ToString()));
                }
                break;

            case "*":     //int, real
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //multiply numbers in list and return result
                    return(IntList.Aggregate((a, x) => a * x).ToString());
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //multiply numbers in list and return result
                    return(EXETypes.AdjustAssignedValue(EXETypes.RealTypeName, DoubleList.Aggregate((a, x) => a * x).ToString()));
                }

                break;

            case "/":     //int, real
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    for (int i = 1; i < IntList.Count; i++)
                    {
                        if (IntList[i] == 0)
                        {
                            return(null);
                        }
                    }
                    //divide numbers in list and return result
                    return(IntList.Aggregate((a, x) => a / x).ToString());
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                            for (int i = 1; i < DoubleList.Count; i++)
                            {
                                if (DoubleList[i] == 0)
                                {
                                    return(EXETypes.BooleanFalse);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //divide numbers in list and return result
                    return(EXETypes.AdjustAssignedValue(EXETypes.RealTypeName, DoubleList.Aggregate((a, x) => a / x).ToString()));
                }

                break;

            case "%":     //int, real
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    for (int i = 1; i < IntList.Count; i++)
                    {
                        if (IntList[i] == 0)
                        {
                            return(null);
                        }
                    }
                    //divide numbers in list and return result
                    return(IntList.Aggregate((a, x) => a % x).ToString());
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                            for (int i = 1; i < DoubleList.Count; i++)
                            {
                                if (DoubleList[i] == 0)
                                {
                                    return(EXETypes.BooleanFalse);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //divide numbers in list and return result
                    return(EXETypes.AdjustAssignedValue(EXETypes.RealTypeName, DoubleList.Aggregate((a, x) => a % x).ToString()));
                }

                break;

            case "==":     //int, real, string, booelan, unique ID
                //return bool
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //compare 2 numbers in list (equals) and return result
                    return(int.Equals(IntList[0], IntList[1]) ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //compare 2 numbers in list (equals) and return result
                    return(Double.Equals(DoubleList[0], DoubleList[1]) ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                if (String.Equals(EXETypes.StringTypeName, VariableType))
                {
                    //compare 2 string in list (equals) and return result
                    return(String.Equals(Operands[0], Operands[1]) ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }

                //only 2 operands
                if (String.Equals(EXETypes.BooleanTypeName, VariableType))
                {
                    //convert to list of int
                    List <bool> BooleanList = Operands.Select(bool.Parse).ToList();
                    //compare 2 boolean in list (equals) and return result
                    return(Boolean.Equals(BooleanList[0], BooleanList[1]) ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                break;

            case "!=":     //int, real, string, booelan, unique ID
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //compare 2 numbers in list (equals) and return result
                    return(!int.Equals(IntList[0], IntList[1]) ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //compare 2 numbers in list (equals) and return result
                    return(!Double.Equals(DoubleList[0], DoubleList[1]) ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                if (String.Equals(EXETypes.StringTypeName, VariableType))
                {
                    //compare 2 string in list (equals) and return result
                    return(!String.Equals(Operands[0], Operands[1]) ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                //true if Operands are boolean
                if (String.Equals(EXETypes.BooleanTypeName, VariableType))
                {
                    //convert to list of boolean
                    List <bool> BooleanList = Operands.Select(bool.Parse).ToList();
                    //compare 2 boolean in list (not equals) and return result
                    return(!Boolean.Equals(BooleanList[0], BooleanList[1]) ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                break;

            //only 2 operands
            case "<":     //int, real, string
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //compare numbers in list (less then) and return result
                    return(IntList[0] < IntList[1] ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //compare numbers in list (less then) and return result
                    return(DoubleList[0] < DoubleList[1] ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                if (String.Equals(EXETypes.StringTypeName, VariableType))
                {
                    //compare 2 strings in list (less then) and return result
                    return(String.Compare(Operands[0], Operands[1]) < 0 ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                break;

            case ">":     //int, real, string
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //compare numbers in list (greater then) and return result
                    return(IntList[0] > IntList[1] ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //compare numbers in list (greater then) and return result
                    return(DoubleList[0] > DoubleList[1] ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                if (String.Equals(EXETypes.StringTypeName, VariableType))
                {
                    //add string in list and return result
                    return(String.Compare(Operands[0], Operands[1]) > 0 ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                break;

            case "<=":     //int, real, string
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //compare numbers in list (less then or equals to) and return result
                    return(IntList[0] <= IntList[1] ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //compare numbers in list (less then or equals to) and return result
                    return(DoubleList[0] <= DoubleList[1] ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                if (String.Equals(EXETypes.StringTypeName, VariableType))
                {
                    //compare strings in list (less then or equals to) and return result
                    return(String.Compare(Operands[0], Operands[1]) <= 0 ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                break;

            case ">=":     //int, real, string
                //true if Operands are integers
                if (String.Equals(EXETypes.IntegerTypeName, VariableType))
                {
                    //convert to list of int
                    List <int> IntList = Operands.Select(int.Parse).ToList();
                    //compare numbers in list (greater then or equals to) and return result
                    return(IntList[0] >= IntList[1] ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                //true if Operands are real numbers
                if (String.Equals(EXETypes.RealTypeName, VariableType))
                {
                    //convert to list of real numbers
                    // List<double> DoubleList = Operands.Select(double.Parse.).ToList();
                    List <decimal> DoubleList = new List <decimal>();
                    foreach (String Operand in Operands)
                    {
                        try
                        {
                            DoubleList.Add(decimal.Parse(Operand, CultureInfo.InvariantCulture));
                        }
                        catch (Exception e)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    //compare numbers in list (greater then or equals to) and return result
                    return(DoubleList[0] >= DoubleList[1] ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                if (String.Equals(EXETypes.StringTypeName, VariableType))
                {
                    //compare numbers in list (greater then or equals to) and return result
                    return(String.Compare(Operands[0], Operands[1]) >= 0 ? EXETypes.BooleanTrue : EXETypes.BooleanFalse);
                }
                break;

            case "and":     //only boolean
                //true if Operands are boolean
                if (String.Equals(EXETypes.BooleanTypeName, VariableType))
                {
                    //convert to list of int
                    List <bool> BooleanList = Operands.Select(bool.Parse).ToList();
                    foreach (bool bool_var in BooleanList)
                    {
                        if (!bool_var)
                        {
                            return(EXETypes.BooleanFalse);
                        }
                    }
                    return(EXETypes.BooleanTrue);
                }
                break;

            case "or":     //only boolean
                //true if Operands are boolean
                if (String.Equals(EXETypes.BooleanTypeName, VariableType))
                {
                    //convert to list of int
                    List <bool> BooleanList = Operands.Select(bool.Parse).ToList();
                    foreach (bool bool_var in BooleanList)
                    {
                        if (bool_var)
                        {
                            return(EXETypes.BooleanTrue);
                        }
                    }
                    return(EXETypes.BooleanFalse);
                }
                break;

            //only 1 operand
            case "not":     //only boolean
                //true if Operands are boolean
                if (String.Equals(EXETypes.BooleanTypeName, VariableType))
                {
                    //return TRUE or FALSE
                    return(String.Equals(Operands[0], EXETypes.BooleanTrue) ? EXETypes.BooleanFalse : EXETypes.BooleanTrue);
                }
                break;
            }

            return(null);
        }