Exemple #1
0
        private SyntaxResult <SyntaxNode> TranslateStatement_With(WithStatement withStatement, TranslatorState state)
        {
            var contextManager = TranslateExpression(withStatement.ContextManager, state);
            var body           = TranslateBlock_Block(withStatement.Body, state);

            if (contextManager.IsError)
            {
                return(SyntaxResult <SyntaxNode> .WithErrors(contextManager.Errors));
            }

            var usingStatement = SyntaxFactory.UsingStatement(body).WithExpression(contextManager.Syntax);

            if (withStatement.Variable != null)
            {
                if (withStatement.Variable is NameExpression nameExpression)
                {
                    var declarationType = SyntaxFactory.ParseTypeName("object");
                    var variable        = SyntaxFactory.VariableDeclarator(nameExpression.Name);
                    var declaration     = SyntaxFactory.VariableDeclaration(declarationType, SyntaxFactory.SingletonSeparatedList(variable));
                    usingStatement = usingStatement.WithDeclaration(declaration);
                }
                else
                {
                    return(SyntaxResult <SyntaxNode> .WithError($"// py2cs: Unknown with statment variable ({withStatement.Variable})"));
                }
            }

            return(usingStatement);
        }
Exemple #2
0
        private (SyntaxResult <SyntaxNode>, TranslatorState) TranslateStatement(Statement statement, TranslatorState state)
        {
            switch (statement)
            {
            // case FromImportStatement fromImportStatement:
            //     return TranslateStatement_FromImportStatement(fromImportStatement, state);
            case ExpressionStatement expressionStatement:
                return(TranslateStatement_Expression(expressionStatement, state), state);

            case AssignmentStatement assignmentStatement:
                return(TranslateStatement_Assignment(assignmentStatement, state), state);

            case ReturnStatement returnStatement:
                return(TranslateStatement_Return(returnStatement, state), state);

            case IfStatement ifStatement:
                return(TranslateStatement_If(ifStatement, state), state);

            case WhileStatement whileStatement:
                return(TranslateStatement_While(whileStatement, state), state);

            case WithStatement withStatement:
                return(TranslateStatement_With(withStatement, state), state);

            case RaiseStatement raiseStatement:
                return(TranslateStatement_Raise(raiseStatement, state), state);

            case AssertStatement assertStatement:
                return(TranslateStatement_Assert(assertStatement, state), state);

            default:
                return(SyntaxResult <SyntaxNode> .WithError($"// py2cs: Unknown statement type ({statement.NodeName})"), state);
            }
        }
Exemple #3
0
        // private (SyntaxResult<SyntaxNode>, TranslatorState) TranslateStatement_FromImportStatement(FromImportStatement fromImportStatement, TranslatorState state)
        // {
        //     for (int nameIndex = 0; nameIndex < fromImportStatement.Names.Count; nameIndex++)
        //     {
        //         var name = fromImportStatement.Names[nameIndex];
        //         var asName = fromImportStatement.AsNames[nameIndex] ?? name;

        //         state = state.WithVariable(asName, name);
        //     }

        //     return (SyntaxFactory.EmptyStatement(), state);
        // }

        private SyntaxResult <SyntaxNode> TranslateStatement_Expression(ExpressionStatement expressionStatement, TranslatorState state)
        {
            if (expressionStatement.Expression is ConstantExpression constantExpression)
            {
                return(SyntaxResult <SyntaxNode> .WithError("/* " + constantExpression.Value + " */"));
            }

            var expression = TranslateExpression(expressionStatement.Expression, state);

            if (expression.IsError)
            {
                return(SyntaxResult <SyntaxNode> .WithErrors(expression.Errors));
            }

            return(SyntaxFactory.ExpressionStatement(expression.Syntax));
        }
Exemple #4
0
        private SyntaxResult <SyntaxNode> TranslateStatement_Assignment(AssignmentStatement assignmentStatement, TranslatorState state)
        {
            if (assignmentStatement.Left.Count != 1)
            {
                return(SyntaxResult <SyntaxNode> .WithError($"// py2cs: Unsupported assignment left expression count"));
            }

            var leftExpression  = TranslateExpression(assignmentStatement.Left[0], state);
            var rightExpression = TranslateExpression(assignmentStatement.Right, state);

            if (leftExpression.IsError || rightExpression.IsError)
            {
                return(SyntaxResult <SyntaxNode> .WithErrors(Enumerable.Concat(leftExpression.Errors, rightExpression.Errors)));
            }

            var expression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, leftExpression.Syntax, rightExpression.Syntax);

            return(SyntaxFactory.ExpressionStatement(expression));
        }
Exemple #5
0
        private (SyntaxResult <SyntaxNode[]>, TranslatorState) TranslateBlock_Members(Statement statement, TranslatorState state)
        {
            if (statement is SuiteStatement suiteStatement)
            {
                var errors  = new List <SyntaxTrivia>();
                var members = new List <SyntaxNode>();

                foreach (Statement memberStatement in suiteStatement.Statements)
                {
                    SyntaxResult <SyntaxNode> member;
                    (member, state) = TranslateStatement(memberStatement, state);

                    if (member.IsError)
                    {
                        errors.AddRange(member.Errors);
                    }
                    else
                    {
                        members.Add(member.Syntax.WithLeadingTrivia(errors));
                        errors.Clear();
                    }
                }

                if (errors.Count != 0)
                {
                    if (members.Count > 0)
                    {
                        members[members.Count - 1] = members[members.Count - 1].WithTrailingTrivia(errors);
                    }
                    else
                    {
                        return(SyntaxResult <SyntaxNode[]> .WithErrors(errors), state);
                    }
                }

                return(members.ToArray(), state);
            }
            else
            {
                return(SyntaxResult <SyntaxNode[]> .WithError($"// py2cs: Expected SuiteStatement"), state);
            }
        }