예제 #1
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);
     }
 }
예제 #2
0
 public override int Evaluate(MyIDictionary <string, int> symTab)
 {
     try
     {
         return(symTab.Get(id));
     }
     catch (InvalidKeyMyDictionaryException)
     {
         throw new UndeclaredVariableException("Exception: Undeclared variable: " + id + ".");
     }
 }
        public ProgramState Execute(ProgramState state)
        {
            MyIDictionary <int, MyPair <string, StreamReader> > fileTable = state.GetFileTab();
            MyIDictionary <string, int> symTable = state.GetSymTab();

            // 1. evaluate var_file_id to a value. If any error occurs, stop the execution
            int value;

            try
            {
                value = var_file_id.Evaluate(symTable);
            }
            catch (ExpressionEvaluationException e)
            {
                throw new MyStatementExecutionException("The file is not opened for reading.");
            }

            // 2. Use the value to get the entry into the file table and get the associated StreamReader
            // object. If there is no entry in the file table for the value, we stop the execution
            StreamReader sr;

            try
            {
                sr = fileTable.Get(value).GetSecond();
            }
            catch (InvalidKeyMyDictionaryException)
            {
                throw new MyStatementExecutionException("Error. There is no entry (" + value + ") in the file table.");
            }

            // 3. call the Close() method
            try
            {
                sr.Close();
            }
            catch (IOException e)
            {
                throw new MyStatementExecutionException("IO Error: " + e.ToString());
            }

            // 4. delete the entry from the file table
            try
            {
                fileTable.Remove(value);
            }
            catch (InvalidKeyMyDictionaryException)
            {
                throw new MyStatementExecutionException("Error. There is no entry (" + value + ") in the file table.");
            }

            // 5. return
            return(null);
        }
        public ProgramState Execute(ProgramState state)
        {
            MyIDictionary <int, MyPair <string, StreamReader> > fileTable = state.GetFileTab();
            MyIDictionary <string, int> symTable = state.GetSymTab();

            // 1. evaluate var_file_id to a value
            int value;

            try
            {
                value = var_file_id.Evaluate(symTable);
            }
            catch (ExpressionEvaluationException e)
            {
                throw new MyStatementExecutionException("Cannot read from var " + var_file_id.ToString() + ".");
            }

            // 2. get the StreamReader object associated in the file table. If there is no entry associated
            // with that value in the file table, we stop the execution
            StreamReader sr;

            try
            {
                sr = fileTable.Get(value).GetSecond();
            }
            catch (InvalidKeyMyDictionaryException)
            {
                throw new MyStatementExecutionException("Error. There is no entry (" + value + ") in the file table.");
            }

            // 3. Read a line from the file using the ReadLine() method of the StreamReader object.
            // If line is null create a zero int value, otherwise translate the string into an int value
            int read;

            try
            {
                string line = sr.ReadLine();
                read = line.Equals(null) ? 0 : Int32.Parse(line);
            }
            catch (IOException)
            {
                throw new MyStatementExecutionException("Error on reading from file.");
            }

            // 4. Add a new mapping (var_name, int value computed at the previous step) into the sym-table.
            // If var_name exists in sym-table, update its associated value instead of adding a new mapping
            symTable.Put(var_name, read);

            // 5. return
            return(null);
        }
 public override int Evaluate(MyIDictionary <string, int> symbolTable)
 {
     return(symbolTable.Get(name));
 }