示例#1
0
        private void EmitUnaryExpression(NonTerm nonTerm)
        {
            BaseSymbol unaryExpression;

            NonTermFactory.GetUnaryExpression(nonTerm, out unaryExpression);

            //_compilerLogger.PrintGenerateNonTerm(nonTerm);

            try
            {
                if (unaryExpression.GrammarMember == GrammarMemberType.Token)
                {
                    Operand operandFirst = OperandTokenInitLocal(unaryExpression);
                    _stackOperandFirst.Push(_g.Local(operandFirst));
                }
                else
                {
                    Generate(unaryExpression);
                    _stackOperandFirst.Push(_g.Local(_currentOperandTempResult));
                }
            }
            catch (InvalidCastException ex)
            {
                throw new CodeGenerationException(MessagesHelper.TypeMismatchEx, unaryExpression.ToStringInfo(), ex);
            }

            Operand currentOperandFirst = _stackOperandFirst.Pop();

            try
            {
                Operand unaryExpr = null;
                switch (nonTerm.TypeNonTerm)
                {
                case NonTermType.UnaryMinusExpression:
                    unaryExpr = -currentOperandFirst;
                    break;

                case NonTermType.UnaryNotExpression:
                    unaryExpr = !currentOperandFirst;
                    break;
                }

                _currentOperandTempResult = unaryExpr;
            }
            catch (InvalidOperationException ex)
            {
                throw new CodeGenerationException(MessagesHelper.InvalidOperationEx, nonTerm.ToStringInfo(), ex);
            }
            catch (InvalidCastException ex)
            {
                throw new CodeGenerationException(MessagesHelper.TypeMismatchEx, nonTerm.ToStringInfo(), ex);
            }
        }
示例#2
0
        private void EmitBinaryExpression(NonTerm nonTerm)
        {
            BaseSymbol first;
            BaseSymbol second;

            NonTermFactory.GetBinaryExpression(nonTerm, out first, out second);

            //_compilerLogger.PrintGenerateNonTerm(nonTerm);
            try
            {
                if (first.GrammarMember == GrammarMemberType.Token)
                {
                    _stackOperandFirst.Push(OperandTokenInitLocal(first));
                }
                else
                {
                    Generate(first);
                    _stackOperandFirst.Push(_g.Local(_currentOperandTempResult));
                }
            }
            catch (InvalidCastException ex)
            {
                throw new CodeGenerationException(MessagesHelper.TypeMismatchEx, first.ToStringInfo(), ex);
            }


            try
            {
                if (second.GrammarMember == GrammarMemberType.Token)
                {
                    _stackOperandSecond.Push(OperandTokenInitLocal(second));
                }
                else
                {
                    Generate(second);
                    _stackOperandSecond.Push(_g.Local(_currentOperandTempResult));
                }
            }
            catch (InvalidCastException ex)
            {
                throw new CodeGenerationException(MessagesHelper.TypeMismatchEx, second.ToStringInfo(), ex);
            }

            Operand currentOperandFirst  = _stackOperandFirst.Pop();
            Operand currentOperandSecond = _stackOperandSecond.Pop();

            try
            {
                switch (nonTerm.TypeNonTerm)
                {
                case NonTermType.EqualExpression:
                    _currentOperandTempResult = currentOperandFirst == currentOperandSecond;
                    break;

                case NonTermType.NotEqualExpression:
                    _currentOperandTempResult = currentOperandFirst != currentOperandSecond;
                    break;

                case NonTermType.LessExpression:
                    _currentOperandTempResult = currentOperandFirst < currentOperandSecond;
                    break;

                case NonTermType.LessEqExpression:
                    _currentOperandTempResult = currentOperandFirst <= currentOperandSecond;
                    break;

                case NonTermType.MoreExpression:
                    _currentOperandTempResult = currentOperandFirst > currentOperandSecond;
                    break;

                case NonTermType.MoreEqExpression:
                    _currentOperandTempResult = currentOperandFirst >= currentOperandSecond;
                    break;

                case NonTermType.LogicalAndExpression:
                    _currentOperandTempResult = currentOperandFirst && currentOperandSecond;
                    break;

                case NonTermType.LogicalOrExpression:
                    _currentOperandTempResult = currentOperandFirst || currentOperandSecond;
                    break;

                case NonTermType.PlusExpression:
                    _currentOperandTempResult = currentOperandFirst + currentOperandSecond;
                    break;

                case NonTermType.MinusExpression:
                    _currentOperandTempResult = currentOperandFirst - currentOperandSecond;
                    break;

                case NonTermType.MultiplyExpression:
                    _currentOperandTempResult = currentOperandFirst * currentOperandSecond;
                    break;

                case NonTermType.DivisionExpression:
                    _currentOperandTempResult = currentOperandFirst / currentOperandSecond;
                    break;
                }
            }
            catch (InvalidOperationException ex)
            {
                throw new CodeGenerationException(MessagesHelper.InvalidOperationEx, nonTerm.ToStringInfo(), ex);
            }
            catch (InvalidCastException ex)
            {
                throw new CodeGenerationException(MessagesHelper.TypeMismatchEx, nonTerm.ToStringInfo(), ex);
            }
        }