public ProgState execute(ProgState prog)
        {
            MyIDictionary <string, int> dict = prog.getDict();

            try
            {
                if (dict.isDefined(this.id) == 1)
                {
                    dict.update(id, exp.eval(dict));
                }
                else
                {
                    dict.add(id, exp.eval(dict));
                }
                return(prog);
            }
            catch (StmtExceptions e)
            {
                throw new ControllerException(e);
            }
            catch (DivizionByZeroException e)
            {
                throw new ControllerException(e);
            }
        }
Exemple #2
0
 public ProgramState(MyIStack <IStatement> executionStack, MyIDictionary <string, int> symbolTable,
                     MyIList <int> output)
 {
     this.executionStack = executionStack;
     this.symbolTable    = symbolTable;
     this.output         = output;
 }
        public ProgState execute(ProgState prog)
        {
            MyIDictionary <string, int> dict = prog.getDict();
            MyIFileTable <Tuple>        Tpl  = prog.getFile();

            try
            {
                int   val = exp.eval(dict);
                Tuple tpl = Tpl.get(val);
                if (tpl != null)
                {
                    tpl.getST().Close();
                    Tpl.remove(val);
                }
                else
                {
                    throw new ControllerException("The file does not exist!! \n");
                }
            }
            catch (StmtExceptions e)
            { throw new ControllerException(e); }
            catch (IOException e)
            { Console.WriteLine(e.ToString()); }
            return(prog);
        }
Exemple #4
0
        public override int evaluate(MyIDictionary <string, int> symTable)
        {
            int x1  = this.e1.evaluate(symTable);
            int x2  = this.e2.evaluate(symTable);
            int ret = x1;

            switch (this.op)
            {
            case Operation.ADD:
                ret += x2;
                break;

            case Operation.SUBTRACT:
                ret -= x2;
                break;

            case Operation.MULTIPLY:
                ret *= x2;
                break;

            case Operation.DIVIDE:
                ret /= x2;
                break;
            }
            return(ret);
        }
Exemple #5
0
        public PrgState execute(PrgState state)
        {
            MyIStack <IStmt> stk = state.getExeStack();
            // stk.pop();

            MyIDictionary <String, int> symTbl  = state.getSymTable();
            IFileTable <int, FileTuple> fileTbl = state.getFileTable();

            int value_exp = this.exp_file_id.eval(symTbl);


            FileTuple ft = fileTbl.getValue(value_exp);

            if (!fileTbl.isDefined(value_exp))
            {
                throw new Exception("The key is not defined in the file table");
            }

            StreamReader streamReader = fileTbl.getValue(value_exp).getStreamReader();

            try
            {
                streamReader.Close();
            }
            catch (IOException ex) { throw new Exception(ex.Message); }

            fileTbl.remove(value_exp);
            return(state);
        }
Exemple #6
0
    public bool NegTest1()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest1: The IDictionary is read-only");

        try
        {
            IDictionary iDictionary = new MyIDictionary();
            iDictionary.Clear();
            TestLibrary.TestFramework.LogError("101", "The NotSupportedException was not thrown as expected");
            retVal = false;
        }
        catch (NotSupportedException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("102", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return(retVal);
    }
        public int Eval(MyIDictionary <string, int> tbl)
        {
            if (op == 1)
            {
                return(e1.Eval(tbl) + e2.Eval(tbl));
            }
            if (op == 2)
            {
                return(e1.Eval(tbl) - e2.Eval(tbl));
            }
            if (op == 3)
            {
                return(e1.Eval(tbl) * e2.Eval(tbl));
            }
            if (op == 4)
            {
                if (e2.Eval(tbl) == 0)
                {
                    MyException ex = new MyException("Division by 0!");
                    throw ex;
                }
                return(e1.Eval(tbl) / e2.Eval(tbl));
            }

            return(0);
        }
        public ProgState execute(ProgState prog)
        {
            MyIDictionary <string, int> dict = prog.getDict();
            MyIFileTable <Tuple>        Tpl  = prog.getFile();

            try
            {
                if (Tpl.isEmpty() == 1)
                {
                    for (int i = 1; i <= Tpl.getKey(); i++)
                    {
                        Tuple tpl = Tpl.get(i);
                        if (tpl != null && tpl.getName() == filename)
                        {
                            throw new ControllerException("The file does not exist!! \n");
                        }
                    }
                }
                StreamReader st  = new StreamReader(filename);
                Tuple        tup = new Tuple(filename, st);
                Tpl.add(tup);
                dict.add(varName, Tpl.getKey());
                return(prog);
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (DictExceptions e)
            { throw new ControllerException(e.ToString()); }
            return(null);
        }
Exemple #9
0
        public PrgState Execute(PrgState state)
        {
            MyIDictionary <String, int> symTable = state.GetSymTable();
            int file_id = this.exp_file_id.Eval(symTable);
            MyIPair <String, StreamReader> fileTable = state.GetFileTable().Lookup(file_id);

            if (fileTable == null)
            {
                throw new MyException("File not opened");
            }
            int    anotherVal;
            String line = fileTable.GetSecond().ReadLine();

            if (line == null)
            {
                anotherVal = 0;
            }
            else
            {
                anotherVal = System.Convert.ToInt32(line);
            }
            if (symTable.IsDefinedU(var_name))
            {
                symTable.Update(var_name, anotherVal);
            }
            else
            {
                symTable.Add(var_name, anotherVal);
            }
            return(state);
        }
Exemple #10
0
        public override int Eval(MyIDictionary <String, int> tbl)
        {
            switch (op)
            {
            case ('+'):
                return(e1.Eval(tbl) + e2.Eval(tbl));

            case ('-'):
                return(e1.Eval(tbl) - e2.Eval(tbl));

            case ('*'):
                return(e1.Eval(tbl) * e2.Eval(tbl));

            case ('/'):
                if (e2.Eval(tbl) == 0)
                {
                    throw new MyException("Division with zero!");
                }
                else
                {
                    return(e1.Eval(tbl) / e2.Eval(tbl));
                }

            default:
                if (e2.Eval(tbl) == 0)
                {
                    throw new MyException("Division with zero!");
                }
                else
                {
                    return(e1.Eval(tbl) % e2.Eval(tbl));
                }
            }
        }
Exemple #11
0
        public ProgramState Execute(ProgramState programState)
        {
            MyIDictionary <string, int> symbolTable = programState.SymbolTable;

            int fileDescriptor = expFileId.Evaluate(symbolTable);

            if (!programState.FileTable.TryGetValue(fileDescriptor, out Tuple <string, TextReader> value))
            {
                throw new StatementException("Invalid file id provided!");
            }

            TextReader reader = value.Item2;

            try
            {
                reader.Close();
            }
            catch (Exception)
            {
                throw new StatementException("Error while closing file!");
            }

            programState.FileTable.Remove(fileDescriptor);

            return(programState);
        }
Exemple #12
0
        public override int Evaluate(MyIDictionary <string, int> symbolTable)
        {
            int l = this.left.Evaluate(symbolTable);
            int r = this.right.Evaluate(symbolTable);

            int ret = l;

            switch (this.op)
            {
            case Operation.ADD:
                ret += r;
                break;

            case Operation.SUBTRACT:
                ret -= r;
                break;

            case Operation.MULTIPLY:
                ret *= r;
                break;

            case Operation.DIVIDE:
                if (r == 0)
                {
                    throw new Exception("Division by zero");
                }
                ret /= r;
                break;
            }
            return(ret);
        }
Exemple #13
0
 public PrgState(MyIStack <IStmt> stk, MyIDictionary <String, int> symtbl, MyIList <int> ot, MyIDictionary <int, MyPair <String, StreamReader> > fT, IStmt prg)
 {
     exeStack  = stk;
     symTable  = symtbl;
     outList   = ot;
     fileTable = fT;
     exeStack.Push(prg);
 }
Exemple #14
0
 public void Clone(MyIDictionary <K, V> toClone)
 {
     dict = new Dictionary <K, V>();
     foreach (K k in toClone.KeySet())
     {
         this.dict[k] = toClone.Get(k);
     }
 }
Exemple #15
0
 public PrgState(MyIStack <IStmt> exeStack, MyIDictionary <string, int> symTable, MyIList <int> output, MyIFileDictionary <int, KeyValuePair <string, StreamReader> > fileTable, IStmt prg)
 {
     this.exeStack  = exeStack;
     this.symTable  = symTable;
     this.output    = output;
     this.fileTable = fileTable;
     exeStack.push(prg);
 }
Exemple #16
0
 public PrgState(IStmt prg)
 {
     exeStack = new MyStack <IStmt>(new Stack <IStmt>());
     exeStack.push(prg);
     symTable  = new MyDictionary <string, int>(new Dictionary <string, int>());
     outList   = new MyList <int>(new List <int>());
     fileTable = new MyDictionary <int, Tuple <string, TextReader> >(new Dictionary <int, Tuple <string, TextReader> >());
 }
Exemple #17
0
 public PrgState(MyIStack<T> stk, MyIDictionary symtbl, MyIList ot, MyIHeap heap, int id)
 {
     exeStack = stk;
     @out = ot;
     symTable = symtbl;
     mHeap = heap;
     mId = id;
 }
Exemple #18
0
 public ProgramState(IStatement statement)
 {
     executionStack = new MyStack <IStatement>(new Stack <IStatement>());
     executionStack.Push(statement);
     symbolTable = new MyDictionary <string, int>(new Dictionary <string, int>());
     outputList  = new MyList <int>(new List <int>());
     fileTable   = new MyDictionary <int, Tuple <string, TextReader> >(new Dictionary <int, Tuple <string, TextReader> >());
 }
        public PrgState execute(PrgState state)
        {
            MyIList <int> outList = state.getOutList();
            MyIDictionary <string, int> symTabel = state.getSymTable();

            outList.add(exp.Eval(symTabel));
            return(null);
        }
        public PrgState execute(PrgState state)
        {
            MyIList <int> ot = state.getOut();
            MyIDictionary <String, int> symTbl = state.getSymTable();

            ot.add(exp.eval(state.getSymTable()));
            return(state);
        }
Exemple #21
0
 public ProgState(MyIStack <IStmt> stk, MyIDictionary <string, int> dict, MyIList <int> list, MyIFileTable <Tuple> fileT, IStmt prog)
 {
     this.stk   = stk;
     this.dict  = dict;
     this.list  = list;
     this.fileT = fileT;
     stk.push(prog);
 }
Exemple #22
0
        public int Evaluate(MyIDictionary <string, int> symbolTable)
        {
            if (!symbolTable.TryGetValue(id, out int value))
            {
                throw new ExpressionException("Unassigned variable used!");
            }

            return(value);
        }
Exemple #23
0
 public int Eval(MyIDictionary <string, int> tbl)
 {
     if (!tbl.isDefined(id))
     {
         MyException ex = new MyException("Variable does not exist!");
         throw ex;
     }
     return(tbl.lookup(id));
 }
Exemple #24
0
 public PrgState(IStmt prg)
 {
     this.exeStack        = new MyStack <IStmt>();
     this.symTable        = new MyDictionary <String, int>();
     this.outTbl          = new MyList <int>();
     this.originalProgram = prg.deepCopy();
     this.fileTable       = new FileTable <int, FileTuple>();
     this.exeStack.push(prg);
 }
Exemple #25
0
        public override int Eval(MyIDictionary tbl, MyIHeap heap)
        {
            GetIntInput inp = new GetIntInput();

            inp.ShowDialog();


            return(Int32.Parse(inp.GetText()));
        }
Exemple #26
0
        public PrgState Execute(PrgState state)
        {
            MyIList <int> ot = state.GetOut();
            MyIDictionary <String, int> symtbl = state.GetSymTable();
            int val = exp.Eval(symtbl);

            ot.Add(val);
            return(state);
        }
 public PrgState(IStmt prg)
 {
     exeStack        = new MyStack <IStmt>();
     symTable        = new MyDictionary <string, int>();
     outList         = new MyList <int>();
     fileTable       = new MyFileTable <int, Tuple <string, StreamReader> >();
     originalProgram = prg;
     exeStack.push(prg);
 }
Exemple #28
0
 public PrgState(MyIStack <IStmt> stk, MyIDictionary <string, int> symtbl,
                 MyIList <int> ot, IFileTable <int, FileTuple> fileTable, IStmt prg)
 {
     this.exeStack        = stk;
     this.symTable        = symtbl;
     this.outTbl          = ot;
     this.originalProgram = prg.deepCopy();
     this.fileTable       = fileTable;
     this.exeStack.push(prg);
 }
Exemple #29
0
 public override int Evaluate(MyIDictionary <string, int> symTab)
 {
     try
     {
         return(symTab.Get(id));
     }
     catch (InvalidKeyMyDictionaryException)
     {
         throw new UndeclaredVariableException("Exception: Undeclared variable: " + id + ".");
     }
 }
Exemple #30
0
 public override int eval(MyIDictionary <string, int> tbl)
 {
     try
     {
         return(tbl.lookup(id));
     }
     catch (MyException ex)
     {
         throw new MyException("" + ex);
     }
 }
    public bool PosTest3()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest3: Check a custom class derived from the interface");

        try
        {
            IDictionary iDictionary = new MyIDictionary();
            if (!iDictionary.IsFixedSize)
            {
                TestLibrary.TestFramework.LogError("005", "The result is not the value as expected ");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e);
            retVal = false;
        }

        return retVal;
    }
Exemple #32
0
    public bool NegTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest2: The IDictionary is readonly");

        try
        {
            IDictionary iDictionary = new MyIDictionary();
            iDictionary.Remove(new MyClass());
            TestLibrary.TestFramework.LogError("103", "The NotSupportedException was not thrown as expected");
            retVal = false;
        }
        catch (NotSupportedException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("104", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return retVal;
    }