Пример #1
0
        /// <summary>
        /// Rule: PowExpr -> UnaryExpr (POW UnaryExpr )* ;
        /// </summary>
        protected override object EvalPowExpr(ParseTree tree, params object[] paramlist)
        {
            if (GetNode(TokenType.POW) == null)
            {
                return((ExpressionBase)GetNode(TokenType.UnaryExpr).Eval(tree));
            }

            var expressions = new Stack <ExpressionBase>(nodes.OfTokenType(TokenType.UnaryExpr).Select(n => n.Eval(tree)).Cast <ExpressionBase>().Reverse());
            var operations  = new Stack <string>(nodes.OfTokenType(TokenType.POW).Select(x => x.Token.Text).Reverse());


            while (expressions.Count > 1)
            {
                var leftExpr  = expressions.Pop();
                var rightExpr = expressions.Pop();
                var newExpr   = new MultPowExpr
                {
                    First         = leftExpr,
                    Second        = rightExpr,
                    Node          = this,
                    OperationText = operations.Pop()
                };
                expressions.Push(newExpr);
            }

            return(expressions.First());
        }
        public dynamic Visit(MultPowExpr expr)
        {
            var left  = _codeGen.Local(Visit((dynamic)expr.First)) as Operand;
            var right = _codeGen.Local(Visit((dynamic)expr.Second)) as Operand;

            switch (expr.Type)
            {
            case MultPowDivType.Mult:
                return(left * right);

            case MultPowDivType.Div:
                var d = _assemblyGen.StaticFactory.Invoke(typeof(Convert), "ToDouble", left);
                return(d / right);

            case MultPowDivType.IntDiv:
                var res = left / right;
                return(_assemblyGen.StaticFactory.Invoke(typeof(Convert), "ToInt32", res));

            case MultPowDivType.Mod:
                return(left.Modulus(right));

            case MultPowDivType.Pow:
                return(_assemblyGen.StaticFactory.Invoke(typeof(Math), "Pow", left, right));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #3
0
        public dynamic Visit(MultPowExpr expr)
        {
            var node = Visit((ExpressionBase)expr);

            node.Text = expr.Type.ToString();
            return(node);
        }
        public dynamic Visit(MultPowExpr expr)
        {
            Visit((ExpressionBase)expr);
            if (OptimizeMode.ExpressionSimplify)
            {
                var left  = expr.First as LiteralExpr;
                var right = expr.Second as LiteralExpr;
                if (left != null && right != null)
                {
                    var literal = new LiteralExpr {
                        Namespace = expr.Namespace, Node = expr.Node, SymbolType = expr.GetExprType()
                    };
                    dynamic result;
                    switch (expr.Type)
                    {
                    case MultPowDivType.Mult:
                        result = left.Value * right.Value;
                        break;

                    case MultPowDivType.Div:
                        result = (double)left.Value / right.Value;
                        break;

                    case MultPowDivType.IntDiv:
                        result = left.Value / right.Value;
                        break;

                    case MultPowDivType.Mod:
                        result = left.Value % right.Value;
                        break;

                    case MultPowDivType.Pow:
                        result = Math.Pow(left.Value, right.Value);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    if (literal.SymbolType == SymbolType.Integer)
                    {
                        result = Convert.ToInt32(result);
                    }
                    literal.Value = result;
                    return(literal);
                }
            }
            return(expr);
        }
 public dynamic Visit(MultPowExpr expr)
 {
     if (expr.Types.Contains(SymbolType.Bool) || expr.Types.Contains(SymbolType.String))
     {
         throw new ParseException(
                   $"Невозможно произвести операцию между типами {expr.FirstType} и {expr.SecondType}",
                   expr.Node);
     }
     if (expr.Type == MultPowDivType.Div)
     {
         return(SymbolType.Double);
     }
     if (expr.Type == MultPowDivType.IntDiv)
     {
         return(SymbolType.Integer);
     }
     if (expr.Types.Contains(SymbolType.Double))
     {
         return(SymbolType.Double);
     }
     return(SymbolType.Integer);
 }
 public dynamic Visit(MultPowExpr expr)
 {
     Visit((ExpressionBase)expr);
     return(null);
 }