Esempio n. 1
0
 protected override void VisitSelectSyntax(SelectSyntax pNode)
 {
     using (var c = Store.AddValue("CaseType", pNode.Condition.Type))
     {
         base.VisitSelectSyntax(pNode);
     }
 }
Esempio n. 2
0
 protected virtual void VisitSelectSyntax(SelectSyntax pNode)
 {
     Visit(pNode.Condition);
     foreach (var c in pNode.Cases)
     {
         Visit(c);
     }
 }
        protected override void VisitSelectSyntax(SelectSyntax pNode)
        {
            Visit(pNode.Condition);
            _itType = pNode.Condition.Type;

            foreach (var c in pNode.Cases)
            {
                Visit(c);
            }
        }
Esempio n. 4
0
        protected virtual SyntaxNode VisitSelectSyntax(SelectSyntax pNode)
        {
            var cond = Visit(pNode.Condition);

            List <CaseSyntax> cases = new List <CaseSyntax>(pNode.Cases.Count);

            foreach (var c in pNode.Cases)
            {
                cases.Add((CaseSyntax)Visit(c));
            }
            return(SyntaxFactory.Select(cond, cases));
        }
Esempio n. 5
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    _syntax.Dispose();
                    _syntax = null;
                }

                disposedValue = true;
            }
        }
        protected override void VisitSelectSyntax(SelectSyntax pNode)
        {
            //Validate if a select is marked as complete, ensure all enum values are used
            if (pNode.Annotation.Value == KeyAnnotations.Complete)
            {
                var t = pNode.Condition.Type;
                if (!t.IsEnum)
                {
                    CompilerErrors.CompleteNonEnum(pNode.Condition.Span);
                }
                else
                {
                    //Get all enum values
                    //We will indicate a field has been found by making the string null
                    List <string> fields = new List <string>();
                    foreach (var f in t.GetFields())
                    {
                        fields.Add(f.Name);
                    }

                    foreach (var c in pNode.Cases)
                    {
                        //Default covers all possible cases so mark all as found
                        if (c.IsDefault)
                        {
                            for (int i = 0; i < fields.Count; i++)
                            {
                                fields[i] = null;
                            }
                        }
                        else
                        {
                            foreach (var cd in c.Conditions)
                            {
                                //We can only check numeric literals and enum access
                                //We cannot validate returning from a method
                                if (cd is NumericLiteralSyntax n)
                                {
                                    for (int i = 0; i < fields.Count; i++)
                                    {
                                        if (t.GetEnumValue(fields[i]) == int.Parse(n.Value))
                                        {
                                            fields[i] = null;
                                        }
                                    }
                                }
                                else if (cd is MemberAccessSyntax m)
                                {
                                    for (int i = 0; i < fields.Count; i++)
                                    {
                                        if (fields[i] == m.Value.Value)
                                        {
                                            fields[i] = null;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    fields = fields.Where((f) => f != null).ToList();
                    if (fields.Count > 0)
                    {
                        CompilerErrors.SelectComplete(fields.Aggregate(new StringBuilder(), (c, f) =>
                        {
                            if (c.Length > 0)
                            {
                                c.Append(", ");
                            }
                            c.Append(f);
                            return(c);
                        }).ToString(), pNode.Span);
                    }
                }
            }
            base.VisitSelectSyntax(pNode);
        }
Esempio n. 7
0
        protected override SyntaxNode VisitSelectSyntax(SelectSyntax pNode)
        {
            var rw = _rewrite;

            _rewrite = false;

            //Save itVar in case we hit a nested for or select statement
            var it = _itVar;

            _itVar = pNode.Condition;
            SyntaxNode retval = base.VisitSelectSyntax(pNode);

            if (_rewrite)
            {
                if (pNode.Annotation.Value == KeyAnnotations.Complete)
                {
                    CompilerErrors.IgnoredComplete(pNode.Span);
                }

                //Only rewrite if we have "it"
                for (int i = pNode.Cases.Count - 1; i >= 0; i--)
                {
                    var currentCase = pNode.Cases[i];
                    //Default cause needs to be the last one. Make a else statement
                    if (currentCase.IsDefault)
                    {
                        _currentElse = SyntaxFactory.Else(null, currentCase.Body);
                    }
                    else
                    {
                        //The condition needs to be a comparison binary expression
                        SyntaxNode baseExpression = Visit(currentCase.Conditions[0]);
                        if (!IsComparison(baseExpression))
                        {
                            //If it isn't make it one
                            baseExpression = SyntaxFactory.BinaryExpression(_itVar, BinaryExpressionOperator.Equals, baseExpression);
                            ((BinaryExpressionSyntax)baseExpression).SetType(SmallTypeCache.Boolean);
                        }

                        for (int j = 0; j < currentCase.Conditions.Count - 1; j++)
                        {
                            var newExpression = currentCase.Conditions[j + 1];
                            if (!IsComparison(newExpression))
                            {
                                //If it isn't make it one
                                newExpression = SyntaxFactory.BinaryExpression(_itVar, BinaryExpressionOperator.Equals, newExpression);
                                ((BinaryExpressionSyntax)newExpression).SetType(SmallTypeCache.Boolean);
                            }

                            baseExpression = SyntaxFactory.BinaryExpression(baseExpression, BinaryExpressionOperator.Or, newExpression);
                            ((BinaryExpressionSyntax)baseExpression).SetType(SmallTypeCache.Boolean);
                        }

                        //Visit body so we can rewrite any "it"
                        var b = (BlockSyntax)Visit(currentCase.Body);
                        _currentIf = SyntaxFactory.If(baseExpression, b, _currentElse);

                        if (i > 0)
                        {
                            //As long as this isn't the last statement, make an else if
                            _currentElse = SyntaxFactory.Else(_currentIf, null);
                        }
                    }
                }

                retval = _currentIf;
            }

            _itVar   = it;
            _rewrite = rw;
            return(retval);
        }