示例#1
0
 public void VisitNode(SelectionStatement node)
 {
     Console.Write("<" + node.ClassName() + ">: ");
     Console.ForegroundColor = ConsoleColor.Green;
     Console.Write("IF");
     Console.ResetColor();
     PrintAttribute(node);
 }
示例#2
0
        private void VisitNode(SelectionStatement node)
        {
            AbstractNode ifExp    = node.Child;
            AbstractNode thanStmt = ifExp.Sib;
            AbstractNode elseStmt = thanStmt.Sib;   // may be null

            ifExp.Accept(this);
            ++_ifCount;
            File.WriteLine("brfalse.s {0}{1}", IF_FALSE, _ifCount);

            thanStmt.Accept(this);
            File.WriteLine("br.s {0}{1}", IF_END, _ifCount);

            File.WriteLine(IF_FALSE + _ifCount + ":");
            elseStmt.Accept(this);
            File.WriteLine(IF_END + _ifCount + ":");
        }
示例#3
0
        private void VisitNode(SelectionStatement node)
        {
            AbstractNode ifExp    = node.Child;
            AbstractNode thanStmt = ifExp.Sib;
            AbstractNode elseStmt = thanStmt.Sib;   // may be null

            SelectionStatementDescriptor ssDesc =
                new SelectionStatementDescriptor();

            String errMsg = "";

            // if expression
            ifExp.Accept(this);
            ssDesc.IfDescriptor = ifExp.TypeDescriptor;
            PrimitiveTypeBooleanDescriptor ifBoolDesc =
                ifExp.TypeDescriptor as PrimitiveTypeBooleanDescriptor;

            if (ifBoolDesc == null)
            {
                ifExp.TypeDescriptor = new ErrorDescriptor("If statement " +
                                                           "does not evaluate to a Boolean expression. (Has type: " +
                                                           ifExp.TypeDescriptor.GetType().Name + ")");
            }
            // than statement
            thanStmt.Accept(this);
            thanStmt.TypeDescriptor = thanStmt.Child.TypeDescriptor;
            ssDesc.ThanDescriptor   = thanStmt.TypeDescriptor;
            if (!IsCompatibleStatement(thanStmt.TypeDescriptor))
            {
                if (errMsg.Length > 0)
                {
                    errMsg += "\n";
                }
                errMsg += "Non-compatible THAN statement type: " +
                          thanStmt.TypeDescriptor.GetType().Name;
            }
            // else statement
            if (elseStmt != null)
            {
                elseStmt.Accept(this);
                ssDesc.HasElseStmt      = true;
                elseStmt.TypeDescriptor = elseStmt.Child.TypeDescriptor;
                ssDesc.ElseDescriptor   = elseStmt.TypeDescriptor;
                if (!IsCompatibleStatement(elseStmt.TypeDescriptor))
                {
                    if (errMsg.Length > 0)
                    {
                        errMsg += "\n";
                    }
                    errMsg += "Non-compatible ELSE statement type: " +
                              elseStmt.TypeDescriptor.GetType().Name;
                }
            }
            else
            {
                ssDesc.HasElseStmt = false;
            }

            // if any components have errors, propogate them up the tree
            if (ifExp.TypeDescriptor is ErrorDescriptor ||
                thanStmt.TypeDescriptor is ErrorDescriptor ||
                elseStmt?.TypeDescriptor is ErrorDescriptor)
            {
                // creates an error containing any and all errors lower in tree
                if (ifExp.TypeDescriptor is ErrorDescriptor)
                {
                    ssDesc.TypeDescriptor = ifExp.TypeDescriptor;
                    if (thanStmt.TypeDescriptor is ErrorDescriptor)
                    {
                        ((ErrorDescriptor)ssDesc.TypeDescriptor).CombineErrors
                            ((ErrorDescriptor)thanStmt.TypeDescriptor);
                    }
                    if (elseStmt?.TypeDescriptor is ErrorDescriptor)
                    {
                        ((ErrorDescriptor)ssDesc.TypeDescriptor).CombineErrors
                            ((ErrorDescriptor)elseStmt.TypeDescriptor);
                    }
                }
                else if (thanStmt.TypeDescriptor is ErrorDescriptor)
                {
                    ssDesc.TypeDescriptor = thanStmt.TypeDescriptor;
                    if (elseStmt?.TypeDescriptor is ErrorDescriptor)
                    {
                        ((ErrorDescriptor)ssDesc.TypeDescriptor).CombineErrors
                            ((ErrorDescriptor)elseStmt.TypeDescriptor);
                    }
                }
                else
                {
                    ssDesc.TypeDescriptor = elseStmt.TypeDescriptor;
                }
            }
            // if IF/THAN/ELSE was incompatible, create new error
            else if (errMsg.Length > 0)
            {
                ssDesc.TypeDescriptor = new ErrorDescriptor(errMsg);
            }
            // otherwise assign evaluated boolean to selection statement desc
            else
            {
                ssDesc.TypeDescriptor = ifExp.TypeDescriptor;
            }
            node.TypeDescriptor = ssDesc;
        }
示例#4
0
 private void VisitNode(SelectionStatement node)
 {
     node.Accept(SemanticsVisitor);
 }