internal static ConditionalBlock CurrentBlock(this ConditionalBlock block)
 {
     if (block.IsComplete)
     {
         return(null);
     }
     if (block.Else != null &&
         block.Else.HasIncompleteChildren())
     {
         return(((IList <ConditionalBlock>)block.Else.ChildBlocks).CurrentBlock());
     }
     if (block.ElseIfs.Count > 0)
     {
         var lastElseIf = block.ElseIfs.Last();
         if (lastElseIf.HasIncompleteChildren())
         {
             return(((IList <ConditionalBlock>)lastElseIf.ChildBlocks).CurrentBlock());
         }
     }
     if (block.If != null &&
         block.If.HasIncompleteChildren())
     {
         return(((IList <ConditionalBlock>)block.If.ChildBlocks).CurrentBlock());
     }
     // If we get to here then there are no child blocks and this must be the current one.
     return(block);
 }
 internal static bool IsElseStatementActive(this ConditionalBlock block)
 {
     if (IsConditionalNodeActive(block.If))
     {
         return(false);
     }
     if (block.ElseIfs.Any(s => IsConditionalNodeActive(s)))
     {
         return(false);
     }
     return(true);
 }
Exemple #3
0
        internal void ExecutePreprocessorStatement(string line)
        {
            if (line.Contains("\n") || line.Contains("\r"))
            {
                throw new ArgumentException("Parameter line cannot contain line break characters.");
            }

            SetupParser(line);
            var tree = parser.preprocessorStatement();

            // TODO handle syntax errors.

            // depending on the type of statement take the appropriate action.
            if (tree.constantDeclaration() != null)
            {
                compilerConstants.Add(tree.constantDeclaration());
                // TODO handle syntax errors.
            }
            else
            {
                var current = conditionalBlocks.CurrentBlock();
                if (current == null)
                {
                    current = new ConditionalBlock();
                    conditionalBlocks.Add(current);
                }

                if (tree.ifStatement() != null)
                {
                    var result = evaluator.Visit(tree);
                    current.AddNode(tree.ifStatement(), result);
                }
                else if (tree.elseIfStatement() != null)
                {
                    var result = evaluator.Visit(tree);
                    current.AddNode(tree.elseIfStatement(), result);
                }
                else if (tree.elseStatement() != null)
                {
                    var result = current.IsElseStatementActive();
                    current.AddNode(tree.elseStatement(), result);
                }
                else if (tree.endIfStatement() != null)
                {
                    current.AddNode(tree.endIfStatement(), null);
                }
                else
                {
                    throw new Exception("Unknown node type.");
                }
                // TODO handle syntax errors.
            }
        }
 internal static bool IsActive(this ConditionalBlock block)
 {
     if (block.EndIf != null)
     {
         return(false);
     }
     if (block.Else != null)
     {
         return(((ConditionalNode <PreprocessorParser.ElseStatementContext>)block.Else).IsActive());
     }
     if (block.ElseIfs.Count > 0)
     {
         return(((ConditionalNode <PreprocessorParser.ElseIfStatementContext>)block.ElseIfs.Last()).IsActive());
     }
     if (block.If != null)
     {
         return(((ConditionalNode <PreprocessorParser.IfStatementContext>)block.If).IsActive());
     }
     throw new Exception("Unknown node type.");
 }