Esempio n. 1
0
        private void addProgram()
        {
            IStmt st1 = new AssignStmt("v", new ConstExp(10));
            IStmt st2 = new NewStmt("a", new ConstExp(22));
            IStmt st3 = new AssignStmt("v", new ConstExp(32));
            IStmt st4 = new PrintStmt(new VarExp("v"));
            IStmt st5 = new PrintStmt(new ReadHeapExp("a"));
            IStmt st8 = new ForkStmt(new CmpStmt(new WriteHeapStmt("a", new ConstExp(30)),
                                                 new CmpStmt(st3, new CmpStmt(st4, st5))));
            IStmt st6     = new PrintStmt(new VarExp("v"));
            IStmt st7     = new PrintStmt(new ReadHeapExp("a"));
            IStmt prgStmt = new CmpStmt(st1, new CmpStmt(st2,
                                                         new CmpStmt(st8, new CmpStmt(st6, st7))));
            //IStmt prgStmt = addNewStmt();
            IStack <IStmt>            exeStk = new ArrayStack <IStmt>();
            IDictionary <String, int> tbl    = new ArrayDictionary <String, int>();
            IList <int> outl = new ArrayList <int>();
            IHeap <int> h    = new MyHeap <int>();

            exeStk.Push(prgStmt);

            PrgState crtPrg = new PrgState(exeStk, tbl, outl, h);

            ctrl.addPrgState(crtPrg);
            ctrl.repoSer();

            try {
                mainMenu();
            } catch (ControllerException) {
                Console.WriteLine("Step evaluation error.");
            } catch (RepositoryException) {
                Console.WriteLine("Program state error.");
            } catch (ConsoleException) {
                Console.WriteLine("Wrong option. Try again.");
            }
        }
Esempio n. 2
0
        private IStmt addNewStmt()
        {
            Console.WriteLine("Choose a type of statement:");
            Console.WriteLine("1. Compound statement");
            Console.WriteLine("2. Assignment statement");
            Console.WriteLine("3. If/then/else statement");
            Console.WriteLine("4. Print statement");
            Console.WriteLine("5. While statement");
            Console.WriteLine("6. Skip statement");
            Console.WriteLine("7. If/then statement");
            Console.WriteLine("8. Switch statement");
            Console.WriteLine("9. New statement");
            Console.WriteLine("10. Write to heap statement");


            int opt = Convert.ToInt32(Console.ReadLine());

            IStmt st;

            switch (opt)
            {
            case 1:
                Console.WriteLine("First statement:");
                IStmt st1 = addNewStmt();
                Console.WriteLine("Second statement:");
                IStmt st2 = addNewStmt();
                st = new CmpStmt(st1, st2);
                break;

            case 2:
                Console.WriteLine("Variable name:");
                String varName = Console.ReadLine();
                Console.WriteLine("Right side expression:");
                Exp exp = addNewExp();
                st = new AssignStmt(varName, exp);
                break;

            case 3:
                Console.WriteLine("Expression to evaluate:");
                Exp expr = addNewExp();
                Console.WriteLine("Then Statement:");
                IStmt then = addNewStmt();
                Console.WriteLine("Else Statement:");
                IStmt el = addNewStmt();
                st = new IfStmt(expr, then, el);
                break;

            case 4:
                Exp e = addNewExp();
                st = new PrintStmt(e);
                break;

            case 5:
                Console.WriteLine("Expression to evaluate:");
                Exp expression = addNewExp();
                Console.WriteLine("Statement:");
                IStmt statement = addNewStmt();
                st = new WhileStmt(expression, statement);
                break;

            case 6:
                st = new SkipStmt();
                break;

            case 7:
                Console.WriteLine("Expression to evaluate:");
                Exp exp1 = addNewExp();
                Console.WriteLine("Then Statement:");
                IStmt then1 = addNewStmt();
                st = new IfThenStmt(exp1, then1);
                break;

            case 8:
                Console.WriteLine("Switch operator:");
                String variableName = Console.ReadLine();
                expr = new VarExp(variableName);

                Console.WriteLine("Case 1 expression:");
                Exp expCase1 = addNewExp();
                Console.WriteLine("Case 1 Statement:");
                IStmt case1 = addNewStmt();

                Console.WriteLine("Case 2 expression:");
                Exp expCase2 = addNewExp();
                Console.WriteLine("Case 2 Statement:");
                IStmt case2 = addNewStmt();

                Console.WriteLine("Default case Statement:");
                IStmt caseDefault = addNewStmt();

                st = new SwitchStmt(expr, expCase1, case1, expCase2, case2, caseDefault);
                break;

            case 9:
                Console.WriteLine("Variable name:");
                String newVar = Console.ReadLine();
                Console.WriteLine("Assigned expression:");
                Exp exp9 = addNewExp();
                st = new NewStmt(newVar, exp9);
                break;

            case 10:
                Console.WriteLine("Variable name:");
                String heapVar = Console.ReadLine();
                Console.WriteLine("Assigned expression:");
                Exp exp10 = addNewExp();
                st = new WriteHeapStmt(heapVar, exp10);
                break;

            default:
                Console.WriteLine("Please try one of the options above.");
                st = addNewStmt();
                break;
            }

            return(st);
        }
Esempio n. 3
0
        private void addProgram()
        {
            IStmt st1 = new AssignStmt("v", new ConstExp(10));
            IStmt st2 = new NewStmt("a", new ConstExp(22));
            IStmt st3 = new AssignStmt("v", new ConstExp(32));
            IStmt st4 = new PrintStmt(new VarExp("v"));
            IStmt st5 = new PrintStmt(new ReadHeapExp("a"));
            IStmt st8 = new ForkStmt(new CmpStmt(new WriteHeapStmt("a", new ConstExp(30)),
                new CmpStmt(st3, new CmpStmt(st4, st5))));
            IStmt st6 = new PrintStmt(new VarExp("v"));
            IStmt st7 = new PrintStmt(new ReadHeapExp("a"));
            IStmt prgStmt = new CmpStmt(st1, new CmpStmt(st2,
                new CmpStmt(st8, new CmpStmt(st6, st7))));
            //IStmt prgStmt = addNewStmt();
            IStack<IStmt> exeStk = new ArrayStack<IStmt>();
            IDictionary<String, int> tbl = new ArrayDictionary<String, int>();
            IList<int> outl = new ArrayList<int>();
            IHeap<int> h = new MyHeap<int>();
            exeStk.Push(prgStmt);

            PrgState crtPrg = new PrgState(exeStk, tbl, outl, h);
            ctrl.addPrgState(crtPrg);
            ctrl.repoSer ();

            try {
                mainMenu();
            } catch (ControllerException) {
                Console.WriteLine("Step evaluation error.");
            } catch (RepositoryException) {
                Console.WriteLine("Program state error.");
            } catch (ConsoleException) {
                Console.WriteLine("Wrong option. Try again.");
            }
        }
Esempio n. 4
0
        private IStmt addNewStmt()
        {
            Console.WriteLine("Choose a type of statement:");
            Console.WriteLine("1. Compound statement");
            Console.WriteLine("2. Assignment statement");
            Console.WriteLine("3. If/then/else statement");
            Console.WriteLine("4. Print statement");
            Console.WriteLine("5. While statement");
            Console.WriteLine("6. Skip statement");
            Console.WriteLine("7. If/then statement");
            Console.WriteLine("8. Switch statement");
            Console.WriteLine("9. New statement");
            Console.WriteLine("10. Write to heap statement");

            int opt = Convert.ToInt32(Console.ReadLine());

            IStmt st;
            switch (opt) {
            case 1:
                Console.WriteLine("First statement:");
                IStmt st1 = addNewStmt();
                Console.WriteLine("Second statement:");
                IStmt st2 = addNewStmt();
                st = new CmpStmt(st1, st2);
                break;
            case 2:
                Console.WriteLine("Variable name:");
                String varName = Console.ReadLine();
                Console.WriteLine("Right side expression:");
                Exp exp = addNewExp();
                st = new AssignStmt(varName, exp);
                break;
            case 3:
                Console.WriteLine("Expression to evaluate:");
                Exp expr = addNewExp();
                Console.WriteLine("Then Statement:");
                IStmt then = addNewStmt();
                Console.WriteLine("Else Statement:");
                IStmt el = addNewStmt();
                st = new IfStmt(expr, then, el);
                break;
            case 4:
                Exp e = addNewExp();
                st = new PrintStmt(e);
                break;
            case 5:
                Console.WriteLine("Expression to evaluate:");
                Exp expression = addNewExp();
                Console.WriteLine("Statement:");
                IStmt statement = addNewStmt();
                st = new WhileStmt(expression, statement);
                break;
            case 6:
                st = new SkipStmt();
                break;
            case 7:
                Console.WriteLine("Expression to evaluate:");
                Exp exp1 = addNewExp();
                Console.WriteLine("Then Statement:");
                IStmt then1 = addNewStmt();
                st = new IfThenStmt(exp1, then1);
                break;
            case 8:
                Console.WriteLine("Switch operator:");
                String variableName = Console.ReadLine();
                expr = new VarExp(variableName);

                Console.WriteLine("Case 1 expression:");
                Exp expCase1 = addNewExp();
                Console.WriteLine("Case 1 Statement:");
                IStmt case1 = addNewStmt();

                Console.WriteLine("Case 2 expression:");
                Exp expCase2 = addNewExp();
                Console.WriteLine("Case 2 Statement:");
                IStmt case2 = addNewStmt();

                Console.WriteLine("Default case Statement:");
                IStmt caseDefault = addNewStmt();

                st = new SwitchStmt(expr, expCase1, case1, expCase2, case2, caseDefault);
                break;
            case 9:
                Console.WriteLine("Variable name:");
                String newVar = Console.ReadLine();
                Console.WriteLine("Assigned expression:");
                Exp exp9 = addNewExp();
                st = new NewStmt(newVar, exp9);
                break;
            case 10:
                Console.WriteLine("Variable name:");
                String heapVar = Console.ReadLine();
                Console.WriteLine("Assigned expression:");
                Exp exp10 = addNewExp();
                st = new WriteHeapStmt(heapVar, exp10);
                break;
            default:
                Console.WriteLine ("Please try one of the options above.");
                st = addNewStmt ();
                break;

            }

            return st;
        }