Beispiel #1
0
        /* @Override */
        public PrgState execute(PrgState state)
        {
            Dictionary <int, MyFile> fileTable = state.getFileTable();
            Dictionary <String, int> symTable  = state.getSymTable();

            int fd = 0;

            try {
                fd = this.fd.eval(symTable);
            } catch (Exception e) {
                Console.Write("Could not evaluate expression: %s\n", this.fd.toString());
                Console.Write(e);
                Environment.Exit(1);
            }

            StreamReader reader = fileTable[fd].getReader();

            if (reader == null)
            {
                Console.Write("File descriptor %d does not exist.\n", fd);
                Environment.Exit(1);
            }

            try {
                reader.Close();
            } catch (Exception e) {
                Console.Write("Could not close reader. Moving on.", fd);
                Console.Write(e);
            }

            fileTable.Remove(fd);

            return(state);
        }
Beispiel #2
0
        /* @Override */
        public PrgState execute(PrgState state)
        {
            Dictionary <int, MyFile> fileTable = state.getFileTable();
            Dictionary <String, int> symTable  = state.getSymTable();

            int fd = this.fd.eval(symTable);

            if (!fileTable.ContainsKey(fd))
            {
                Console.Write("File descriptor %d does not exist.\n", fd);
                Environment.Exit(1);
            }

            StreamReader reader = fileTable[fd].getReader();
            int          val;

            try {
                val = Int32.Parse(reader.ReadLine());
            } catch (Exception e) {
                Console.Write(e);
                val = 0;
            }

            symTable[target_var] = val;
            return(state);
        }
Beispiel #3
0
        /* @Override */
        public PrgState execute(PrgState state)
        {
            Dictionary <int, MyFile> fileTable = state.getFileTable();
            Dictionary <String, int> symTable  = state.getSymTable();

            foreach (MyFile entry in fileTable.Values)
            {
                if (entry.getFilename() == this.filename)
                {
                    Console.WriteLine("File is already open");
                    Environment.Exit(1);
                }
            }

            try {
                StreamReader reader = new StreamReader(this.filename);
                // Assign the first unused file descriptor to this file.
                for (int fd = 3; ; fd++)
                {
                    if (!fileTable.ContainsKey(fd))
                    {
                        fileTable[fd]      = new MyFile(this.filename, reader);
                        symTable[variable] = fd;
                        break;
                    }
                }
            } catch (IOException e) {
                Console.Write("Could not open %s for reading.\n", this.filename);
                Console.Write(e.ToString());
                Environment.Exit(1);
            }

            return(state);
        }
Beispiel #4
0
        public void logPrgStateExec()
        {
            /*
             * try {
             *  // TODO: first file open overwrites file.
             *  PrintWriter logFile = new PrintWriter(new BufferedWriter(new FileWriter(this.logFilePath, true)));
             *
             *  logFile.println(this.getCurrentState().toString());
             *  logFile.close();
             * } catch(Exception e) {
             *      System.err.println(e);
             *      throw e;
             * }*/

            PrgState state = getCurrentState();

            using (StreamWriter logFile = new StreamWriter(logFilePath, true)) {
                try {
                    logFile.WriteLine("Execution Stack\n");
                    foreach (IStmt statement in state.getExeStack())
                    {
                        logFile.WriteLine("  " + statement + "\n");
                    }
                    logFile.WriteLine("\n");

                    logFile.WriteLine("Symbol Table\n");
                    foreach (KeyValuePair <string, int> entry in state.getSymTable())
                    {
                        logFile.WriteLine("  " + entry.Key + " --> " + entry.Value + "\n");
                    }
                    logFile.WriteLine("\n");

                    logFile.WriteLine("File Table\n");
                    foreach (KeyValuePair <int, MyFile> entry in state.getFileTable())
                    {
                        logFile.WriteLine("  " + entry.Key + " --> " + entry.Value + "\n");
                    }
                    logFile.WriteLine("\n");

                    logFile.WriteLine("Output\n");
                    foreach (int output in state.getStdout())
                    {
                        logFile.Write("  %d\n", output);
                    }
                    logFile.WriteLine("-------------------\n");
                }
                catch (Exception) {
                    Console.WriteLine("error: could not write to the given file");
                }
            }
        }