Esempio 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);
        }
Esempio 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;
        }