Пример #1
0
        private PythonNode ExtractStatement_Function(FunctionDefinition definition, TranslatorState state)
        {
            var node = PythonNode.CreateFunction(definition, state);

            (node, _) = Extract_Children(node, definition.Body, state);
            return(node);
        }
Пример #2
0
        private (PythonNode, TranslatorState) Extract_Children(PythonNode node, Statement statement, TranslatorState state)
        {
            if (statement is SuiteStatement suiteStatement)
            {
                foreach (Statement memberStatement in suiteStatement.Statements)
                {
                    switch (memberStatement)
                    {
                    // case FromImportStatement fromImportStatement:
                    //     state = ExtractStatement_FromImportStatement(fromImportStatement, state);
                    //     break;
                    case ClassDefinition classDefinition:
                        var pythonClass = ExtractStatement_Class(classDefinition, state);
                        node = node.WithChild(pythonClass);
                        break;

                    case FunctionDefinition functionDefinition:
                        var pythonFunction = ExtractStatement_Function(functionDefinition, state);
                        node = node.WithChild(pythonFunction);
                        break;
                    }
                }

                return(node, state);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Пример #3
0
        public PythonNode Extract(PythonAst ast)
        {
            var rootNode = PythonNode.CreateRoot();
            var state    = TranslatorState.Empty;

            (rootNode, _) = Extract_Children(rootNode, ast.Body, state);

            return(rootNode);
        }
Пример #4
0
        public BlockSyntax TranslateFunctionBody(PythonNode node)
        {
            if (node.NodeType != PythonNodeType.Function)
            {
                throw new ArgumentException();
            }

            var function = (FunctionDefinition)node.Statement;
            var body     = TranslateBlock_Block(function.Body, node.State);

            return(body);
        }
Пример #5
0
        public MethodDeclarationSyntax TranslateFunctionDefinition(PythonNode node)
        {
            if (node.NodeType != PythonNodeType.Function)
            {
                throw new ArgumentException();
            }

            var function = (FunctionDefinition)node.Statement;

            var returnType        = SyntaxFactory.ParseTypeName("void");
            var methodDeclaration = SyntaxFactory.MethodDeclaration(returnType, function.Name)
                                    .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)))
                                    .WithBody(SyntaxFactory.Block());

            foreach (Parameter pyParameter in function.Parameters)
            {
                var parameterSyntax = SyntaxFactory.Parameter(SyntaxFactory.Identifier(pyParameter.Name))
                                      .WithType(SyntaxFactory.ParseTypeName("object"));

                if (pyParameter.DefaultValue != null)
                {
                    var parameterExpression = TranslateExpression(pyParameter.DefaultValue, node.State);

                    if (parameterExpression.IsError)
                    {
                        parameterSyntax = parameterSyntax.WithTrailingTrivia(parameterExpression.Errors);
                    }
                    else
                    {
                        parameterSyntax = parameterSyntax.WithDefault(SyntaxFactory.EqualsValueClause(parameterExpression.Syntax));
                    }
                }

                methodDeclaration = methodDeclaration.AddParameterListParameters(parameterSyntax);
            }

            return(methodDeclaration);
        }
Пример #6
0
        public PythonNode WithChild(PythonNode child)
        {
            var children = Children.Add(child);

            return(new PythonNode(NodeType, Statement, Name, State, children));
        }