コード例 #1
0
        public void CompoundStatementProducesBlockWithStatements()
        {
            var c = new CompoundStatement(new NullExpression(), new NullExpression(), new ReturnStatement());

            Assert.AreEqual(3, c.Statements.Count);
            Assert.AreEqual("{null;null;return;}", c.ToString());
        }
コード例 #2
0
        /// <summary>
        /// Generates the code for a CompoundStatement node.
        /// </summary>
        /// <param name="cs">The CompoundStatement node.</param>
        /// <returns>String containing C# code for CompoundStatement cs.</returns>
        private void GenerateCompoundStatement(SYMBOL previousSymbol, CompoundStatement cs, StringBuilder sb)
        {
            // opening brace
            GenerateIndentedLine("{", sb);
            m_braceCount++;

            if (m_insertCoopTerminationChecks)
            {
                // We have to check in event functions as well because the user can manually call these.
                if (previousSymbol is GlobalFunctionDefinition ||
                    previousSymbol is WhileStatement ||
                    previousSymbol is DoWhileStatement ||
                    previousSymbol is ForLoop ||
                    previousSymbol is StateEvent)
                {
                    GenerateIndentedLine(m_coopTerminationCheck, sb);
                }
            }

            foreach (SYMBOL kid in cs.kids)
            {
                GenerateNodeToSB(cs, kid, sb);
            }

            // closing brace
            m_braceCount--;
            GenerateIndentedLine("}", sb);
        }
コード例 #3
0
        public void CompoundStatementProducesEmptyBlock()
        {
            var c = new CompoundStatement();

            Assert.AreEqual(0, c.Statements.Count);
            Assert.AreEqual("{}", c.ToString());
        }
コード例 #4
0
        public void CompoundStatementProducesEmptyBlock()
        {
            var c = new CompoundStatement();

            Assert.AreEqual(0, c.Statements.Count);
            Assert.AreEqual("{}", c.ToString());
        }
コード例 #5
0
 public ForStatement(Statement initializer, Expression condition, Expression afterLoop, CompoundStatement body)
 {
     Initializer = initializer;
     Condition   = condition;
     AfterLoop   = afterLoop;
     Body        = body;
 }
コード例 #6
0
            public override void Visit(CompoundStatement compoundStatement)
            {
                bool suppressNewLine = mSuppressCompoundStatementNewline.Count != 0 && mSuppressCompoundStatementNewline.Pop();

                if (mInsideLine)
                {
                    WriteNewLine();
                }

                WriteIndentedLine("{");

                IncreaseIndentation();
                base.Visit(compoundStatement);
                DecreaseIndentation();

                if (mTabLevel != 0 && mInsideLine)
                {
                    WriteNewLine();
                }

                WriteIndentedLine("}");

                if (!suppressNewLine)
                {
                    WriteNewLine();
                }
            }
コード例 #7
0
        /**
         * a = 2 + 3 * 5; b = a - 4 / 2 + 7; Print(b)
         *
         */
        private static IStatement Example2()
        {
            IStatement ret;
            IStatement st1 = new AssignmentStatement("a",
                                                     new ArithmeticExpression(
                                                         new ConstantExpression(2),
                                                         '+',
                                                         new ArithmeticExpression(
                                                             new ConstantExpression(3),
                                                             '*',
                                                             new ConstantExpression(5))
                                                         ));
            IStatement st2 = new AssignmentStatement("b",
                                                     new ArithmeticExpression(
                                                         new ArithmeticExpression(
                                                             new VariableExpression("a"),
                                                             '-',
                                                             new ArithmeticExpression(
                                                                 new ConstantExpression(4),
                                                                 '/',
                                                                 new ConstantExpression(2)
                                                                 )),
                                                         '+',
                                                         new ConstantExpression(7)));
            IStatement st3 = new PrintStatement(new VariableExpression("b"));

            ret = new CompoundStatement(st1, new CompoundStatement(st2, st3));

            return(ret);
        }
コード例 #8
0
 /// <summary>
 /// Transforms a compound statement. The default implementation re-directs to its child statements.
 /// </summary>
 /// <param name="stmt">compount statement</param>
 public virtual void AcceptCompoundStatement(CompoundStatement stmt)
 {
     foreach (Statement child in stmt.Statements)
     {
         child.Accept(this);
     }
 }
コード例 #9
0
        static void Main(string[] args)
        {
            // v = 2; print(v)
            IStatement ex1 = new CompoundStatement(new AssignmentStatement("v", new ConstantExpression(2)), new PrintStatement(new VariableExpression("v")));

            // open csharptestlogfile.txt, write the numbers on the first 2 lines and -1
            IStatement ex2 = new CompoundStatement(new OpenRFileStatement("var_f", "csharptestlogfile.txt"),
                                                   new CompoundStatement(new ReadRFileStatement(new VariableExpression("var_f"), "var_c"),
                                                                         new CompoundStatement(new PrintStatement(new VariableExpression("var_c")),
                                                                                               new CompoundStatement(new IfStatement(new VariableExpression("var_c"),
                                                                                                                                     new CompoundStatement(new ReadRFileStatement(new VariableExpression("var_f"), "var_c"), new PrintStatement(new VariableExpression("var_c"))),
                                                                                                                                     new PrintStatement(new ConstantExpression(0))), new PrintStatement(new ConstantExpression(-1))))));

            ProgramState          ps1   = new ProgramState(ex1);
            IRepository           repo1 = new Repository(ps1);
            InterpreterController ctrl1 = new InterpreterController(repo1);

            ProgramState          ps2   = new ProgramState(ex2);
            IRepository           repo2 = new Repository(ps2);
            InterpreterController ctrl2 = new InterpreterController(repo2);

            TextMenu menu = new TextMenu();

            menu.addCommand(new ExitCommand("0", "Exit"));
            menu.addCommand(new RunExample("1", ex1.ToString(), ctrl1));
            menu.addCommand(new RunExample("2", ex2.ToString(), ctrl2));

            menu.show();
        }
コード例 #10
0
        public void CompoundStatementProducesBlockWithStatements()
        {
            var c = new CompoundStatement(new NullExpression(), new NullExpression(), new ReturnStatement());

            Assert.AreEqual(3, c.Statements.Count);
            Assert.AreEqual("{null;null;return;}", c.ToString());
        }
コード例 #11
0
 public virtual void Visit(CompoundStatement compoundStatement)
 {
     foreach (var statement in compoundStatement)
     {
         Visit(statement);
     }
 }
コード例 #12
0
        public void AcceptCompoundStatement(CompoundStatement stmt)
        {
            List<Statement> newBody = new List<Statement>();
            int count = stmt.Statements.Count;
            for (int i = 0; i < count; i++)
            {
                stmt.Statements[i].Accept(this);

                if (count != 2 &&
                    i < count - 1 &&
                    (stmt.Statements[i] is StoreStatement) &&
                    (stmt.Statements[i + 1] is LoopBlock))
                {
                    stmt.Statements[i + 1].Accept(this);
                    CompoundStatement cs = new CompoundStatement();
                    cs.Statements.Add(stmt.Statements[i]);
                    cs.Statements.Add(stmt.Statements[i + 1]);
                    newBody.Add(cs);
                    i++; // skip loop statement as it is processed here
                }
                else
                    newBody.Add(stmt.Statements[i]);
            }
            stmt.Statements.Clear();
            stmt.Statements.AddRange(newBody);
        }
コード例 #13
0
        public void AcceptCompoundStatement(CompoundStatement stmt)
        {
            List <Statement> newBody = new List <Statement>();
            int count = stmt.Statements.Count;

            for (int i = 0; i < count; i++)
            {
                stmt.Statements[i].Accept(this);

                if (count != 2 &&
                    i < count - 1 &&
                    (stmt.Statements[i] is StoreStatement) &&
                    (stmt.Statements[i + 1] is LoopBlock))
                {
                    stmt.Statements[i + 1].Accept(this);
                    CompoundStatement cs = new CompoundStatement();
                    cs.Statements.Add(stmt.Statements[i]);
                    cs.Statements.Add(stmt.Statements[i + 1]);
                    newBody.Add(cs);
                    i++; // skip loop statement as it is processed here
                }
                else
                {
                    newBody.Add(stmt.Statements[i]);
                }
            }
            stmt.Statements.Clear();
            stmt.Statements.AddRange(newBody);
        }
コード例 #14
0
        static void Main(string[] args)
        {
            IStatement ex1 = new CompoundStatement(new AssignStatement("v", new ConstantExpression(2)),
                                                   new PrintStatement(new VariableExpression("v")));

            IStatement ex2 = new CompoundStatement(new AssignStatement("a", new ArithmeticExpression(new ConstantExpression(2), new
                                                                                                     ArithmeticExpression(new ConstantExpression(3), new ConstantExpression(5), Operation.MULTIPLY), Operation.ADD)),
                                                   new CompoundStatement(new AssignStatement("b", new ArithmeticExpression(new VariableExpression("a"), new
                                                                                                                           ConstantExpression(1), Operation.ADD)), new PrintStatement(new VariableExpression("b"))));

            IStatement ex3 = new CompoundStatement(new AssignStatement("a", new ArithmeticExpression(new ConstantExpression(2), new ConstantExpression(2), Operation.SUBTRACT)),
                                                   new CompoundStatement(new IfStatement(new VariableExpression("a"), new AssignStatement("v", new ConstantExpression(2)),
                                                                                         new AssignStatement("v", new ConstantExpression(3))), new PrintStatement(new VariableExpression("v"))));

            IStatement ex4 = new CompoundStatement(new OpenStatement("var_f", "test1.in"),
                                                   new CompoundStatement(new ReadStatement(new VariableExpression("var_f"), "var_c"),
                                                                         new CompoundStatement(new PrintStatement(new VariableExpression("var_c")),
                                                                                               new CompoundStatement(new IfStatement(new VariableExpression("var_c"),
                                                                                                                                     new CompoundStatement(new ReadStatement(new VariableExpression("var_f"), "var_c"),
                                                                                                                                                           new PrintStatement(new VariableExpression("var_c"))), new PrintStatement(new ConstantExpression(0))),
                                                                                                                     new CloseStatement(new VariableExpression("var_f"))))));

            TextMenu menu = new TextMenu(new MyDictionary <string, Command>(new Dictionary <string, Command>()));

            menu.AddCommand(new ExitCommand("0", "exit"));
            menu.AddCommand(new RunCommand("1", ex1.ToString(), CreateController(ex1, "log1.txt")));
            menu.AddCommand(new RunCommand("2", ex2.ToString(), CreateController(ex2, "log2.txt")));
            menu.AddCommand(new RunCommand("3", ex3.ToString(), CreateController(ex3, "log3.txt")));
            menu.AddCommand(new RunCommand("4", ex4.ToString(), CreateController(ex4, "log4.txt")));

            menu.show();
        }
コード例 #15
0
        void ButtonCompoundClick(object sender, EventArgs e)
        {
            Statement firstStatement  = null;
            Statement secondStatement = null;

            using (StatementForm sf = new StatementForm("First Compound Statement"))
            {
                if (sf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    firstStatement = sf.getStatement();
                    using (StatementForm sf2 = new StatementForm("Second Compound"))
                    {
                        if (sf2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            secondStatement = sf2.getStatement();
                        }
                    }
                }
            }
            CompoundStatement cs = new CompoundStatement();

            cs.setS1(firstStatement);
            cs.setS2(secondStatement);
            this.stmt = cs;
            finished();
        }
コード例 #16
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     stmt.Statements.Accept(this);
     List<Statement> list = new List<Statement>(
         stmt.Statements.Where(s => !s.Equals(Match)));
     stmt.Statements.Clear();
     stmt.Statements.AddRange(list);
 }
コード例 #17
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     CreateLabelForNextInstruction(stmt);
     foreach (Statement substmt in stmt.Statements)
     {
         substmt.Accept(this);
     }
 }
コード例 #18
0
        public void AcceptCompoundStatement(CompoundStatement stmt)
        {
            stmt.Statements.Accept(this);
            List <Statement> list = new List <Statement>(
                stmt.Statements.Where(s => !s.Equals(Match)));

            stmt.Statements.Clear();
            stmt.Statements.AddRange(list);
        }
コード例 #19
0
        public bool Visit(CompoundStatement node)
        {
            foreach (var nodeStatement in node.Statements)
            {
                nodeStatement.Accept(this);
            }

            return(true);
        }
コード例 #20
0
        /**
         * v = 2; Print(v)
         *
         */
        private static IStatement Example1()
        {
            IStatement ret;
            IStatement st1 = new AssignmentStatement("v", new ConstantExpression(2));
            IStatement st2 = new PrintStatement(new VariableExpression("v"));

            ret = new CompoundStatement(st1, st2);

            return(ret);
        }
コード例 #21
0
ファイル: UI.cs プロジェクト: S7012MY/toy-interpreter
        private Statement readCS()
        {
            CompoundStatement cs = new CompoundStatement();

            Console.WriteLine("Input first statement");
            cs.setS1(readStatement());
            Console.WriteLine("Input second statement");
            cs.setS2(readStatement());
            return(cs);
        }
コード例 #22
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     if (stmt.Statements.Count == 1)
     {
         stmt.Statements.Single().Accept(this);
     }
     else
     {
         Result = null;
     }
 }
コード例 #23
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     if (stmt.Statements.Count == 1)
     {
         stmt.Statements.Single().Accept(this);
     }
     else
     {
         Result = null;
     }
 }
 public override IEnumerable <string> Visit(CompoundStatement node)
 {
     foreach (var item in  node.Nodes <Statement>()[0].Accept(this))
     {
         yield return(item);
     }
     foreach (var item in node.Nodes <Statement>()[1].Accept(this))
     {
         yield return(item);
     }
 }
コード例 #25
0
 public void interpretNextMT()
 {
     for (int i = 1; i <= r.getNumProg(); ++i)
     {
         Statement st = r.getPS(i).es.top();
         r.getPS(i).es.pop();
         if (st is AssignStatement)
         {
             AssignStatement _as = (AssignStatement)st;
             r.getPS(i).vt.set(
                 _as.getVar(),
                 _as.getE().eval(r.getPS(i).vt, r.getPS(i).hp));
             r.getPS(i).vt.printTable();
         }
         else if (st is CompoundStatement)
         {
             CompoundStatement cs = (CompoundStatement)st;
             r.getPS(i).es.push(cs.getS2());
             r.getPS(i).es.push(cs.getS1());
         }
         else if (st is IfStatement)
         {
             IfStatement _is = (IfStatement)st;
             if (_is.getE().eval(r.getPS(i).vt, r.getPS(i).hp) != 0)
             {
                 r.getPS(i).es.push(_is.getS1());
             }
             else
             {
                 r.getPS(i).es.push(_is.getS2());
             }
         }
         else if (st is PrintStatement)
         {
             PrintStatement pr = (PrintStatement)st;
             r.getPS(i).ob.add(pr.getE().eval(r.getPS(i).vt, r.getPS(i).hp).ToString());
         }
         else if (st is ForkStatement)
         {
             r.addPs(r.getPS(i).ob, r.getPS(i).vt, ((ForkStatement)st).getS());
         }
         else if (st is WHStatement)
         {
             WHStatement wh   = (WHStatement)st;
             int         addr = r.getPS(i).vt.get(wh.getVN());
             r.getPS(i).hp.mod(addr, wh.getV().eval(r.getPS(i).vt, r.getPS(i).hp));
         }
         if (r.getPS(i).es.isEmpty())
         {
             r.removePs(i);
             --i;
         }
     }
 }
コード例 #26
0
        public AstPrinterNode Visit(CompoundStatement node)
        {
            var printer = new AstPrinterNode(node.ToString());

            foreach (var stmt in node.Statements)
            {
                printer.AddChild(stmt.Accept(this));
            }

            return(printer);
        }
コード例 #27
0
        /**
         * openRFile(f, "test.in");
         * openRFile(g, "test.in");
         * closeRFile(g);
         * closeRFile(f);
         *
         */
        private static IStatement Example5()
        {
            IStatement ret;
            IStatement st1 = new OpenRFileStatement("f", "..\\..\\res\\test.in");
            IStatement st2 = new OpenRFileStatement("g", "..\\..\\res\\test.in");
            IStatement st3 = new CloseRFileStatement(new VariableExpression("g"));
            IStatement st4 = new CloseRFileStatement(new VariableExpression("f"));

            ret = new CompoundStatement(st1, new CompoundStatement(st2, new CompoundStatement(st3, st4)));

            return(ret);
        }
コード例 #28
0
        bool ParseCompoundStatement(out CompoundStatement clauseStatement)
        {
            clauseStatement = new CompoundStatement();

            if (tokenReader.Expect(LexKind.BraceOpen))
            {
                tokenReader.Skip(1);
                clauseStatement.Statements = ParseStatements();
                return(true);
            }

            return(false);
        }
コード例 #29
0
        static void Main(string[] args)
        {
            IExeStack <Statement> exeStack = new ExeStack <Statement>();

            Model.ADT.IDictionary <string, int> symbolTable = new DictionaryC <string, int>();
            IOutput <int> messages        = new Output <int>();
            IFileTable <int, FileData> fd = new FileTable <int, FileData>();

            Statement s = new CompoundStatement(new AssignStatement("v", new ConstantExpression(2)), new PrintStatement(new VariableExpression("v")));

            exeStack.Push(s);
            PrgState      state = new PrgState(symbolTable, exeStack, messages, fd);
            IMyRepository repo  = new MyRepository(state);

            Controller.MyController ctrl = new Controller.MyController(repo);

            IExeStack <Statement> exeStack1 = new ExeStack <Statement>();

            Model.ADT.IDictionary <string, int> symbolTable1 = new DictionaryC <string, int>();
            IOutput <int> messages1        = new Output <int>();
            IFileTable <int, FileData> fd1 = new FileTable <int, FileData>();

            Statement s1 = new CompoundStatement(
                new CompoundStatement(
                    new CompoundStatement(
                        new OpenFileStatement("var_f", "C:\\FACULTATE\\MAP\\Lab7\\Lab7\\test.in"),

                        new ReadFileStatement(new VariableExpression("var_f"), "var_c")
                        ),
                    new CompoundStatement(
                        new PrintStatement(new VariableExpression("var_c")),
                        new IfStatement(
                            new VariableExpression("var_c"),
                            new PrintStatement(new ConstantExpression(0))
                            , new CompoundStatement(
                                new ReadFileStatement(new VariableExpression("var_f"), "var_c"),
                                new PrintStatement(new VariableExpression("var_c")))))), new CloseFileStatement(new VariableExpression("var_f")));

            exeStack1.Push(s1);
            PrgState      state1 = new PrgState(symbolTable1, exeStack1, messages1, fd1);
            IMyRepository repo1  = new MyRepository(state1);

            Controller.MyController ctrl1 = new Controller.MyController(repo1);

            TextMenu menu = new TextMenu();

            menu.addCommand(new ExitCommand("0", "exit"));
            //menu.addCommand(new RunExample("1", s.ToString(), ctrl));
            menu.addCommand(new RunExample("1", s1.ToString(), ctrl1));
            menu.show();
        }
コード例 #30
0
ファイル: Program.cs プロジェクト: TamasFlorin/UBB
        static IStatement MakeCompoundStatement(params IStatement [] statements)
        {
            Debug.Assert(statements.Length > 0);

            IStatement statement = statements[0];

            for (int i = 1; i < statements.Length; i++)
            {
                IStatement current = statements[i];
                statement = new CompoundStatement(statement, current);
            }

            return(statement);
        }
コード例 #31
0
        private bool TryResolveTypesInCompoundStatement(CompoundStatement compoundStatement)
        {
            LogTrace($"{nameof( TryResolveTypesInCompoundStatement )}( statement = {compoundStatement})");

            PushScope();

            foreach (var statement in compoundStatement)
            {
                if (!TryResolveTypesInStatement(statement))
                {
                    return(false);
                }
            }

            PopScope();

            return(true);
        }
コード例 #32
0
        /**
         * openRFile(var_f, "test.in");
         * readFile(var_f, var_c); print(var_c);
         * (if var_c then readFile(var_f, var_c); print(var_c); else print(0));
         * closeRFile(var_f);
         *
         */
        private static IStatement Example4()
        {
            IStatement ret;
            IStatement st1 = new OpenRFileStatement("var_f", "..\\..\\res\\test.in");
            IStatement st2 = new CompoundStatement(
                new ReadFileStatement(new VariableExpression("var_f"), "var_c"),
                new PrintStatement(new VariableExpression("var_c")));
            IStatement st3 = new IfStatement(
                new VariableExpression("var_c"),
                new CompoundStatement(
                    new ReadFileStatement(new VariableExpression("var_f"), "var_c"),
                    new PrintStatement(new VariableExpression("var_c"))),
                new PrintStatement(new ConstantExpression(0)));
            IStatement st4 = new CloseRFileStatement(new VariableExpression("var_f"));

            ret = new CompoundStatement(st1, new CompoundStatement(st2, new CompoundStatement(st3, st4)));

            return(ret);
        }
コード例 #33
0
ファイル: ScriptCompiler.cs プロジェクト: jhurliman/simian
        /// <summary>
        /// Generates the code for a CompoundStatement node.
        /// </summary>
        /// <param name="cs">The CompoundStatement node.</param>
        /// <returns>String containing C# code for CompoundStatement cs.</returns>
        private string GenerateCompoundStatement(CompoundStatement cs)
        {
            string retstr = String.Empty;

            // opening brace
            retstr += GenerateIndentedLine("{");
            m_braceCount++;

            foreach (SYMBOL kid in cs.kids)
            {
                retstr += GenerateNode(kid);
            }

            // closing brace
            m_braceCount--;
            retstr += GenerateIndentedLine("}");

            return(retstr);
        }
コード例 #34
0
        public void AcceptCompoundStatement(CompoundStatement stmt)
        {
            LoopBlock forLoop = stmt.AsForLoop(EForLoopLevel.Strict);

            if (forLoop != null)
            {
                Result = forLoop;
                if (forLoop.Trailer != null)
                {
                    forLoop.Trailer = forLoop.Trailer.Forify();
                }
            }
            else
            {
                for (int i = 0; i < stmt.Statements.Count; i++)
                {
                    stmt.Statements[i] = stmt.Statements[i].Forify();
                }
                Result = stmt;
            }
        }
コード例 #35
0
        public void interpretNext()
        {
            Statement st = r.ps.es.top();

            r.ps.es.pop();
            if (st is AssignStatement)
            {
                AssignStatement _as = (AssignStatement)st;
                r.ps.vt.set(
                    _as.getVar(),
                    _as.getE().eval(r.ps.vt, r.ps.hp));
                r.ps.vt.printTable();
            }
            else if (st is CompoundStatement)
            {
                CompoundStatement cs = (CompoundStatement)st;
                r.ps.es.push(cs.getS2());
                r.ps.es.push(cs.getS1());
            }
            else if (st is IfStatement)
            {
                IfStatement _is = (IfStatement)st;
                if (_is.getE().eval(r.ps.vt, r.ps.hp) != 0)
                {
                    r.ps.es.push(_is.getS1());
                }
                else
                {
                    r.ps.es.push(_is.getS2());
                }
            }
            else if (st is PrintStatement)
            {
                PrintStatement pr = (PrintStatement)st;
                r.ps.ob.add(pr.getE().eval(r.ps.vt, r.ps.hp).ToString());
            }
        }
コード例 #36
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     Check(stmt);
     stmt.Statements.Accept(this);
 }
コード例 #37
0
ファイル: CParser.Nodes.cs プロジェクト: soywiz/ilcc
 public SwitchStatement(PositionInfo PositionInfo, Expression ReferenceExpression, CompoundStatement Statements)
     : base(PositionInfo, ReferenceExpression, Statements)
 {
     this.ReferenceExpression = ReferenceExpression;
     this.Statements = Statements;
 }
コード例 #38
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     Result = stmt.Statements;
 }
コード例 #39
0
        /// <summary>
        ///     Generates the code for a CompoundStatement node.
        /// </summary>
        /// <param name="cs">The CompoundStatement node.</param>
        /// <returns>String containing C# code for CompoundStatement cs.</returns>
        private string GenerateCompoundStatement(CompoundStatement cs)
        {
            StringBuilder retVal = new StringBuilder();
            // opening brace
            retVal.Append(GenerateIndentedLine("{"));
            //            if (IsParentEnumerable)
            //                retstr += GenerateLine("if (CheckSlice()) yield return null;");
            m_braceCount++;

            foreach (SYMBOL kid in cs.kids)
                if (kid is Statement && kid.kids.Top is BinaryExpression &&
                    ((BinaryExpression) kid.kids.Top).ExpressionSymbol == "==")
                    continue;
                else
                    retVal.Append(GenerateNode(kid));

            // closing brace
            m_braceCount--;

            retVal.Append(GenerateIndentedLine("}"));

            return retVal.ToString();
        }
コード例 #40
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
 }
コード例 #41
0
        public void AcceptLoopBlock(LoopBlock stmt)
        {
            IfStatement cond = stmt.Body.AsSingleStatement() as IfStatement;
            if (cond == null)
                return;

            if (cond.Conditions.Count != 1 ||
                cond.Branches.Count != 2)
                return;

            IList<Statement> trueBranch = cond.Branches[0].AsStatementList();
            if (trueBranch.Count == 0 ||
                !trueBranch.Last().Equals(new ContinueLoopStatement() { Loop = stmt }))
                return;

            IList<Statement> falseBranch = cond.Branches[1].AsStatementList();
            if (falseBranch.Count == 0)
                return;

#if false
            BreakLoopStatement breaker = new BreakLoopStatement() { Loop = stmt };
            Statement trailer = cond.Branches[1].Clone;
            trailer.RemoveAll(breaker);
#endif

            Statement trailer = cond.Branches[1].Clone;
            BreakLoopReplacer blr = new BreakLoopReplacer(stmt);
            trailer.Accept(blr);

            if (trailer.NeedsLoopScope(stmt))
                return;
#if false
            CompoundStatement trailer = new CompoundStatement();
            trailer.Statements.AddRange(falseBranch.Take(falseBranch.Count - 1));
            if (trailer.NeedsLoopScope(stmt))
                return;

            BreakLoopStatement breaker = falseBranch.Last() as BreakLoopStatement;
            if (breaker == null)
                return;

            if (breaker.Loop.IsAncestor(stmt))
            {
                /* Consider the following situation:
                 * 
                 * L1: loop
                 *   some outer loop work
                 *   L2: loop
                 *     if someCondition then
                 *       do something
                 *       continue L2
                 *     else
                 *       do something different
                 *       break L1
                 *     end if
                 *   end loop L2
                 * end loop L1
                 * 
                 * As the inner break statement breaks the outer loop, this loop
                 * must be transformed into the following code:
                 * 
                 * L1: loop
                 *   some outer loop work
                 *   while someCondition loop
                 *     do something
                 *   end while
                 *   do something different
                 *   break L1
                 * end loop
                 * */

                if (breaker.Loop != stmt)
                {
                    trailer.Statements.Add(breaker);
                }
            }
            else
                return;
#endif

            CompoundStatement newBody = new CompoundStatement();
            newBody.Statements.AddRange(trueBranch.Take(trueBranch.Count - 1));

            LoopBlock whileBlock = (LoopBlock)stmt.Clone;
            whileBlock.HeadCondition = cond.Conditions[0];
            whileBlock.Body = newBody;
            whileBlock.Trailer = trailer;

            Result = whileBlock;
        }
コード例 #42
0
        public void AcceptCompoundStatement(CompoundStatement stmt)
        {
            if (stmt.Statements.Count != 2)
                return;

            StoreStatement initializer = stmt.Statements[0] as StoreStatement;
            LoopBlock loop = stmt.Statements[1].AsWhileLoop();
            if (initializer == null || loop == null)
                return;

            IList<Statement> body = loop.Body.AsStatementList();
            if (body.Count == 0)
                return;

            StoreStatement step = body.Last() as StoreStatement;
            if (step == null)
                return;

            CompoundStatement newBody = new CompoundStatement();
            newBody.Statements.AddRange(body.Take(body.Count - 1));

            loop.Initializer = initializer;
            loop.Body = newBody;
            loop.Step = step;

            if (Level == EForLoopLevel.Strict ||
                Level == EForLoopLevel.StrictOneInc)
            {
                StoreStatement initStore = initializer;

                if (initStore.Container == null)
                    return;
                
                Variable counterVar = initStore.Container as Variable;
                if (counterVar == null)
                    return;

                loop.CounterVariable = counterVar;

                Type counterType = counterVar.Type.CILType;
                if (!counterType.IsEnumerable())
                    return;

                if (loop.Body.Modifies(counterVar))
                    return;

                loop.CounterStart = initStore.Value;

                if (step.Container == null)
                    return;

                if (!step.Container.Equals(counterVar))
                    return;

                Expression stepExpr = step.Value;

                Matching x = new Matching();
                Matching mctr = (LiteralReference)counterVar;
                Matching mctrInc = mctr + x;
                Matching mctrDec = mctr - x;
                if (((Expression.MatchFunction)mctrInc)(stepExpr))
                {
                    loop.CounterStep = x.Result;
                    loop.CounterDirection = LoopBlock.ECounterDirection.Increment;
                }
                else if (((Expression.MatchFunction)mctrDec)(stepExpr))
                {
                    loop.CounterStep = x.Result;
                    loop.CounterDirection = LoopBlock.ECounterDirection.Decrement;
                }
                else
                    return;

                BinOp cmp = loop.HeadCondition as BinOp;
                if (cmp == null)
                    return;

                LiteralReference lhs = cmp.Children[0] as LiteralReference;
                if (lhs == null)
                    return;
                if (!lhs.ReferencedObject.Equals(counterVar))
                    return;

                loop.CounterStop = cmp.Children[1];

                switch (loop.CounterDirection)
                {
                    case LoopBlock.ECounterDirection.Decrement:
                        switch (cmp.Operation)
                        {
                            case BinOp.Kind.Gt:
                            case BinOp.Kind.NEq:
                                loop.CounterLimitKind = LoopBlock.ELimitKind.ExcludingStopValue;
                                break;

                            case BinOp.Kind.GtEq:
                                loop.CounterLimitKind = LoopBlock.ELimitKind.IncludingStopValue;
                                break;

                            default:
                                return;
                        }
                        break;

                    case LoopBlock.ECounterDirection.Increment:
                        switch (cmp.Operation)
                        {
                            case BinOp.Kind.Lt:
                            case BinOp.Kind.NEq:
                                loop.CounterLimitKind = LoopBlock.ELimitKind.ExcludingStopValue;
                                break;

                            case BinOp.Kind.LtEq:
                                loop.CounterLimitKind = LoopBlock.ELimitKind.IncludingStopValue;
                                break;

                            default:
                                return;
                        }
                        break;
                }

                if (Level == EForLoopLevel.StrictOneInc)
                {
                    object inc;
                    try
                    {
                        inc = loop.CounterStep.Eval(new DefaultEvaluator());
                    }
                    catch (BreakEvaluationException)
                    {
                        return;
                    }
                    if (inc == null)
                        return;
                    long stepValue = TypeConversions.ToLong(inc);
                    if (stepValue == 1)
                    {
                        switch (loop.CounterDirection)
                        {
                            case LoopBlock.ECounterDirection.Increment:
                                loop.CounterDirection = LoopBlock.ECounterDirection.IncrementOne;
                                break;

                            case LoopBlock.ECounterDirection.Decrement:
                                loop.CounterDirection = LoopBlock.ECounterDirection.DecrementOne;
                                break;
                        }
                    }
                    else if (stepValue == -1)
                    {
                        switch (loop.CounterDirection)
                        {
                            case LoopBlock.ECounterDirection.Increment:
                                loop.CounterDirection = LoopBlock.ECounterDirection.DecrementOne;
                                break;

                            case LoopBlock.ECounterDirection.Decrement:
                                loop.CounterDirection = LoopBlock.ECounterDirection.IncrementOne;
                                break;
                        }
                    }
                    else
                        return;
                }
            }

            Result = loop;
        }
コード例 #43
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     throw new InvalidOperationException();
 }
コード例 #44
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     if (stmt.Statements.Count > 0)
         stmt.Statements.First().Accept(this);
 }
コード例 #45
0
ファイル: CParser.Nodes.cs プロジェクト: soywiz/ilcc
 public FunctionDeclaration(PositionInfo PositionInfo, CFunctionType CFunctionType, CompoundStatement FunctionBody)
     : base(PositionInfo, FunctionBody)
 {
     this.CFunctionType = CFunctionType;
     this.FunctionBody = FunctionBody;
 }
コード例 #46
0
 public void AcceptCompoundStatement(CompoundStatement stmt)
 {
     foreach (Statement child in stmt.Statements)
         child.Accept(this);
 }