Example #1
0
        public string VisitIfThenElseNode(IIfThenElseASTNode ifStatementNode)
        {
            StringBuilder branchingStr = new StringBuilder();

            branchingStr.AppendFormat("IF {0} {1} THEN {2} ELSE {3}",
                                      (ifStatementNode.Variable as IASTNode).Accept(this),
                                      (ifStatementNode.Predicate as IASTNode).Accept(this),
                                      ifStatementNode.ThenBranch.Accept(this),
                                      ifStatementNode.ElseBranch.Accept(this));

            return(branchingStr.ToString());
        }
Example #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));
        }