示例#1
0
        public override void ExitAssignment(GoParser.AssignmentContext context)
        {
            // assignment
            //     : expressionList assign_op expressionList

            // TODO: Properly handle assignment order of operations, see https://gridprotectionalliance.github.io/go2cs/ConversionStrategies.html#inline-assignment-order-of-operations
            if (ExpressionLists.TryGetValue(context.expressionList(0), out ExpressionInfo[] leftOperands) && ExpressionLists.TryGetValue(context.expressionList(1), out ExpressionInfo[] rightOperands))
示例#2
0
        public override void ExitExprCaseClause(GoParser.ExprCaseClauseContext context)
        {
            // exprSwitchStmt
            //     : 'switch'(simpleStmt ';') ? expression ? '{' exprCaseClause * '}'

            // exprCaseClause
            //     : exprSwitchCase ':' statementList

            // exprSwitchCase
            //     : 'case' expressionList | 'default'

            IndentLevel--;

            ExprSwitchStatement exprSwitchStatement = m_exprSwitchStatements.Peek();

            GoParser.ExpressionListContext expressionList = context.exprSwitchCase().expressionList();

            if (expressionList is null)
            {
                exprSwitchStatement.defaultCase.block = PopBlock(false);
            }
            else
            {
                if (!ExpressionLists.TryGetValue(expressionList, out ExpressionInfo[] expressions))
示例#3
0
        public override void ExitAssignment(GoParser.AssignmentContext context)
        {
            // assignment
            //     : expressionList assign_op expressionList

            if (ExpressionLists.TryGetValue(context.expressionList(0), out string[] leftOperands) && ExpressionLists.TryGetValue(context.expressionList(1), out string[] rightOperands))
        public override void ExitPrimaryExpr(GoParser.PrimaryExprContext context)
        {
            // primaryExpr
            //     : operand
            //     | conversion
            //     | primaryExpr selector
            //     | primaryExpr index
            //     | primaryExpr slice
            //     | primaryExpr typeAssertion
            //     | primaryExpr arguments

            PrimaryExpressions.TryGetValue(context.primaryExpr(), out string primaryExpression);

            if (!string.IsNullOrEmpty(primaryExpression))
            {
                primaryExpression = SanitizedIdentifier(primaryExpression);
            }

            if (Operands.TryGetValue(context.operand(), out string operand))
            {
                PrimaryExpressions[context] = SanitizedIdentifier(operand);
            }
            else if (context.conversion() != null)
            {
                // conversion
                //     : type '(' expression ',' ? ')'

                if (Types.TryGetValue(context.conversion().type_(), out TypeInfo typeInfo) && Expressions.TryGetValue(context.conversion().expression(), out string expression))
                {
                    if (typeInfo.TypeName.StartsWith("*(*"))
                    {
                        // TODO: Complex pointer expression needs special handling consideration - could opt for unsafe implementation
                        PrimaryExpressions[context] = $"{typeInfo.TypeName}{expression}";
                    }
                    else
                    {
                        if (typeInfo.IsPointer)
                        {
                            PrimaryExpressions[context] = $"new Ptr<{typeInfo.TypeName}>({expression})";
                        }
                        else if (typeInfo.TypeClass == TypeClass.Struct)
                        {
                            PrimaryExpressions[context] = $"{typeInfo.TypeName}_cast({expression})";
                        }
                        else if (typeInfo.TypeClass == TypeClass.Simple)
                        {
                            PrimaryExpressions[context] = $"{typeInfo.TypeName}({expression})";
                        }
                        else
                        {
                            PrimaryExpressions[context] = $"({typeInfo.TypeName}){expression}";
                        }
                    }
                }
                else
                {
                    AddWarning(context, $"Failed to find type or sub-expression for the conversion expression in \"{context.GetText()}\"");
                }
            }
            else if (context.DOT() != null)
            {
                // selector
                //     : '.' IDENTIFIER

                PrimaryExpressions[context] = $"{primaryExpression}.{SanitizedIdentifier(context.IDENTIFIER().GetText())}";
            }
            else if (context.index() != null)
            {
                // index
                //     : '[' expression ']'

                if (Expressions.TryGetValue(context.index().expression(), out string expression))
                {
                    PrimaryExpressions[context] = $"{primaryExpression}[{expression}]";
                }
                else
                {
                    AddWarning(context, $"Failed to find index expression for \"{context.GetText()}\"");
                }
            }
            else if (context.slice() != null)
            {
                // slice
                //     : '['((expression ? ':' expression ? ) | (expression ? ':' expression ':' expression)) ']'

                GoParser.SliceContext sliceContext = context.slice();

                if (sliceContext.children.Count == 3)
                {
                    // primaryExpr[:]
                    PrimaryExpressions[context] = $"{primaryExpression}.slice()";
                }
                else if (sliceContext.children.Count == 4)
                {
                    bool expressionIsLeft = sliceContext.children[1] is GoParser.ExpressionContext;

                    // primaryExpr[low:] or primaryExpr[:high]
                    if (Expressions.TryGetValue(sliceContext.expression(0), out string expression))
                    {
                        PrimaryExpressions[context] = $"{primaryExpression}.slice({(expressionIsLeft ? expression : $"high:{expression}")})";
                    }
                    else
                    {
                        AddWarning(context, $"Failed to find slice expression for \"{context.GetText()}\"");
                    }
                }
                else if (sliceContext.children.Count == 5)
                {
                    if (sliceContext.children[1] is GoParser.ExpressionContext && sliceContext.children[3] is GoParser.ExpressionContext)
                    {
                        // primaryExpr[low:high]
                        if (Expressions.TryGetValue(sliceContext.expression(0), out string lowExpression) && Expressions.TryGetValue(sliceContext.expression(1), out string highExpression))
                        {
                            PrimaryExpressions[context] = $"{primaryExpression}.slice({lowExpression}, {highExpression})";
                        }
                        else
                        {
                            AddWarning(context, $"Failed to find one of the slice expressions for \"{context.GetText()}\"");
                        }
                    }
                    else
                    {
                        AddWarning(context, $"Failed to find slice expression for \"{context.GetText()}\"");
                    }
                }
                else if (sliceContext.children.Count == 6)
                {
                    // primaryExpr[:high:max]
                    if (Expressions.TryGetValue(sliceContext.expression(0), out string highExpression) && Expressions.TryGetValue(sliceContext.expression(1), out string maxExpression))
                    {
                        PrimaryExpressions[context] = $"{primaryExpression}.slice(-1, {highExpression}, {maxExpression})";
                    }
                    else
                    {
                        AddWarning(context, $"Failed to find one of the slice expressions for \"{context.GetText()}\"");
                    }
                }
                else if (sliceContext.children.Count == 7)
                {
                    // primaryExpr[low:high:max]
                    if (Expressions.TryGetValue(sliceContext.expression(0), out string lowExpression) && Expressions.TryGetValue(sliceContext.expression(1), out string highExpression) && Expressions.TryGetValue(sliceContext.expression(2), out string maxExpression))
                    {
                        PrimaryExpressions[context] = $"{primaryExpression}.slice({lowExpression}, {highExpression}, {maxExpression})";
                    }
                    else
                    {
                        AddWarning(context, $"Failed to find one of the slice expressions for \"{context.GetText()}\"");
                    }
                }
            }
            else if (context.typeAssertion() != null)
            {
                // typeAssertion
                //     : '.' '(' type ')'

                if (Types.TryGetValue(context.typeAssertion().type_(), out TypeInfo typeInfo))
                {
                    PrimaryExpressions[context] = $"{primaryExpression}.TypeAssert<{typeInfo.TypeName}>()";
                }
                else
                {
                    AddWarning(context, $"Failed to find type for the type assertion expression in \"{context.GetText()}\"");
                }
            }
            else if (context.arguments() != null)
            {
                // arguments
                //     : '('((expressionList | type(',' expressionList) ? ) '...' ? ',' ? ) ? ')'

                GoParser.ArgumentsContext argumentsContext = context.arguments();
                List <string>             arguments        = new List <string>();

                if (Types.TryGetValue(argumentsContext.type_(), out TypeInfo typeInfo))
                {
                    arguments.Add($"typeof({typeInfo.TypeName})");
                }

                if (ExpressionLists.TryGetValue(argumentsContext.expressionList(), out string[] expressions))