Exemplo n.º 1
0
        private Exp addNewExp()
        {
            Console.WriteLine("Choose a type of expression:");
            Console.WriteLine("1. Arithmetical expression");
            Console.WriteLine("2. Constant expression");
            Console.WriteLine("3. Variable expression");
            Console.WriteLine("4. Comparison expression");
            Console.WriteLine("5. Logical expression");
            Console.WriteLine("6. Read expression");
            Console.WriteLine("7. Read heap expression");

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

            Exp expr;

            switch (opt)
            {
            case 1:
                Console.WriteLine("Choose operation: +, -, *, /");
                String        op         = Console.ReadLine();
                List <string> optionList = new List <string>
                {
                    "+", "-", "*", "/"
                };
                if (optionList.Contains(op))
                {
                    Console.WriteLine("Left hand side:");
                    Exp left = addNewExp();
                    Console.WriteLine("Right hand side:");
                    Exp right = addNewExp();
                    expr = new ArithmExp(left, right, op[0]);
                }
                else
                {
                    Console.WriteLine("Operand MUST be +, -, *, /. Try again");
                    expr = addNewExp();
                }
                break;

            case 2:
                Console.WriteLine("Give a number:");
                int no = Convert.ToInt32(Console.ReadLine());
                expr = new ConstExp(no);
                break;

            case 3:
                Console.WriteLine("Variable name:");
                String varName = Console.ReadLine();
                expr = new VarExp(varName);
                break;

            case 4:
                Console.WriteLine("Choose comp. operand: <, <=, ==, !=, >=, >");
                String        op1      = Console.ReadLine();
                List <string> optsList = new List <string>
                {
                    "<", "<=", "==", "!=", ">=", ">"
                };
                if (optsList.Contains(op1))
                {
                    Console.WriteLine("Left hand side:");
                    Exp left = addNewExp();
                    Console.WriteLine("Right hand side:");
                    Exp right = addNewExp();
                    expr = new CompExp(left, right, op1);
                }
                else
                {
                    Console.WriteLine("Operand MUST be a comp. operator. Try again");
                    expr = addNewExp();
                }
                break;

            case 5:
                Console.WriteLine("Choose logical operand: && (and), ||(or), !(not)");
                String        op2         = Console.ReadLine();
                List <string> optionsList = new List <string>
                {
                    "&&", "||"
                };
                if (optionsList.Contains(op2))
                {
                    Console.WriteLine("Left hand side:");
                    Exp left = addNewExp();
                    Console.WriteLine("Right hand side:");
                    Exp right = addNewExp();
                    expr = new LogicExp(left, right, op2);
                }
                else if (op2 == "!")
                {
                    Console.WriteLine("Expression:");
                    Exp singleExp = addNewExp();
                    expr = new LogicExp(singleExp, op2);
                }
                else
                {
                    Console.WriteLine("Operand MUST be a logical operator. Try again");
                    expr = addNewExp();
                }
                break;

            case 6:
                expr = new ReadExp();
                break;

            case 7:
                Console.WriteLine("Variable name:");
                String varNew = Console.ReadLine();
                expr = new ReadHeapExp(varNew);
                break;

            default:
                Console.WriteLine("Please try one of the options above.");
                expr = addNewExp();
                break;
            }
            return(expr);
        }
Exemplo n.º 2
0
        private Exp addNewExp()
        {
            Console.WriteLine("Choose a type of expression:");
            Console.WriteLine("1. Arithmetical expression");
            Console.WriteLine("2. Constant expression");
            Console.WriteLine("3. Variable expression");
            Console.WriteLine("4. Comparison expression");
            Console.WriteLine("5. Logical expression");
            Console.WriteLine("6. Read expression");
            Console.WriteLine("7. Read heap expression");

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

            Exp expr;
            switch (opt) {
            case 1:
                Console.WriteLine ("Choose operation: +, -, *, /");
                String op = Console.ReadLine ();
                List<string> optionList = new List<string>
                                        { "+", "-", "*", "/"};
                if (optionList.Contains(op)) {
                    Console.WriteLine("Left hand side:");
                    Exp left = addNewExp();
                    Console.WriteLine("Right hand side:");
                    Exp right = addNewExp();
                    expr = new ArithmExp(left, right, op[0]);
                } else {
                    Console.WriteLine("Operand MUST be +, -, *, /. Try again");
                    expr = addNewExp();
                }
                break;
            case 2:
                Console.WriteLine("Give a number:");
                int no = Convert.ToInt32(Console.ReadLine());
                expr = new ConstExp(no);
                break;
            case 3:
                Console.WriteLine("Variable name:");
                String varName = Console.ReadLine ();
                expr = new VarExp(varName);
                break;
            case 4:
                Console.WriteLine("Choose comp. operand: <, <=, ==, !=, >=, >");
                String op1 = Console.ReadLine ();
                List<string> optsList = new List<string>
                {"<", "<=", "==", "!=", ">=", ">"};
                if (optsList.Contains(op1)) {
                    Console.WriteLine("Left hand side:");
                    Exp left = addNewExp();
                    Console.WriteLine("Right hand side:");
                    Exp right = addNewExp();
                    expr = new CompExp(left, right, op1);
                } else {
                    Console.WriteLine("Operand MUST be a comp. operator. Try again");
                    expr = addNewExp();
                }
                break;
            case 5:
                Console.WriteLine("Choose logical operand: && (and), ||(or), !(not)");
                String op2 = Console.ReadLine ();
                List<string> optionsList = new List<string>
                {"&&", "||"};
                if (optionsList.Contains(op2)) {
                    Console.WriteLine("Left hand side:");
                    Exp left = addNewExp();
                    Console.WriteLine("Right hand side:");
                    Exp right = addNewExp();
                    expr = new LogicExp(left, right, op2);
                } else if(op2 == "!") {
                    Console.WriteLine("Expression:");
                    Exp singleExp = addNewExp();
                    expr = new LogicExp(singleExp, op2);
                } else {
                    Console.WriteLine("Operand MUST be a logical operator. Try again");
                    expr = addNewExp();
                }
                break;
            case 6:
                expr = new ReadExp();
                break;
            case 7:
                Console.WriteLine ("Variable name:");
                String varNew = Console.ReadLine ();
                expr = new ReadHeapExp(varNew);
                break;
            default:
                Console.WriteLine("Please try one of the options above.");
                expr = addNewExp();
                break;
            }
            return expr;
        }
Exemplo n.º 3
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);
        }
Exemplo 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;
        }