Example #1
0
        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="node">対象Node</param>
        /// <param name="semanticModel">対象ソースのsemanticModel</param>
        /// <param name="parent">親IAnalyzeItem</param>
        /// <param name="container">イベントコンテナ</param>
        public ItemIf(IfStatementSyntax node, SemanticModel semanticModel, IAnalyzeItem parent, EventContainer container) : base(parent, node, semanticModel, container)
        {
            ItemType = ItemTypes.MethodStatement;

            var condition = semanticModel.GetOperation(node.Condition);

            Conditions.AddRange(OperationFactory.GetExpressionList(condition, container));

            TrueBlock.AddRange(GetBlock(node.Statement, semanticModel));
            FalseBlocks.AddRange(GetElseBlock(node.Else));

            List <IAnalyzeItem> GetElseBlock(ElseClauseSyntax elseNode)
            {
                var result = new List <IAnalyzeItem>();

                if (elseNode is null)
                {
                    return(result);
                }

                result.Add(ItemFactory.Create(elseNode, semanticModel, container, parent));

                // else ifの場合はさらに続ける
                if (elseNode.Statement is IfStatementSyntax ifNode)
                {
                    result.AddRange(GetElseBlock(ifNode.Else));
                }

                return(result);
            }
        }
Example #2
0
File: if.cs Project: formist/LinkMe
 public override AstNode Clone()
 {
     return(new IfNode(
                (Context == null ? null : Context.Clone()),
                Parser,
                (Condition == null ? null : Condition.Clone()),
                (TrueBlock == null ? null : TrueBlock.Clone()),
                (FalseBlock == null ? null : FalseBlock.Clone())
                ));
 }
Example #3
0
        public override void Calculate(CalculationItemStack calculationStack, bool isToAdd = true)
        {
            Condition.Calculate(calculationStack, false);

            if (Condition.IsTrue.Value)
            {
                TrueBlock.Calculate(calculationStack);
            }
            else
            {
                if (null != FalseBlock)
                {
                    FalseBlock.Calculate(calculationStack);
                }
            }
        }
Example #4
0
 public override void Walk(AstVisitor visitor)
 {
     if (visitor.Walk(this))
     {
         m_condition.Walk(visitor);
         if (TrueBlock != null)
         {
             TrueBlock.Walk(visitor);
         }
         if (FalseBlock != null)
         {
             FalseBlock.Walk(visitor);
         }
     }
     visitor.PostWalk(this);
 }
        public override List <InstructionModel> GetInstructions(CompilationModel model)
        {
            var trueBlockInstructions  = TrueBlock.GetInstructions(model);
            var falseBlockInstructions = TrueBlock.GetInstructions(model);
            var jmpToFalse             = (trueBlockInstructions.Count + 2).ToString(CultureInfo.InvariantCulture); // + 1 to following + 1 added jump
            var jmpOver = (falseBlockInstructions.Count + 1).ToString(CultureInfo.InvariantCulture);

            var instructions = new List <InstructionModel>();

            instructions.Add(new InstructionModel(Instructions.PushIntInstruction, "1"));
            instructions.Latest().Comment = model.GetComment(this);
            instructions.AddRange(Expression.GetInstructions(model));
            instructions.Add(new InstructionModel(Instructions.IfIntEqInstruction, jmpToFalse));
            instructions.AddRange(trueBlockInstructions);
            instructions.Add(new InstructionModel(Instructions.JumpInstruction, jmpOver));
            return(instructions);
        }
Example #6
0
File: if.cs Project: formist/LinkMe
 internal override bool EncloseBlock(EncloseBlockType type)
 {
     // if there's an else block, recurse down that branch
     if (FalseBlock != null)
     {
         return(FalseBlock.EncloseBlock(type));
     }
     else if (type == EncloseBlockType.IfWithoutElse)
     {
         // there is no else branch -- we might have to enclose the outer block
         return(true);
     }
     else if (TrueBlock != null)
     {
         return(TrueBlock.EncloseBlock(type));
     }
     return(false);
 }
Example #7
0
 internal override bool EncloseBlock(EncloseBlockType type)
 {
     // if there's an else block, recurse down that branch.
     // if we aren't forcing braces and the block contains nothing, then we don't
     // really have a false block.
     if (FalseBlock != null && (FalseBlock.ForceBraces || FalseBlock.Count > 0))
     {
         return(FalseBlock.EncloseBlock(type));
     }
     else if (type == EncloseBlockType.IfWithoutElse)
     {
         // there is no else branch -- we might have to enclose the outer block
         return(true);
     }
     else if (TrueBlock != null)
     {
         return(TrueBlock.EncloseBlock(type));
     }
     return(false);
 }
Example #8
0
File: if.cs Project: formist/LinkMe
        public override string ToCode(ToCodeFormat format)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("if(");
            sb.Append(Condition.ToCode());
            sb.Append(')');

            // if we're in Safari-quirks mode, we will need to wrap the if block
            // in curly braces if it only includes a function declaration. Safari
            // throws parsing errors in those situations
            ToCodeFormat elseFormat = ToCodeFormat.Normal;

            if (FalseBlock != null && FalseBlock.Count == 1)
            {
                if (Parser.Settings.MacSafariQuirks &&
                    FalseBlock[0] is FunctionObject)
                {
                    elseFormat = ToCodeFormat.AlwaysBraces;
                }
                else if (FalseBlock[0] is IfNode)
                {
                    elseFormat = ToCodeFormat.ElseIf;
                }
            }

            // get the else block -- we need to know if there is anything in order
            // to fully determine if the true-branch needs curly-braces
            string elseBlock = (
                FalseBlock == null
                ? string.Empty
                : FalseBlock.ToCode(elseFormat));

            // we'll need to force the true block to be enclosed in curly braces if
            // there is an else block and the true block contains a single statement
            // that ends in an if that doesn't have an else block
            ToCodeFormat trueFormat = (FalseBlock != null &&
                                       TrueBlock != null &&
                                       TrueBlock.EncloseBlock(EncloseBlockType.IfWithoutElse)
                ? ToCodeFormat.AlwaysBraces
                : ToCodeFormat.Normal);

            if (elseBlock.Length > 0 &&
                TrueBlock != null &&
                TrueBlock.EncloseBlock(EncloseBlockType.SingleDoWhile))
            {
                trueFormat = ToCodeFormat.AlwaysBraces;
            }

            // if we're in Safari-quirks mode, we will need to wrap the if block
            // in curly braces if it only includes a function declaration. Safari
            // throws parsing errors in those situations
            if (Parser.Settings.MacSafariQuirks &&
                TrueBlock != null &&
                TrueBlock.Count == 1 &&
                TrueBlock[0] is FunctionObject)
            {
                trueFormat = ToCodeFormat.AlwaysBraces;
            }

            // add the true block
            string trueBlock = (
                TrueBlock == null
                ? string.Empty
                : TrueBlock.ToCode(trueFormat));

            sb.Append(trueBlock);

            if (elseBlock.Length > 0)
            {
                if (trueFormat != ToCodeFormat.AlwaysBraces &&
                    !trueBlock.EndsWith(";", StringComparison.Ordinal) &&
                    (TrueBlock == null || TrueBlock.RequiresSeparator))
                {
                    sb.Append(';');
                }

                // if we are in pretty-print mode, drop the else onto a new line
                Parser.Settings.NewLine(sb);
                sb.Append("else");
                // if the first character could be interpreted as a continuation
                // of the "else" statement, then we need to add a space
                if (JSScanner.StartsWithIdentifierPart(elseBlock))
                {
                    sb.Append(' ');
                }

                sb.Append(elseBlock);
            }
            return(sb.ToString());
        }
Example #9
0
        /// <summary>
        /// Parse all conditions in template
        /// </summary>
        private void ParseConditions()
        {
            int idxPrevious = 0;
            int idxCurrent  = 0;

            this._ParsedBlock = "";
            while ((idxCurrent = this._strTemplateBlock.IndexOf(this.ConditionTagIfBegin, idxCurrent)) != -1)
            {
                string VarName;
                string TrueBlock, FalseBlock;
                string ElseStatment, EndIfStatment;
                int    idxIfBegin, idxIfEnd, idxElseBegin, idxEndIfBegin;
                bool   boolValue;

                idxIfBegin  = idxCurrent;
                idxCurrent += this.ConditionTagIfBegin.Length;

                // Searching for EndIf Index

                idxIfEnd = this._strTemplateBlock.IndexOf(this.ConditionTagIfEnd, idxCurrent);
                if (idxIfEnd == -1)
                {
                    throw new Exception("Could not find ConditionTagIfEnd");
                }

                // Getting Value Name

                VarName = this._strTemplateBlock.Substring(idxCurrent, (idxIfEnd - idxCurrent));

                idxCurrent = idxIfEnd + this.ConditionTagIfEnd.Length;

                // Compare ElseIf and EndIf Indexes

                ElseStatment  = this.ConditionTagElseBegin + VarName + this.ConditionTagElseEnd;
                EndIfStatment = this.ConditionTagEndIfBegin + VarName + this.ConditionTagEndIfEnd;
                idxElseBegin  = this._strTemplateBlock.IndexOf(ElseStatment, idxCurrent);
                idxEndIfBegin = this._strTemplateBlock.IndexOf(EndIfStatment, idxCurrent);
                if (idxElseBegin > idxEndIfBegin)
                {
                    throw new Exception("Condition Else Tag placed after Condition Tag EndIf for '" + VarName + "'");
                }

                // Getting True and False Condition Blocks

                if (idxElseBegin != -1)
                {
                    TrueBlock  = this._strTemplateBlock.Substring(idxCurrent, (idxElseBegin - idxCurrent));
                    FalseBlock = this._strTemplateBlock.Substring((idxElseBegin + ElseStatment.Length), (idxEndIfBegin - idxElseBegin - ElseStatment.Length));
                }
                else
                {
                    TrueBlock  = this._strTemplateBlock.Substring(idxCurrent, (idxEndIfBegin - idxCurrent));
                    FalseBlock = "";
                }

                // Parse Condition

                try
                {
                    boolValue = Convert.ToBoolean(this._hstValues[VarName]);
                }
                catch
                {
                    boolValue = false;
                }

                string BeforeBlock = this._strTemplateBlock.Substring(idxPrevious, (idxIfBegin - idxPrevious));

                if (this._hstValues.ContainsKey(VarName) && boolValue)
                {
                    this._ParsedBlock += BeforeBlock + TrueBlock.Trim();
                }
                else
                {
                    this._ParsedBlock += BeforeBlock + FalseBlock.Trim();
                }

                idxCurrent  = idxEndIfBegin + EndIfStatment.Length;
                idxPrevious = idxCurrent;
            }
            this._ParsedBlock += this._strTemplateBlock.Substring(idxPrevious);
        }
Example #10
0
        protected override object OnExecute(IExecutionContext ctx, params object[] parms)
        {
            var condValue = IsTrue(ctx);

            return(condValue ? TrueBlock.InvokeGeneric(ctx) : FalseBlock?.InvokeGeneric(ctx));
        }
Example #11
0
 public override void Dispose()
 {
     Condition.Dispose();
     TrueBlock.Dispose();
     FalseBlock?.Dispose();
 }