Esempio n. 1
0
        public string Print(IASTNode tree)
        {
            if (tree == null)
            {
                throw new ArgumentNullException("tree", "The argument cannot equal to null");
            }

            return(tree.Accept(this));
        }
Esempio n. 2
0
        public Object VisitIfThenElseNode(IIfThenElseASTNode ifStatementNode)
        {
            if (ifStatementNode == null)
            {
                throw new ArgumentNullException("ifStatementNode", "The argument cannot equal to null");
            }

            if (ifStatementNode.Variable == null)
            {
                throw new ArgumentNullException("ifStatementNode.Variable", "The argument cannot equal to null");
            }

            if (ifStatementNode.Predicate == null)
            {
                throw new ArgumentNullException("ifStatementNode.Predicate", "The argument cannot equal to null");
            }

            Func <int, bool> predicate = (Func <int, bool>)(ifStatementNode.Predicate as IASTNode).Accept(this);

            int[] evaluatedExpr = null;

            IASTNode variableNode = ifStatementNode.Variable;

            if ((variableNode.Type == E_NODE_TYPE.NT_IDENTIFIER) &&
                ((variableNode.Attributes & E_NODE_ATTRIBUTES.NA_LVALUE) == E_NODE_ATTRIBUTES.NA_LVALUE))
            {
                evaluatedExpr = mEnvironment.Get((string)(variableNode).Accept(this)); // variable is lvalue
            }
            else
            {
                evaluatedExpr = (int[])variableNode.Accept(this); // variable is rvalue, replace it with its value
            }

            bool conditionResult = evaluatedExpr.Length >= 1 ? predicate(evaluatedExpr[0]) : false;

            if (conditionResult)
            {
                if (ifStatementNode.ThenBranch == null)
                {
                    throw new ArgumentNullException("ifStatementNode.ThenBranch", "The argument cannot equal to null");
                }

                return(ifStatementNode.ThenBranch.Accept(this));
            }

            if (ifStatementNode.ElseBranch == null)
            {
                throw new ArgumentNullException("ifStatementNode.ElseBranch", "The argument cannot equal to null");
            }

            return(ifStatementNode.ElseBranch.Accept(this));
        }
Esempio n. 3
0
        /// <summary>
        /// The method evaluates (executes) a program, which is represented as a syntax tree of nodes.
        /// An input parameters could be integer arrays or integer values (represented as an array with single value).
        /// </summary>
        /// <param name="program">A program is represented as an AST</param>
        /// <param name="inputStream">Program's input data</param>
        /// <returns>An evaluated array of some data</returns>

        public int[] Eval(IASTNode program, IInputStream inputStream)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program", "The argument cannot equal to null");
            }

            if (inputStream == null)
            {
                throw new ArgumentNullException("inputData", "The argument cannot equal to null");
            }

            mInputStream = inputStream;

            mEnvironment.Clear();

            return((int[])program.Accept(this));
        }