Exemplo n.º 1
0
        public Gui()
        {
            repository = new Repository();
            controller = new Controller(repository);
            InitializeComponent();

            IMyStatement fork =
                           new CompoundStatement(new AssignStatement("v", new Model.Expressions.ConstantExpression(10)),
                                   new CompoundStatement(new HeapAllocation("a", new Model.Expressions.ConstantExpression(22)),
                                           new CompoundStatement(new ForkStatement(new WriteHeapStatement("a", new Model.Expressions.ConstantExpression(30))),
                                                   new CompoundStatement(new AssignStatement("v", new Model.Expressions.ConstantExpression(32)),
                                                           new CompoundStatement(new PrintStatement(new VariableExpression("v")),
                                                                   new CompoundStatement(new PrintStatement(new ReadHeap("a")),
                                                                            new CompoundStatement(new PrintStatement(new VariableExpression("v")), new PrintStatement(new ReadHeap("a")))))))));

            //  v = 0;
            //  (while (v < 3) (fork(print(v); v = v + 1); v = v + 1);
            //  sleep(5);
            //  print(v * 10)
            //The final Out should be { 0,1,2,30}

            IMyStatement sleep = new CompoundStatement(new AssignStatement("v", new Model.Expressions.ConstantExpression(0)),
                new CompoundStatement(new WhileStatement(new CompoundStatement(new CompoundStatement(
                    new ForkStatement(new PrintStatement(new VariableExpression("v"))), new AssignStatement("v", new ArithmeticExpression("+", new VariableExpression("v"), new Model.Expressions.ConstantExpression(1)))),
                    new AssignStatement("v", new ArithmeticExpression("+", new VariableExpression("v"), new Model.Expressions.ConstantExpression(1)))), new BooleanExpression("<", new VariableExpression("v"), new Model.Expressions.ConstantExpression(2))),
                new CompoundStatement(new SleepStatement(new Model.Expressions.ConstantExpression(5)),
                new PrintStatement(new ArithmeticExpression("*", new VariableExpression("v"), new Model.Expressions.ConstantExpression(10))))));
            controller.createProgram(sleep);
            controller.setListener(this);
        }
Exemplo n.º 2
0
        /**
         * Runs the ui
         */
        public void run()
        {
            do
            {
                Console.WriteLine("--1 Compound statement");
                Console.WriteLine("--2 Run");
                Console.WriteLine("--3 Print flag");
                Console.WriteLine("--4 Serialize");
                Console.WriteLine("--5 Deserialize");
                Console.WriteLine("\n>");

                int input = Convert.ToInt32(Console.ReadLine());
                switch (input)
                {
                    case 1: // everything starts with a compound
                        IMyStatement statement = createCompoundStatement();
                        controller.createProgram(statement);
                        break;
                    case 2: //run
                        Console.WriteLine(MenuUtils.getSteps());

                        int step = Convert.ToInt32(Console.ReadLine());
                        if (step == 1)
                        { // one step at a time
                            try
                            {
                                controller.oneStepForAll();
                            }
                            catch (StatementExecutionException e)
                            {
                                Console.WriteLine("Your program is not valid!");
                            }
                            catch (InvalidPositionException e)
                            {
                                Console.WriteLine("The position you are trying to access is not valid!");
                            }
                            catch(EmptyContainerException e)
                            {
                                Console.WriteLine("Your container is empty!");
                            }
                        }
                        else
                        { //all steps
                            try
                            {
                                controller.runAllSteps();
                            }
                            catch (StatementExecutionException e)
                            {
                                Console.WriteLine("Your program is not valid!");
                            }
                            catch (InvalidPositionException e)
                            {
                                Console.WriteLine("The position you are trying to access is not valid!");
                            }
                            catch (EmptyContainerException e)
                            {
                                Console.WriteLine("Your container is empty!");
                            }
                        }
                        break;
                    case 3://set print flasg
                        Console.WriteLine("1 to print on console");
                        Console.WriteLine("2 to save in file");
                        Console.WriteLine("3 to print");
                        int result = Convert.ToInt32(Console.ReadLine());
                        if (result == 1)
                        {
                            Controller.PRINT_FLAG = Constants.PRINT_CONSOLE;
                        }
                        else if( result == 2)
                        {
                            Controller.PRINT_FLAG = Constants.PRINT_FILE;
                        }
                        else
                        {
                            Controller.PRINT_FLAG = Constants.NO_PRINT;
                        }
                        break;
                    case 4:
                        controller.Serialize();
                        break;
                    case 5:
                        controller.DeSerialize();
                        break;
                    case 6: // for testing
                            //                    IStatement logical1 = new CompoundStatement(new AssignStatement("a", new LogicExpression("&&", new ConstantExpression(10), new ArithmeticExpression("-", new ConstantExpression(10),
                            //                            new ConstantExpression(10)))), new PrintStatement(new VariableExpression("a")));
                            //
                            //                    IStatement readStatement = new CompoundStatement(new AssignStatement("a", new ArithmeticExpression("+", new ConstantExpression(1), new ReadExpression())),
                            //                            new PrintStatement(new VariableExpression("a")));
                        IMyStatement whileStatement = new CompoundStatement(new AssignStatement("a", new ConstantExpression(12)),
                                new WhileStatement(new CompoundStatement(new PrintStatement(new VariableExpression("a")),
                                        new AssignStatement("a", new ArithmeticExpression("-", new VariableExpression("a"), new ConstantExpression(1)))), new VariableExpression("a")));

                        IMyStatement switchStatement = new CompoundStatement(new AssignStatement("a", new ConstantExpression(2)),
                                new SwitchStatement(new VariableExpression("a"), new ConstantExpression(3), new ConstantExpression(2), new PrintStatement(new ConstantExpression(100)), new PrintStatement(new ConstantExpression(3)), new PrintStatement(new ConstantExpression(2))));

                        IMyStatement allocation =
                           new CompoundStatement(new AssignStatement("v", new ConstantExpression(10)),
                               new CompoundStatement(new HeapAllocation("v", new ConstantExpression(20)),
                                   new CompoundStatement(new HeapAllocation("a", new ConstantExpression(22)), new PrintStatement(new VariableExpression("v")))));

                        IMyStatement readHeap =
                                new CompoundStatement(new AssignStatement("v", new ConstantExpression(10)),
                                    new CompoundStatement(new HeapAllocation("v", new ConstantExpression(20)),
                                        new CompoundStatement(new HeapAllocation("a", new ConstantExpression(22)),
                                            new CompoundStatement(new PrintStatement(new ArithmeticExpression("+", new ConstantExpression(100), new ReadHeap("v"))),
                                                new PrintStatement(new ArithmeticExpression("+", new ConstantExpression(100), new ReadHeap("a")))))));

                        IMyStatement writeHeap =
                                new CompoundStatement(new AssignStatement("v", new ConstantExpression(10)),
                                    new CompoundStatement(new HeapAllocation("v", new ConstantExpression(20)),
                                            new CompoundStatement(new HeapAllocation("a", new ConstantExpression(22)),
                                                    new CompoundStatement(new WriteHeapStatement("a", new ConstantExpression(30)),
                                                            new CompoundStatement(new PrintStatement(new VariableExpression("a")), new PrintStatement(new ReadHeap("a")))))));

                        IMyStatement fork =
                           new CompoundStatement(new AssignStatement("v", new ConstantExpression(10)),
                                   new CompoundStatement(new HeapAllocation("a", new ConstantExpression(22)),
                                           new CompoundStatement(new ForkStatement(new WriteHeapStatement("a", new ConstantExpression(30))),
                                                   new CompoundStatement(new AssignStatement("v", new ConstantExpression(32)),
                                                           new CompoundStatement(new PrintStatement(new VariableExpression("v")),
                                                                   new CompoundStatement(new PrintStatement(new ReadHeap("a")),
                                                                           new CompoundStatement(new PrintStatement(new VariableExpression("v")), new PrintStatement(new ReadHeap("a")))))))));

                        controller.createProgram(fork);
                        break;

                }
            } while (true);
        }