public override Yarn.Type VisitElse_if_clause(YarnSpinnerV1Parser.Else_if_clauseContext context)
            {
                YarnSpinnerV1Parser.ExpressionContext expressionContext = context.expression();
                this.CheckAndRewriteIfClause(context, expressionContext);

                return(Type.Bool);
            }
Exemplo n.º 2
0
            public override Yarn.IType VisitIf_clause(YarnSpinnerV1Parser.If_clauseContext context)
            {
                YarnSpinnerV1Parser.ExpressionContext expressionContext = context.expression();
                this.CheckAndRewriteIfClause(context, expressionContext);

                return(BuiltinTypes.Boolean);
            }
            private void CheckAndRewriteIfClause(ParserRuleContext context, YarnSpinnerV1Parser.ExpressionContext expressionContext)
            {
                // Evaluate the if statement's expression. In YS2, if
                // statement expressions are required to be bool, but in
                // YS1, they can be any type. In this upgrader, we 'permit'
                // the type to also be int, and if it is, we'll later
                // rewrite the expression to be of the type "EXP != 0" by
                // running it through the checker with 'upgrades' enabled
                var expressions = new[] { expressionContext };

                var type = this.CheckOperation(context, expressions, "if statement", Yarn.Type.Bool, Yarn.Type.Number);

                if (this.GenerateReplacements && type == Type.Number)
                {
                    // This if statement resolved to a number expression,
                    // and we want to generate replacements. We'll generate
                    // a replacement that converts it to a bool.

                    // A 'simple expression' is on where the expression is
                    // just a value, eg <<if $x>>. A complex expression is
                    // one with operators (<<if $x + 1>>). We'll wrap
                    // complex expressions in parentheses.
                    var isSimpleExpression = expressionContext is YarnSpinnerV1Parser.ExpValueContext;

                    // Get the original text of expressionContext. We can't
                    // use "expressionContext.GetText()" here, because that
                    // just concatenates the text of all captured tokens,
                    // and doesn't include text on hidden channels (e.g.
                    // whitespace and comments).
                    var    interval     = new Interval(expressionContext.Start.StartIndex, expressionContext.Stop.StopIndex);
                    string originalText = expressionContext.Start.InputStream.GetText(interval);

                    string replacementText;

                    if (isSimpleExpression)
                    {
                        // No need to wrap
                        replacementText = originalText + " != 0";
                    }
                    else
                    {
                        // Wrap with parentheses
                        replacementText = "(" + originalText + ") != 0";
                    }

                    this.replacements.Add(new TextReplacement
                    {
                        Comment         = "Converting if statement expression to a boolean",
                        OriginalText    = originalText,
                        StartLine       = expressionContext.Start.Line,
                        Start           = expressionContext.Start.StartIndex,
                        ReplacementText = replacementText,
                    });
                }
            }