コード例 #1
0
ファイル: Converter_IfStmt.cs プロジェクト: zjmit/go2cs
        public override void EnterIfStmt(GoParser.IfStmtContext context)
        {
            // ifStmt
            //     : 'if '(simpleStmt ';') ? expression block ( 'else' ( ifStmt | block ) ) ?

            m_ifExpressionLevel++;

            if (!(context.simpleStmt() is null) && context.simpleStmt().emptyStmt() is null)
            {
                // Any declared variable will be scoped to if statement, so create a sub-block for it
                if (!(context.simpleStmt().shortVarDecl() is null))
                {
                    m_targetFile.AppendLine($"{Spacing()}{{");
                    IndentLevel++;

                    // Handle storing of current values of any redeclared variables
                    m_targetFile.Append(OpenRedeclaredVariableBlock(context.simpleStmt().shortVarDecl().identifierList(), m_ifExpressionLevel));
                }

                m_targetFile.Append(string.Format(IfStatementMarker, m_ifExpressionLevel));
            }

            m_targetFile.AppendLine($"{string.Format(IfElseBreakMarker, m_ifExpressionLevel)}{Spacing()}{string.Format(IfElseMarker, m_ifExpressionLevel)}if ({string.Format(IfExpressionMarker, m_ifExpressionLevel)})");

            if (context.block().Length == 2)
            {
                PushOuterBlockSuffix(null);  // For current block
                PushOuterBlockSuffix($"{Environment.NewLine}{Spacing()}else{(LineTerminatorAhead(context.block(0)) ? "" : Environment.NewLine)}");
            }
            else
            {
                PushOuterBlockSuffix(null);  // For current block
            }
        }
コード例 #2
0
ファイル: Converter_IfStmt.cs プロジェクト: zjmit/go2cs
        public override void ExitIfStmt(GoParser.IfStmtContext context)
        {
            // ifStmt
            //     : 'if '(simpleStmt ';') ? expression block ( 'else' ( ifStmt | block ) ) ?

            if (Expressions.TryGetValue(context.expression(), out ExpressionInfo expression))
            {
                bool isElseIf = context.Parent is GoParser.IfStmtContext;

                // Replace if markers
                m_targetFile.Replace(string.Format(IfExpressionMarker, m_ifExpressionLevel), expression.Text);
                m_targetFile.Replace(string.Format(IfElseBreakMarker, m_ifExpressionLevel), isElseIf ? Environment.NewLine : "");
                m_targetFile.Replace(string.Format(IfElseMarker, m_ifExpressionLevel), isElseIf ? "else " : "");
            }
            else
            {
                AddWarning(context, $"Failed to find expression for if statement: {context.GetText()}");
            }

            if (!(context.simpleStmt() is null) && context.simpleStmt().emptyStmt() is null)
            {
                if (m_simpleStatements.TryGetValue(context.simpleStmt(), out string statement))
                {
                    m_targetFile.Replace(string.Format(IfStatementMarker, m_ifExpressionLevel), statement + Environment.NewLine);
                }
                else
                {
                    AddWarning(context, $"Failed to find simple statement for if statement: {context.simpleStmt().GetText()}");
                }

                // Close any locally scoped declared variable sub-block
                if (!(context.simpleStmt().shortVarDecl() is null))
                {
                    // Handle restoration of previous values of any redeclared variables
                    m_targetFile.Append(CloseRedeclaredVariableBlock(context.simpleStmt().shortVarDecl().identifierList(), m_ifExpressionLevel));

                    IndentLevel--;
                    m_targetFile.AppendLine();
                    m_targetFile.Append($"{Spacing()}}}{CheckForCommentsRight(context)}");
                }
            }

            if (!EndsWithLineFeed(m_targetFile.ToString()))
            {
                m_targetFile.AppendLine();
            }

            m_ifExpressionLevel--;
        }