コード例 #1
0
        protected override IPhpValue VisitPhpMethodInvokeValue(PhpMethodInvokeValue node)
        {
            var nv = strip(node.Expression);

            if (PhpSourceBase.EqualCode(nv, node.Expression))
            {
                return(node);
            }
            return(new PhpMethodInvokeValue(nv)
            {
                ByRef = node.ByRef
            });
        }
コード例 #2
0
 protected override IPhpValue VisitPhpMethodCallExpression(PhpMethodCallExpression node)
 {
     if (node.Name == "_urlencode_" || node.Name == "_htmlspecialchars_")
     {
         if (node.Arguments[0].Expression is PhpConstValue)
         {
             var cv = (node.Arguments[0].Expression as PhpConstValue).Value;
             if (cv == null)
             {
                 return(Simplify(node.Arguments[0].Expression));
             }
             if (cv is int)
             {
                 cv = cv.ToString();
             }
             else if (cv is string)
             {
                 if (node.Name == "_urlencode_")
                 {
                     cv = HttpUtility.UrlEncode(cv as string);
                 }
                 else
                 {
                     cv = HttpUtility.HtmlEncode(cv as string);
                 }
             }
             else
             {
                 throw new NotSupportedException();
             }
             return(Simplify(new PhpConstValue(cv)));
         }
     }
     {
         var list1 = node.Arguments.Select(Simplify).Cast <PhpMethodInvokeValue>().ToList();
         var to    = node.TargetObject == null ? null : Simplify(node.TargetObject);
         if (PhpSourceBase.EqualCode_List(list1, node.Arguments) && PhpSourceBase.EqualCode(to, node.TargetObject))
         {
             return(node);
         }
         var xx = new PhpMethodCallExpression(node.Name)
         {
             Arguments        = list1,
             DontIncludeClass = node.DontIncludeClass,
             TargetObject     = to
         };
         xx.SetClassName(node.ClassName, node.TranslationInfo);
         return(xx);
     }
     return(node);
 }
コード例 #3
0
        protected override IPhpStatement VisitPhpCodeBlock(PhpCodeBlock node)
        {
            var newNode = new PhpCodeBlock();

            foreach (var i in node.GetPlain())
            {
                newNode.Statements.Add(Simplify(i));
            }

            #region Łączenie kolejnych echo
            if (op.JoinEchoStatements)
            {
                //newNode.Statements.Clear();
                {
                    for (var i = 1; i < newNode.Statements.Count; i++)
                    {
                        var e1 = GetPhpNativeMethodCall(newNode.Statements[i - 1], "echo");
                        if (e1 == null)
                        {
                            continue;
                        }
                        var e2 = GetPhpNativeMethodCall(newNode.Statements[i], "echo");
                        if (e2 == null)
                        {
                            continue;
                        }

                        Func <IPhpValue, IPhpValue> AddBracketsIfNecessary = (ee) =>
                        {
                            if (ee is PhpParenthesizedExpression || ee is PhpConstValue || ee is PhpPropertyAccessExpression)
                            {
                                return(ee);
                            }

                            if (ee is PhpBinaryOperatorExpression && ((PhpBinaryOperatorExpression)ee).Operator == ".")
                            {
                                return(ee);
                            }
                            return(new PhpParenthesizedExpression(ee));
                        };

                        var a1 = AddBracketsIfNecessary(e1.Arguments[0].Expression);
                        var a2 = AddBracketsIfNecessary(e2.Arguments[0].Expression);

                        IPhpValue e = new PhpBinaryOperatorExpression(".", a1, a2);
                        e = Simplify(e);
                        IPhpValue echo = new PhpMethodCallExpression("echo", e);
                        newNode.Statements[i - 1] = new PhpExpressionStatement(echo);
                        newNode.Statements.RemoveAt(i);
                        i--;
                    }
                    for (var i = 0; i < newNode.Statements.Count; i++)
                    {
                        var a = newNode.Statements[i];
                        if (a is PhpSourceBase)
                        {
                            newNode.Statements[i] = Visit(a as PhpSourceBase);
                        }
                    }
                }
            }
            #endregion
            return(PhpSourceBase.EqualCode_List(node.Statements, newNode.Statements) ? node : newNode);
        }
コード例 #4
0
ファイル: PhpBaseVisitor.cs プロジェクト: yekainew/cs2php
 public static PhpSourceItems GetKind(PhpSourceBase x)
 {
     if (x == null)
     {
         throw new ArgumentNullException();
     }
     if (x.GetType() == typeof(PhpCodeBlock))
     {
         return(PhpSourceItems.PhpCodeBlock);
     }
     if (x.GetType() == typeof(PhpArrayCreateExpression))
     {
         return(PhpSourceItems.PhpArrayCreateExpression);
     }
     if (x.GetType() == typeof(PhpDefinedConstExpression))
     {
         return(PhpSourceItems.PhpDefinedConstExpression);
     }
     if (x.GetType() == typeof(PhpFreeExpression))
     {
         return(PhpSourceItems.PhpFreeExpression);
     }
     if (x.GetType() == typeof(PhpBreakStatement))
     {
         return(PhpSourceItems.PhpBreakStatement);
     }
     if (x.GetType() == typeof(PhpCodeModuleName))
     {
         return(PhpSourceItems.PhpCodeModuleName);
     }
     if (x.GetType() == typeof(PhpArrayAccessExpression))
     {
         return(PhpSourceItems.PhpArrayAccessExpression);
     }
     if (x.GetType() == typeof(PhpAssignExpression))
     {
         return(PhpSourceItems.PhpAssignExpression);
     }
     if (x.GetType() == typeof(PhpClassFieldAccessExpression))
     {
         return(PhpSourceItems.PhpClassFieldAccessExpression);
     }
     if (x.GetType() == typeof(PhpBinaryOperatorExpression))
     {
         return(PhpSourceItems.PhpBinaryOperatorExpression);
     }
     if (x.GetType() == typeof(PhpConditionalExpression))
     {
         return(PhpSourceItems.PhpConditionalExpression);
     }
     if (x.GetType() == typeof(PhpConstValue))
     {
         return(PhpSourceItems.PhpConstValue);
     }
     if (x.GetType() == typeof(PhpElementAccessExpression))
     {
         return(PhpSourceItems.PhpElementAccessExpression);
     }
     if (x.GetType() == typeof(PhpContinueStatement))
     {
         return(PhpSourceItems.PhpContinueStatement);
     }
     if (x.GetType() == typeof(PhpExpressionStatement))
     {
         return(PhpSourceItems.PhpExpressionStatement);
     }
     if (x.GetType() == typeof(PhpForEachStatement))
     {
         return(PhpSourceItems.PhpForEachStatement);
     }
     if (x.GetType() == typeof(PhpForStatement))
     {
         return(PhpSourceItems.PhpForStatement);
     }
     if (x.GetType() == typeof(PhpIfStatement))
     {
         return(PhpSourceItems.PhpIfStatement);
     }
     if (x.GetType() == typeof(PhpIncrementDecrementExpression))
     {
         return(PhpSourceItems.PhpIncrementDecrementExpression);
     }
     if (x.GetType() == typeof(PhpInstanceFieldAccessExpression))
     {
         return(PhpSourceItems.PhpInstanceFieldAccessExpression);
     }
     if (x.GetType() == typeof(PhpPropertyAccessExpression))
     {
         return(PhpSourceItems.PhpPropertyAccessExpression);
     }
     if (x.GetType() == typeof(PhpMethodInvokeValue))
     {
         return(PhpSourceItems.PhpMethodInvokeValue);
     }
     if (x.GetType() == typeof(PhpMethodCallExpression))
     {
         return(PhpSourceItems.PhpMethodCallExpression);
     }
     if (x.GetType() == typeof(PhpParenthesizedExpression))
     {
         return(PhpSourceItems.PhpParenthesizedExpression);
     }
     if (x.GetType() == typeof(PhpReturnStatement))
     {
         return(PhpSourceItems.PhpReturnStatement);
     }
     if (x.GetType() == typeof(PhpThisExpression))
     {
         return(PhpSourceItems.PhpThisExpression);
     }
     if (x.GetType() == typeof(PhpUnaryOperatorExpression))
     {
         return(PhpSourceItems.PhpUnaryOperatorExpression);
     }
     if (x.GetType() == typeof(PhpVariableExpression))
     {
         return(PhpSourceItems.PhpVariableExpression);
     }
     if (x.GetType() == typeof(PhpWhileStatement))
     {
         return(PhpSourceItems.PhpWhileStatement);
     }
     if (x.GetType() == typeof(PhpSwitchStatement))
     {
         return(PhpSourceItems.PhpSwitchStatement);
     }
     throw new NotSupportedException(x.GetType().FullName);
 }
コード例 #5
0
ファイル: PhpBaseVisitor.cs プロジェクト: yekainew/cs2php
        public virtual T Visit(PhpSourceBase node)
        {
            if (node == null)
            {
                return(VisitNull());
            }
            switch (node.Kind)
            {
            case PhpSourceItems.PhpCodeBlock:
                return(VisitPhpCodeBlock(node as PhpCodeBlock));

            case PhpSourceItems.PhpArrayCreateExpression:
                return(VisitPhpArrayCreateExpression(node as PhpArrayCreateExpression));

            case PhpSourceItems.PhpDefinedConstExpression:
                return(VisitPhpDefinedConstExpression(node as PhpDefinedConstExpression));

            case PhpSourceItems.PhpFreeExpression:
                return(VisitPhpFreeExpression(node as PhpFreeExpression));

            case PhpSourceItems.PhpBreakStatement:
                return(VisitPhpBreakStatement(node as PhpBreakStatement));

            case PhpSourceItems.PhpCodeModuleName:
                return(VisitPhpCodeModuleName(node as PhpCodeModuleName));

            case PhpSourceItems.PhpArrayAccessExpression:
                return(VisitPhpArrayAccessExpression(node as PhpArrayAccessExpression));

            case PhpSourceItems.PhpAssignExpression:
                return(VisitPhpAssignExpression(node as PhpAssignExpression));

            case PhpSourceItems.PhpClassFieldAccessExpression:
                return(VisitPhpClassFieldAccessExpression(node as PhpClassFieldAccessExpression));

            case PhpSourceItems.PhpBinaryOperatorExpression:
                return(VisitPhpBinaryOperatorExpression(node as PhpBinaryOperatorExpression));

            case PhpSourceItems.PhpConditionalExpression:
                return(VisitPhpConditionalExpression(node as PhpConditionalExpression));

            case PhpSourceItems.PhpConstValue:
                return(VisitPhpConstValue(node as PhpConstValue));

            case PhpSourceItems.PhpElementAccessExpression:
                return(VisitPhpElementAccessExpression(node as PhpElementAccessExpression));

            case PhpSourceItems.PhpContinueStatement:
                return(VisitPhpContinueStatement(node as PhpContinueStatement));

            case PhpSourceItems.PhpExpressionStatement:
                return(VisitPhpExpressionStatement(node as PhpExpressionStatement));

            case PhpSourceItems.PhpForEachStatement:
                return(VisitPhpForEachStatement(node as PhpForEachStatement));

            case PhpSourceItems.PhpForStatement:
                return(VisitPhpForStatement(node as PhpForStatement));

            case PhpSourceItems.PhpIfStatement:
                return(VisitPhpIfStatement(node as PhpIfStatement));

            case PhpSourceItems.PhpIncrementDecrementExpression:
                return(VisitPhpIncrementDecrementExpression(node as PhpIncrementDecrementExpression));

            case PhpSourceItems.PhpInstanceFieldAccessExpression:
                return(VisitPhpInstanceFieldAccessExpression(node as PhpInstanceFieldAccessExpression));

            case PhpSourceItems.PhpPropertyAccessExpression:
                return(VisitPhpPropertyAccessExpression(node as PhpPropertyAccessExpression));

            case PhpSourceItems.PhpMethodInvokeValue:
                return(VisitPhpMethodInvokeValue(node as PhpMethodInvokeValue));

            case PhpSourceItems.PhpMethodCallExpression:
                return(VisitPhpMethodCallExpression(node as PhpMethodCallExpression));

            case PhpSourceItems.PhpParenthesizedExpression:
                return(VisitPhpParenthesizedExpression(node as PhpParenthesizedExpression));

            case PhpSourceItems.PhpReturnStatement:
                return(VisitPhpReturnStatement(node as PhpReturnStatement));

            case PhpSourceItems.PhpThisExpression:
                return(VisitPhpThisExpression(node as PhpThisExpression));

            case PhpSourceItems.PhpUnaryOperatorExpression:
                return(VisitPhpUnaryOperatorExpression(node as PhpUnaryOperatorExpression));

            case PhpSourceItems.PhpVariableExpression:
                return(VisitPhpVariableExpression(node as PhpVariableExpression));

            case PhpSourceItems.PhpWhileStatement:
                return(VisitPhpWhileStatement(node as PhpWhileStatement));

            case PhpSourceItems.PhpSwitchStatement:
                return(VisitPhpSwitchStatement(node as PhpSwitchStatement));

            default: throw new NotSupportedException(node.Kind.ToString() + "," + node.GetType().Name);
            }
        }