Пример #1
0
        /// <summary>
        /// Calculate an algorithmic expression
        /// </summary>
        /// <param name="_nodeExpression">Node containing the algorithmic expression</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Result of the calculation</returns>
        public static int?CalculExpression(Node _nodeExpression, Subprogram _parentSubprogram)
        {
            int?value = 0;

            for (int i = 0; i < _nodeExpression.GetChildCount(); i++)
            {
                switch (_nodeExpression.GetChildAt(i).GetId())
                {
                case (int)KropConstants.TERM:
                    Console.WriteLine(_nodeExpression.GetParent().GetChildAt(0));
                    if (_nodeExpression.GetParent().GetChildAt(0).GetId() == (int)KropConstants.SUB)
                    {
                        value -= CalculTerm(_nodeExpression.GetChildAt(i), _parentSubprogram);
                    }
                    else
                    {
                        value += CalculTerm(_nodeExpression.GetChildAt(i), _parentSubprogram);
                    }
                    break;

                case (int)KropConstants.EXPRESSION_REST:
                    value += CalculExpressionRest(_nodeExpression.GetChildAt(i), _parentSubprogram);
                    break;
                }
            }
            return(value);
        }
Пример #2
0
        /// <summary>
        /// Calculate a string expression
        /// </summary>
        /// <param name="_nodeExpression">Node containing the string expression</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Result of the expression</returns>
        public static string CalculStringExpression(Node _nodeExpression, Subprogram _parentSubprogram)
        {
            string value = "";

            for (int i = 0; i < _nodeExpression.GetChildCount(); i++)
            {
                switch (_nodeExpression.GetChildAt(i).GetId())
                {
                case (int)KropConstants.STRING_VALUE:
                    Console.WriteLine(_nodeExpression.GetParent().GetChildAt(0));
                    value += GetStringValue(_nodeExpression.GetChildAt(i), _parentSubprogram);
                    break;

                case (int)KropConstants.WORD:
                    Token token = (Token)_nodeExpression.GetChildAt(i);
                    value += _parentSubprogram.GetVar(token.GetImage(), _parentSubprogram).ToString();
                    break;

                case (int)KropConstants.STRING_EXPRESSION_REST:
                    value += CalculStringExpressionRest(_nodeExpression.GetChildAt(i), _parentSubprogram);
                    break;
                }
            }

            return(value);
        }
Пример #3
0
        /// <summary>
        /// Calculate the termRest of a term
        /// </summary>
        /// <param name="_value">Value of the term</param>
        /// <param name="_nodeTermRest">Node containing the termRest</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Result of the calculation</returns>
        private static int?CalculTermRest(int?_value, Node _nodeTermRest, Subprogram _parentSubprogram)
        {
            int?value = _value;

            for (int i = 0; i < _nodeTermRest.GetChildCount(); i++)
            {
                if (_nodeTermRest.GetChildAt(i).GetId() == (int)KropConstants.TERM)
                {
                    for (int y = 0; y < _nodeTermRest.GetChildAt(i).GetChildCount(); y++)
                    {
                        switch (_nodeTermRest.GetChildAt(i).GetChildAt(y).GetId())
                        {
                        case (int)KropConstants.FACTOR:
                            if (_nodeTermRest.GetChildAt(0).GetId() == (int)KropConstants.MUL)
                            {
                                value *= CalculFactor(_nodeTermRest.GetChildAt(i).GetChildAt(y), _parentSubprogram);
                            }
                            else
                            {
                                value /= CalculFactor(_nodeTermRest.GetChildAt(i).GetChildAt(y), _parentSubprogram);
                            }
                            break;

                        case (int)KropConstants.TERM_REST:
                            value = CalculTermRest(value, _nodeTermRest.GetChildAt(i).GetChildAt(y), _parentSubprogram);
                            break;
                        }
                    }
                }
            }

            return(value);
        }
Пример #4
0
        public Subprogram(Node _nodeProgram, Subprogram _subprogramSubprogram)
        {
            Prg              = new List <Executable>();
            ListVar          = new List <IVariable>();
            ParentSubprogram = _subprogramSubprogram;

            BuildExecutionTree(_nodeProgram);
        }
Пример #5
0
        private List <Executable> Prg;      // List of objects

        public Subprogram(Node _nodeProgram)
        {
            Prg              = new List <Executable>();
            ListVar          = new List <IVariable>();
            ParentSubprogram = null;

            this.ListVar.Add(new IntVar("antspeed", 1));

            bool b = VarExists("AntSpeed", this);

            BuildExecutionTree(_nodeProgram);
        }
Пример #6
0
        /// <summary>
        /// Check if the variable already exists
        /// </summary>
        /// <param name="_varName">Variable Name</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Result</returns>
        public static bool VarExists(string _varName, Subprogram _parentSubprogram)
        {
            foreach (IVariable var in _parentSubprogram.ListVar)
            {
                if (var.GetName() == _varName)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #7
0
        /// <summary>
        /// Calculate the stringExpressionRest of an stringExpression
        /// </summary>
        /// <param name="_nodeExpressionRest">Node containing the stringExpressionRest</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Result of the calculation</returns>
        private static string CalculStringExpressionRest(Node _nodeExpressionRest, Subprogram _parentSubprogram)
        {
            string value = "";

            for (int i = 0; i < _nodeExpressionRest.GetChildCount(); i++)
            {
                switch (_nodeExpressionRest.GetChildAt(i).GetId())
                {
                case (int)KropConstants.STRING_EXPRESSION:
                    value = CalculStringExpression(_nodeExpressionRest.GetChildAt(i), _parentSubprogram);
                    break;
                }
            }

            return(value);
        }
Пример #8
0
        /// <summary>
        /// Return the variable
        /// </summary>
        /// <param name="_varName">Variable Name</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Variable</returns>
        public IVariable GetVar(string _varName, Subprogram _parentSubprogram)
        {
            foreach (IVariable var in _parentSubprogram.ListVar)
            {
                if (var.GetName() == _varName)
                {
                    return(var);
                }
            }

            if (_parentSubprogram.ParentSubprogram != null)
            {
                return(GetVar(_varName, _parentSubprogram.ParentSubprogram));
            }
            else
            {
                return(null);
            }
        }
Пример #9
0
        /// <summary>
        /// Return the variable string
        /// </summary>
        /// <param name="_varName">Variable Name</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Variable string</returns>
        public static string VarToString(string _varName, Subprogram _parentSubprogram)
        {
            foreach (IVariable var in _parentSubprogram.ListVar)
            {
                if (var.GetName() == _varName)
                {
                    return(var.ToString());
                }
            }

            if (_parentSubprogram.ParentSubprogram != null)
            {
                return(VarToString(_varName, _parentSubprogram.ParentSubprogram));
            }
            else
            {
                return(null);
            }
        }
Пример #10
0
        /// <summary>
        /// Return the value of the term factor
        /// </summary>
        /// <param name="_nodeFactor">Node containing the factor</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Factor value</returns>
        private static int?CalculFactor(Node _nodeFactor, Subprogram _parentSubprogram)
        {
            int?value = 0;

            for (int i = 0; i < _nodeFactor.GetChildCount(); i++)
            {
                switch (_nodeFactor.GetChildAt(i).GetId())
                {
                case (int)KropConstants.ATOM:
                    value = CalcultAtom(_nodeFactor.GetChildAt(i), _parentSubprogram);
                    break;

                case (int)KropConstants.EXPRESSION:
                    value = CalculExpression(_nodeFactor.GetChildAt(i), _parentSubprogram);
                    break;
                }
            }

            return(value);
        }
Пример #11
0
        /// <summary>
        /// Calculate the term of an algorithmic expression
        /// </summary>
        /// <param name="_nodeTerm">Node containing the term</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Result of the calculation</returns>
        private static int?CalculTerm(Node _nodeTerm, Subprogram _parentSubprogram)
        {
            int?value = 0;

            for (int i = 0; i < _nodeTerm.GetChildCount(); i++)
            {
                switch (_nodeTerm.GetChildAt(i).GetId())
                {
                case (int)KropConstants.FACTOR:
                    value = CalculFactor(_nodeTerm.GetChildAt(i), _parentSubprogram);
                    break;

                case (int)KropConstants.TERM_REST:
                    value = CalculTermRest(value, _nodeTerm.GetChildAt(i), _parentSubprogram);
                    break;
                }
            }

            return(value);
        }
Пример #12
0
        /// <summary>
        /// Return the value of a String variable
        /// </summary>
        /// <param name="_varName">Variable Name</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Int variable value</returns>
        public static string GetStringVarValue(string _varName, Subprogram _parentSubprogram)
        {
            foreach (IVariable var in _parentSubprogram.ListVar)
            {
                if (var is StringVar stringVar)
                {
                    if (stringVar.GetName() == _varName)
                    {
                        return(stringVar.GetValue());
                    }
                }
            }

            if (_parentSubprogram.ParentSubprogram != null)
            {
                return(GetStringVarValue(_varName, _parentSubprogram.ParentSubprogram));
            }
            else
            {
                return(null);
            }
        }
Пример #13
0
        /// <summary>
        /// Return the value of the factor atom
        /// </summary>
        /// <param name="_nodeAtom">Node containing the atom</param>
        /// <param name="_parentSubprogram">Parent Subprogram</param>
        /// <returns>Atom value</returns>
        private static int?CalcultAtom(Node _nodeAtom, Subprogram _parentSubprogram)
        {
            Token token = (Token)_nodeAtom.GetChildAt(0);
            int   value;

            switch (_nodeAtom.GetChildAt(0).GetId())
            {
            case (int)KropConstants.SUB:
                token = (Token)_nodeAtom.GetChildAt(1);
                Int32.TryParse(token.GetImage(), out value);
                return(-value);

            case (int)KropConstants.NUMBER:
                Int32.TryParse(token.GetImage(), out value);
                return(value);

            case (int)KropConstants.WORD:
                return(Subprogram.GetIntVarValue(token.GetImage(), _parentSubprogram));

            default:
                return(null);
            }
        }
Пример #14
0
        public static string GetStringValue(Node _nodeStringValue, Subprogram _parentSubprogram)
        {
            string value = "";

            for (int i = 0; i < _nodeStringValue.GetChildCount(); i++)
            {
                switch (_nodeStringValue.GetChildAt(i).GetId())
                {
                case (int)KropConstants.STRING_CHARACTER:
                    Node  _nodeSentence = _nodeStringValue.GetChildAt(i);
                    Token token;

                    for (int j = 0; j < _nodeSentence.GetChildCount(); j++)
                    {
                        switch (_nodeSentence.GetChildAt(j).GetId())
                        {
                        case (int)KropConstants.BACKSLASH_APOSTROPHE:
                            value += '\'';
                            break;

                        case (int)KropConstants.SPECIAL_CHAR:
                            token  = (Token)_nodeSentence.GetChildAt(j).GetChildAt(0);
                            value += token.GetImage();
                            break;

                        default:
                            token  = (Token)_nodeSentence.GetChildAt(j);
                            value += token.GetImage();
                            break;
                        }
                    }
                    break;
                }
            }

            return(value);
        }
Пример #15
0
        /// <summary>
        /// Determine the condition type and create it
        /// </summary>
        /// <param name="_nodeConditionStatement">Node containing the Condition</param>
        /// <returns></returns>
        public static Dictionary <Measurable <Boolean>, string> SetCond(Node _nodeConditionStatement, Subprogram _parentSubprogram)
        {
            bool  isNot = false;
            Token token;

            Dictionary <Measurable <Boolean>, string> condList = new Dictionary <Measurable <bool>, string>();

            for (int i = 0; i < _nodeConditionStatement.GetChildCount(); i++)
            {
                if (_nodeConditionStatement.GetChildAt(i).GetId() == (int)KropConstants.CONDITION_EXPR)
                {
                    if (_nodeConditionStatement.GetChildAt(i).GetChildAt(0).GetId() == (int)KropConstants.NOT)
                    {
                        isNot = true;
                    }

                    for (int y = 0; y < _nodeConditionStatement.GetChildAt(i).GetChildCount(); y++)
                    {
                        switch (_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetId())
                        {
                        case (int)KropConstants.CONDITION_PARAMETER:
                            switch (_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(0).GetId())
                            {
                            case (int)KropConstants.CONDITION:
                                token = (Token)_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(0);
                                condList.Add(new BooleanFunction(token.GetImage(), isNot), "");
                                break;

                            case (int)KropConstants.TRUE:
                                condList.Add(new BooleanVar(true, isNot), "");
                                break;

                            case (int)KropConstants.FALSE:
                                condList.Add(new BooleanVar(false, isNot), "");
                                break;

                            case (int)KropConstants.BOOLEAN_EXPRESSION:
                                condList.Add(new BooleanExpression(_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(0), _parentSubprogram, isNot), "");
                                break;
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }
                else if (_nodeConditionStatement.GetChildAt(i).GetId() == (int)KropConstants.CONDITION_REST)
                {
                    string logicalOperator = "";

                    for (int y = 0; y < _nodeConditionStatement.GetChildAt(i).GetChildCount(); y++)
                    {
                        switch (_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetId())
                        {
                        case (int)KropConstants.LOGICAL_OPERATOR:
                            token           = (Token)_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(0);
                            logicalOperator = token.GetImage();
                            break;

                        case (int)KropConstants.CONDITION_EXPR:

                            if (_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(0).GetId() == (int)KropConstants.NOT)
                            {
                                isNot = true;
                            }

                            for (int j = 0; j < _nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildCount(); j++)
                            {
                                switch (_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(j).GetId())
                                {
                                case (int)KropConstants.CONDITION_PARAMETER:
                                    switch (_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(j).GetChildAt(0).GetId())
                                    {
                                    case (int)KropConstants.CONDITION:
                                        token = (Token)_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(j).GetChildAt(0);
                                        condList.Add(new BooleanFunction(token.GetImage(), isNot), logicalOperator);
                                        break;

                                    case (int)KropConstants.TRUE:
                                        condList.Add(new BooleanVar(true, isNot), logicalOperator);
                                        break;

                                    case (int)KropConstants.FALSE:
                                        condList.Add(new BooleanVar(false, isNot), logicalOperator);
                                        break;

                                    case (int)KropConstants.BOOLEAN_EXPRESSION:
                                        condList.Add(new BooleanExpression(_nodeConditionStatement.GetChildAt(i).GetChildAt(y).GetChildAt(j).GetChildAt(0), _parentSubprogram, isNot), logicalOperator);
                                        break;
                                    }
                                    break;

                                default:
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
            }

            return(condList);
        }