Пример #1
0
 public static PySourceItems GetKind(PySourceBase x)
 {
     if (x == null)
     {
         throw new ArgumentNullException();
     }
     if (x.GetType() == typeof(PyArrayAccessExpression))
     {
         return(PySourceItems.PyArrayAccessExpression);
     }
     if (x.GetType() == typeof(PyArrayCreateExpression))
     {
         return(PySourceItems.PyArrayCreateExpression);
     }
     if (x.GetType() == typeof(PyAssignExpression))
     {
         return(PySourceItems.PyAssignExpression);
     }
     if (x.GetType() == typeof(PyAssignVariableStatement))
     {
         return(PySourceItems.PyAssignVariableStatement);
     }
     if (x.GetType() == typeof(PyBinaryOperatorExpression))
     {
         return(PySourceItems.PyBinaryOperatorExpression);
     }
     if (x.GetType() == typeof(PyBreakStatement))
     {
         return(PySourceItems.PyBreakStatement);
     }
     if (x.GetType() == typeof(PyClassFieldAccessExpression))
     {
         return(PySourceItems.PyClassFieldAccessExpression);
     }
     if (x.GetType() == typeof(PyCodeBlock))
     {
         return(PySourceItems.PyCodeBlock);
     }
     if (x.GetType() == typeof(PyConditionalExpression))
     {
         return(PySourceItems.PyConditionalExpression);
     }
     if (x.GetType() == typeof(PyConstValue))
     {
         return(PySourceItems.PyConstValue);
     }
     if (x.GetType() == typeof(PyContinueStatement))
     {
         return(PySourceItems.PyContinueStatement);
     }
     if (x.GetType() == typeof(PyDefinedConstExpression))
     {
         return(PySourceItems.PyDefinedConstExpression);
     }
     if (x.GetType() == typeof(PyDictionaryCreateExpression))
     {
         return(PySourceItems.PyDictionaryCreateExpression);
     }
     if (x.GetType() == typeof(PyElementAccessExpression))
     {
         return(PySourceItems.PyElementAccessExpression);
     }
     if (x.GetType() == typeof(PyExpressionStatement))
     {
         return(PySourceItems.PyExpressionStatement);
     }
     if (x.GetType() == typeof(PyForEachStatement))
     {
         return(PySourceItems.PyForEachStatement);
     }
     if (x.GetType() == typeof(PyForStatement))
     {
         return(PySourceItems.PyForStatement);
     }
     if (x.GetType() == typeof(PyIfStatement))
     {
         return(PySourceItems.PyIfStatement);
     }
     if (x.GetType() == typeof(PyIncrementDecrementExpression))
     {
         return(PySourceItems.PyIncrementDecrementExpression);
     }
     if (x.GetType() == typeof(PyInstanceFieldAccessExpression))
     {
         return(PySourceItems.PyInstanceFieldAccessExpression);
     }
     if (x.GetType() == typeof(PyMethodCallExpression))
     {
         return(PySourceItems.PyMethodCallExpression);
     }
     if (x.GetType() == typeof(PyMethodInvokeValue))
     {
         return(PySourceItems.PyMethodInvokeValue);
     }
     if (x.GetType() == typeof(PyModuleExpression))
     {
         return(PySourceItems.PyModuleExpression);
     }
     if (x.GetType() == typeof(PyParenthesizedExpression))
     {
         return(PySourceItems.PyParenthesizedExpression);
     }
     if (x.GetType() == typeof(PyPropertyAccessExpression))
     {
         return(PySourceItems.PyPropertyAccessExpression);
     }
     if (x.GetType() == typeof(PyReturnStatement))
     {
         return(PySourceItems.PyReturnStatement);
     }
     if (x.GetType() == typeof(PySwitchStatement))
     {
         return(PySourceItems.PySwitchStatement);
     }
     if (x.GetType() == typeof(PyThisExpression))
     {
         return(PySourceItems.PyThisExpression);
     }
     if (x.GetType() == typeof(PyUnaryOperatorExpression))
     {
         return(PySourceItems.PyUnaryOperatorExpression);
     }
     if (x.GetType() == typeof(PyUsingStatement))
     {
         return(PySourceItems.PyUsingStatement);
     }
     if (x.GetType() == typeof(PyVariableExpression))
     {
         return(PySourceItems.PyVariableExpression);
     }
     if (x.GetType() == typeof(PyWhileStatement))
     {
         return(PySourceItems.PyWhileStatement);
     }
     throw new NotSupportedException(x.GetType().FullName);
 }
Пример #2
0
        protected override IPyStatement VisitPyCodeBlock(PyCodeBlock node)
        {
            var newNode = new PyCodeBlock();

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

            if (op.JoinEchoStatements)
            {
                for (var i = 1; i < newNode.Statements.Count; i++)
                {
                    var e1 = GetPyNativeMethodCall(newNode.Statements[i - 1], "echo");
                    if (e1 == null)
                    {
                        continue;
                    }
                    var e2 = GetPyNativeMethodCall(newNode.Statements[i], "echo");
                    if (e2 == null)
                    {
                        continue;
                    }

                    Func <IPyValue, IPyValue> AddBracketsIfNecessary = ee =>
                    {
                        if (ee is PyParenthesizedExpression || ee is PyConstValue ||
                            ee is PyPropertyAccessExpression)
                        {
                            return(ee);
                        }

                        if (ee is PyBinaryOperatorExpression && ((PyBinaryOperatorExpression)ee).Operator == ".")
                        {
                            return(ee);
                        }
                        return(new PyParenthesizedExpression(ee));
                    };

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

                    IPyValue e = new PyBinaryOperatorExpression(".", a1, a2);
                    e = Simplify(e);
                    IPyValue echo = new PyMethodCallExpression("echo", e);
                    newNode.Statements[i - 1] = new PyExpressionStatement(echo);
                    newNode.Statements.RemoveAt(i);
                    i--;
                }

                for (var i = 0; i < newNode.Statements.Count; i++)
                {
                    var a = newNode.Statements[i];
                    if (a is PySourceBase)
                    {
                        newNode.Statements[i] = Visit(a as PySourceBase);
                    }
                }
            }

            return(PySourceBase.EqualCode_List(node.Statements, newNode.Statements) ? node : newNode);
        }
Пример #3
0
        public virtual T Visit(PySourceBase node)
        {
            if (node == null)
            {
                return(VisitNull());
            }
            switch (node.Kind)
            {
            case PySourceItems.PyArrayAccessExpression:
                return(VisitPyArrayAccessExpression(node as PyArrayAccessExpression));

            case PySourceItems.PyArrayCreateExpression:
                return(VisitPyArrayCreateExpression(node as PyArrayCreateExpression));

            case PySourceItems.PyAssignExpression:
                return(VisitPyAssignExpression(node as PyAssignExpression));

            case PySourceItems.PyAssignVariableStatement:
                return(VisitPyAssignVariableStatement(node as PyAssignVariableStatement));

            case PySourceItems.PyBinaryOperatorExpression:
                return(VisitPyBinaryOperatorExpression(node as PyBinaryOperatorExpression));

            case PySourceItems.PyBreakStatement:
                return(VisitPyBreakStatement(node as PyBreakStatement));

            case PySourceItems.PyClassFieldAccessExpression:
                return(VisitPyClassFieldAccessExpression(node as PyClassFieldAccessExpression));

            case PySourceItems.PyCodeBlock:
                return(VisitPyCodeBlock(node as PyCodeBlock));

            case PySourceItems.PyConditionalExpression:
                return(VisitPyConditionalExpression(node as PyConditionalExpression));

            case PySourceItems.PyConstValue:
                return(VisitPyConstValue(node as PyConstValue));

            case PySourceItems.PyContinueStatement:
                return(VisitPyContinueStatement(node as PyContinueStatement));

            case PySourceItems.PyDefinedConstExpression:
                return(VisitPyDefinedConstExpression(node as PyDefinedConstExpression));

            case PySourceItems.PyDictionaryCreateExpression:
                return(VisitPyDictionaryCreateExpression(node as PyDictionaryCreateExpression));

            case PySourceItems.PyElementAccessExpression:
                return(VisitPyElementAccessExpression(node as PyElementAccessExpression));

            case PySourceItems.PyExpressionStatement:
                return(VisitPyExpressionStatement(node as PyExpressionStatement));

            case PySourceItems.PyForEachStatement:
                return(VisitPyForEachStatement(node as PyForEachStatement));

            case PySourceItems.PyForStatement:
                return(VisitPyForStatement(node as PyForStatement));

            case PySourceItems.PyIfStatement:
                return(VisitPyIfStatement(node as PyIfStatement));

            case PySourceItems.PyIncrementDecrementExpression:
                return(VisitPyIncrementDecrementExpression(node as PyIncrementDecrementExpression));

            case PySourceItems.PyInstanceFieldAccessExpression:
                return(VisitPyInstanceFieldAccessExpression(node as PyInstanceFieldAccessExpression));

            case PySourceItems.PyMethodCallExpression:
                return(VisitPyMethodCallExpression(node as PyMethodCallExpression));

            case PySourceItems.PyMethodInvokeValue:
                return(VisitPyMethodInvokeValue(node as PyMethodInvokeValue));

            case PySourceItems.PyModuleExpression:
                return(VisitPyModuleExpression(node as PyModuleExpression));

            case PySourceItems.PyParenthesizedExpression:
                return(VisitPyParenthesizedExpression(node as PyParenthesizedExpression));

            case PySourceItems.PyPropertyAccessExpression:
                return(VisitPyPropertyAccessExpression(node as PyPropertyAccessExpression));

            case PySourceItems.PyReturnStatement:
                return(VisitPyReturnStatement(node as PyReturnStatement));

            case PySourceItems.PySwitchStatement:
                return(VisitPySwitchStatement(node as PySwitchStatement));

            case PySourceItems.PyThisExpression:
                return(VisitPyThisExpression(node as PyThisExpression));

            case PySourceItems.PyUnaryOperatorExpression:
                return(VisitPyUnaryOperatorExpression(node as PyUnaryOperatorExpression));

            case PySourceItems.PyUsingStatement:
                return(VisitPyUsingStatement(node as PyUsingStatement));

            case PySourceItems.PyVariableExpression:
                return(VisitPyVariableExpression(node as PyVariableExpression));

            case PySourceItems.PyWhileStatement:
                return(VisitPyWhileStatement(node as PyWhileStatement));

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